From 2176e529740ec0c402e761efe3f7daf087751846 Mon Sep 17 00:00:00 2001 From: Heyward Fann Date: Fri, 2 Jun 2023 18:43:35 +0800 Subject: [PATCH] feat(semanticTokens)!: token highlight groups CocSem + type CocSem + modifier CocSem + modifier + type The highlight group didn't need to be defined first related #4659 --- src/__tests__/handler/semanticTokens.test.ts | 10 +--- src/handler/semanticTokens/buffer.ts | 57 ++++++++++---------- src/handler/semanticTokens/index.ts | 12 +---- 3 files changed, 30 insertions(+), 49 deletions(-) diff --git a/src/__tests__/handler/semanticTokens.test.ts b/src/__tests__/handler/semanticTokens.test.ts index 2d9fceb7169..c6f1bc2a47e 100644 --- a/src/__tests__/handler/semanticTokens.test.ts +++ b/src/__tests__/handler/semanticTokens.test.ts @@ -266,7 +266,6 @@ describe('semanticTokens', () => { } } }, { tokenModifiers: [], tokenTypes: [] })) - await semanticTokens.fetchHighlightGroups() await semanticTokens.showHighlightInfo() let buf = await nvim.buffer let lines = await buf.lines @@ -339,7 +338,7 @@ describe('semanticTokens', () => { let buf = await win.buffer let lines = await buf.lines let content = lines.join('\n') - expect(content).toMatch('CocSemDeclarationFunction') + expect(content).toMatch('CocSemFunction') await window.moveTo({ line: 1, character: 0 }) await commandManager.executeCommand('semanticTokens.inspect') win = await helper.getFloat() @@ -761,17 +760,10 @@ describe('semanticTokens', () => { // @ts-ignore workspace._env.updateHighlight = true expect(enabled).toBe(false) - semanticTokens.staticConfig.highlightGroups = ['CocSemKeyword'] expect(() => { item.checkState() }).toThrow('provider not found') registerProvider() - doc = await helper.createDocument('t.rs') - item = semanticTokens.getItem(doc.bufnr) - semanticTokens.staticConfig.highlightGroups = [] - expect(() => { - item.checkState() - }).toThrow('Unable to find highlight groups') }) }) diff --git a/src/handler/semanticTokens/buffer.ts b/src/handler/semanticTokens/buffer.ts index ba3904e2639..a7157c3c18a 100644 --- a/src/handler/semanticTokens/buffer.ts +++ b/src/handler/semanticTokens/buffer.ts @@ -64,7 +64,6 @@ const highlightGroupMap: Map = new Map() export interface StaticConfig { filetypes: string[] | null - highlightGroups: ReadonlyArray } export default class SemanticTokensBuffer implements SyncItem { @@ -190,7 +189,6 @@ export default class SemanticTokensBuffer implements SyncItem { if (!workspace.env.updateHighlight) throw new Error(`Can't perform highlight update, highlight update requires vim >= 8.1.1719 or neovim >= 0.5.0`) if (!this.configEnabled) throw new Error(`Semantic tokens highlight not enabled for current filetype: ${this.doc.filetype}`) if (!this.hasProvider || !this.hasLegend) throw new Error(`SemanticTokens provider not found for ${this.doc.uri}`) - if (this.staticConfig.highlightGroups.length === 0) throw new Error(`Unable to find highlight groups starts with CocSem`) } public async getTokenRanges( @@ -232,42 +230,41 @@ export default class SemanticTokensBuffer implements SyncItem { * Single line only. */ private addHighlightItems(highlights: SemanticTokenRange[], range: [number, number, number], tokenType: string, tokenModifiers: string[]): void { + // highlight groups: + // CocSem + type + // CocSem + modifier + // CocSem + modifier + type + let { combinedModifiers } = this.config - let { highlightGroups } = this.staticConfig - let highlightGroup: string let combine = false - // Compose highlight group CocSem + modifier + type - for (let item of tokenModifiers) { - let hlGroup = HLGROUP_PREFIX + toHighlightPart(item) + toHighlightPart(tokenType) - if (highlightGroups.includes(hlGroup)) { - combine = combinedModifiers.includes(item) - highlightGroup = hlGroup - break - } - } - if (!highlightGroup) { - for (let modifier of tokenModifiers) { - let hlGroup = HLGROUP_PREFIX + toHighlightPart(modifier) - if (highlightGroups.includes(hlGroup)) { - highlightGroup = hlGroup - combine = combinedModifiers.includes(modifier) - break - } - } - } - if (!highlightGroup) { - let hlGroup = HLGROUP_PREFIX + toHighlightPart(tokenType) - if (highlightGroups.includes(hlGroup)) { - highlightGroup = hlGroup - } - } + highlights.push({ range, tokenType, combine, - hlGroup: highlightGroup, + hlGroup: HLGROUP_PREFIX + toHighlightPart(tokenType), tokenModifiers, }) + + for (let modifier of tokenModifiers) { + combine = combinedModifiers.includes(modifier) + + highlights.push({ + range, + tokenType, + combine, + hlGroup: HLGROUP_PREFIX + toHighlightPart(modifier), + tokenModifiers, + }) + + highlights.push({ + range, + tokenType, + combine, + hlGroup: HLGROUP_PREFIX + toHighlightPart(modifier) + toHighlightPart(tokenType), + tokenModifiers, + }) + } } private toHighlightItems(highlights: ReadonlyArray, startLine?: number, endLine?: number): HighlightItem[] { diff --git a/src/handler/semanticTokens/index.ts b/src/handler/semanticTokens/index.ts index 63b44adfb42..45bbcd846c8 100644 --- a/src/handler/semanticTokens/index.ts +++ b/src/handler/semanticTokens/index.ts @@ -7,9 +7,9 @@ import BufferSync from '../../model/bufferSync' import Highlighter from '../../model/highligher' import { Documentation, FloatFactory } from '../../types' import { disposeAll } from '../../util' -import { distinct, isFalsyOrEmpty, toArray } from '../../util/array' +import { distinct, toArray } from '../../util/array' import type { Disposable } from '../../util/protocol' -import { toErrorText, toText, upperFirst } from '../../util/string' +import { toErrorText, toText } from '../../util/string' import window from '../../window' import workspace from '../../workspace' import SemanticTokensBuffer, { HLGROUP_PREFIX, NAMESPACE, StaticConfig, toHighlightPart } from './buffer' @@ -28,7 +28,6 @@ export default class SemanticTokens { constructor(private nvim: Neovim) { this.staticConfig = { filetypes: getFiletypes(), - highlightGroups: [] } workspace.onDidChangeConfiguration(e => { if (e.affectsConfiguration('semanticTokens')) { @@ -76,7 +75,6 @@ export default class SemanticTokens { return new SemanticTokensBuffer(this.nvim, doc, this.staticConfig) }) languages.onDidSemanticTokensRefresh(async selector => { - if (isFalsyOrEmpty(this.staticConfig.highlightGroups)) await this.fetchHighlightGroups() let visibleBufs = window.visibleTextEditors.map(o => o.document.bufnr) for (let item of this.highlighters.items) { if (!workspace.match(selector, item.doc)) continue @@ -157,11 +155,6 @@ export default class SemanticTokens { floatFactory?.close() } - public async fetchHighlightGroups(): Promise { - let highlightGroups = await this.nvim.call('coc#util#semantic_hlgroups') as string[] - this.staticConfig.highlightGroups = highlightGroups - } - public async getCurrentItem(): Promise { let buf = await this.nvim.buffer return this.getItem(buf.id) @@ -176,7 +169,6 @@ export default class SemanticTokens { public async highlightCurrent(): Promise { let item = await this.getCurrentItem() if (!item || !item.enabled) throw new Error(`Unable to perform semantic highlights for current buffer.`) - await this.fetchHighlightGroups() await item.forceHighlight() }