]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Postpone placing signs for diagnostic messages in insert and replace modes
authorYegappan Lakshmanan <yegappan@yahoo.com>
Thu, 28 Jan 2021 16:44:32 +0000 (08:44 -0800)
committerYegappan Lakshmanan <yegappan@yahoo.com>
Thu, 28 Jan 2021 16:44:32 +0000 (08:44 -0800)
autoload/buf.vim
autoload/handlers.vim
autoload/lsp.vim
autoload/lspserver.vim
autoload/util.vim
test/test_lsp.vim

index e46823bd4bcba556dea2db6cabbbf9821211345e..e11a21a971294b04f01d094529e4e1971c35123b 100644 (file)
@@ -16,6 +16,14 @@ export def LspDiagsUpdated(lspserver: dict<any>, bnr: number)
     return
   endif
 
+  var curmode: string = mode()
+  if curmode == 'i' || curmode == 'R' || curmode == 'Rv'
+    # postpone placing signs in insert mode and replace mode. These will be
+    # placed after the user returns to Normal mode.
+    b:LspDiagsUpdatePending = true
+    return
+  endif
+
   # Remove all the existing diagnostic signs
   sign_unplace('LSPDiag', {buffer: bnr})
 
index ea760dab8f0079f9a569e4bcbc2e9233107dbae7..dd86f4a278466be39cb7973eaf92548cfac49cba 100644 (file)
@@ -190,7 +190,8 @@ def s:processCompletionReply(lspserver: dict<any>, req: dict<any>, reply: dict<a
     return
   endif
 
-  # Find the start column for the completion
+  # Find the start column for the completion.  If any of the entries returned
+  # by the LSP server has a starting position, then use that.
   var start_col: number = 0
   for item in items
     if item->has_key('textEdit')
@@ -199,6 +200,8 @@ def s:processCompletionReply(lspserver: dict<any>, req: dict<any>, reply: dict<a
     endif
   endfor
 
+  # LSP server didn't return a starting position for completion, search
+  # backwards from the current cursor position for a non-keyword character.
   if start_col == 0
     var line: string = getline('.')
     var start = col('.') - 1
@@ -530,7 +533,7 @@ def s:applyTextEdits(bnr: number, text_edits: list<dict<any>>): void
   if !bnr->bufloaded()
     bnr->bufload()
   endif
-  bnr->setbufvar('&buflisted', v:true)
+  bnr->setbufvar('&buflisted', true)
 
   var start_line: number = 4294967295          # 2 ^ 32
   var finish_line: number = -1
@@ -552,7 +555,7 @@ def s:applyTextEdits(bnr: number, text_edits: list<dict<any>>): void
 
     updated_edits->add({A: [start_row, start_col],
                        B: [end_row, end_col],
-                       lines: e.newText->split("\n", v:true)})
+                       lines: e.newText->split("\n", true)})
   endfor
 
   # Reverse sort the edit operations by descending line and column numbers so
@@ -591,10 +594,10 @@ def s:applyTextEdits(bnr: number, text_edits: list<dict<any>>): void
   # if the buffer is empty, appending lines before the first line adds an
   # extra empty line at the end. Delete the empty line after appending the
   # lines.
-  var dellastline: bool = v:false
+  var dellastline: bool = false
   if start_line == 0 && bnr->getbufinfo()[0].linecount == 1 &&
                                                bnr->getbufline(1)[0] == ''
-    dellastline = v:true
+    dellastline = true
   endif
 
   # Append the updated lines
@@ -983,7 +986,7 @@ def s:processApplyEditReq(lspserver: dict<any>, request: dict<any>)
   endif
   s:applyWorkspaceEdit(workspaceEditParams.edit)
   # TODO: Need to return the proper result of the edit operation
-  lspserver.sendResponse(request, {applied: v:true}, v:null)
+  lspserver.sendResponse(request, {applied: true}, v:null)
 enddef
 
 def s:processUnsupportedReq(lspserver: dict<any>, request: dict<any>)
index e37669b00e29665a9427a0168e5bd3217ba32f44..f551ca31f69f2e12b9c9e2b38a1bced0e6ab0299 100644 (file)
@@ -8,6 +8,7 @@ import {WarnMsg,
        lsp_server_trace,
        ClearTraceLogs,
        GetLineByteFromPos} from './util.vim'
+import {LspDiagsUpdated} from './buf.vim'
 
 # Needs Vim 8.2.2342 and higher
 if v:version < 802 || !has('patch-8.2.2342')
@@ -56,7 +57,7 @@ enddef
 
 def lsp#enableServerTrace()
   ClearTraceLogs()
