mirror of
				https://github.com/neovim/neovim.git
				synced 2025-10-26 12:27:24 +00:00 
			
		
		
		
	fix(lsp): avoid duplicates in client attached buffers (#16099)
closes https://github.com/neovim/neovim/issues/16058 * add client.attached_buffers * only update client.attached_buffers in on_attach * use table instead of list for attached_buffers to avoid duplication
This commit is contained in:
		 Michael Lingelbach
					Michael Lingelbach
				
			
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			 GitHub
						GitHub
					
				
			
						parent
						
							dc6c9fe442
						
					
				
				
					commit
					c5525f265b
				
			| @@ -122,9 +122,6 @@ local active_clients = {} | |||||||
| local all_buffer_active_clients = {} | local all_buffer_active_clients = {} | ||||||
| local uninitialized_clients = {} | local uninitialized_clients = {} | ||||||
|  |  | ||||||
| -- Tracks all buffers attached to a client. |  | ||||||
| local all_client_active_buffers = {} |  | ||||||
|  |  | ||||||
| ---@private | ---@private | ||||||
| --- Invokes a function for each LSP client attached to the buffer {bufnr}. | --- Invokes a function for each LSP client attached to the buffer {bufnr}. | ||||||
| --- | --- | ||||||
| @@ -742,7 +739,6 @@ function lsp.start_client(config) | |||||||
|  |  | ||||||
|     lsp.diagnostic.reset(client_id, all_buffer_active_clients) |     lsp.diagnostic.reset(client_id, all_buffer_active_clients) | ||||||
|     changetracking.reset(client_id) |     changetracking.reset(client_id) | ||||||
|     all_client_active_buffers[client_id] = nil |  | ||||||
|     for _, client_ids in pairs(all_buffer_active_clients) do |     for _, client_ids in pairs(all_buffer_active_clients) do | ||||||
|       client_ids[client_id] = nil |       client_ids[client_id] = nil | ||||||
|     end |     end | ||||||
| @@ -771,6 +767,7 @@ function lsp.start_client(config) | |||||||
|     rpc = rpc; |     rpc = rpc; | ||||||
|     offset_encoding = offset_encoding; |     offset_encoding = offset_encoding; | ||||||
|     config = config; |     config = config; | ||||||
|  |     attached_buffers = {}; | ||||||
|  |  | ||||||
|     handlers = handlers; |     handlers = handlers; | ||||||
|     -- for $/progress report |     -- for $/progress report | ||||||
| @@ -989,7 +986,6 @@ function lsp.start_client(config) | |||||||
|  |  | ||||||
|     lsp.diagnostic.reset(client_id, all_buffer_active_clients) |     lsp.diagnostic.reset(client_id, all_buffer_active_clients) | ||||||
|     changetracking.reset(client_id) |     changetracking.reset(client_id) | ||||||
|     all_client_active_buffers[client_id] = nil |  | ||||||
|     for _, client_ids in pairs(all_buffer_active_clients) do |     for _, client_ids in pairs(all_buffer_active_clients) do | ||||||
|       client_ids[client_id] = nil |       client_ids[client_id] = nil | ||||||
|     end |     end | ||||||
| @@ -1032,6 +1028,7 @@ function lsp.start_client(config) | |||||||
|       -- TODO(ashkan) handle errors. |       -- TODO(ashkan) handle errors. | ||||||
|       pcall(config.on_attach, client, bufnr) |       pcall(config.on_attach, client, bufnr) | ||||||
|     end |     end | ||||||
|  |     client.attached_buffers[bufnr] = true | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   initialize() |   initialize() | ||||||
| @@ -1142,12 +1139,6 @@ function lsp.buf_attach_client(bufnr, client_id) | |||||||
|     }) |     }) | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   if not all_client_active_buffers[client_id] then |  | ||||||
|     all_client_active_buffers[client_id] = {} |  | ||||||
|   end |  | ||||||
|  |  | ||||||
|   table.insert(all_client_active_buffers[client_id], bufnr) |  | ||||||
|  |  | ||||||
|   if buffer_client_ids[client_id] then return end |   if buffer_client_ids[client_id] then return end | ||||||
|   -- This is our first time attaching this client to this buffer. |   -- This is our first time attaching this client to this buffer. | ||||||
|   buffer_client_ids[client_id] = true |   buffer_client_ids[client_id] = true | ||||||
| @@ -1172,7 +1163,7 @@ end | |||||||
| --- Gets a client by id, or nil if the id is invalid. | --- Gets a client by id, or nil if the id is invalid. | ||||||
| --- The returned client may not yet be fully initialized. | --- The returned client may not yet be fully initialized. | ||||||
| -- | -- | ||||||
| ---@param client_id client id number | ---@param client_id number client id | ||||||
| --- | --- | ||||||
| ---@returns |vim.lsp.client| object, or nil | ---@returns |vim.lsp.client| object, or nil | ||||||
| function lsp.get_client_by_id(client_id) | function lsp.get_client_by_id(client_id) | ||||||
| @@ -1181,15 +1172,11 @@ end | |||||||
|  |  | ||||||
| --- Returns list of buffers attached to client_id. | --- Returns list of buffers attached to client_id. | ||||||
| -- | -- | ||||||
| ---@param client_id client id | ---@param client_id number client id | ||||||
| ---@returns list of buffer ids | ---@returns list of buffer ids | ||||||
| function lsp.get_buffers_by_client_id(client_id) | function lsp.get_buffers_by_client_id(client_id) | ||||||
|   local active_client_buffers = all_client_active_buffers[client_id] |   local client = lsp.get_client_by_id(client_id) | ||||||
|   if active_client_buffers then |   return client and vim.tbl_keys(client.attached_buffers) or {} | ||||||
|     return active_client_buffers |  | ||||||
|   else |  | ||||||
|     return {} |  | ||||||
|   end |  | ||||||
| end | end | ||||||
|  |  | ||||||
| --- Stops a client(s). | --- Stops a client(s). | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user