|
| 1 | +import { Buffer, Neovim } from '@chemzqm/neovim' |
| 2 | +import { Disposable, SemanticTokensLegend } from 'vscode-languageserver-protocol' |
| 3 | +import languages from '../../languages' |
| 4 | +import { disposeAll } from '../../util' |
| 5 | +import workspace from '../../workspace' |
| 6 | +import helper from '../helper' |
| 7 | + |
| 8 | +let nvim: Neovim |
| 9 | +let disposables: Disposable[] = [] |
| 10 | +let highlighter |
| 11 | +let legend: SemanticTokensLegend = { |
| 12 | + tokenTypes: [ |
| 13 | + "comment", |
| 14 | + "keyword", |
| 15 | + "string", |
| 16 | + "number", |
| 17 | + "regexp", |
| 18 | + "operator", |
| 19 | + "namespace", |
| 20 | + "type", |
| 21 | + "struct", |
| 22 | + "class", |
| 23 | + "interface", |
| 24 | + "enum", |
| 25 | + "enumMember", |
| 26 | + "typeParameter", |
| 27 | + "function", |
| 28 | + "method", |
| 29 | + "property", |
| 30 | + "macro", |
| 31 | + "variable", |
| 32 | + "parameter", |
| 33 | + "angle", |
| 34 | + "arithmetic", |
| 35 | + "attribute", |
| 36 | + "bitwise", |
| 37 | + "boolean", |
| 38 | + "brace", |
| 39 | + "bracket", |
| 40 | + "builtinType", |
| 41 | + "character", |
| 42 | + "colon", |
| 43 | + "comma", |
| 44 | + "comparison", |
| 45 | + "constParameter", |
| 46 | + "dot", |
| 47 | + "escapeSequence", |
| 48 | + "formatSpecifier", |
| 49 | + "generic", |
| 50 | + "label", |
| 51 | + "lifetime", |
| 52 | + "logical", |
| 53 | + "operator", |
| 54 | + "parenthesis", |
| 55 | + "punctuation", |
| 56 | + "selfKeyword", |
| 57 | + "semicolon", |
| 58 | + "typeAlias", |
| 59 | + "union", |
| 60 | + "unresolvedReference" |
| 61 | + ], |
| 62 | + tokenModifiers: [ |
| 63 | + "documentation", |
| 64 | + "declaration", |
| 65 | + "definition", |
| 66 | + "static", |
| 67 | + "abstract", |
| 68 | + "deprecated", |
| 69 | + "readonly", |
| 70 | + "constant", |
| 71 | + "controlFlow", |
| 72 | + "injected", |
| 73 | + "mutable", |
| 74 | + "consuming", |
| 75 | + "async", |
| 76 | + "library", |
| 77 | + "public", |
| 78 | + "unsafe", |
| 79 | + "attribute", |
| 80 | + "trait", |
| 81 | + "callable", |
| 82 | + "intraDocLink" |
| 83 | + ] |
| 84 | +} |
| 85 | + |
| 86 | +beforeAll(async () => { |
| 87 | + await helper.setup() |
| 88 | + nvim = helper.nvim |
| 89 | + highlighter = helper.plugin.getHandler().semanticHighlighter |
| 90 | +}) |
| 91 | + |
| 92 | +afterAll(async () => { |
| 93 | + await helper.shutdown() |
| 94 | +}) |
| 95 | + |
| 96 | +beforeEach(async () => { |
| 97 | + async function createBuffer(code: string): Promise<Buffer> { |
| 98 | + let buf = await nvim.buffer |
| 99 | + await nvim.command('setf rust') |
| 100 | + await buf.setLines(code.split('\n'), { start: 0, end: -1, strictIndexing: false }) |
| 101 | + let doc = await workspace.document |
| 102 | + doc.forceSync() |
| 103 | + return buf |
| 104 | + } |
| 105 | + |
| 106 | + disposables.push(languages.registerDocumentSemanticTokensProvider([{ language: 'rust' }], { |
| 107 | + legend, |
| 108 | + provideDocumentSemanticTokens: () => { |
| 109 | + return { |
| 110 | + resultId: '1', |
| 111 | + data: [ |
| 112 | + 0, 0, 2, 1, 0, |
| 113 | + 0, 3, 4, 14, 2, |
| 114 | + 0, 4, 1, 41, 0, |
| 115 | + 0, 1, 1, 41, 0, |
| 116 | + 0, 2, 1, 25, 0, |
| 117 | + 1, 4, 8, 17, 0, |
| 118 | + 0, 8, 1, 41, 0, |
| 119 | + 0, 1, 3, 2, 0, |
| 120 | + 0, 3, 1, 41, 0, |
| 121 | + 0, 1, 1, 44, 0, |
| 122 | + 1, 0, 1, 25, 0, |
| 123 | + ] |
| 124 | + } |
| 125 | + } |
| 126 | + }, legend)) |
| 127 | + await createBuffer(`fn main() { |
| 128 | + println!("H"); |
| 129 | +}`) |
| 130 | +}) |
| 131 | + |
| 132 | +afterEach(async () => { |
| 133 | + workspace.configurations.updateUserConfig({ |
| 134 | + 'coc.preferences.semanticTokensHighlights': true |
| 135 | + }) |
| 136 | + await helper.reset() |
| 137 | + disposeAll(disposables) |
| 138 | + disposables = [] |
| 139 | +}) |
| 140 | + |
| 141 | +describe('semanticTokens', () => { |
| 142 | + describe('triggerSemanticTokens', () => { |
| 143 | + it('should be disabled', async () => { |
| 144 | + workspace.configurations.updateUserConfig({ |
| 145 | + 'coc.preferences.semanticTokensHighlights': false |
| 146 | + }) |
| 147 | + const enabled = await highlighter.enabled |
| 148 | + expect(enabled).toBe(false) |
| 149 | + }) |
| 150 | + |
| 151 | + it('should get legend by API', async () => { |
| 152 | + const doc = await workspace.document |
| 153 | + const l = languages.getLegend(doc.textDocument) |
| 154 | + expect(l).toEqual(legend) |
| 155 | + }) |
| 156 | + |
| 157 | + it('should get semanticTokens by API', async () => { |
| 158 | + const doc = await workspace.document |
| 159 | + const highlights = await highlighter.getHighlights(doc.bufnr) |
| 160 | + expect(highlights.length).toBe(11) |
| 161 | + expect(highlights[0].group).toBe('CocSem_keyword') |
| 162 | + }) |
| 163 | + |
| 164 | + it('should doHighlight', async () => { |
| 165 | + const doc = await workspace.document |
| 166 | + await nvim.call('CocAction', 'semanticHighlight') |
| 167 | + const highlights = await nvim.call("coc#highlight#get_highlights", [doc.bufnr, 'semanticTokens']) |
| 168 | + expect(highlights.length).toBe(11) |
| 169 | + expect(highlights[0].group).toBe('CocSem_keyword') |
| 170 | + }) |
| 171 | + |
| 172 | + it('should clear highlights', async () => { |
| 173 | + const doc = await workspace.document |
| 174 | + await nvim.call('CocAction', 'semanticHighlight') |
| 175 | + await helper.wait(100) |
| 176 | + await highlighter.clearHighlight(doc.bufnr) |
| 177 | + await helper.wait(100) |
| 178 | + const highlights = await nvim.call("coc#highlight#get_highlights", [doc.bufnr, 'semanticTokens']) |
| 179 | + expect(highlights.length).toBe(0) |
| 180 | + }) |
| 181 | + }) |
| 182 | +}) |
0 commit comments