# Refer to https://microsoft.github.io/language-server-protocol/specification
# for the Language Server Protocol (LSP) specification.
-import './options.vim' as opt
import './util.vim'
import './diag.vim'
-import './outline.vim'
import './textedit.vim'
-import './symbol.vim'
# Process various reply messages from the LSP server
export def ProcessReply(lspserver: dict<any>, req: dict<any>, reply: dict<any>): void
diagsMap: {},
workspaceSymbolPopup: -1,
workspaceSymbolQuery: '',
+ peekSymbolPopup: -1,
callHierarchyType: '',
selection: {}
}
if peekSymbol
# open the definition/declaration in the preview window and highlight the
# matching symbol
- exe $'pedit {fname}'
- var cur_wid = win_getid()
- wincmd P
- var pvwbuf = bufnr()
- setcursorcharpos(location.range.start.line + 1,
- location.range.start.character + 1)
- silent! matchdelete(101)
+
+ var bnum = bufadd(fname)
+ if bnum == 0
+ # Failed to create or find a buffer
+ return
+ endif
+
+ if lspserver.peekSymbolPopup->winbufnr() != -1
+ # If the symbol popup window is already present, close it.
+ lspserver.peekSymbolPopup->popup_close()
+ endif
+ var ptitle = $"{fnamemodify(fname, ':t')} ({fnamemodify(fname, ':h')})"
+ lspserver.peekSymbolPopup = popup_atcursor(bnum, {moved: 'any',
+ title: ptitle,
+ minwidth: 10,
+ maxwidth: 60,
+ minheight: 10,
+ maxheight: 10,
+ mapping: false,
+ wrap: false})
+
+ # Highlight the symbol name and center the line in the popup
+ var pwid = lspserver.peekSymbolPopup
+ var pwbuf = winbufnr(pwid)
var pos: list<number> = []
var start_col: number
var end_col: number
- start_col = util.GetLineByteFromPos(pvwbuf, location.range.start) + 1
- end_col = util.GetLineByteFromPos(pvwbuf, location.range.end) + 1
+ start_col = util.GetLineByteFromPos(pwbuf, location.range.start) + 1
+ end_col = util.GetLineByteFromPos(pwbuf, location.range.end) + 1
pos->add(location.range.start.line + 1)
pos->extend([start_col, end_col - start_col])
- matchaddpos('Search', [pos], 10, 101)
- win_gotoid(cur_wid)
+ matchaddpos('Search', [pos], 10, 101, {window: pwid})
+ var cmds =<< trim eval END
+ cursor({location.range.start.line + 1}, 1)
+ normal! z.
+ END
+ win_execute(pwid, cmds, 'silent!')
else
# jump to the file and line containing the symbol
var bnr: number = fname->bufnr()
setcursorcharpos(location.range.start.line + 1,
location.range.start.character + 1)
endif
- redraw!
enddef
# Process the LSP server reply message for a 'textDocument/definition' request
:LspGotoDeclaration Go to the declaration of the symbol under cursor
:LspGotoTypeDef Go to the type definition of the symbol under cursor
:LspGotoImpl Go to the implementation of the symbol under cursor
-:LspPeekDefinition Open the definition of the symbol under cursor in the
- preview window.
-:LspPeekDeclaration Open the declaration of the symbol under cursor in the
- preview window.
+:LspPeekDefinition Open the definition of the symbol under cursor in a
+ popup window.
+:LspPeekDeclaration Open the declaration of the symbol under cursor in a
+ popup window.
:LspPeekTypeDef Open the type definition of the symbol under cursor in
- the preview window.
+ a popup window.
:LspPeekImpl Open the implementation of the symbol under cursor in
- the preview window.
+ a popup window.
:LspShowSignature Display the signature of the symbol under cursor.
:LspDiagShow Display the diagnostics messages from the LSP server
for the current buffer in a location list.
<
*:LspPeekDefinition*
:LspPeekDefinition Displays the line where the symbol under the cursor is
- defined in the |preview-window|. The symbol is
- highlighted in the preview window.
+ defined in a popup window. The symbol is highlighted
+ in the popup window. Moving the cursor or pressing
+ <Esc> will close the popup window.
*:LspPeekDeclaration*
:LspPeekDeclaration Displays the line where the symbol under the
- cursor is declared in the |preview-window|. The
+ cursor is declared in a popup window. The
behavior of this command is similar to the
|:LspPeekDefinition| command.
*:LspPeekTypeDef*
:LspPeekTypeDef Displays the line where the type of the symbol under
- the cursor is defined in the |preview-window|. The
+ the cursor is defined in a popup window. The
behavior of this command is similar to the
|:LspPeekDefinition| command. Note that not all the
LSP servers support this feature.
*:LspPeekImpl*
:LspPeekImpl Displays the implementation of the symbol under the
- cursor in the |preview-window|. The behavior of this
+ cursor in a popup window. The behavior of this
command is similar to the |:LspPeekDefinition|
command. Note that not all the LSP servers support
this feature.
assert_equal(1, getloclist(0)->len())
qfl = getloclist(0)
assert_equal([1, 5], [qfl[0].lnum, qfl[0].col])
+ :lclose
+
+ # Test for LspPeekReferences
+
+ # Opening the preview window with an unsaved buffer displays the "E37: No
+ # write since last change" error message. To disable this message, mark the
+ # buffer as not modified.
+ setlocal nomodified
+ cursor(1, 5)
+ :LspPeekReferences
+ assert_equal([3, 3], [winnr('$'), winnr()])
+ assert_equal('preview', win_gettype(1))
+ assert_equal('loclist', win_gettype(2))
+
bw!
# empty file
enddef
# Test for :LspGotoDefinition, :LspGotoDeclaration and :LspGotoImpl
-def Test_LspGotoDefinition()
+def Test_LspGotoSymbol()
silent! edit Xtest.cpp
sleep 200m
var lines: list<string> =<< trim END
m = execute('messages')->split("\n")
assert_equal('Error: implementation is not found', m[1])
endif
- bw!
+
+ # Test for LspPeekDeclaration
+ cursor(24, 6)
+ var bnum = bufnr()
+ :LspPeekDeclaration
+ var plist = popup_list()
+ assert_true(1, plist->len())
+ assert_equal(bnum, plist[0]->winbufnr())
+ assert_equal(6, line('.', plist[0]))
+ popup_clear()
+ # tag stack should not be changed
+ assert_fails("normal! \<C-t>", 'E555:')
+
+ # Test for LspPeekDefinition
+ :LspPeekDefinition
+ plist = popup_list()
+ assert_true(1, plist->len())
+ assert_equal(bnum, plist[0]->winbufnr())
+ assert_equal(9, line('.', plist[0]))
+ popup_clear()
+ # tag stack should not be changed
+ assert_fails("normal! \<C-t>", 'E555:')
+
+ :%bw!
# empty file
assert_equal('', execute('LspGotoDefinition'))