From ede6aac2f7c037ba05319f09d3bab2d37df43b72 Mon Sep 17 00:00:00 2001 From: Jeremy Chua Date: Sat, 13 Jan 2024 08:09:56 +0800 Subject: [PATCH 1/3] fix: use buffer number instead of a boolean --- lua/typescript-tools/init.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lua/typescript-tools/init.lua b/lua/typescript-tools/init.lua index 6558aa4..1a81b67 100644 --- a/lua/typescript-tools/init.lua +++ b/lua/typescript-tools/init.lua @@ -48,16 +48,14 @@ function M.setup(config) -- I prefer it here than in huge file tsserver.lua -- Rationale: `on_attach` is called based on response from `configure` request and because we -- have two servers nvim get also two responses - local on_attach_called = false + local last_bufnr = -1 local config_on_attach = config.on_attach - local function on_attach(...) - if on_attach_called then - return + local function on_attach(client, bufnr) + if bufnr ~= last_bufnr then + last_bufnr = bufnr + config_on_attach(client, bufnr) end - - on_attach_called = true - config_on_attach(...) end if config.on_attach then From 7b0fbd56297b74b67759a042f4476342ce941d1e Mon Sep 17 00:00:00 2001 From: Jeremy Chua Date: Sat, 13 Jan 2024 15:41:35 +0800 Subject: [PATCH 2/3] fix: use map instead of variable --- lua/typescript-tools/init.lua | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lua/typescript-tools/init.lua b/lua/typescript-tools/init.lua index 1a81b67..4ae9e66 100644 --- a/lua/typescript-tools/init.lua +++ b/lua/typescript-tools/init.lua @@ -48,16 +48,27 @@ function M.setup(config) -- I prefer it here than in huge file tsserver.lua -- Rationale: `on_attach` is called based on response from `configure` request and because we -- have two servers nvim get also two responses - local last_bufnr = -1 + local buf_map = {} local config_on_attach = config.on_attach local function on_attach(client, bufnr) - if bufnr ~= last_bufnr then - last_bufnr = bufnr - config_on_attach(client, bufnr) + local buf_key = tostring(bufnr) + if buf_map[buf_key] then + return end + buf_map[buf_key] = true + config_on_attach(client, bufnr) end + local augroup = vim.api.nvim_create_augroup("TypescriptToolsOnAttachGroup", { clear = true }) + vim.api.nvim_create_autocmd("BufDelete", { + callback = function(args) + local buf_key = tostring(args.buf) + buf_map[buf_key] = false + end, + group = augroup, + }) + if config.on_attach then config.on_attach = on_attach end From ecc87bc68f96f549d317eb279f5a72d554165528 Mon Sep 17 00:00:00 2001 From: Jeremy Chua Date: Sat, 13 Jan 2024 16:26:21 +0800 Subject: [PATCH 3/3] fix: move autocommand with the others --- lua/typescript-tools/autocommands/init.lua | 3 +++ .../autocommands/on_attach.lua | 18 ++++++++++++++++++ lua/typescript-tools/init.lua | 16 ++++------------ 3 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 lua/typescript-tools/autocommands/on_attach.lua diff --git a/lua/typescript-tools/autocommands/init.lua b/lua/typescript-tools/autocommands/init.lua index 4c2ce5b..afd9ad5 100644 --- a/lua/typescript-tools/autocommands/init.lua +++ b/lua/typescript-tools/autocommands/init.lua @@ -2,6 +2,7 @@ local diagnostics = require "typescript-tools.autocommands.diagnostics" local code_lens = require "typescript-tools.autocommands.code_lens" local config = require "typescript-tools.config" local jsx_close_tag = require "typescript-tools.autocommands.jsx_close_tag" +local on_attach = require "typescript-tools.autocommands.on_attach" local M = {} @@ -9,6 +10,8 @@ local M = {} function M.setup_autocommands(dispatchers) diagnostics.setup_diagnostic_autocmds(dispatchers) + on_attach.setup_on_attach_autocmds() + if config.code_lens ~= config.code_lens_mode.off then code_lens.setup_code_lens_autocmds() end diff --git a/lua/typescript-tools/autocommands/on_attach.lua b/lua/typescript-tools/autocommands/on_attach.lua new file mode 100644 index 0000000..41c9d33 --- /dev/null +++ b/lua/typescript-tools/autocommands/on_attach.lua @@ -0,0 +1,18 @@ +local api = vim.api + +local M = {} + +M.buf_map = {} + +function M.setup_on_attach_autocmds() + local augroup = api.nvim_create_augroup("TypescriptToolsOnAttachGroup", { clear = true }) + api.nvim_create_autocmd("BufDelete", { + callback = function(args) + local buf_key = tostring(args.buf) + M.buf_map[buf_key] = false + end, + group = augroup, + }) +end + +return M diff --git a/lua/typescript-tools/init.lua b/lua/typescript-tools/init.lua index 4ae9e66..322fc85 100644 --- a/lua/typescript-tools/init.lua +++ b/lua/typescript-tools/init.lua @@ -3,6 +3,7 @@ local configs = require "lspconfig.configs" local util = require "lspconfig.util" local rpc = require "typescript-tools.rpc" local plugin_config = require "typescript-tools.config" +local on_attach_aucmd = require "typescript-tools.autocommands.on_attach" local M = {} @@ -48,10 +49,10 @@ function M.setup(config) -- I prefer it here than in huge file tsserver.lua -- Rationale: `on_attach` is called based on response from `configure` request and because we -- have two servers nvim get also two responses - local buf_map = {} + local buf_map = on_attach_aucmd.buf_map local config_on_attach = config.on_attach - local function on_attach(client, bufnr) + local function wrapped_on_attach(client, bufnr) local buf_key = tostring(bufnr) if buf_map[buf_key] then return @@ -60,17 +61,8 @@ function M.setup(config) config_on_attach(client, bufnr) end - local augroup = vim.api.nvim_create_augroup("TypescriptToolsOnAttachGroup", { clear = true }) - vim.api.nvim_create_autocmd("BufDelete", { - callback = function(args) - local buf_key = tostring(args.buf) - buf_map[buf_key] = false - end, - group = augroup, - }) - if config.on_attach then - config.on_attach = on_attach + config.on_attach = wrapped_on_attach end lspconfig[plugin_config.plugin_name].setup(config)