mirror of
				https://github.com/neovim/neovim.git
				synced 2025-11-04 01:34:25 +00:00 
			
		
		
		
	fix(lsp): trigger LspDetach on buffer delete (#28795)
Co-authored-by: Mathias Fussenegger <f.mathias@zignar.net>
This commit is contained in:
		@@ -576,10 +576,19 @@ local function buf_attach(bufnr)
 | 
				
			|||||||
    on_detach = function()
 | 
					    on_detach = function()
 | 
				
			||||||
      local params = { textDocument = { uri = uri } }
 | 
					      local params = { textDocument = { uri = uri } }
 | 
				
			||||||
      for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do
 | 
					      for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do
 | 
				
			||||||
 | 
					        api.nvim_exec_autocmds('LspDetach', {
 | 
				
			||||||
 | 
					          buffer = bufnr,
 | 
				
			||||||
 | 
					          modeline = false,
 | 
				
			||||||
 | 
					          data = { client_id = client.id },
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        changetracking.reset_buf(client, bufnr)
 | 
					        changetracking.reset_buf(client, bufnr)
 | 
				
			||||||
        if client.supports_method(ms.textDocument_didClose) then
 | 
					        if client.supports_method(ms.textDocument_didClose) then
 | 
				
			||||||
          client.notify(ms.textDocument_didClose, params)
 | 
					          client.notify(ms.textDocument_didClose, params)
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        local namespace = lsp.diagnostic.get_namespace(client.id)
 | 
				
			||||||
 | 
					        vim.diagnostic.reset(namespace, bufnr)
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
      for _, client in ipairs(all_clients) do
 | 
					      for _, client in ipairs(all_clients) do
 | 
				
			||||||
        client.attached_buffers[bufnr] = nil
 | 
					        client.attached_buffers[bufnr] = nil
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -475,6 +475,12 @@ describe('LSP', function()
 | 
				
			|||||||
        local server = _create_server()
 | 
					        local server = _create_server()
 | 
				
			||||||
        local bufnr = vim.api.nvim_create_buf(false, true)
 | 
					        local bufnr = vim.api.nvim_create_buf(false, true)
 | 
				
			||||||
        vim.api.nvim_set_current_buf(bufnr)
 | 
					        vim.api.nvim_set_current_buf(bufnr)
 | 
				
			||||||
 | 
					        local detach_called = false
 | 
				
			||||||
 | 
					        vim.api.nvim_create_autocmd("LspDetach", {
 | 
				
			||||||
 | 
					          callback = function()
 | 
				
			||||||
 | 
					            detach_called = true
 | 
				
			||||||
 | 
					          end
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
        local client_id = vim.lsp.start({ name = 'detach-dummy', cmd = server.cmd })
 | 
					        local client_id = vim.lsp.start({ name = 'detach-dummy', cmd = server.cmd })
 | 
				
			||||||
        assert(client_id, "lsp.start must return client_id")
 | 
					        assert(client_id, "lsp.start must return client_id")
 | 
				
			||||||
        local client = vim.lsp.get_client_by_id(client_id)
 | 
					        local client = vim.lsp.get_client_by_id(client_id)
 | 
				
			||||||
@@ -486,11 +492,13 @@ describe('LSP', function()
 | 
				
			|||||||
          client_id = client_id,
 | 
					          client_id = client_id,
 | 
				
			||||||
          num_attached_before = num_attached_before,
 | 
					          num_attached_before = num_attached_before,
 | 
				
			||||||
          num_attached_after = num_attached_after,
 | 
					          num_attached_after = num_attached_after,
 | 
				
			||||||
 | 
					          detach_called = detach_called,
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      ]])
 | 
					      ]])
 | 
				
			||||||
      eq(true, result ~= nil, 'exec_lua must return result')
 | 
					      eq(true, result ~= nil, 'exec_lua must return result')
 | 
				
			||||||
      eq(1, result.num_attached_before)
 | 
					      eq(1, result.num_attached_before)
 | 
				
			||||||
      eq(0, result.num_attached_after)
 | 
					      eq(0, result.num_attached_after)
 | 
				
			||||||
 | 
					      eq(true, result.detach_called)
 | 
				
			||||||
    end)
 | 
					    end)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it('client should return settings via workspace/configuration handler', function()
 | 
					    it('client should return settings via workspace/configuration handler', function()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user