Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use buffer number instead of a boolean to limit on_attach calls #218

Merged
merged 3 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lua/typescript-tools/autocommands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ 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 = {}

---@param dispatchers Dispatchers
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
Expand Down
18 changes: 18 additions & 0 deletions lua/typescript-tools/autocommands/on_attach.lua
Original file line number Diff line number Diff line change
@@ -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
15 changes: 8 additions & 7 deletions lua/typescript-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -48,20 +49,20 @@ 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 buf_map = on_attach_aucmd.buf_map
local config_on_attach = config.on_attach

local function on_attach(...)
if on_attach_called then
local function wrapped_on_attach(client, bufnr)
local buf_key = tostring(bufnr)
if buf_map[buf_key] then
return
end

on_attach_called = true
config_on_attach(...)
buf_map[buf_key] = true
config_on_attach(client, bufnr)
end

if config.on_attach then
config.on_attach = on_attach
config.on_attach = wrapped_on_attach
end

lspconfig[plugin_config.plugin_name].setup(config)
Expand Down
Loading