From fe38101db7253e450f67893583d1a5dfeefd9ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Janou=C5=A1ek?= Date: Tue, 15 Aug 2023 11:20:47 +0100 Subject: Fix error loading "text" syntax for hover (#4574) rust-analyzer sometimes returns a hover result with language being "text", but there's no syntax/text.vim, so this would fail with: Error detected while processing function 150_VimOutputCallback[6]..8[1]..ale#lsp#HandleMessage[30]..ale#hover#HandleLSPResponse[42]..ale#floating_preview#Show[13]..161_VimShow: line 13: E484: Cannot open file syntax/text.vim Only including the file when it actually exists fixes this. --- autoload/ale/hover.vim | 21 +++++++++++++++++---- test/test_hover.vader | 6 +++--- test/test_hover_parsing.vader | 36 ++++++++++++++++++++++++++---------- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/autoload/ale/hover.vim b/autoload/ale/hover.vim index 0954b802..6ad43316 100644 --- a/autoload/ale/hover.vim +++ b/autoload/ale/hover.vim @@ -70,6 +70,18 @@ function! s:ConvertLanguageName(language) abort return a:language endfunction +" Cache syntax file (non-)existence to avoid calling globpath repeatedly. +let s:syntax_file_exists_cache = {} + +function! s:SyntaxFileExists(syntax_file) abort + if !has_key(s:syntax_file_exists_cache, a:syntax_file) + let s:syntax_file_exists_cache[a:syntax_file] = + \ !empty(globpath(&runtimepath, a:syntax_file)) + endif + + return s:syntax_file_exists_cache[a:syntax_file] +endfunction + function! ale#hover#ParseLSPResult(contents) abort let l:includes = {} let l:highlights = [] @@ -160,10 +172,11 @@ function! ale#hover#ParseLSPResult(contents) abort let l:language = s:ConvertLanguageName(l:language) if !empty(l:language) - let l:includes[l:language] = printf( - \ 'syntax/%s.vim', - \ l:language, - \) + let l:syntax_file = printf('syntax/%s.vim', l:language) + + if s:SyntaxFileExists(l:syntax_file) + let l:includes[l:language] = l:syntax_file + endif let l:start = len(l:lines) + 1 let l:end = l:start + len(l:marked_lines) diff --git a/test/test_hover.vader b/test/test_hover.vader index 7a9c8d91..580b8846 100644 --- a/test/test_hover.vader +++ b/test/test_hover.vader @@ -168,7 +168,7 @@ Execute(LSP hover response with lists of strings should be handled): Execute(LSP hover response with lists of strings and marked strings should be handled): call HandleValidLSPResult({'contents': [ - \ {'language': 'rust', 'value': 'foo'}, + \ {'language': 'python', 'value': 'foo'}, \ "bar\n", \]}) @@ -178,8 +178,8 @@ Execute(LSP hover response with lists of strings and marked strings should be ha \ { \ 'commands': [ \ 'unlet! b:current_syntax', - \ 'syntax include @ALE_hover_rust syntax/rust.vim', - \ 'syntax region ALE_hover_1 start=/\%1l/ end=/\%2l/ contains=@ALE_hover_rust', + \ 'syntax include @ALE_hover_python syntax/python.vim', + \ 'syntax region ALE_hover_1 start=/\%1l/ end=/\%2l/ contains=@ALE_hover_python', \ ], \ }, \ ], diff --git a/test/test_hover_parsing.vader b/test/test_hover_parsing.vader index 4129c26a..221b42ac 100644 --- a/test/test_hover_parsing.vader +++ b/test/test_hover_parsing.vader @@ -35,12 +35,12 @@ Execute(A string with a code fence should be handled): \ [ \ [ \ 'unlet! b:current_syntax', - \ 'syntax include @ALE_hover_python syntax/python.vim', + \ 'syntax include @ALE_hover_javascript syntax/javascript.vim', \ 'unlet! b:current_syntax', - \ 'syntax include @ALE_hover_typescript syntax/typescript.vim', + \ 'syntax include @ALE_hover_python syntax/python.vim', \ 'syntax region ALE_hover_1 start=/\%1l/ end=/\%3l/ contains=@ALE_hover_python', \ 'syntax region ALE_hover_2 start=/\%5l/ end=/\%8l/ contains=@ALE_hover_python', - \ 'syntax region ALE_hover_3 start=/\%8l/ end=/\%10l/ contains=@ALE_hover_typescript', + \ 'syntax region ALE_hover_3 start=/\%8l/ end=/\%10l/ contains=@ALE_hover_javascript', \ ], \ [ \ 'def foo():', @@ -64,7 +64,7 @@ Execute(A string with a code fence should be handled): \ 'def bar():', \ ' pass', \ '```', - \ '```typescript', + \ '```javascript', \ 'const baz = () => undefined', \ '```', \ ], "\n")) @@ -74,12 +74,12 @@ Execute(Multiple strings with fences should be handled): \ [ \ [ \ 'unlet! b:current_syntax', - \ 'syntax include @ALE_hover_python syntax/python.vim', + \ 'syntax include @ALE_hover_javascript syntax/javascript.vim', \ 'unlet! b:current_syntax', - \ 'syntax include @ALE_hover_typescript syntax/typescript.vim', + \ 'syntax include @ALE_hover_python syntax/python.vim', \ 'syntax region ALE_hover_1 start=/\%1l/ end=/\%3l/ contains=@ALE_hover_python', \ 'syntax region ALE_hover_2 start=/\%5l/ end=/\%8l/ contains=@ALE_hover_python', - \ 'syntax region ALE_hover_3 start=/\%8l/ end=/\%10l/ contains=@ALE_hover_typescript', + \ 'syntax region ALE_hover_3 start=/\%8l/ end=/\%10l/ contains=@ALE_hover_javascript', \ ], \ [ \ 'def foo():', @@ -106,7 +106,7 @@ Execute(Multiple strings with fences should be handled): \ 'def bar():', \ ' pass', \ '```', - \ '```typescript', + \ '```javascript', \ 'const baz = () => undefined', \ '```', \ ], "\n"), @@ -124,7 +124,7 @@ Execute(Objects with kinds should be handled): \ 'def foo():', \ ' pass', \ '', - \ '```typescript', + \ '```javascript', \ 'const baz = () => undefined', \ '```', \ ], @@ -142,7 +142,7 @@ Execute(Objects with kinds should be handled): \ { \ 'kind': 'plaintext', \ 'value': join([ - \ '```typescript', + \ '```javascript', \ 'const baz = () => undefined', \ '```', \ ], "\n"), @@ -171,3 +171,19 @@ Execute(Simple markdown formatting should be handled): \ '```', \ 'formatted \_ line \_', \ ], "\n")) + +Execute(Non-existent syntax files shouldn't be loaded): + AssertEqual + \ [ + \ [ + \ 'syntax region ALE_hover_1 start=/\%1l/ end=/\%2l/ contains=@ALE_hover_text', + \ ], + \ [ + \ 'hello', + \ ], + \ ], + \ ale#hover#ParseLSPResult(join([ + \ '```text', + \ 'hello', + \ '```', + \ ], "\n")) -- cgit v1.2.3