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

feat: allow conditional attachment of LSP servers #3302

Closed

Conversation

ronandalton
Copy link

This PR adds the ability for users to conditionally attach an LSP server to a buffer based on a user provided function that will be evaluated. This functionality is useful for disabling an LSP server for a particular project, or for files in a particular subdirectory. Currently there is no good way of doing this.

With this addition, disabling an LSP for a given project is easy:

server.should_attach = function()
  return not vim.g.disable_lsp
end

Then in the project's .exrc file the following can be added:

let g:disable_lsp = v:true

Related issues:

@glepnir
Copy link
Member

glepnir commented Sep 15, 2024

This functionality is useful for disabling an LSP server for a particular project, or for files in a particular subdirectory. Currently there is no good way of doing this.

emm i think there has a way to do .

vim.api.nvim_create_autocmd('LspAttach' ,
      callback = function(args)
              local bufnr = args.buf
              local bufname = vim.api.nvim_buf_get_name(bufnr)
              if should_not_attach then
                       vim.lsp.buf_detach_client(bufnr, args.data.client_id)
              end
      end

@glepnir glepnir closed this Sep 15, 2024
@justinmk
Copy link
Member

same answer as before #2477 (comment)

nvim-lspconfig is a bunch of configs. It's not a platform. It is not going to continue gaining random features.

Use vim.lsp.start() for full control.

@ronandalton
Copy link
Author

emm i think there has a way to do .

Like I said in my PR, there isn't currently a good way of doing this. Starting the LSP and then stopping it again is not an ideal solution.

nvim-lspconfig is a bunch of configs. It's not a platform. It is not going to continue gaining random features.
Use vim.lsp.start() for full control.

lspconfig already has a bunch of code that handles launching of servers so the user doesn't need to worry about the technicalities of it. Using vim.lsp.start() directly would probably involve a bunch more custom code the user needs to write (and have the time and knowledge to know how to write). This PR only adds a few lines of code (really just 3) and solves this problem for these users. I don't really see how that can be a bad thing 🙂

@glepnir
Copy link
Member

glepnir commented Sep 15, 2024

this is not stop any client it just remove buffer of client on_attach

@ronandalton
Copy link
Author

Actually I tried that code but it doesn't seem to work. I just get an error (even after putting the callback in braces). attempt to index local 'buf_state' (a nil value).

@glepnir
Copy link
Member

glepnir commented Sep 15, 2024

wrap in vim.schedule(function() vim.lsp.buf_detach_client(bufnr, args.data.client_id) end)

@ronandalton
Copy link
Author

Thanks, that worked. So just to clarify, this will stop the server from ever starting, correct? If this is the case then it seems to achieve the same goal as my code change. Given that this the solution you provided is robust, would it be a good idea to document it somewhere (perhaps in a FAQ if we have one)?

@ronandalton
Copy link
Author

The docs seem to say that LspAttach is triggered after an LSP client attaches to a buffer. That would make me think that the server was already started, unless the autocommands are run before doing that.

@glepnir
Copy link
Member

glepnir commented Sep 15, 2024

This will not prevent the server process from starting. It will just remove the buffer from the server's attatched_buffers. If you want to stop the client after remove buffer from server add check continue like
vim.tbl_isempty(client.attached_buffer) then vim.lsp.stop_client

If you want more detailed control over the start and stop of the server, please read :help vim.lsp.start interface to customize , We focus on providing any useful capabilities.

lsp is asynchronous in the editor. There is an event loop queue scheduling.

@neovim neovim locked as resolved and limited conversation to collaborators Sep 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants