diff options
Diffstat (limited to '.config/nvim/lua/custom')
| -rw-r--r-- | .config/nvim/lua/custom/config/commands.lua | 33 | ||||
| -rw-r--r-- | .config/nvim/lua/custom/config/language-servers.lua | 42 | ||||
| -rw-r--r-- | .config/nvim/lua/custom/config/lsp.lua | 12 |
3 files changed, 66 insertions, 21 deletions
diff --git a/.config/nvim/lua/custom/config/commands.lua b/.config/nvim/lua/custom/config/commands.lua index acb2cbb..50fca05 100644 --- a/.config/nvim/lua/custom/config/commands.lua +++ b/.config/nvim/lua/custom/config/commands.lua @@ -48,3 +48,36 @@ end, { desc = 'Toggle autoformat on save', bang = true, }) + +-- NOTE: highlight %v, %s etc in go string literals +vim.api.nvim_create_autocmd({ 'BufEnter', 'TextChanged', 'InsertLeave' }, { + pattern = '*.go', + callback = function() + local query = vim.treesitter.query.parse('go', '(interpreted_string_literal) @string') + local parser = vim.treesitter.get_parser(0, 'go') + local tree = parser:parse()[1] + local root = tree:root() + local bufnr = vim.api.nvim_get_current_buf() + + for id, node in query:iter_captures(root, bufnr, 0, -1) do + if query.captures[id] == 'string' then + local start_row, start_col, end_row, end_col = node:range() + + -- Get the text of the string literal + local text = vim.api.nvim_buf_get_text(bufnr, start_row, start_col, end_row, end_col, {})[1] + + -- Highlight only the parts matching `%[a-z]` + for match_start, match_end in text:gmatch('()%%[a-z]()') do + vim.api.nvim_buf_add_highlight( + bufnr, + -1, + '@lsp.type.formatSpecifier.go', -- Higlight group + start_row, + start_col + match_start - 1, + start_col + match_end - 1 + ) + end + end + end + end, +}) diff --git a/.config/nvim/lua/custom/config/language-servers.lua b/.config/nvim/lua/custom/config/language-servers.lua index 7700493..7da2eaf 100644 --- a/.config/nvim/lua/custom/config/language-servers.lua +++ b/.config/nvim/lua/custom/config/language-servers.lua @@ -83,28 +83,28 @@ return { tidy = true, upgrade_dependency = true, vendor = true, - hints = { - assignVariableTypes = true, - compositeLiteralFields = true, - compositeLiteralTypes = true, - constantValues = true, - functionTypeParameters = true, - parameterNames = true, - rangeVariableTypes = true, - }, - analyses = { - fieldalignment = true, - nilness = true, - unusedparams = true, - unusedwrite = true, - useany = true, - }, - usePlaceholders = false, - completeUnimported = true, - staticcheck = true, - directoryFilters = { '-.git', '-.vscode', '-.idea', '-.vscode-test', '-node_modules' }, - semanticTokens = true, }, + hints = { + assignVariableTypes = true, + compositeLiteralFields = true, + compositeLiteralTypes = true, + constantValues = true, + functionTypeParameters = true, + -- parameterNames = true, + rangeVariableTypes = true, + }, + analyses = { + fieldalignment = true, + nilness = true, + unusedparams = true, + unusedwrite = true, + useany = true, + }, + usePlaceholders = false, + completeUnimported = true, + staticcheck = true, + directoryFilters = { '-.git', '-.vscode', '-.idea', '-.vscode-test', '-node_modules' }, + semanticTokens = true, }, }, }, -- Go LSP diff --git a/.config/nvim/lua/custom/config/lsp.lua b/.config/nvim/lua/custom/config/lsp.lua index 817b087..02ff31f 100644 --- a/.config/nvim/lua/custom/config/lsp.lua +++ b/.config/nvim/lua/custom/config/lsp.lua @@ -55,6 +55,18 @@ vim.api.nvim_create_autocmd('LspAttach', { end end + -- Workaround for semantic tokens apparently? + -- https://www.lazyvim.org/extras/lang/go + -- https://github.com/golang/go/issues/54531#issuecomment-1464982242 + if client.name == 'gopls' and not client.server_capabilities.semanticTokensProvider then + local semantic = client.config.capabilities.textDocument.semanticTokens + client.server_capabilities.semanticTokensProvider = { + full = true, + legend = { tokenModifiers = semantic.tokenModifiers, tokenTypes = semantic.tokenTypes }, + range = true, + } + end + -- Register buffer local LSP keymaps require('custom.config.lsp-keymaps').setup(bufnr) end, |