-  lsp_server_trace = v:true
+  lsp_server_trace = true
 enddef
 
 # Show information about all the LSP servers
@@ -225,6 +226,25 @@ def g:LspDiagExpr(): string
   return diagInfo.message
 enddef
 
+# Called after leaving insert mode. Used to process diag messages (if any)
+def lsp#leftInsertMode()
+  if !exists('b:LspDiagsUpdatePending')
+    return
+  endif
+  :unlet b:LspDiagsUpdatePending
+
+  var ftype: string = &filetype
+  if ftype == ''
+    return
+  endif
+
+  var lspserver: dict<any> = s:lspGetServer(ftype)
+  if lspserver->empty() || !lspserver.running
+    return
+  endif
+  LspDiagsUpdated(lspserver, bufnr())
+enddef
+
 # A new buffer is opened. If LSP is supported for this buffer, then add it
 def lsp#addFile(bnr: number): void
   if bufnrToServer->has_key(bnr)
@@ -261,6 +281,7 @@ def lsp#addFile(bnr: number): void
   # autocmd for insert mode completion
   exe 'autocmd SafeState <buffer=' .. bnr
        .. '> if mode() == "i" | call lsp#complete() | endif'
+  exe 'autocmd InsertLeave <buffer=' .. bnr .. '> call lsp#leftInsertMode()'
 
   # map characters that trigger signature help
   if lspserver.caps->has_key('signatureHelpProvider')
@@ -612,9 +633,9 @@ enddef
 
 # clear the symbol reference highlight
 def lsp#docHighlightClear()
-  prop_remove({'type': 'LspTextRef', 'all': v:true}, 1, line('$'))
-  prop_remove({'type': 'LspReadRef', 'all': v:true}, 1, line('$'))
-  prop_remove({'type': 'LspWriteRef', 'all': v:true}, 1, line('$'))
+  prop_remove({'type': 'LspTextRef', 'all': true}, 1, line('$'))
+  prop_remove({'type': 'LspReadRef', 'all': true}, 1, line('$'))
+  prop_remove({'type': 'LspWriteRef', 'all': true}, 1, line('$'))
 enddef
 
 # jump to a symbol selected in the outline window
@@ -938,9 +959,9 @@ def lsp#textDocFormat(range_args: number, line1: number, line2: number)
   endif
 
   if range_args > 0
-    lspserver.textDocFormat(fname, v:true, line1, line2)
+    lspserver.textDocFormat(fname, true, line1, line2)
   else
-    lspserver.textDocFormat(fname, v:false, 0, 0)
+    lspserver.textDocFormat(fname, false, 0, 0)
   endif
 enddef
 
@@ -1069,7 +1090,7 @@ def s:filterSymbols(lspserver: dict<any>, popupID: number, key: string): bool
   lspserver.workspaceSymbolQuery = query
 
   if key_handled
-    return v:true
+    return true
   endif
 
   return popupID->popup_filter_menu(key)
