VonHeikemen / searchbox.nvim

Start your search from a more comfortable place, say the upper right corner?

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for live substitution

Akianonymus opened this issue · comments

Tried to do that once, neovim would not let me modify the text of the buffer while the user is typing on the input.

How about using a virtual text ? 🤔

like on top of the original text? Or what do you have in mind?

like on top of the original text? Or what do you have in mind?

Yeah, virtual text can write over original text, with highlights and such too.

Basically my target is to replicate live substitution like : substitute command.

can virtual text move the rest of the text in a "natural way"? If I change awesome with awe will the rest of the line adapt to that?

Well, didn't think about it. I have a way, but it's kind of hacky.

Fetch the whole line, replace the text and then set that as virtual text as a whole line.

Probably highlights will be broken.

I'm not really interested in adding this feature, so unless there is an easy way to do it I probably wont add it.

I guess the closest thing people could do right now is use the on_done hook to drop into commandline-mode and do the substitution.

Here is some untested code to show people:

require('searchbox').setup({
  hooks = {
    on_done = function(value, search_type)
      if value == nil then return end

      if vim.g.live_substitution then
        local keys = ':%s///g<Left><Left>'
        vim.api.nvim_feedkeys(
          vim.api.nvim_replace_termcodes(keys, true, true, true),
          'n',
          true
        )
        vim.g.live_substitution = false
      end
    end
  }
})

Then you could have keybinding like this.

vim.keymap.set('n', '<leader>r', function()
  vim.g.live_substitution = true
  require('searchbox').match_all()
end)

vim.keymap.set is in neovim 0.7, so keep that in mind.

I know is not ideal.

Fair enough.