vim-lsp icon indicating copy to clipboard operation
vim-lsp copied to clipboard

[question]How to know if LspDefinition fail or success in vim script

Open tracyone opened this issue 3 years ago • 2 comments

I can see "No definition found" message if LspDefinition can find any definiton, but how to know this in vim script?

I have tried following, but neither of it working

redir => l:msg
:LspDefinition
redir END
if l:msg == "No definition found"
let l:ret = 1
endif
try
:LspDefinition
catch
 let l:ret = 1
endtry

I try to use cscope or ctags if lsp not working, that's why I need to know if Lsp command fail or success.

tracyone avatar Jul 29 '22 12:07 tracyone

Have you found a way around this? I'm having the same issue.

lucas-mior avatar Aug 18 '22 01:08 lucas-mior

Vim-lsp does not throw exceptions, that is why your second approach didn't work. The solution I found:

diff --git a/autoload/lsp/utils.vim b/autoload/lsp/utils.vim
index 5bcdb7b..a8292c3 100644
--- a/autoload/lsp/utils.vim
+++ b/autoload/lsp/utils.vim
@@ -271,6 +271,7 @@ function! lsp#utils#error(msg) abort
     echohl ErrorMsg
     echom a:msg
     echohl NONE
+    throw "LspError"
 endfunction
 
 function! lsp#utils#echo_with_truncation(msg) abort

In my .vimrc:

function! Gototo()
    try
        :LspDefinition
    catch
        :tag
    endtry
endfunction

lucas-mior avatar Aug 18 '22 02:08 lucas-mior

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 01 '22 20:11 stale[bot]