From ca6a72b3dbffa0a9260dfb400871b3ec17d8a4a8 Mon Sep 17 00:00:00 2001 From: Yegappan Lakshmanan Date: Mon, 17 Jul 2023 21:46:22 -0700 Subject: [PATCH] Optimize the diags handling code --- autoload/lsp/diag.vim | 54 +++++++++++++++++++++++++++---------------- test/runner.vim | 2 +- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/autoload/lsp/diag.vim b/autoload/lsp/diag.vim index 33c7fe8..dd902be 100644 --- a/autoload/lsp/diag.vim +++ b/autoload/lsp/diag.vim @@ -127,17 +127,21 @@ export def BufferInit(lspserver: dict, bnr: number) endif enddef +# Function to sort the diagnostics in ascending order based on the line and +# character offset +def DiagsSortFunc(a: dict, b: dict): number + var a_start: dict = a.range.start + var b_start: dict = b.range.start + var linediff: number = a_start.line - b_start.line + if linediff == 0 + return a_start.character - b_start.character + endif + return linediff +enddef + # Sort diagnostics ascending based on line and character offset def SortDiags(diags: list>): list> - return diags->sort((a, b) => { - var a_start = a.range.start - var b_start = b.range.start - var linediff = a_start.line - b_start.line - if linediff == 0 - return a_start.character - b_start.character - endif - return linediff - }) + return diags->sort(DiagsSortFunc) enddef # Remove the diagnostics stored for buffer "bnr" @@ -255,6 +259,7 @@ export def DiagsRefresh(bnr: number, all: bool = false) var signs: list> = [] var diags: list> = diagsMap[bnr].sortedDiagnostics + var inlineHLprops: list>> = [[], [], [], [], []] for diag in diags # TODO: prioritize most important severity if there are multiple # diagnostics from the same line @@ -270,15 +275,14 @@ export def DiagsRefresh(bnr: number, all: bool = false) try if lspOpts.highlightDiagInline - prop_add(lnum, util.GetLineByteFromPos(bnr, d_start) + 1, - {end_lnum: d_end.line + 1, - end_col: util.GetLineByteFromPos(bnr, d_end) + 1, - bufnr: bnr, - type: DiagSevToInlineHLName(diag.severity)}) + var propLocation: list = [ + lnum, util.GetLineByteFromPos(bnr, d_start) + 1, + d_end.line + 1, util.GetLineByteFromPos(bnr, d_end) + 1 + ] + inlineHLprops[diag.severity]->add(propLocation) endif if lspOpts.showDiagWithVirtualText - var padding: number var symbol: string = diag_symbol @@ -301,11 +305,23 @@ export def DiagsRefresh(bnr: number, all: bool = false) text_padding_left: padding}) endif catch /E966\|E964/ # Invalid lnum | Invalid col - # Diagnostics arrive asynchronous and the document changed while they - # wore send. Ignore this as new once will arrive shortly. + # Diagnostics arrive asynchronously and the document changed while they + # were in transit. Ignore this as new once will arrive shortly. endtry endfor + if lspOpts.highlightDiagInline + for i in range(1, 4) + if !inlineHLprops[i]->empty() + try + prop_add_list({bufnr: bnr, type: DiagSevToInlineHLName(i)}, + inlineHLprops[i]) + catch /E966\|E964/ # Invalid lnum | Invalid col + endtry + endif + endfor + endif + if lspOpts.showDiagWithSign signs->sign_placelist() endif @@ -426,9 +442,7 @@ export def DiagNotification(lspserver: dict, uri: string, diags_arg: list> = [] for diags in serverDiags->values() - for diag in diags - joinedServerDiags->add(diag) - endfor + joinedServerDiags->extend(diags) endfor var sortedDiags = SortDiags(joinedServerDiags) diff --git a/test/runner.vim b/test/runner.vim index f706b27..14c849a 100644 --- a/test/runner.vim +++ b/test/runner.vim @@ -47,7 +47,7 @@ try g:StartLangServer() LspRunTests() catch - writefile([$'FAIL: Tests in {g:TestName} failed with exception {v:exception} at {v:throwpoint} '], 'results.txt', 'a') + writefile(['FAIL: Tests in ' .. g:TestName .. ' failed with exception ' .. v:exception .. ' at ' .. v:throwpoint], 'results.txt', 'a') endtry qall! -- 2.48.1