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

bug: Treesitter and LSP not loaded when editing new file #169

Closed
3 tasks done
leonasdev opened this issue Feb 4, 2023 · 12 comments · Fixed by #206
Closed
3 tasks done

bug: Treesitter and LSP not loaded when editing new file #169

leonasdev opened this issue Feb 4, 2023 · 12 comments · Fixed by #206
Labels
bug Something isn't working

Comments

@leonasdev
Copy link

Did you check docs and existing issues?

  • I have read all the LazyVim docs
  • I have searched the existing issues of LazyVim
  • I have searched the exsiting issues of plugins related to this issue

Neovim version (nvim -v)

0.8.1

Operating system/version

Ubuntu 22.04

Describe the bug

When you edit a new file (e.g. nvim newfile.lua), treesitter and LSP will not be loaded, and I think it's because treesitter and lsp-config are lazy-load with BufReadPre or BufReadPost.

In Neovim docs, it says BufReadPre BufReadPost starting to edit an existing file.

Shouldn't editing a new file behave the same as editing an existing file?

The same situation also exists with these plugins: gitsigns, indent-blankline, mini.indentscope, null-ls, persistence, todo-comments, vim-illuminate

Steps To Reproduce

  1. nvim newfile.lua

Expected Behavior

Editing a new file behaves the same as editing an existing file.

Repro

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")
-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  "folke/LazyVim",
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here
@leonasdev leonasdev added the bug Something isn't working label Feb 4, 2023
@dpetka2001
Copy link
Contributor

If you edit the nvim-lspconfig for event BufNewFile it should work. I just tried it and it works
as expected. As an example:

  {
    "neovim/nvim-lspconfig",
    event = { "BufReadPre", "BufNewFile" },
  },

should do the work. Just put the above in a spec under your plugins directory.

@leonasdev
Copy link
Author

If you edit the nvim-lspconfig for event BufNewFile it should work. I just tried it and it works as expected. As an example:

  {
    "neovim/nvim-lspconfig",
    event = { "BufReadPre", "BufNewFile" },
  },

should do the work. Just put the above in a spec under your plugins directory.

yeah, that should work. or just BufEnter.

@dpetka2001
Copy link
Contributor

You should also configure the same for null-ls to have formatting support in new files as well.

@leonasdev
Copy link
Author

You should also configure the same for null-ls to have formatting support in new files as well.

yeah I think all my metions before should be concerned.

@dpetka2001
Copy link
Contributor

I just tried BufEnter but although it recognizes the server that should be attached, it doesn't actually attach
it. BufNewFile attaches the server as expected.
As for your concerns I don't know if this should be the default behavior or not, that would up to the developer
to decide. And making these changes to get the support that you need is really trivial, so it shouldn't be much of
a problem.

@leonasdev
Copy link
Author

leonasdev commented Feb 4, 2023

And making these changes to get the support that you need is really trivial, so it shouldn't be much of
a problem.

Yeah, it’s up to maintainers, however I would never say that editing a new file with fully feature support is "just my personal use case".

@folke
Copy link
Collaborator

folke commented Feb 6, 2023

When I edit a new file as you describe, then treesitter does attach. Am I missing something here?

Lsp servers only attach after you saved the file and then reload it :e. That's a limitation on the Neovim lsp implementation I believe.

What difference does the BufNewFile make for you?

@dpetka2001
Copy link
Contributor

I didn't know about this LSP limitation. Good to know. This renders BufNewFile needless indeed. Thanks for
your help. Although it didn't bother me personally as I usually used NeoTree to create and open new files
and it always attached LSP (though that must be because it inherently does what you said when creating and
opening the file).

@xulongwu4
Copy link

xulongwu4 commented Feb 7, 2023

In my experience, adding the BufNewFile event does help when I open a non-existing file. For example, if I do nvim abc.py where abc.py was not in my filesystem, the LSP won't attach until I reload it if I don't use the BufNewFile event. On the contrary, if I use the BufNewFile event, the LSP will attach automatically and I don't have to reload it. I guess opening a non-existing file will not trigger the BufReadPost event.

@leonasdev
Copy link
Author

leonasdev commented Feb 7, 2023

When I edit a new file as you describe, then treesitter does attach. Am I missing something here?

yes...treesitter does attach, think I have accidentally opened a file that does not have the ts parser when I'm testing.

Treesitter will be loaded due to notify plugin does require treesitter.

Lsp servers only attach after you saved the file and then reload it :e. That's a limitation on the Neovim lsp implementation I believe.

What difference does the BufNewFile make for you?

When I'm testing this, using BufNewFile event to lazy-load lspconfig, it truly does attach the lsp server when editing an non-existing file.

BufEnter and BufReadPre both do not attach to lsp server just like @dpetka2001 said.

2023-02-07.10-27-40.mp4

@folke folke closed this as completed in 5f5a564 Feb 7, 2023
@folke
Copy link
Collaborator

folke commented Feb 7, 2023

Damn, I always thought that was a limitation of lspconfig, so thank you for this!

Just updated all events to include BufNewFile.

Everything now indeed seems to work for new files as well!

@leonasdev
Copy link
Author

Everything now indeed seems to work for new files as well!

Glad you fix it!👍👍

ofrades pushed a commit to ofrades/LazyVim that referenced this issue May 30, 2023
joshmedeski pushed a commit to joshmedeski/LazyVim that referenced this issue Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants