From 60d3026945ff69c6ab49247da8fc7b8193c26d34 Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Fri, 4 Oct 2024 00:12:33 +0300 Subject: feat: go lsp changes --- .config/nvim/lua/custom/config/commands.lua | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to '.config/nvim/lua/custom/config/commands.lua') 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, +}) -- cgit v1.3.1