-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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: autostart=false: attach when editing new, nonexistent file (#2712 attempt 2) #3355
base: master
Are you sure you want to change the base?
Conversation
BufReadPost does not trigger when editing a nonexistent file, so language servers would not automatically attach when editing a new file if autostart was disabled or if the server had no associated filetypes. This fixes that by adding BufNewFile to autocommands hooked to BufReadPost.
Sometimes, BufNewFile triggers before 'filetype' is set. Using vim.schedule() should ensure filetype detection runs before the callback.
I don't think schedule is correct here. r pyright started in single file mode which means we send empty root to the server instead of pwd. Register a FileType event to check if it matches pwd. does this works ? diff --git a/lua/lspconfig/configs.lua b/lua/lspconfig/configs.lua
index 391a329..8f166ea 100644
--- a/lua/lspconfig/configs.lua
+++ b/lua/lspconfig/configs.lua
@@ -177,6 +177,20 @@ function configs.__newindex(t, config_name, config_def)
end
local pseudo_root = #bufname == 0 and pwd or util.path.dirname(util.path.sanitize(bufname))
M.manager:add(pseudo_root, true, bufnr)
+ api.nvim_create_autocmd('FileType', {
+ pattern = config.filetypes or '*',
+ callback = function(arg)
+ local parent = vim.fs.dirname(api.nvim_buf_get_name(arg.buf))
+ if not (parent == pseudo_root) then
+ return
+ end
+ if #M.manager:clients() == 0 then
+ return true
+ end
+ M.manager:try_add_wrapper(arg.buf, root_dir)
+ end,
+ group = lsp_group,
+ })
end
end)
end
|
lua/lspconfig/configs.lua
Outdated
-- Sometimes, BufNewFile triggers before 'filetype' is set. | ||
vim.schedule(function() | ||
if api.nvim_buf_is_valid(opt.buf) then | ||
M.manager:try_add(opt.buf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't try_add / try_add_wrapper check nvim_buf_is_valid
? they are "try" patterns, after all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered this myself, actually, but didn't have a strong opinion either way.
- Both functions would need a check, so code size/complexity is about the same either way.
- Moving the checks might prevent another bug like this in the future.
try_add_wrapper()
would do the check twice, but that's not really a problem.
If your intuition is to move the checks, I might as well.
how is that related to the use of schedule? in this PR, schedule is sending the |
Personally, I wouldn't expect the server to auto-attach to newly-opened files in single file mode, though this isn't a bad thing. This doesn't fix the problem when not running in single file mode, though. The only reason |
Maybe it wasn't obvious enough in #2711, but the init script to reproduce that issue forces the workspace root to be the working directory when the server is started: root_dir = function() return vim.fn.getcwd() end, |
Currently avoids an issue where a `FileType` autocommand schedules a call to one of these functions but the buffer gets wiped out before the next event loop. `nvim_get_option_value()` with the `filetype` argument can cause this as it creates a transient buffer that only lasts for a single function call.
to unblock this, i suggest:
|
f34b21a
to
a673c83
Compare
We never recommend setting the root directory like this. https://github.com/neovim/nvim-lspconfig/blob/master/CONTRIBUTING.md#adding-a-server-to-lspconfig I'm guessing is because the eval functions like getcwd cwd etc are not api-fast. Another point. Do we need BufReadPost at all? First it will only work if the file exists. Registering this event will match the pattern for any file opened. Second lsp has specific file type support, so shouldn't it only need FileType? And FileType will trigger even if the buffer does not exist. diff --git a/lua/lspconfig/configs.lua b/lua/lspconfig/configs.lua
index 391a329..81f46be 100644
--- a/lua/lspconfig/configs.lua
+++ b/lua/lspconfig/configs.lua
@@ -142,9 +142,12 @@ function configs.__newindex(t, config_name, config_def)
end
if root_dir then
- api.nvim_create_autocmd('BufReadPost', {
- pattern = fn.fnameescape(root_dir) .. '/*',
+ api.nvim_create_autocmd('FileType', {
+ pattern = config.filetypes,
callback = function(arg)
+ if not (vim.fs.dirname(api.nvim_buf_get_name(arg.buf)) == root_dir) then
+ return
+ end
if #M.manager:clients() == 0 then
return true
end |
The method of setting Currently, a server is automatically started/attached in any of these cases, when
A server is NOT started/attached, when
Regardless of
Unless
In any of the above cases, if a server would be started/attached and I can see these solutions:
Your alternate root detection logic would work, but not for files with no detected filetype. To handle nesting, the condition needs to be changed to something like |
we also have |
Is this possible? If a server is already attached, this doesn't matter, and if a server isn't attached, what would try to interact with it that can be triggered without the server already attached? |
These are the only servers under
|
if no filetypes can't we use wildcards like |
Both If, however, we can assume that the buffer's |
it triggers in that case because filetype=text. though it doesn't trigger for |
Okay, that was a terrible example. I should have checked that first. 😆
I don't know how important it is to preserve autostart/autoattach for these files, as I don't need this myself, but it should currently work (as long as the file already exists), so it would be nice not to go backwards here. I'm hoping |
It might actually be a bug if lsp is activated for unknown buffers.
I don't understand why lsp configs would not have any filetype associations. Do people want lsp activated for every random file regardless of its type? If it simplifies the code, maybe it's not worth supporting that. |
My last comment was question(s) for @glepnir . Main question is, can we try this out, or is there an obvious, serious risk that I'm missing? |
This is a fixed version of #2712, which was merged and reverted.
Fixes #2711