mirror of
https://github.com/dense-analysis/ale.git
synced 2026-02-12 05:01:47 +08:00
Close #3274 - Handle basic LSP markdown formatting
This commit is contained in:
@@ -56,6 +56,137 @@ function! ale#hover#HandleTSServerResponse(conn_id, response) abort
|
||||
endif
|
||||
endfunction
|
||||
|
||||
" Convert a language name to another one.
|
||||
" The language name could be an empty string or v:null
|
||||
function! s:ConvertLanguageName(language) abort
|
||||
return a:language
|
||||
endfunction
|
||||
|
||||
function! ale#hover#ParseLSPResult(contents) abort
|
||||
let l:includes = {}
|
||||
let l:highlights = []
|
||||
let l:lines = []
|
||||
let l:list = type(a:contents) is v:t_list ? a:contents : [a:contents]
|
||||
let l:region_index = 0
|
||||
|
||||
for l:item in l:list
|
||||
if !empty(l:lines)
|
||||
call add(l:lines, '')
|
||||
endif
|
||||
|
||||
if type(l:item) is v:t_dict && has_key(l:item, 'kind')
|
||||
if l:item.kind is# 'markdown'
|
||||
" Handle markdown values as we handle strings below.
|
||||
let l:item = get(l:item, 'value', '')
|
||||
elseif l:item.kind is# 'plaintext'
|
||||
" We shouldn't try to parse plaintext as markdown.
|
||||
" Pass the lines on and skip parsing them.
|
||||
call extend(l:lines, split(get(l:item, 'value', ''), "\n"))
|
||||
|
||||
continue
|
||||
endif
|
||||
endif
|
||||
|
||||
let l:marked_list = []
|
||||
|
||||
" If the item is a string, then we should parse it as Markdown text.
|
||||
if type(l:item) is v:t_string
|
||||
let l:fence_language = v:null
|
||||
let l:fence_lines = []
|
||||
|
||||
for l:line in split(l:item, "\n")
|
||||
if l:fence_language is v:null
|
||||
" Look for the start of a code fence. (```python, etc.)
|
||||
let l:match = matchlist(l:line, '^```\(.*\)$')
|
||||
|
||||
if !empty(l:match)
|
||||
let l:fence_language = l:match[1]
|
||||
|
||||
if !empty(l:marked_list)
|
||||
call add(l:fence_lines, '')
|
||||
endif
|
||||
else
|
||||
if !empty(l:marked_list)
|
||||
\&& l:marked_list[-1][0] isnot v:null
|
||||
call add(l:marked_list, [v:null, ['']])
|
||||
endif
|
||||
|
||||
call add(l:marked_list, [v:null, [l:line]])
|
||||
endif
|
||||
elseif l:line =~# '^```$'
|
||||
" When we hit the end of a code fence, pass the fenced
|
||||
" lines on to the next steps below.
|
||||
call add(l:marked_list, [l:fence_language, l:fence_lines])
|
||||
let l:fence_language = v:null
|
||||
let l:fence_lines = []
|
||||
else
|
||||
" Gather lines inside of a code fence.
|
||||
call add(l:fence_lines, l:line)
|
||||
endif
|
||||
endfor
|
||||
" If the result from the LSP server is a {language: ..., value: ...}
|
||||
" Dictionary, then that should be interpreted as if it was:
|
||||
"
|
||||
" ```${language}
|
||||
" ${value}
|
||||
" ```
|
||||
elseif type(l:item) is v:t_dict
|
||||
\&& has_key(l:item, 'language')
|
||||
\&& type(l:item.language) is v:t_string
|
||||
\&& has_key(l:item, 'value')
|
||||
\&& type(l:item.value) is v:t_string
|
||||
call add(
|
||||
\ l:marked_list,
|
||||
\ [l:item.language, split(l:item.value, "\n")],
|
||||
\)
|
||||
endif
|
||||
|
||||
for [l:language, l:marked_lines] in l:marked_list
|
||||
if l:language is v:null
|
||||
" NOTE: We could handle other Markdown formatting here.
|
||||
call map(
|
||||
\ l:marked_lines,
|
||||
\ 'substitute(v:val, ''\\_'', ''_'', ''g'')',
|
||||
\)
|
||||
else
|
||||
let l:language = s:ConvertLanguageName(l:language)
|
||||
|
||||
if !empty(l:language)
|
||||
let l:includes[l:language] = printf(
|
||||
\ 'syntax/%s.vim',
|
||||
\ l:language,
|
||||
\)
|
||||
|
||||
let l:start = len(l:lines) + 1
|
||||
let l:end = l:start + len(l:marked_lines)
|
||||
let l:region_index += 1
|
||||
|
||||
call add(l:highlights, 'syntax region'
|
||||
\ . ' ALE_hover_' . l:region_index
|
||||
\ . ' start=/\%' . l:start . 'l/'
|
||||
\ . ' end=/\%' . l:end . 'l/'
|
||||
\ . ' contains=@ALE_hover_' . l:language
|
||||
\)
|
||||
endif
|
||||
endif
|
||||
|
||||
call extend(l:lines, l:marked_lines)
|
||||
endfor
|
||||
endfor
|
||||
|
||||
let l:include_commands = []
|
||||
|
||||
for [l:language, l:lang_path] in sort(items(l:includes))
|
||||
call add(l:include_commands, 'unlet! b:current_syntax')
|
||||
call add(
|
||||
\ l:include_commands,
|
||||
\ printf('syntax include @ALE_hover_%s %s', l:language, l:lang_path),
|
||||
\)
|
||||
endfor
|
||||
|
||||
return [l:include_commands + l:highlights, l:lines]
|
||||
endfunction
|
||||
|
||||
function! ale#hover#HandleLSPResponse(conn_id, response) abort
|
||||
if has_key(a:response, 'id')
|
||||
\&& has_key(s:hover_map, a:response.id)
|
||||
@@ -82,40 +213,25 @@ function! ale#hover#HandleLSPResponse(conn_id, response) abort
|
||||
return
|
||||
endif
|
||||
|
||||
let l:result = l:result.contents
|
||||
let [l:commands, l:lines] = ale#hover#ParseLSPResult(l:result.contents)
|
||||
|
||||
if type(l:result) is v:t_string
|
||||
" The result can be just a string.
|
||||
let l:result = [l:result]
|
||||
endif
|
||||
|
||||
if type(l:result) is v:t_dict
|
||||
" If the result is an object, then it's markup content.
|
||||
let l:result = has_key(l:result, 'value') ? [l:result.value] : []
|
||||
endif
|
||||
|
||||
if type(l:result) is v:t_list
|
||||
" Replace objects with text values.
|
||||
call filter(l:result, '!(type(v:val) is v:t_dict && !has_key(v:val, ''value''))')
|
||||
call map(l:result, 'type(v:val) is v:t_string ? v:val : v:val.value')
|
||||
let l:str = join(l:result, "\n")
|
||||
let l:str = substitute(l:str, '^\s*\(.\{-}\)\s*$', '\1', '')
|
||||
|
||||
if !empty(l:str)
|
||||
if get(l:options, 'hover_from_balloonexpr', 0)
|
||||
\&& exists('*balloon_show')
|
||||
\&& ale#Var(l:options.buffer, 'set_balloons')
|
||||
call balloon_show(l:str)
|
||||
elseif get(l:options, 'truncated_echo', 0)
|
||||
call ale#cursor#TruncatedEcho(split(l:str, "\n")[0])
|
||||
elseif g:ale_hover_to_preview
|
||||
call ale#preview#Show(split(l:str, "\n"), {
|
||||
\ 'filetype': 'ale-preview.message',
|
||||
\ 'stay_here': 1,
|
||||
\})
|
||||
else
|
||||
call ale#util#ShowMessage(l:str)
|
||||
endif
|
||||
if !empty(l:lines)
|
||||
if get(l:options, 'hover_from_balloonexpr', 0)
|
||||
\&& exists('*balloon_show')
|
||||
\&& ale#Var(l:options.buffer, 'set_balloons')
|
||||
call balloon_show(join(l:lines, "\n"))
|
||||
elseif get(l:options, 'truncated_echo', 0)
|
||||
call ale#cursor#TruncatedEcho(l:lines[0])
|
||||
elseif g:ale_hover_to_preview
|
||||
call ale#preview#Show(l:lines, {
|
||||
\ 'filetype': 'ale-preview.message',
|
||||
\ 'stay_here': 1,
|
||||
\ 'commands': l:commands,
|
||||
\})
|
||||
else
|
||||
call ale#util#ShowMessage(join(l:lines, "\n"), {
|
||||
\ 'commands': l:commands,
|
||||
\})
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
@@ -31,6 +31,10 @@ function! ale#preview#Show(lines, ...) abort
|
||||
setlocal readonly
|
||||
let &l:filetype = get(l:options, 'filetype', 'ale-preview')
|
||||
|
||||
for l:command in get(l:options, 'commands', [])
|
||||
call execute(l:command)
|
||||
endfor
|
||||
|
||||
if get(l:options, 'stay_here')
|
||||
wincmd p
|
||||
endif
|
||||
|
||||
@@ -16,7 +16,9 @@ endfunction
|
||||
" Vim 8 does not support echoing long messages from asynchronous callbacks,
|
||||
" but NeoVim does. Small messages can be echoed in Vim 8, and larger messages
|
||||
" have to be shown in preview windows.
|
||||
function! ale#util#ShowMessage(string) abort
|
||||
function! ale#util#ShowMessage(string, ...) abort
|
||||
let l:options = get(a:000, 0, {})
|
||||
|
||||
if !has('nvim')
|
||||
call ale#preview#CloseIfTypeMatches('ale-preview.message')
|
||||
endif
|
||||
@@ -25,10 +27,13 @@ function! ale#util#ShowMessage(string) abort
|
||||
if has('nvim') || (a:string !~? "\n" && len(a:string) < &columns)
|
||||
execute 'echo a:string'
|
||||
else
|
||||
call ale#preview#Show(split(a:string, "\n"), {
|
||||
\ 'filetype': 'ale-preview.message',
|
||||
\ 'stay_here': 1,
|
||||
\})
|
||||
call ale#preview#Show(split(a:string, "\n"), extend(
|
||||
\ {
|
||||
\ 'filetype': 'ale-preview.message',
|
||||
\ 'stay_here': 1,
|
||||
\ },
|
||||
\ l:options,
|
||||
\))
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
Reference in New Issue
Block a user