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

Rename handler #19

Open
kristijanhusak opened this issue Dec 28, 2020 · 9 comments
Open

Rename handler #19

kristijanhusak opened this issue Dec 28, 2020 · 9 comments

Comments

@kristijanhusak
Copy link

Hi,

any chance to introduce rename handler? CoC for example shows floating window at cursor to enter new name, and triggers lsp rename on enter.
I tried doing this myself, but popfix doesn't allow setting modifiable on the floating buffer.

@RishabhRD
Copy link
Owner

Hi! I guess this is possible. Great feature advice. I would be glad to add it.
I would add the PR in an hour or two. You can also help me in completing PR. 😄

@kristijanhusak
Copy link
Author

I can add a PR for this handler, I just need you to introduce ability on popfix to allow editing the buffer. Once I have that I can do everything else myself :)

@RishabhRD
Copy link
Owner

RishabhRD commented Dec 28, 2020

Sure, I would just explain the basic process of creating prompt using popfix
and handling the event. 😄

Mostly, we need a prompt buffer for this. Popfix provides an easy way of having prompt buffer on cursor using:

    local opts = {
	prompt = {
	    border = true,
	    title = 'Renamed',
	    highlight = 'Normal',
	    prompt_highlight = 'Normal',
	    init_text = init_text
	},
	mode = 'cursor',
	keymaps = {
	    i = {
		['<C-c>'] = close_cancelled,
		['<CR>'] = edit_close,
	    },
	    n = {
		['<CR>'] = edit_close,
		['q'] = close_cancelled,
		['<C-c>'] = close_cancelled,
		['<Esc>'] = close_cancelled,
	    }
	},
    }
    local popup = popfix:new(opts)

where close_cancelled, edit_close are functions that accept the parameter popup. They will be called when these keymaps are invoked.

One good example of reference can be my
nvim-cheat.sh plugin. This
plugin creates a prompt but just relative to the editor.

In the file init.lua of nvim-cheat.sh, you can just have a look at the last 50
lines. Those will show the process of creating prompt. And jumping to one of
the definitions of either edit_close or close_cancelled would explain how to close
the prompt and act on the event.

I hope nvim-cheat.sh would be a good example. If not sufficient please tell me, I
would explain further. 😄

There is also a popfix bug that I have also shown in nvim-cheat.sh code. I am
trying to fix it. If fixed before merging PR we can just remove those
additional 4 lines due to bug.

@kristijanhusak
Copy link
Author

Yeah prompt solves most of my issues.
I noticed one thing though.

I have this code that I call on some word:

local function edit_callback(_, line)
  print(line)
  vim.cmd('stopinsert')
end

local function close_popup(popup)
  popup:close()
  vim.cmd('stopinsert')
end

function _G.test_popup()
  local rename_opts = {
    mode = 'cursor',
    prompt = {
      border = true,
      title = '',
      init_text = vim.fn.expand('<cword>')
    },
    keymaps = {
      i = {
        ['<CR>'] = function(popup)
          popup:close(edit_callback)
        end,
        ['<Esc>'] = close_popup,
        ['<C-c>'] = close_popup
      },
    }
  }
  require'popfix':new(rename_opts)
end

And when I'm on some word, i do :call v:lua.test_popup().
It properly shows everything, but I'm not able to delete init_text. How can I provide a default value to the prompt that is editable?

@RishabhRD
Copy link
Owner

RishabhRD commented Dec 28, 2020

Hi! Actually this is not a popfix bug. But this is a neovim bug (atlest neovim
nightly, I didn't check with neovim stable). Vim doesn't have this
issue with it's buftype prompt. That's why I didn't try to change the
behavior of plugin as it should be fixed by neovim not by me.

I would recommend an experiment that would make it more clear.
Just open an empty buffer in vim and neovim separately.
Just make buftype to be prompt using:

:set buftype=prompt

Then write some text in the buffer in insert mode. Switch to normal mode using
<esc> and again switch to insert mode using i. Just try to delete characters
using backspace now. You will notice that neovim would not delete character
however, vim would.

Similarly, you can try to use I (insert to start of the line) after writing
some text in neovim prompt buffer and vim prompt buffer. You would notice that
adding text in start would do in reverse order for neovim but correctly for
vim.

But there is still a way to edit
You can still delete characters using normal mode. And insert text should work
fine except for I insert switching.

I really wanted to work on this behavior in neovim code but couldn't due to
academics. But this is the neovim prompt buftype situation currently.
😅

@RishabhRD
Copy link
Owner

This is the behavior difference:

For vim:
vim

For neovim:
neovim

(Bottom right of gif also shows my keypresses)

@kristijanhusak
Copy link
Author

I wasn't even aware of the buftype=prompt.
I guess we will put this on hold until that is fixed, because without default value it's kinda pointless.

@RishabhRD
Copy link
Owner

Yeah! that's why I didn't make fuzzy search of symbols as default till now due to this weird prompt behavior. I don't know even this is mentioned in the neovim issue list currently. I searched but didn't find anything related to it.

I would mention you here if I find it to be fixed. 😄

@eruizc-dev
Copy link

What's the state of this? I'd like to have this feature and may be able to code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants