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

Question: Completion in nvim's terminal window #1219

Open
jamilraichouni opened this issue Oct 7, 2022 · 26 comments
Open

Question: Completion in nvim's terminal window #1219

jamilraichouni opened this issue Oct 7, 2022 · 26 comments

Comments

@jamilraichouni
Copy link

jamilraichouni commented Oct 7, 2022

Hi!

First: Thank you very much for the different completion/ source plugins @hrsh7th! The stuff is really helpful!

My question:
Is there any chance/ trick how I could achieve that nvim-cmp triggers in the internal terminal window?

Use cases:

  • Shell script snippets in zsh shell
  • Python snippets in IPython shell or Python shell
  • In general stuff provided by the cmp-buffer

That would be really awesome!

Many thanks and all best,
Jamil

@Shougo
Copy link

Shougo commented Oct 7, 2022

I know it is useful.
But it is very hard. Because neovim does not support it...

@jamilraichouni
Copy link
Author

Thanks for the quick response. What exactly is not supported? Is it popup menu's in the terminal buffer? Or what else is missing?
Maybe it makes sense to raise an according feature request directly addressed at the Neovim contributors!?

@Shougo
Copy link

Shougo commented Oct 7, 2022

well, I had to implement the feature in pum.vim.
The terminal mode doesn't support get current cursor and it doesn't support the text insertion(need feedkeys).

@Shougo
Copy link

Shougo commented Oct 7, 2022

The popup menu can be displayed in terminal mode.
But the completion doesn't work well.
You can request the feature in neovim.

@jamilraichouni
Copy link
Author

jamilraichouni commented Oct 7, 2022

well, I had to implement the feature in pum.vim. The terminal mode doesn't support get current cursor and it doesn't support the text insertion(need feedkeys).

I'm not an expert in automating (n)vim and at the same time I can command

:call feedkeys("\i") | call feedkeys("ls -l<cr>")

when I'm in the terminal to enter insert mode and command ls -l.
I assume you mean something else.

@Shougo
Copy link

Shougo commented Oct 7, 2022

I will test it later...

@Shougo
Copy link

Shougo commented Oct 8, 2022

I have tested it and I found the critical problem.

In terminal mode, it does not have CmdChanged/InsertChanged autocmd.

@Shougo
Copy link

Shougo commented Oct 10, 2022

I have created the PR for it.

neovim/neovim#20567

@yyy33
Copy link

yyy33 commented Oct 14, 2022

I have tested it and I found the critical problem.

In terminal mode, it does not have CmdChanged/InsertChanged autocmd.

@Shougo For zsh, I'm not sure if this can be done with zle + $NVIM(unix socket) + user autocmd

@Shougo
Copy link

Shougo commented Oct 14, 2022

Well, it may be work. But this is just hack. I don't want to hack it.

@yyy33
Copy link

yyy33 commented Oct 14, 2022

Well, it may be work. But this is just hack. I don't want to hack it.

Yes, it would be best if it could be implemented in neovim, it looks like you are familiar with both neovim and nvim-cmp projects, I am a newbie, mind sharing how you read open source projects quickly?

@Shougo
Copy link

Shougo commented Oct 14, 2022

Hm... It is hard problem.
It needs very long experience.
I have worked for Vim in 15 years.

@yyy33
Copy link

yyy33 commented Oct 14, 2022

Hm... It is hard problem. It needs very long experience. I have worked for Vim in 15 years.

Excellent!

@Shougo
Copy link

Shougo commented Nov 8, 2022

The patch is already merged!

@jamilraichouni
Copy link
Author

Great, so I just installed the latest nightly build for nvim. How can I get nvim-cmp to trigger and show any snippets?
A small example would be really cool.

Thank you very much @Shougo

@yyy33
Copy link

yyy33 commented Nov 12, 2022

Great, so I just installed the latest nightly build for nvim. How can I get nvim-cmp to trigger and show any snippets? A small example would be really cool.

Thank you very much @Shougo

I don't think nvim-cmp supports it yet, so you should manually add the trigger event TextChangedT to trigger the completion of nvim-cmp, #519 (comment)

@yyy33
Copy link

yyy33 commented Nov 12, 2022

Great, so I just installed the latest nightly build for nvim. How can I get nvim-cmp to trigger and show any snippets? A small example would be really cool.

Thank you very much @Shougo

You may also need to install a source, such as zsh:
https://github.com/tamago324/cmp-zsh

@Shougo
Copy link

Shougo commented Nov 12, 2022

I don't think nvim-cmp supports it yet, so you should manually add the trigger event TextChangedT to trigger the completion of nvim-cmp, #519 (comment)

Yes. It needs nvim-cmp's support.
And the terminal completion is not perfect.

@jamilraichouni
Copy link
Author

Hi!

I tried it out and could not get a snippet popup to appear in the terminal.

So when I configure nvim-cmp correctly it should work? Or do we need to wait for some feature addition here in this repo?

What I tried:

I added the zsh source and I also put the snippet from #519 (comment) into my init.vim. At the start of the lua function I added a print of the current time incl. seconds and always see an update of the time when I type something in the terminal.

autocmd TextChangedT * call s:on_complete_check()