index 44f16916b23526b66967906db8cc016f1264fdeb..3a24e6ca6f7a70afad2c424c079abbc9543a8430 100644 (file)
@@ -17,21 +17,21 @@ import {WarnMsg,
 
 # LSP server standard output handler
 def s:output_cb(lspserver: dict<any>, chan: channel, msg: string): void
-  TraceLog(v:false, msg)
+  TraceLog(false, msg)
   lspserver.data = lspserver.data .. msg
   lspserver.processMessages()
 enddef
 
 # LSP server error output handler
 def s:error_cb(lspserver: dict<any>, chan: channel, emsg: string,): void
-  TraceLog(v:true, emsg)
+  TraceLog(true, emsg)
 enddef
 
 # LSP server exit callback
 def s:exit_cb(lspserver: dict<any>, job: job, status: number): void
   WarnMsg("LSP server exited with status " .. status)
   lspserver.job = v:none
-  lspserver.running = v:false
+  lspserver.running = false
   lspserver.requests = {}
 enddef
 
@@ -69,7 +69,7 @@ def s:startServer(lspserver: dict<any>): number
   sleep 10m
 
   lspserver.job = job
-  lspserver.running = v:true
+  lspserver.running = true
 
   lspserver.initServer()
 
@@ -84,15 +84,15 @@ def s:initServer(lspserver: dict<any>)
   # client capabilities (ClientCapabilities)
   var clientCaps: dict<any> = {
     workspace: {
-      workspaceFolders: v:true,
-      applyEdit: v:true,
+      workspaceFolders: true,
+      applyEdit: true,
     },
     textDocument: {
-      foldingRange: {lineFoldingOnly: v:true},
+      foldingRange: {lineFoldingOnly: true},
       completion: {
        completionItem: {
          documentationFormat: ['plaintext', 'markdown'],
-         snippetSupport: v:false
+         snippetSupport: false
        },
        completionItemKind: {valueSet: range(1, 25)}
       },
@@ -100,7 +100,7 @@ def s:initServer(lspserver: dict<any>)
         contentFormat: ['plaintext', 'markdown']
       },
       documentSymbol: {
-       hierarchicalDocumentSymbolSupport: v:true,
+       hierarchicalDocumentSymbolSupport: true,
        symbolKind: {valueSet: range(1, 25)}
       },
     },
@@ -166,7 +166,7 @@ def s:stopServer(lspserver: dict<any>): number
 
   lspserver.job->job_stop()
   lspserver.job = v:none
-  lspserver.running = v:false
+  lspserver.running = false
   lspserver.requests = {}
   return 0
 enddef
@@ -525,7 +525,7 @@ def s:showReferences(lspserver: dict<any>): void
   # interface ReferenceParams
   #   interface TextDocumentPositionParams
   req.params->extend(s:getLspTextDocPosition())
-  req.params->extend({context: {includeDeclaration: v:true}})
+  req.params->extend({context: {includeDeclaration: true}})
 
   lspserver.sendMessage(req)
 enddef
@@ -600,7 +600,7 @@ def s:textDocFormat(lspserver: dict<any>, fname: string, rangeFormat: bool,
   # interface FormattingOptions
   var fmtopts: dict<any> = {
     tabSize: tabsz,
-    insertSpaces: &expandtab ? v:true : v:false,
+    insertSpaces: &expandtab ? true : false,
   }
   req.params->extend({options: fmtopts})
   if rangeFormat
@@ -783,7 +783,7 @@ export def NewLspServer(path: string, args: list<string>): dict<any>
   var lspserver: dict<any> = {
     path: path,
     args: args,
-    running: v:false,
+    running: false,
     job: v:none,
     data: '',
     nextID: 1,
index d930fd348e3b96004725a83bd89258bca764c2f5..4da768e83928d2fcd130337e96c7affc3967a1cd 100644 (file)
@@ -21,10 +21,10 @@ if has('unix')
 else
   lsp_log_dir = $TEMP .. '\\'
 endif
-export var lsp_server_trace: bool = v:false
+export var lsp_server_trace: bool = false
 
-# Log a message from the LSP server. stderr is v:true for logging messages
-# from the standard error and v:false for stdout.
+# Log a message from the LSP server. stderr is true for logging messages
+# from the standard error and false for stdout.
 export def TraceLog(stderr: bool, msg: string)
   if !lsp_server_trace
     return
@@ -67,9 +67,9 @@ enddef
 export def LspFileToUri(fname: string): string
   var uri: string = fnamemodify(fname, ':p')
 
-  var on_windows: bool = v:false
+  var on_windows: bool = false
   if uri =~? '^\a:'
-    on_windows = v:true
+    on_windows = true
   endif
 
   if on_windows
index d18bfbdeafd4d163f923beb102f8dd21ca1d0296..d8330fc55dbf389d342ede62ad2c7424f7101850 100644 (file)
@@ -120,7 +120,7 @@ def Test_lsp_show_references()
   var bnr: number = bufnr()
   :LspShowReferences
   :sleep 1
-  var qfl: list<dict<any>> = getqflist()
+  var qfl: list<dict<any>> = getloclist(0)
   assert_equal('quickfix', getwinvar(winnr('$'), '&buftype'))
   assert_equal(bnr, qfl[0].bufnr)
   assert_equal(3, qfl->len())
@@ -131,7 +131,7 @@ def Test_lsp_show_references()
   cursor(1, 5)
   :LspShowReferences
   :sleep 1
-  qfl = getqflist()
+  qfl = getloclist()
   assert_equal(1, qfl->len())
   assert_equal([1, 5], [qfl[0].lnum, qfl[0].col])
 
@@ -150,7 +150,11 @@ def LspRunTests()
                    ->map("v:val->substitute('^def <SNR>\\d\\+_', '', '')")
   for f in fns
     v:errors = []
-    exe f
+    try
+      exe f
+    catch
+      echomsg "Error: Test " .. f .. " failed with exception " .. v:exception
+    endtry
     if v:errors->len() != 0
       new Lsp-Test-Results
       setline(1, ["Error: Test " .. f .. " failed"]->extend(v:errors))