vigoux / architext.nvim

:rocket: Structural editing powered by treesitter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: call `cmd.run` with `table` arg and apply changes to `file/path` instead of open `buffer`.

molleweide opened this issue · comments

Hi Vigoux,

This plugin is such a cool tool and I am studying the source now when I am learning about treesitter.

feat req

I would like to be able to apply edits with architext to a filepath instead of open buffer. My thinking is to modify the cmd.run func and make it so that you can pass a table with all necessary params, (including path = ...) so that

Do you have any recommendations on how to write the edits to a file that has not been loaded into a buffer? I see that many of you who are more proficient with treesitter and lsp use the lsp_range in order to make edits but I am not sure how to use this when writing to a file not yet loaded into buffer. Browsing through the docs myself I am not yet sure which is the more correct way of doing this?

If you could give me some guidance on how to write edits to a file, then I would like to try to make a pull request for this feature.

edit start

I have been thinking about the vim.lsp.utils.apply_text_edits function and realise that it updates all captures across the whole project and so maybe it is a requirement that one first reads the file into a buffer.

Maybe, something like vim.lsp.buf.references could be used to find references for the filepath and then you would manually update all paths that include the references?

edit end

Please tell me if you understand my idea :)

my use case

I am using doom-nvim and am working on some fun UI for the next unreleased version. If it was possible to use Architext on a filepath, then it would be quite easy to make some cool UI for doom that allows you to toggle and update config settings with eg. telescope really fast.

An example would be toggle module(s) action where I'd pipe all config modules into telescope, and then if you select module x then I would tell Architext find module name x in the module_enabled file and then comment it out and reload the config. So that one could eg. batch turn on/off all config modules in one single command with Architext.

question about the code

How does the command understand that everything after :A is the argument for the command? I read about under the map.txt docs but I don't understand what it is that specifies this?

Cheers.

So, first of all, we can't make edits to a file that is not loaded into neovim, and thus we have to load it in a buffer.

Now to the other part of the question (if I undersand it correctly): the idea would be to make the implementation more modular in order to run the edits on other buffers.

From what I remember, the cmd.run function could take an additional bufnr as argument without too much effort.

Is that what you would need ?

Thank you for attending to my idea Vigoux!

From what I remember, the cmd.run function could take an additional bufnr as argument without too much effort.

Is that what you would need ?

Yes I believe this is exactly what I am asking for. I have a query and I want to perform the replacement to file not loaded.

Based on my reading, the following would be one way to write the changes to the path.
Does this make sense to you?

local function path_transform_with_architext(edits, path)

  local buf_prefix = "architext_replace_path_tmp_buf"
  local fd = vim.fn.readfile(path)
  local b = vim.api.nvim_create_buf(false, true)
  local blc = vim.api.nvim_buf_line_count(b)
  vim.api.nvim_buf_set_name(b, buf_prefix .. ":" .. path)
  vim.api.nvim_buf_set_text(b, 0, 0, blc, "$", fd) -- I dunno if "$" is correct for last position
  vim.lsp.util.apply_text_edits(edits, b)
  
  local text_to_write = vim.api.nvim_buf_get_lines(b, 0, "S")
  vim.fn.writefile(text_to_write, path)
end

I think you can also use lsp utility functions to read the file based on an URI.
I don't remember which one though... And I am away from my computer at the moment.

Allright cool! I will do some more reading on my own and add it to my codesnippet above if I figure something out for myself.

without too much effort.

Ahaa, I realise now that then this would just be a matter of an if bufnr == nil then bufnr = a.nvim_get_cur_buf() end.

The function was called vim.uri_to_bufnr.