function! s:on_complete_check() abort
lua <<EOF
 print(os.date("%X"))
 local line = vim.api.nvim_get_current_line()
 local cursor = vim.api.nvim_win_get_cursor(0)[2]

 local current = string.sub(line, cursor, cursor + 1)
 if current == "." or current == "," or current == " " then
    require('cmp').close()
 end

 local before_line = string.sub(line, 1, cursor + 1)
 local after_line = string.sub(line, cursor + 1, -1)
 if not string.match(before_line, '^%s+$') then
    if after_line == "" or string.match(before_line, " $") or string.match(before_line, "%.$") then
        require('cmp').complete()
    end
 end
EOF
endfunction

I get no snippet list to appear.
What I hope that it will be possible at some day (like a Christmas present) is that I can see zsh snippets in the terminal and Python snippets if I like to (running IPython in the terminal).

In fact I can put some wrapping code around the IPython command and do the switch in the background.

Hence, if I can get any zsh snippet to be shown, that would already be a big win.

@yyy33
Copy link

yyy33 commented Nov 28, 2022

Sorry, I don't know what the problem is, maybe we should wait for @Shougo's pr to solve the problem

@Shougo
Copy link

Shougo commented Nov 29, 2022

If you need the feature, nvim-cmp should support TextChangedT autocmd.
Unfortunately, it is not my work.

@Shougo
Copy link

Shougo commented Nov 29, 2022

@jamilraichouni I have tested your snippet.
TextChangedT works, but require('cmp').complete() does not work in terminal mode.

@Shougo
Copy link

Shougo commented Nov 29, 2022

diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua
index 6ec11a0..2cde661 100644
--- a/lua/cmp/core.lua
+++ b/lua/cmp/core.lua
@@ -371,7 +371,7 @@ core.confirm = function(self, e, option, callback)
   feedkeys.call('', 'n', function()
     -- Restore the line at the time of request.
     local ctx = context.new()
-    if api.is_cmdline_mode() then
+    if api.is_cmdline_mode() or api.is_terminal_mode() then
       local keys = {}
       table.insert(keys, keymap.backspace(ctx.cursor_before_line:sub(e:get_offset())))
       table.insert(keys, string.sub(e.context.cursor_before_line, e:get_offset()))
diff --git a/lua/cmp/utils/api.lua b/lua/cmp/utils/api.lua
index 8153c9e..89f3075 100644
--- a/lua/cmp/utils/api.lua
+++ b/lua/cmp/utils/api.lua
@@ -13,6 +13,8 @@ api.get_mode = function()
     return 's' -- select
   elseif mode == 'c' and vim.fn.getcmdtype() ~= '=' then
     return 'c' -- cmdline
+  elseif mode == 't' then
+    return 't' -- terminal
   end
 end
 
@@ -32,9 +34,13 @@ api.is_visual_mode = function()
   return api.get_mode() == 'x'
 end
 
+api.is_terminal_mode = function()
+  return api.get_mode() == 't'
+end
+
 api.is_suitable_mode = function()
   local mode = api.get_mode()
-  return mode == 'i' or mode == 'c'
+  return mode == 'i' or mode == 'c' or mode == 't'
 end
 
 api.get_current_line = function()

Here is the minimal patch to support terminal mode.

@yyy33
Copy link

yyy33 commented Dec 4, 2022

diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua
index 6ec11a0..2cde661 100644
--- a/lua/cmp/core.lua
+++ b/lua/cmp/core.lua
@@ -371,7 +371,7 @@ core.confirm = function(self, e, option, callback)
   feedkeys.call('', 'n', function()
     -- Restore the line at the time of request.
     local ctx = context.new()
-    if api.is_cmdline_mode() then
+    if api.is_cmdline_mode() or api.is_terminal_mode() then
       local keys = {}
       table.insert(keys, keymap.backspace(ctx.cursor_before_line:sub(e:get_offset())))
       table.insert(keys, string.sub(e.context.cursor_before_line, e:get_offset()))
diff --git a/lua/cmp/utils/api.lua b/lua/cmp/utils/api.lua
index 8153c9e..89f3075 100644
--- a/lua/cmp/utils/api.lua
+++ b/lua/cmp/utils/api.lua
@@ -13,6 +13,8 @@ api.get_mode = function()
     return 's' -- select
   elseif mode == 'c' and vim.fn.getcmdtype() ~= '=' then
     return 'c' -- cmdline
+  elseif mode == 't' then
+    return 't' -- terminal
   end
 end
 
@@ -32,9 +34,13 @@ api.is_visual_mode = function()
   return api.get_mode() == 'x'
 end
 
+api.is_terminal_mode = function()
+  return api.get_mode() == 't'
+end
+
 api.is_suitable_mode = function()
   local mode = api.get_mode()
-  return mode == 'i' or mode == 'c'
+  return mode == 'i' or mode == 'c' or mode == 't'
 end
 
 api.get_current_line = function()

Here is the minimal patch to support terminal mode.

@hrsh7th Can we merge this patch?

@jamilraichouni
Copy link
Author

jamilraichouni commented Dec 5, 2022

Here is the minimal patch to support terminal mode.

Thank you very much @Shougo !

I got it to run and will try that out within the next weeks.
Really cool!

@Shougo
Copy link

Shougo commented Dec 6, 2022

@hrsh7th Can we merge this patch?

Well, the patch is just works and more changes are needed. You can test it before.

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

Successfully merging a pull request may close this issue.

3 participants