From bc391ff4ba2dc8f43e667d2056755e3b8c74b11d Mon Sep 17 00:00:00 2001 From: ray-x Date: Fri, 22 Dec 2023 01:46:35 +1100 Subject: [PATCH] inlay_hint toggle improvement, luasnip timestamp --- lua/go/inlay.lua | 37 +++++++++++++++++++++++-------------- lua/snips/all.lua | 3 +++ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lua/go/inlay.lua b/lua/go/inlay.lua index 4924713..0667f98 100644 --- a/lua/go/inlay.lua +++ b/lua/go/inlay.lua @@ -10,13 +10,14 @@ local trace = utils.trace local config local inlay_display = vim.fn.has('nvim-0.10') == 1 and _GO_NVIM_CFG.lsp_inlay_hints.style == 'inlay' - and vim.lsp.inlay_hint and type(vim.lsp.inlay_hint) == 'table' + and vim.lsp.inlay_hint + and type(vim.lsp.inlay_hint) == 'table' if type(vim.lsp.inlay_hint) == 'function' then utils.warn('unsupported neovim nightly,please upgrade') end -- local inlay_display = true -- whether the hints are enabled or not -local enabled = nil +local enabled = {} -- Update inlay hints when opening a new buffer and when writing a buffer to a -- file -- opts is a string representation of the table of options @@ -27,7 +28,10 @@ function M.setup() if not config or config.enable == false then -- diabled return end - enabled = config.enable + local bufnr = tostring(vim.api.nvim_get_current_buf()) + if config.enable then + enabled[bufnr] = true + end if config.only_current_line then local user_events = vim.split(config.only_current_line_autocmd, ',') events = vim.tbl_extend('keep', events, user_events) @@ -38,7 +42,7 @@ function M.setup() group = cmd_group, pattern = { '*.go', '*.mod' }, callback = function() - if not vim.wo.diff and enabled then + if not vim.wo.diff and enabled[bufnr] then require('go.inlay').set_inlay_hints() end end, @@ -50,7 +54,7 @@ function M.setup() if not vim.wo.diff then local inlay = require('go.inlay') inlay.disable_inlay_hints(true) - if enabled then + if enabled[tostring(vim.api.nvim_get_current_buf())] then inlay.set_inlay_hints() end end @@ -155,6 +159,8 @@ end local function handler(err, result, ctx) trace(result, ctx) if err then + -- disable inlay hints if there is an error + M.disable_inlay_hints() return end local bufnr = ctx.bufnr @@ -262,33 +268,36 @@ local function handler(err, result, ctx) end -- update state - enabled = true + enabled[tostring(bufnr)] = true end end end function M.toggle_inlay_hints() + local bufnr = vim.api.nvim_get_current_buf() + local bfnrstr = tostring(bufnr) if inlay_display then - local bufnr=vim.api.nvim_get_current_buf() - enabled = vim.lsp.inlay_hint.is_enabled(bufnr) - vim.lsp.inlay_hint.enable(bufnr, not enabled) - elseif enabled then + enabled[bfnrstr] = vim.lsp.inlay_hint.is_enabled(bufnr) + vim.lsp.inlay_hint.enable(bufnr, not enabled[bfnrstr]) + elseif enabled[bfnrstr] then M.disable_inlay_hints(true) else M.set_inlay_hints() end - enabled = not enabled + enabled[bfnrstr] = not enabled[bfnrstr] end -function M.disable_inlay_hints(update) +function M.disable_inlay_hints(update, bufnr) + bufnr = bufnr or vim.api.nvim_get_current_buf() if inlay_display then - local toggle = vim.lsp.inlay_hint - local bufnr = vim.api.nvim_get_current_buf() + -- disable inlay hints vim.lsp.inlay_hint.enable(bufnr, false) + enabled[tostring(bufnr)] = false return end -- clear namespace which clears the virtual text as well vim.api.nvim_buf_clear_namespace(0, namespace, 0, -1) + enabled[tostring(bufnr)] = false if update then local fname = fn.expand('%:p') diff --git a/lua/snips/all.lua b/lua/snips/all.lua index 8e9af98..70d7ae8 100644 --- a/lua/snips/all.lua +++ b/lua/snips/all.lua @@ -44,6 +44,8 @@ end ls.add_snippets("all", { ls.s("time", partial(vim.fn.strftime, "%H:%M:%S")), ls.s("date", partial(vim.fn.strftime, "%Y-%m-%d")), + ls.s("timestamp", partial(vim.fn.strftime, "%Y-%m-%d %H:%M:%S")), + ls.s("rfc3339", partial(vim.fn.strftime, "%Y-%m-%dT%H:%M:%SZ")), ls.s("pwd", { partial(utils.run_command, "pwd") }), -- ls.s({ trig = "aws(%d)", regTrig = true, name = "aws region", dscr = "input 2 char matching a region name" }, { -- ls.d(1, function(args) @@ -55,6 +57,7 @@ ls.add_snippets("all", { -- return filter(args[1][1]) -- end, { 1 }), -- }), + ls.s('tsf', ls.t("2006-01-02T15:04:05Z07:00")), ls.s("hlc", ls.t("http://localhost")), ls.s("hl1", ls.t("http://127.0.0.1")), ls.s("lh", ls.t("localhost")),