How would one apply LSP text edits to filepath instead of buffer?

Hi,

There is a function vim.lsp.utils.apply_text_edits(edits, buf) which makes changes to a buffer.

Is there some equivalent pattern for applying edits/changes to a file path not loaded into a buffer yet?

Or should one load the file into a buffer first and then apply the edits, and then write it and close the buffer?

(I put this under Neovim category for now, but maybe it should be LSP category?)

edit

I just found out about the readfile and writefile commands which I have to look into.

Also, my question maybe also doesn’t make sense because I am quite new to this stuff…

1 Like

Yeah, you need to load a buffer or implement everything from scratch if you want to operate on file. All the helper functions in neovim operate on buffers. If you’re dealing with response from a language server, you’re gonna get a uri, which can be converted to a buffer with the helper function vim.uri_to_bufnr, which will handle creating the buffer for you if it doesn’t exist.

1 Like

Thanks for answering!

I was searching for a while trying to find the uri_to_bufnr helper but could never figure out how to find it so thank you very much for telling me about the function.