Skip to content

Commit

Permalink
feat(semanticTokens)!: token highlight groups
Browse files Browse the repository at this point in the history
CocSem + type
CocSem + modifier
CocSem + modifier + type

The highlight group didn't need to be defined first

related #4659
  • Loading branch information
fannheyward committed Jun 5, 2023
1 parent edcc376 commit 2176e52
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 49 deletions.
10 changes: 1 addition & 9 deletions src/__tests__/handler/semanticTokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ describe('semanticTokens', () => {
}
}
}, { tokenModifiers: [], tokenTypes: [] }))
await semanticTokens.fetchHighlightGroups()
await semanticTokens.showHighlightInfo()
let buf = await nvim.buffer
let lines = await buf.lines
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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')
})
})

Expand Down
57 changes: 27 additions & 30 deletions src/handler/semanticTokens/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ const highlightGroupMap: Map<string, string> = new Map()

export interface StaticConfig {
filetypes: string[] | null
highlightGroups: ReadonlyArray<string>
}

export default class SemanticTokensBuffer implements SyncItem {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<SemanticTokenRange>, startLine?: number, endLine?: number): HighlightItem[] {
Expand Down
12 changes: 2 additions & 10 deletions src/handler/semanticTokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -28,7 +28,6 @@ export default class SemanticTokens {
constructor(private nvim: Neovim) {
this.staticConfig = {
filetypes: getFiletypes(),
highlightGroups: []
}
workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('semanticTokens')) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -157,11 +155,6 @@ export default class SemanticTokens {
floatFactory?.close()
}

public async fetchHighlightGroups(): Promise<void> {
let highlightGroups = await this.nvim.call('coc#util#semantic_hlgroups') as string[]
this.staticConfig.highlightGroups = highlightGroups
}

public async getCurrentItem(): Promise<SemanticTokensBuffer | undefined> {
let buf = await this.nvim.buffer
return this.getItem(buf.id)
Expand All @@ -176,7 +169,6 @@ export default class SemanticTokens {
public async highlightCurrent(): Promise<void> {
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()
}

Expand Down

0 comments on commit 2176e52

Please sign in to comment.