Skip to content

Conversation

@Konfekt
Copy link
Contributor

@Konfekt Konfekt commented Nov 2, 2025

A stab at #688 ; if @813ethan could test and comment

@Konfekt Konfekt marked this pull request as draft November 2, 2025 12:32
@813ethan
Copy link

813ethan commented Nov 2, 2025

looks nice, is there a reason why the preview window is so small? compared to :LspHover
Screenshot_20251103-000805_Termux
i think the builtins line could be removed to save some space

Also is it possible if the preview window can be automatically closed after exiting the autocompletion menu? like jedi-vim and ycm

@813ethan
Copy link

813ethan commented Nov 2, 2025

now the preview closes after the autocompletion got selected, but it's still opened when there're no completions

also the preview window's size is still too small?

screen-20251103-010957.2.mp4

@813ethan
Copy link

813ethan commented Nov 3, 2025

ok tried testing it for a bit, seems like

d.info = 'Lazy doc'

the preview window's was initialized with 'Lazy doc', maybe this messed the window size up?

also can i suggest if there can be a toggle for (not) autoclosing the preview window? if it's not hard to implement

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 3, 2025

I have no idea why the preview window height is not sufficiently high; by default it should be (=12) and Vim respect it?

@813ethan
Copy link

813ethan commented Nov 3, 2025

the height seems to change accordingly with the documentation's length
(this is ycm btw)

screen-20251103-215927.2.mp4

@813ethan
Copy link

813ethan commented Nov 4, 2025

maybe resize the window after the doc is shown? This works for me

*** completion.vim.old	2025-11-05 08:35:36.036497263 +1100
--- completion.vim	2025-11-05 08:36:08.768497250 +1100
***************
*** 456,461 ****
--- 456,462 ----
        infoText->append(0)
        [1, 1]->cursor()
        exe $'setlocal ft={infoKind}'
+       exe 'resize' &previewheight
        :wincmd p
      catch /E441/ # No preview window
      endtry

@Konfekt Konfekt marked this pull request as ready for review November 4, 2025 22:00
@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 4, 2025

Thank you! We should be good to go @yegappan

@813ethan
Copy link

813ethan commented Nov 5, 2025

One more problem, there's this error showing up when im using preview with clangd?

Screenshot_20251105-151838_Termux

this is my lsp config:

{
          'name': 'clangd',
          'filetype': ['c', 'cpp'],
          'path': TERMUX_PREFIX .. '/usr/bin/clangd',
          'args': ['--background-index', '--clang-tidy']
}

All good otherwise, the error didn't happen with other clients like python-language-server

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 5, 2025

Could it be related to the just added

exe 'resize' &previewheight

?

Does it still show up without it?
When using silent! exe ... or better try .. catch E565 ... endtry?

@813ethan
Copy link

813ethan commented Nov 5, 2025

Could it be related to the just added

exe 'resize' &previewheight

?

error still shows up with the line removed

Does it still show up without it? When using silent! exe ... or better try .. catch E565 ... endtry?

same error with try ... catch, prolly caused by something else

@813ethan
Copy link

813ethan commented Nov 5, 2025

ok nvm try ... catch E565 here silenced the errors, but markdown isn't applied to the preview window

try
:wincmd P
setlocal ft=lspgfm
:wincmd p
catch /E441/

screen-20251105-181844.2.mp4

the error still exists after removing setlocal ft=lspgfm

@813ethan
Copy link

813ethan commented Nov 5, 2025

https://groups.google.com/g/vim_dev/c/UX9-Dgw86xA
maybe this is related?

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 5, 2025

Patch 8.2.2427 and 8.2.2426 disallow changing windows in the completefunc.

So with

   :wincmd P 
   setlocal ft=lspgfm 

make sure that P does not jump to the preview window

@813ethan
Copy link

813ethan commented Nov 5, 2025

Patch 8.2.2427 and 8.2.2426 disallow changing windows in the completefunc.

So with

   :wincmd P 
   setlocal ft=lspgfm 

make sure that P does not jump to the preview window

im unsure how it works, there's no error when im using python lsp, it only appears when using clangd

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 5, 2025

Can you try to make sense of the solution justmao945/vim-clang#140 ?

@813ethan
Copy link

813ethan commented Nov 6, 2025

