zbirenbaum / copilot.lua

Fully featured & enhanced replacement for copilot.vim complete with API for interacting with Github Copilot
MIT License
2.45k stars 67 forks source link

Idea: This plugin should cache the last copilot buffer change for single-repeat. #146

Closed LiamKearn closed 1 year ago

LiamKearn commented 1 year ago

It would be cool if I could accept a copilot suggestion (word or line) and that suggestion would be "redoable" via the . key.

I'm not of a nvim lua api user but at a quick look:

vim.fn.setreg('.', 'etc')

doesn't seem to work. However:

print(vim.fn.inspect(vim.fn.getreg('.')))

does, maybe it's possible via some other API?

MunifTanjim commented 1 year ago

Not sure what you mean by "redoable". You want to insert the same text again? What's the use-case?

LiamKearn commented 1 year ago

Maybe I should have said "repeat" instead of redo | ..

I (like others) most usually use copilot for completion and I'd like to be able to repeat a buffer change it made elsewhere, usually I use it to complete signatures based on types and those changes can often be repeated.

Here is a quick screencast demoing what I mean: https://www.youtube.com/watch?v=t1fN4XNwIGk

MunifTanjim commented 1 year ago

You can write wrapper function for this.

local function accept_line()
  local start_cursor = vim.api.nvim_win_get_cursor(0)
  require("copilot.suggestion").accept_line()
  local end_cursor = vim.api.nvim_win_get_cursor(0)

  local text = table.concat(
    vim.api.nvim_buf_get_text(0, start_cursor[1] - 1, start_cursor[2], end_cursor[1] - 1, end_cursor[2], {}),
    "\n"
  )

  vim.fn.setreg("*", text)
end

vim.keymap.set("i", "<M-Down>", accept_line, {})

This will copy the inserted text into clipboard. Then you can paste it wherever you want.

But this is outside the scope of this plugin.

LiamKearn commented 1 year ago

Firstly, thank you for effort of piecing together a quick code example :)


But this is outside the scope of this plugin.

I wouldn't say I disagree but every other way (that I can recall) of interacting with my buffers respects the redo register. To me integrating with an established core feature is the sort of thing I'd call polish on a plugin (as opposed to bloat).


By any chance do you use the copilot-cmp integration for this plugin? Could you advise me if that works with the redo register via nvim-cmp?

MunifTanjim commented 1 year ago

To me integrating with an established core feature is the sort of thing I'd call polish on a plugin

Copilot acts as a language server that gives text suggestion. I don't think any of the language server clients support dot repeat for inserted text.

By any chance do you use the copilot-cmp integration for this plugin?

Sorry, nope.

Could you advise me if that works with the redo register via nvim-cmp?

If nvim-cmp supports it, sure. copilot-cmp integration just gets the suggestion from copilot, then feeds it to cmp for handling it.