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

Create an open buffer when LSP requests a file #2261

Closed
Invertisment opened this issue Nov 27, 2022 · 9 comments
Closed

Create an open buffer when LSP requests a file #2261

Invertisment opened this issue Nov 27, 2022 · 9 comments
Labels
bug Something isn't working

Comments

@Invertisment
Copy link

Description

When I'm editing a source file and use LSP server's refactoring to add a test then I can't view the file.
I can't see the file in NvimTree and I can't see the file in :ls. The buffer isn't loaded and the file isn't saved on disk.

Neovim version

NVIM v0.8.0
Build type: Release
LuaJIT 2.1.0-beta3

Nvim-lspconfig version

abe6c99

Operating system and version

Linux 6.0.2-2

Affected language servers

clojure-lsp, maybe others as well

Steps to reproduce

  1. Open a project which an LSP server can handle (I used clojure-lsp but it can be any project that has LSP server refactoring that creates files)
  2. Trigger a file creation from LSP side
  3. File is not visible in NvimTree buffer
  4. File is not visible in :ls (maybe that's a clue, probably)

Actual behavior

When I choose 10: Create test for 'my-function' in clojure-lsp server's refactoring menu then I can't see the file.
If the test file that I'm adding doesn't exist then LSP server will send a file creation request to nvim so that it would be created (but not written yet).
So these "created but non-written" files are not displayed in vim.

Do you think it's a good idea to display them? These files also don't show up in the :ls menu. So it may be a problem with nvim itself or a problem with how nvim handles LSP. I don't know much how it's handled so I write here.

Expected behavior

Files should at least show up in :ls command menu. NvimTree could also show them (persist empty file to disk and load it) but first the :ls should show that the buffer exists.

Minimal config

I think this should be easily reproducible with any language server and any config as long as it has "create test" or a similar refactoring option.

LSP log

none

@Invertisment Invertisment added the bug Something isn't working label Nov 27, 2022
@glepnir
Copy link
Member

glepnir commented Nov 28, 2022

10: Create test for 'my-function'

about this from code action ?

@Invertisment
Copy link
Author

When I trigger that code action for clojure-lsp then it calls this function:
https://github.com/clojure-lsp/clojure-lsp/blob/01500c34e00efa2b4affc627bdc80e89af18f55d/lib/src/clojure_lsp/refactor/transform.clj#L970

But this function doesn't actually create files and only instructs the editor that LSP has edited that file.
That means that the editor doesn't pick it up.

@glepnir
Copy link
Member

glepnir commented Nov 28, 2022

looks like a code action kind is refactor and it will create a test file.

@Invertisment
Copy link
Author

Invertisment commented Nov 28, 2022

I know. But then why doesn't it show up in :ls command menu?
If it doesn't show then I can't navigate there. And if I type edit my-new-file then it creates a new file and the LSP file is lost because nvim didn't know about it.

@glepnir
Copy link
Member

glepnir commented Nov 28, 2022

ls command will output the buffers which set the buflisted option.

@glepnir
Copy link
Member

glepnir commented Nov 28, 2022

you can use this after you called the code action

local buf = vim.fn.bufadd('your new file')
vim.bo[buf].buflisted = true

then you will see this file in your ls command

@Invertisment
Copy link
Author

Ok. So... this needs to run when nvim gets input from LSP server, not when I decide it has to run 🤔
Can I even control it for each and every LSP server...?

@glepnir
Copy link
Member

glepnir commented Nov 29, 2022

you can..need rewrite the default handler of code action.

@sigmaSd
Copy link
Contributor

sigmaSd commented Nov 30, 2022

I did some debugging, it seems like clojure lsp wants the test file to exist before the code action, if you do that it will apply the edits correctly.

If you don't have the file already it sends workspace/applyEdit with no params which is weird
image

@glepnir glepnir closed this as completed Jan 15, 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

No branches or pull requests

3 participants