Can you try to make sense of the solution justmao945/vim-clang#140 ?

it tried to move the problematic calls away from the autocomplete function?

I'm not an expert about the internals of this plugin (lsp), so i don't know if there's a way to adapt that here, not counting that this plugin is not clangd specific

yet something I noticed is that in python-language-server, LspSetPopupFileType() which causes the error was never called,

while clangd never uses lazy loading (lspserver.completionLazyDoc) and renders the documentation directly instead, maybe it's something about it?

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 6, 2025

it tried to move the problematic calls away from the autocomplete function?

Yes, could you distill which?

@813ethan
Copy link

813ethan commented Nov 6, 2025

my understanding is LspSetPopupFileType() is called to setup the completion popup's markdown highlighting, while it works for popup windows, it fails with preview windows bc of the error,

while lazy loading uses ShowCompletionDocumentation(), which also setups the markdown but didn't get any errors

maybe something with the implementation or timing of those functions? I have no idea

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 6, 2025

Thank you, so doing it the roundabout ShowCompletionDocumentation() way

#  autoload/lsp/completion.vim (lines 453-460)
  :wincmd P
  :setlocal modifiable
  bufnr()->deletebufline(1, '$')
  infoText->append(0)
  [1, 1]->cursor()
 setlocal ft=lspgfm
 :wincmd p
catch /E441/ # No preview window

solves that problem?

@813ethan
Copy link

813ethan commented Nov 6, 2025

ok nvm try ... catch E565 here silenced the errors, but markdown isn't applied to the preview window

try
:wincmd P
setlocal ft=lspgfm
:wincmd p
catch /E441/

screen-20251105-181844.2.mp4
the error still exists after removing setlocal ft=lspgfm

won't, the error will still happen with :wincmd P..

@813ethan
Copy link

813ethan commented Nov 6, 2025

acmds->add({bufnr: bnr,
event: 'CompleteChanged',
group: 'LSPBufferAutocmds',
cmd: 'LspSetPopupFileType()'})

actually can we even change the state of the preview window during CompleteChanged

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 7, 2025

Changing windows while inside a completion function is forbidden now.

I think changing means less ambiguously switching here; so we can still change, say the filetype, but have to stay inside the window

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 7, 2025

Here's the corresponding fix for php complete

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 7, 2025

Thank you, so doing it the roundabout ShowCompletionDocumentation() way

#  autoload/lsp/completion.vim (lines 453-460)
  :wincmd P
  :setlocal modifiable
  bufnr()->deletebufline(1, '$')
  infoText->append(0)
  [1, 1]->cursor()
 setlocal ft=lspgfm
 :wincmd p
catch /E441/ # No preview window

How come then this doesn't give any errors ?

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 7, 2025

More exactly, the file type is correctly set

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 7, 2025

For short, get the popup window id and then call win_execute(..) to change it, say &filetype. Could you look into that?

@813ethan
Copy link

813ethan commented Nov 7, 2025

Thank you, so doing it the roundabout ShowCompletionDocumentation() way

#  autoload/lsp/completion.vim (lines 453-460)
  :wincmd P
  :setlocal modifiable
  bufnr()->deletebufline(1, '$')
  infoText->append(0)
  [1, 1]->cursor()
 setlocal ft=lspgfm
 :wincmd p
catch /E441/ # No preview window

How come then this doesn't give any errors ?

It wasn't run on CompleteChanged, it's a separate process called (during the completion?) from lspserver.vim

@813ethan
Copy link

813ethan commented Nov 7, 2025

Changing windows while inside a completion function is forbidden now.

I think changing means less ambiguously switching here; so we can still change, say the filetype, but have to stay inside the window

the buffer is still being changed via the markdown renderer (lspgfm.vim)

tried setting the file type indirectly via

setbufvar(winbufnr(winnr('#')), '&ft', 'lspgfm')

but it gives another error
Screenshot_20251107-193318_Termux

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 7, 2025

The culprit is the wincmd p/P dance; instead, get the popup window id and then call win_execute(..) to change &filetype

@813ethan
Copy link

813ethan commented Nov 7, 2025

Changing windows while inside a completion function is forbidden now.

I think changing means less ambiguously switching here; so we can still change, say the filetype, but have to stay inside the window

the buffer is still being changed via the markdown renderer (lspgfm.vim)

