|
3 | 3 | ---@field lsp_setup fun(self: Autocmds) |
4 | 4 | local Autocmds = {} |
5 | 5 |
|
6 | | -function Autocmds:lsp_setup() |
7 | | - local events = { "BufEnter", "LspAttach" } |
8 | | - |
9 | | - vim.api.nvim_create_autocmd(events, { |
10 | | - desc = "Use LSP folding if available", |
11 | | - group = vim.api.nvim_create_augroup("lsp-folding", { clear = true }), |
12 | | - callback = function(args) |
13 | | - local has_lsp_folding = vim.iter(vim.lsp.get_clients({ bufnr = args.buf })):any( |
14 | | - function(client) return client:supports_method("textDocument/foldingRange") end |
15 | | - ) |
16 | | - local foldexpr = has_lsp_folding and "v:lua.vim.lsp.foldexpr()" |
17 | | - or "v:lua.vim.treesitter.foldexpr()" |
18 | | - |
19 | | - vim |
20 | | - .iter(vim.fn.win_findbuf(args.buf)) |
21 | | - :filter(function(winid) return vim.wo[winid].foldexpr ~= foldexpr end) |
22 | | - :each(function(winid) |
23 | | - vim.wo[winid].foldexpr = foldexpr |
24 | | - vim.api.nvim_win_call(winid, function() vim.cmd("normal! zx") end) |
25 | | - end) |
26 | | - end, |
27 | | - }) |
28 | | - |
29 | | - vim.api.nvim_create_autocmd(events, { |
30 | | - desc = "Don't use LSP document colors if available", |
31 | | - group = vim.api.nvim_create_augroup("lsp-document-colors", { clear = true }), |
32 | | - callback = function(args) |
33 | | - local has_document_color = vim.iter(vim.lsp.get_clients({ bufnr = args.buf })):any( |
34 | | - function(client) return client:supports_method("textDocument/documentColor") end |
35 | | - ) |
36 | | - if has_document_color then |
37 | | - vim.lsp.document_color.enable(false, { bufnr = args.buf }, { style = "virtual" }) |
38 | | - end |
39 | | - end, |
40 | | - }) |
41 | | - |
42 | | - vim.api.nvim_create_autocmd(events, { |
43 | | - desc = "Enable inlay hints", |
44 | | - group = vim.api.nvim_create_augroup("lsp-inlay-hints", { clear = true }), |
45 | | - callback = function(args) |
46 | | - local has_inlay_hints = vim.iter(vim.lsp.get_clients({ bufnr = args.buf })):any( |
47 | | - function(client) return client:supports_method("textDocument/inlayHint") end |
48 | | - ) |
49 | | - if has_inlay_hints then vim.lsp.inlay_hint.enable(true, { bufnr = args.buf }) end |
50 | | - end, |
51 | | - }) |
52 | | - |
53 | | - vim.api.nvim_create_autocmd(events, { |
54 | | - desc = "Enable code lens", |
55 | | - group = vim.api.nvim_create_augroup("lsp-code-lens", { clear = true }), |
56 | | - callback = function(args) |
57 | | - local has_code_lens = vim.iter(vim.lsp.get_clients({ bufnr = args.buf })):any( |
58 | | - function(client) return client:supports_method("textDocument/codeLens") end |
59 | | - ) |
60 | | - local should_enable = |
61 | | - not vim.tbl_contains(require("nikero.config").hide_code_lens_ft, vim.bo[args.buf].filetype) |
62 | | - if has_code_lens then vim.lsp.codelens.enable(should_enable, { bufnr = args.buf }) end |
63 | | - end, |
64 | | - }) |
65 | | -end |
66 | | - |
67 | 6 | function Autocmds:setup() |
68 | 7 | -- Custom "User File" Event |
69 | 8 | -- |
@@ -123,6 +62,49 @@ function Autocmds:setup() |
123 | 62 | end, |
124 | 63 | }) |
125 | 64 |
|
| 65 | + vim.api.nvim_create_autocmd({ "BufEnter", "LspAttach" }, { |
| 66 | + desc = "Use LSP folding if available", |
| 67 | + group = vim.api.nvim_create_augroup("lsp-folding", { clear = true }), |
| 68 | + callback = function(args) |
| 69 | + local functionality = vim.iter(vim.lsp.get_clients({ bufnr = args.buf })):fold( |
| 70 | + { folding = false, document_color = false, inlay_hints = false, code_lens = false }, |
| 71 | + ---@param client vim.lsp.Client |
| 72 | + function(acc, client) |
| 73 | + return { |
| 74 | + folding = client:supports_method("textDocument/foldingRange") or acc.folding, |
| 75 | + document_color = client:supports_method("textDocument/documentColor") or acc.folding, |
| 76 | + inlay_hints = client:supports_method("textDocument/inlayHint") or acc.folding, |
| 77 | + code_lens = client:supports_method("textDocument/codeLens") or acc.folding, |
| 78 | + } |
| 79 | + end |
| 80 | + ) |
| 81 | + |
| 82 | + local foldexpr = functionality.folding and "v:lua.vim.lsp.foldexpr()" |
| 83 | + or "v:lua.vim.treesitter.foldexpr()" |
| 84 | + vim |
| 85 | + .iter(vim.fn.win_findbuf(args.buf)) |
| 86 | + :filter(function(winid) return vim.wo[winid].foldexpr ~= foldexpr end) |
| 87 | + :each(function(winid) |
| 88 | + vim.wo[winid].foldexpr = foldexpr |
| 89 | + vim.api.nvim_win_call(winid, function() vim.cmd("normal! zx") end) |
| 90 | + end) |
| 91 | + |
| 92 | + if functionality.inlay_hints then vim.lsp.inlay_hint.enable(true, { bufnr = args.buf }) end |
| 93 | + |
| 94 | + if functionality.code_lens then |
| 95 | + local should_enable = not vim.tbl_contains( |
| 96 | + require("nikero.config").hide_code_lens_ft, |
| 97 | + vim.bo[args.buf].filetype |
| 98 | + ) |
| 99 | + vim.lsp.codelens.enable(should_enable, { bufnr = args.buf }) |
| 100 | + end |
| 101 | + |
| 102 | + if functionality.document_color then |
| 103 | + vim.lsp.document_color.enable(false, { bufnr = args.buf }, { style = "virtual" }) |
| 104 | + end |
| 105 | + end, |
| 106 | + }) |
| 107 | + |
126 | 108 | vim.api.nvim_create_autocmd("TextYankPost", { |
127 | 109 | desc = "Highlight when yanking text", |
128 | 110 | group = vim.api.nvim_create_augroup("highlight-yank", { clear = true }), |
|
0 commit comments