]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Markdown is not applied in the preview window. Fixes #359
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sat, 15 Jul 2023 19:37:08 +0000 (12:37 -0700)
committerYegappan Lakshmanan <yegappan@yahoo.com>
Sat, 15 Jul 2023 19:37:08 +0000 (12:37 -0700)
autoload/lsp/hover.vim
ftplugin/lspgfm.vim
test/clangd_tests.vim

index d69b7972970a649ef77c72b39b3e22ac87ed6335..4ee4e684e5cce55ce2933ce71b257286f0a5950c 100644 (file)
@@ -84,7 +84,7 @@ export def HoverReply(lspserver: dict<any>, hoverResult: any, cmdmods: string):
   endif
 
   if opt.lspOptions.hoverInPreview
-    execute $':silent! {cmdmods} pedit LspHoverReply'
+    execute $':silent! {cmdmods} pedit LspHover'
     :wincmd P
     :setlocal buftype=nofile
     :setlocal bufhidden=delete
index 73dfba8c0d3791440a92afbdc24f84ce871d978d..2e9bf768306f5be5b888715eb621f0fc94447375 100644 (file)
@@ -2,22 +2,77 @@ vim9script
 
 import autoload 'lsp/markdown.vim' as md
 
-var bnr: number = bufnr()
-var popup_id: number
-var document: dict<list<any>>
-
-try
-  popup_id = bnr->getbufinfo()[0].popups[0]
-  document = md.ParseMarkdown(bnr->getbufline(1, '$'), popup_id->winwidth())
-catch /.*/
-  b:markdown_fallback = v:true
-  finish
-endtry
-
-b:lsp_syntax = document.syntax
-md.list_pattern->setbufvar(bnr, '&formatlistpat')
-var settings = 'linebreak breakindent breakindentopt=list:-1'
-win_execute(popup_id, $'setlocal {settings}')
-popup_id->popup_settext(document.content)
+# Update the preview window with the github flavored markdown text
+def UpdatePreviewWindowContents(bnr: number, contentList: list<dict<any>>)
+  :silent! bnr->deletebufline(1, '$')
+
+  var lines: list<string> = []
+  var props: dict<list<list<number>>>
+  var lnum = 0
+
+  # Each item in "contentList" is a Dict with the following items:
+  #   text: text for this line
+  #   props: list of text properties.  Each list item is a Dict.  See
+  #   |popup-props| for more information.
+  #
+  # Need to convert the text properties from the format used by
+  # popup_settext() to that used by prop_add_list().
+  for entry in contentList
+    lines->add(entry.text)
+    lnum += 1
+    if entry->has_key('props')
+      for p in entry.props
+       if !props->has_key(p.type)
+         props[p.type] = []
+       endif
+       if p->has_key('end_lnum')
+         props[p.type]->add([lnum, p.col, p.end_lnum, p.end_col])
+       else
+         props[p.type]->add([lnum, p.col, lnum, p.col + p.length])
+       endif
+      endfor
+    endif
+  endfor
+  setbufline(bnr, 1, lines)
+  for prop_type in props->keys()
+    prop_add_list({type: prop_type}, props[prop_type])
+  endfor
+enddef
+
+# Render the github flavored markdown text.
+# Text can be displayed either in a popup window or in a preview window.
+def RenderGitHubMarkdownText()
+  var bnr: number = bufnr()
+  var winId: number = win_getid()
+  var document: dict<list<any>>
+  var inPreviewWindow = false
+
+  if win_gettype() == 'preview'
+    inPreviewWindow = true
+  endif
+
+  try
+    if !inPreviewWindow
+      winId = bnr->getbufinfo()[0].popups[0]
+    endif
+    # parse the github markdown content and convert it into a list of text and
+    # list of associated text properties.
+    document = md.ParseMarkdown(bnr->getbufline(1, '$'), winId->winwidth())
+  catch /.*/
+    b:markdown_fallback = v:true
+    return
+  endtry
+
+  b:lsp_syntax = document.syntax
+  md.list_pattern->setbufvar(bnr, '&formatlistpat')
+  var settings = 'linebreak breakindent breakindentopt=list:-1'
+  win_execute(winId, $'setlocal {settings}')
+  if inPreviewWindow
+    UpdatePreviewWindowContents(bnr, document.content)
+  else
+    winId->popup_settext(document.content)
+  endif
+enddef
+RenderGitHubMarkdownText()
 
 # vim: tabstop=8 shiftwidth=2 softtabstop=2
index f1e585d19461558c7730e7115fd877eda0d674b2..59811f61c71beb6730690295328b74d45d659723 100644 (file)
@@ -1102,9 +1102,9 @@ def g:Test_LspHover()
   END
   setline(1, lines)
   if clangdVerMajor > 14
-       g:WaitForServerFileLoad(1)
+    g:WaitForServerFileLoad(1)
   else
-       g:WaitForServerFileLoad(0)
+    g:WaitForServerFileLoad(0)
   endif
   cursor(8, 4)
   var output = execute(':LspHover')->split("\n")
@@ -1129,6 +1129,18 @@ def g:Test_LspHover()
   assert_equal(1, popup_list()->len())
   popup_clear()
 
+  # Show hover information in a preview window
+  g:LspOptionsSet({hoverInPreview: true})
+  cursor(8, 4)
+  :LspHover
+  assert_equal([2, 2, 'preview'], [winnr('$'), winnr(), win_gettype(1)])
+  assert_equal('LspHover', winbufnr(1)->bufname())
+  cursor(9, 9)
+  :LspHover
+  assert_equal([2, 2, 'preview'], [winnr('$'), winnr(), win_gettype(1)])
+  g:LspOptionsSet({hoverInPreview: false})
+  :pclose
+
   :%bw!
 enddef