Переглянути джерело

Typst auto cmd with sioyek

Thomas 1 рік тому
батько
коміт
2cbc3ae509
3 змінених файлів з 243 додано та 65 видалено
  1. 82 16
      lua/plugins/completion.lua
  2. 1 1
      lua/plugins/mason.lua
  3. 160 48
      lua/plugins/typst.lua

+ 82 - 16
lua/plugins/completion.lua

@@ -1,3 +1,41 @@
+-- return {
+-- 	{ "hrsh7th/nvim-cmp", enabled = true },
+-- 	{
+-- 		"saghen/blink.cmp",
+-- 		lazy = false, -- lazy loading handled internally
+-- 		-- optional: provides snippets for the snippet source
+-- 		dependencies = "rafamadriz/friendly-snippets",
+--
+-- 		-- use a release tag to download pre-built binaries
+-- 		version = "v0.*",
+-- 		-- OR build from source, requires nightly: https://rust-lang.github.io/rustup/concepts/channels.html#working-with-nightly-rust
+-- 		-- build = 'cargo build --release',
+-- 		-- On musl libc based systems you need to add this flag
+-- 		-- build = 'RUSTFLAGS="-C target-feature=-crt-static" cargo build --release',
+--
+-- 		opts = {
+-- 			highlight = {
+-- 				-- sets the fallback highlight groups to nvim-cmp's highlight groups
+-- 				-- useful for when your theme doesn't support blink.cmp
+-- 				-- will be removed in a future release, assuming themes add support
+-- 				use_nvim_cmp_as_default = true,
+-- 			},
+--       keymap = {
+--
+--       }
+-- 			-- set to 'mono' for 'Nerd Font Mono' or 'normal' for 'Nerd Font'
+-- 			-- adjusts spacing to ensure icons are aligned
+-- 			nerd_font_variant = "normal",
+--
+-- 			-- experimental auto-brackets support
+-- 			accept = { auto_brackets = { enabled = true } },
+--
+-- 			-- experimental signature help support
+-- 			trigger = { signature_help = { enabled = true } }
+-- 		},
+-- 	},
+-- 	{ "hrsh7th/cmp-nvim-lsp", lazy = false },
+-- }
 return {
 	{ "hrsh7th/cmp-nvim-lsp" },
 	{
@@ -10,10 +48,35 @@ return {
 	},
 	{
 		"hrsh7th/nvim-cmp",
-		dependencies = { "hrsh7th/cmp-buffer", "f3fora/cmp-spell", "hrsh7th/cmp-path", "hrsh7th/cmp-emoji", "R-nvim/cmp-r", "hrsh7th/cmp-nvim-lsp-signature-help" },
+		dependencies = {
+			"hrsh7th/cmp-buffer",
+			"f3fora/cmp-spell",
+			"hrsh7th/cmp-path",
+			"hrsh7th/cmp-emoji",
+			"R-nvim/cmp-r",
+			"hrsh7th/cmp-nvim-lsp-signature-help",
+			"onsails/lspkind.nvim",
+		},
 		config = function()
 			local cmp = require("cmp")
 			local luasnip = require("luasnip")
+			local lspkind = require("lspkind")
+			-- gray
+			vim.api.nvim_set_hl(0, "CmpItemAbbrDeprecated", { bg = "NONE", strikethrough = true, fg = "#808080" })
+			-- blue
+			vim.api.nvim_set_hl(0, "CmpItemAbbrMatch", { bg = "NONE", fg = "#569CD6" })
+			vim.api.nvim_set_hl(0, "CmpItemAbbrMatchFuzzy", { link = "CmpIntemAbbrMatch" })
+			-- light blue
+			vim.api.nvim_set_hl(0, "CmpItemKindVariable", { bg = "NONE", fg = "#9CDCFE" })
+			vim.api.nvim_set_hl(0, "CmpItemKindInterface", { link = "CmpItemKindVariable" })
+			vim.api.nvim_set_hl(0, "CmpItemKindText", { link = "CmpItemKindVariable" })
+			-- pink
+			vim.api.nvim_set_hl(0, "CmpItemKindFunction", { bg = "NONE", fg = "#C586C0" })
+			vim.api.nvim_set_hl(0, "CmpItemKindMethod", { link = "CmpItemKindFunction" })
+			-- front
+			vim.api.nvim_set_hl(0, "CmpItemKindKeyword", { bg = "NONE", fg = "#D4D4D4" })
+			vim.api.nvim_set_hl(0, "CmpItemKindProperty", { link = "CmpItemKindKeyword" })
+			vim.api.nvim_set_hl(0, "CmpItemKindUnit", { link = "CmpItemKindKeyword" })
 
 			require("luasnip.loaders.from_vscode").lazy_load()
 
@@ -28,10 +91,24 @@ return {
 						-- vim.snippet.expand(args.body) -- For native neovim snippets (Neovim v0.10+)
 					end,
 				},
+				view = {
+					entries = "custom", -- can be "custom", "wildmenu" or "native"
+				},
 				window = {
+					documentation = {
+						-- max_height = math.floor(WIDE_HEIGHT * (WIDE_HEIGHT / vim.o.lines)),
+						-- max_width = math.floor((WIDE_HEIGHT * 2) * (vim.o.columns / (WIDE_HEIGHT * 2 * 16 / 9))),
+						border = { "", "", "", " ", "", "", "", " " },
+						winhighlight = "Normal:Pmenu",
+						winblend = vim.o.pumblend,
+					},
 					-- completion = cmp.config.window.bordered(),
 					-- documentation = cmp.config.window.bordered(),
 				},
+				formatting = {
+					format = lspkind.cmp_format(),
+				},
+
 				mapping = cmp.mapping.preset.insert({
 					["<C-b>"] = cmp.mapping.scroll_docs(-4),
 					["<C-f>"] = cmp.mapping.scroll_docs(4),
@@ -75,10 +152,10 @@ return {
 					{ name = "nvim_lsp" },
 					{ name = "buffer" },
 					{ name = "luasnip" }, -- For luasnip users.
-          { name = 'emoji' },
-          { name = "crates" }, -- cf rust
-          { name = 'cmp_r' },
-          { name = 'nvim_lsp_signature_help' },
+					{ name = "emoji" },
+					{ name = "crates" }, -- cf rust
+					{ name = "cmp_r" },
+					{ name = "nvim_lsp_signature_help" },
 					{
 						name = "spell",
 						option = {
@@ -101,17 +178,6 @@ return {
 					{ name = "buffer" },
 				}),
 			})
-
-			-- To use git you need to install the plugin petertriho/cmp-git and uncomment lines below
-			-- Set configuration for specific filetype.
-			--[[ cmp.setup.filetype('gitcommit', {
-    sources = cmp.config.sources({
-      { name = 'git' },
-    }, {
-      { name = 'buffer' },
-    })
- })
- require("cmp_git").setup() ]]
 			--
 
 			-- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).

+ 1 - 1
lua/plugins/mason.lua

@@ -39,7 +39,7 @@ return {
 				},
 			})
 			require("lspconfig").ts_ls.setup({
-				capabilities = capabilities,
+				-- capabilities = capabilities,
 				init_options = {
 					plugins = {},
 				},

+ 160 - 48
lua/plugins/typst.lua

@@ -1,50 +1,162 @@
+
+
+local function parse_stderr(data)
+  local message = table.concat(data, "\n")
+  local level = "info"
+  
+  -- Check each line for error or warning
+  for line in message:gmatch("[^\r\n]+") do
+    local lower_line = line:lower()
+    if lower_line:match("^%s*error:") then
+      level = "error"
+      break
+    elseif lower_line:match("^%s*warning:") then
+      level = "warn"
+      -- Don't break here, in case an error appears later
+    end
+  end
+  
+  return message, level
+end
+
+
+local function typst_build_and_open_sioyek()
+  local bufname = vim.fn.expand('%:p')
+  local pdf_path = vim.fn.fnamemodify(bufname, ':r') .. '.pdf'
+  local cmd = string.format('typst compile "%s"', bufname)
+
+  vim.fn.jobstart(cmd, {
+    on_exit = function(_, exit_code)
+      if exit_code == 0 then
+        vim.notify("Typst build completed successfully", vim.log.levels.INFO, {
+          title = "Typst Build",
+          timeout = 3000,
+        })
+        
+        -- Now open or reload Sioyek
+        local sioyek_running = vim.fn.system('pgrep sioyek'):match('%d+')
+        
+        if sioyek_running then
+          vim.fn.system('sioyek --execute-command reload_no_flicker')
+          vim.notify("Sioyek reloaded", vim.log.levels.INFO, {
+            title = "Sioyek",
+            timeout = 2000,
+          })
+        else
+          vim.fn.jobstart('sioyek "' .. pdf_path .. '"', {
+            on_exit = function(_, sioyek_exit_code)
+              if sioyek_exit_code == 0 then
+                vim.notify("Sioyek opened", vim.log.levels.INFO, {
+                  title = "Sioyek",
+                  timeout = 2000,
+                })
+              else
+                vim.notify("Failed to open Sioyek", vim.log.levels.ERROR, {
+                  title = "Sioyek",
+                  timeout = 3000,
+                })
+              end
+            end
+          })
+        end
+      else
+        vim.notify("Typst build failed", vim.log.levels.ERROR, {
+          title = "Typst Build",
+          timeout = 5000,
+        })
+      end
+    end,
+    stderr_buffered = true,
+    on_stderr = function(_, data)
+      if data and #data > 0 then
+        local message, level = parse_stderr(data)
+        vim.notify(message, vim.log.levels[level:upper()], {
+          title = "Typst Build",
+          timeout = 5000,
+        })
+      end
+    end,
+  })
+end
+
+vim.api.nvim_create_user_command('TypstBuild', typst_build_and_open_sioyek, {})
+
+-- Create an autocommand group
+local typst_group = vim.api.nvim_create_augroup('TypstBuildGroup', { clear = true })
+
+-- Create a command to toggle the autocommand
+local typst_auto_build_enabled = false
+
+-- Function to create the autocommand
+local function create_typst_autocmd()
+  vim.api.nvim_create_autocmd('BufWritePost', {
+    group = typst_group,
+    pattern = '*.typ',
+    callback = function()
+      vim.cmd('TypstBuild')
+    end,
+  })
+end
+
+-- Create a command to toggle the autocommand
+vim.api.nvim_create_user_command('TypstAutoBuild', function()
+  typst_auto_build_enabled = not typst_auto_build_enabled
+  if typst_auto_build_enabled then
+    create_typst_autocmd()
+    vim.notify('Typst auto-build enabled', vim.log.levels.INFO)
+  else
+    vim.api.nvim_clear_autocmds({ group = typst_group })
+    vim.notify('Typst auto-build disabled', vim.log.levels.INFO)
+  end
+end, {})
+
 return {
-	{ "kaarmu/typst.vim", ft = "typst", lazy = false },
-	{
-		"chomosuke/typst-preview.nvim",
-		lazy = false, -- or ft = 'typst'
-		version = "0.3.*",
-		config = function()
-			require("typst-preview").setup({
-				-- Setting this true will enable printing debug information with print()
-				debug = false,
-
-				-- Custom format string to open the output link provided with %s
-				-- Example: open_cmd = 'firefox %s -P typst-preview --class typst-preview'
-				open_cmd = nil,
-
-				-- Setting this to 'always' will invert black and white in the preview
-				-- Setting this to 'auto' will invert depending if the browser has enable
-				-- dark mode
-				invert_colors = "never",
-
-				-- Whether the preview will follow the cursor in the source file
-				follow_cursor = true,
-
-				-- Provide the path to binaries for dependencies.
-				-- Setting this will skip the download of the binary by the plugin.
-				-- Warning: Be aware that your version might be older than the one
-				-- required.
-				dependencies_bin = {
-					-- if you are using tinymist, just set ['typst-preview'] = "tinymist".
-					["typst-preview"] = nil,
-					["websocat"] = nil,
-				},
-
-				-- This function will be called to determine the root of the typst project
-				get_root = function(path_of_main_file)
-					return vim.fn.fnamemodify(path_of_main_file, ":p:h")
-				end,
-
-				-- This function will be called to determine the main file of the typst
-				-- project.
-				get_main_file = function(path_of_buffer)
-					return path_of_buffer
-				end,
-			})
-		end,
-		build = function()
-			require("typst-preview").update()
-		end,
-	},
+	-- { "kaarmu/typst.vim", ft = "typst", lazy = false },
+	-- {
+	-- 	"chomosuke/typst-preview.nvim",
+	-- 	lazy = false, -- or ft = 'typst'
+	-- 	version = "0.3.*",
+	-- 	config = function()
+	-- 		require("typst-preview").setup({
+	-- 			-- Setting this true will enable printing debug information with print()
+	-- 			debug = false,
+	--
+	-- 			-- Custom format string to open the output link provided with %s
+	-- 			-- Example: open_cmd = 'firefox %s -P typst-preview --class typst-preview'
+	-- 			open_cmd = 'sioyek --execute-command reload_no_flicker %s',
+	--
+	-- 			-- Setting this to 'always' will invert black and white in the preview
+	-- 			-- Setting this to 'auto' will invert depending if the browser has enable
+	-- 			-- dark mode
+	-- 			invert_colors = "never",
+	--
+	-- 			-- Whether the preview will follow the cursor in the source file
+	-- 			follow_cursor = true,
+	--
+	-- 			-- Provide the path to binaries for dependencies.
+	-- 			-- Setting this will skip the download of the binary by the plugin.
+	-- 			-- Warning: Be aware that your version might be older than the one
+	-- 			-- required.
+	-- 			dependencies_bin = {
+	-- 				-- if you are using tinymist, just set ['typst-preview'] = "tinymist".
+	-- 				["typst-preview"] = nil,
+	-- 				["websocat"] = nil,
+	-- 			},
+	--
+	-- 			-- This function will be called to determine the root of the typst project
+	-- 			get_root = function(path_of_main_file)
+	-- 				return vim.fn.fnamemodify(path_of_main_file, ":p:h")
+	-- 			end,
+	--
+	-- 			-- This function will be called to determine the main file of the typst
+	-- 			-- project.
+	-- 			get_main_file = function(path_of_buffer)
+	-- 				return path_of_buffer
+	-- 			end,
+	-- 		})
+	-- 	end,
+	-- 	build = function()
+	-- 		require("typst-preview").update()
+	-- 	end,
+	-- },
 }