tried setting the file type indirectly via

setbufvar(winbufnr(winnr('#')), '&ft', 'lspgfm')

but it gives another error Screenshot_20251107-193318_Termux

This changes the &filetype of the preview buffer, without doing the wincmdP/p dance.

how can I get the window id for win_execute(...)?

@813ethan
Copy link

813ethan commented Nov 7, 2025

Changing windows while inside a completion function is forbidden now.

I think changing means less ambiguously switching here; so we can still change, say the filetype, but have to stay inside the window

the buffer is still being changed via the markdown renderer (lspgfm.vim)

tried setting the file type indirectly via

setbufvar(winbufnr(winnr('#')), '&ft', 'lspgfm')

but it gives another error Screenshot_20251107-193318_Termux

var bnum = win_getid(winnr('#'))
call win_execute(bnum, 'setlocal ft=lspgfm')

seems like this gives the same error

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 7, 2025

setlocal modifiable has to be called, as above.

To get the preview window id, you can try

get(filter(getwininfo(), 'getwinvar(v:val.winid, "&previewwindow")'), 0, {'winid': 0}).winid

though surely there's a more elegant way

@813ethan
Copy link

813ethan commented Nov 7, 2025

setlocal modifiable has to be called, as above.

To get the preview window id, you can try

get(filter(getwininfo(), 'getwinvar(v:val.winid, "&previewwindow")'), 0, {'winid': 0}).winid

though surely there's a more elegant way

still E21 after setlocal modifiable

@813ethan
Copy link

813ethan commented Nov 7, 2025

setting modifiable upstream

setbufvar(bnr, '&modifiable', 1)

at UpdatePreviewWindowContents() gives another E565
Screenshot_20251107-204552_Termux

@813ethan
Copy link

813ethan commented Nov 8, 2025

*** completion.vim.old	2025-11-07 15:55:44.318862207 +1100
--- completion.vim	2025-11-08 12:10:36.013632178 +1100
***************
*** 612,617 ****
--- 612,627 ----
    id->popup_setoptions(opt.PopupConfigure('Completion', {}))
  enddef
  
+ def LspSetPreviewFileType(timer_id: number)
+   try
+     :wincmd P
+     setlocal modifiable
+     setlocal ft=lspgfm
+     :wincmd p
+   catch /E441/
+   endtry
+ enddef
+ 
  # If the completion popup documentation window displays "markdown" content,
  # then set the 'filetype' to "lspgfm".
  def LspSetPopupFileType()
***************
*** 628,639 ****
    endif
  
    if opt.lspOptions.completionInPreview
!     try
!       :wincmd P
!       setlocal ft=lspgfm
!       :wincmd p
!     catch /E441/
!     endtry
    else
      var id = popup_findinfo()
      if id > 0
--- 638,644 ----
    endif
  
    if opt.lspOptions.completionInPreview
!     timer_start(0, 'LspSetPreviewFileType')
    else
      var id = popup_findinfo()
      if id > 0

tested for a bit, seems like using some async magic works

@813ethan
Copy link

813ethan commented Nov 8, 2025

maybe resize the window after the doc is shown? This works for me

*** completion.vim.old	2025-11-05 08:35:36.036497263 +1100
--- completion.vim	2025-11-05 08:36:08.768497250 +1100
***************
*** 456,461 ****
--- 456,462 ----
        infoText->append(0)
        [1, 1]->cursor()
        exe $'setlocal ft={infoKind}'
+       exe 'resize' &previewheight
        :wincmd p
      catch /E441/ # No preview window
      endtry

also an improved version of window resizing

exe 'resize ' .. min([line('$') + 1, &previewheight])

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 12, 2025

Is only the latter or both patches needed?

@813ethan
Copy link

both, the latter is for resizing the preview window

@Konfekt
Copy link
Contributor Author

Konfekt commented Nov 12, 2025

good to go?

@813ethan
Copy link

Yes for now, all good 👍

def LspCompleteDone(bnr: number)
var lspserver: dict<any> = buf.BufLspServerGet(bnr, 'completion')
if lspserver->empty()
if opt.lspOptions.completionInPreview && opt.lspOptions.closePreviewOnComplete
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe @yegappan prefers, if ever merged, a silent! one-liner as it appears thrice

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 this pull request may close these issues.

2 participants