Skip to content

Commit

Permalink
fix(diagnostic): stack hl groups (#5046)
Browse files Browse the repository at this point in the history
  • Loading branch information
fannheyward authored Jun 18, 2024
1 parent 974b9c8 commit 79ccfea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/__tests__/modules/diagnosticBuffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('diagnostic buffer', () => {
buf.updateHighlights('', [diagnostic])
await nvim.resumeNotification()
let res = await nvim.call('nvim_buf_get_extmarks', [buf.bufnr, ns, 0, -1, {}]) as [number, number, number][]
expect(res.length).toBe(1)
expect(res.length).toBe(2)
})

it('should not refresh for empty diagnostics', async () => {
Expand Down Expand Up @@ -370,11 +370,13 @@ describe('diagnostic buffer', () => {
diagnostics[0].tags = [DiagnosticTag.Unnecessary]
diagnostics[1].tags = [DiagnosticTag.Deprecated]
let res = buf.getHighlightItems(diagnostics)
expect(res.length).toBe(5)
expect(res.length).toBe(7)
expect(res.map(o => o.hlGroup)).toEqual([
'CocUnusedHighlight',
'CocWarningHighlight',
'CocErrorHighlight',
'CocDeprecatedHighlight',
'CocWarningHighlight',
'CocHintHighlight',
'CocErrorHighlight'
])
Expand Down
6 changes: 4 additions & 2 deletions src/diagnostic/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,10 @@ export class DiagnosticBuffer implements SyncItem {
let res: HighlightItem[] = []
for (let i = 0; i < Math.min(this._config.highlightLimit, diagnostics.length); i++) {
let diagnostic = diagnostics[i]
let hlGroup = getHighlightGroup(diagnostic)
this.doc.addHighlights(res, hlGroup, diagnostic.range)
let hlGroups = getHighlightGroup(diagnostic)
for (const hlGroup of hlGroups) {
this.doc.addHighlights(res, hlGroup, diagnostic.range)
}
}
// needed for iteration performance and since diagnostic highlight may cross lines.
res.sort((a, b) => {
Expand Down
27 changes: 17 additions & 10 deletions src/diagnostic/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,31 @@ export function sortDiagnostics(a: Diagnostic, b: Diagnostic): number {
return a.source > b.source ? 1 : -1
}

export function getHighlightGroup(diagnostic: Diagnostic): DiagnosticHighlight {
export function getHighlightGroup(diagnostic: Diagnostic): DiagnosticHighlight[] {
let hlGroups: DiagnosticHighlight[] = []
let tags = diagnostic.tags || []
if (tags.includes(DiagnosticTag.Deprecated)) {
return DiagnosticHighlight.Deprecated
hlGroups.push(DiagnosticHighlight.Deprecated)
}
if (tags.includes(DiagnosticTag.Unnecessary)) {
return DiagnosticHighlight.Unused
hlGroups.push(DiagnosticHighlight.Unused)
}
switch (diagnostic.severity) {
case DiagnosticSeverity.Warning:
return DiagnosticHighlight.Warning
case DiagnosticSeverity.Information:
return DiagnosticHighlight.Information
case DiagnosticSeverity.Hint:
return DiagnosticHighlight.Hint
default:
return DiagnosticHighlight.Error
hlGroups.push(DiagnosticHighlight.Hint)
break
case DiagnosticSeverity.Information:
hlGroups.push(DiagnosticHighlight.Information)
break
case DiagnosticSeverity.Warning:
hlGroups.push(DiagnosticHighlight.Warning)
break
case DiagnosticSeverity.Error:
hlGroups.push(DiagnosticHighlight.Error)
break
}

return hlGroups
}

export function adjustDiagnostics(diagnostics: ReadonlyArray<Diagnostic>, edit: TextEdit): ReadonlyArray<Diagnostic> {
Expand Down

0 comments on commit 79ccfea

Please sign in to comment.