Skip to content

Commit cadcfb9

Browse files
Register semantic tokens provider. Closes #44.
1 parent f256843 commit cadcfb9

1 file changed

Lines changed: 5 additions & 198 deletions

File tree

‎src/extension.ts‎

Lines changed: 5 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -327,212 +327,19 @@ export async function activate(context: vscode.ExtensionContext) {
327327

328328
const enabledLangs: string[] =
329329
vscode.workspace.getConfiguration("syntax").get("highlightLanguages");
330-
let supportedLangs: string[] = [];
330+
let supportedLangs: { language: string }[] = [];
331331
availableGrammars.forEach(lang => {
332332
if (availableParsers.includes(lang) && enabledLangs.includes(lang))
333-
supportedLangs.push(lang);
333+
supportedLangs.push({language: lang});
334334
});
335335

336-
// Term colors
337-
const availableTerms: string[] = [
338-
"type", "scope", "function", "variable", "number", "string", "comment",
339-
"constant", "directive", "control", "operator", "modifier", "punctuation",
340-
];
341-
const enabledTerms: string[] =
342-
vscode.workspace.getConfiguration("syntax").get("highlightTerms");
343-
let supportedTerms: string[] = [];
344-
availableTerms.forEach(term => {
345-
if (enabledTerms.includes(term))
346-
supportedTerms.push(term);
347-
});
348-
if (!vscode.workspace.getConfiguration("syntax").get("highlightComment"))
349-
if (supportedTerms.includes("comment"))
350-
supportedTerms.splice(supportedTerms.indexOf("comment"), 1);
351-
352-
// Debug depth
353-
debugDepth = vscode.workspace.getConfiguration("syntax").get("debugDepth");
354-
355-
// Decoration definitions
356-
const highlightDecors: { [color: string]: vscode.TextEditorDecorationType } = {};
357-
for (const c of supportedTerms)
358-
highlightDecors[c] = vscode.window.
359-
createTextEditorDecorationType({
360-
color: new vscode.ThemeColor("syntax." + c)
361-
});
362-
// Decoration cache
363-
const decorCache: { [doc: string]: { [color: string]: vscode.Range[] } } = {};
364-
365-
// Timer to schedule decoration update and refresh
366-
let updateTimer: NodeJS.Timer | undefined = undefined;
367-
let refreshTimer: NodeJS.Timer | undefined = undefined;
368-
console.log('{Syntax Highlighter} has been activated');
369-
370-
let visibleEditors = vscode.window.visibleTextEditors;
371-
let visibleUris: string[] = [];
372-
let refreshUris: string[] = [];
373-
374-
function refreshDecor() {
375-
for (const e of visibleEditors)
376-
{
377-
const uri = e.document.uri.toString();
378-
if (!refreshUris.includes(uri))
379-
continue;
380-
if (!(uri in decorCache))
381-
continue;
382-
const decorations = decorCache[uri];
383-
for (const c in decorations)
384-
e.setDecorations(highlightDecors[c], decorations[c]);
385-
}
386-
refreshUris = [];
387-
}
388-
389-
function enqueueDecorRefresh() {
390-
if (refreshTimer) {
391-
clearTimeout(refreshTimer);
392-
refreshTimer = undefined;
393-
}
394-
refreshTimer = setTimeout(refreshDecor, 20);
395-
}
396-
397-
function buildDecor(doc: vscode.TextDocument) {
398-
// Document syntax tree
399-
const uri = doc.uri.toString();
400-
if (!(uri in trees))
401-
return;
402-
// Parse syntax tree
403-
const grammar = grammars[doc.languageId];
404-
const terms = grammar.parse(trees[uri]);
405-
// Decorations
406-
let decorations: { [color: string]: vscode.Range[] } = {};
407-
for (const c in highlightDecors)
408-
decorations[c] = [];
409-
// Add decorations for terms
410-
for (const t of terms)
411-
if (t.term in highlightDecors)
412-
decorations[t.term].push(t.range);
413-
// Cache and refresh decorations
414-
decorCache[uri] = decorations;
415-
if (!refreshUris.includes(uri))
416-
refreshUris.push(uri);
417-
}
418-
419-
function updateDecor() {
420-
for (const e of visibleEditors) {
421-
const uri = e.document.uri.toString();
422-
if (!(uri in trees))
423-
continue;
424-
if (uri in decorCache)
425-
continue;
426-
buildDecor(e.document);
427-
}
428-
if (refreshUris.length > 0)
429-
enqueueDecorRefresh();
430-
}
431-
432-
function enqueueDecorUpdate() {
433-
if (updateTimer) {
434-
clearTimeout(updateTimer);
435-
updateTimer = undefined;
436-
}
437-
updateTimer = setTimeout(updateDecor, 20);
438-
}
439-
440-
async function initTree(doc: vscode.TextDocument) {
441-
const lang = doc.languageId;
442-
if (!supportedLangs.includes(lang))
443-
return;
444-
if (!(lang in grammars)) {
445-
grammars[lang] = new Grammar(lang);
446-
await grammars[lang].init();
447-
}
448-
const uri = doc.uri.toString();
449-
trees[uri] = grammars[lang].tree(doc.getText());
450-
enqueueDecorUpdate();
451-
}
452-
453-
function updateTree(doc: vscode.TextDocument) {
454-
const uri = doc.uri.toString();
455-
const lang = doc.languageId;
456-
if (!(uri in trees))
457-
return;
458-
459-
// Update tree
460-
trees[uri] = grammars[lang].tree(doc.getText())
461-
462-
// Invalidate decoration cache and enqueue update
463-
delete decorCache[uri];
464-
if (visibleUris.includes(uri))
465-
enqueueDecorUpdate();
466-
}
467-
468-
469-
// Create trees for already opened documents
470-
for (const doc of vscode.workspace.textDocuments)
471-
await initTree(doc);
472-
enqueueDecorUpdate();
473-
474-
vscode.workspace.onDidOpenTextDocument(async doc => {
475-
await initTree(doc);
476-
}, null, context.subscriptions)
477-
478-
vscode.workspace.onDidCloseTextDocument(doc => {
479-
const uri = doc.uri.toString();
480-
delete trees[uri];
481-
delete decorCache[uri];
482-
if (refreshUris.includes(uri))
483-
refreshUris.splice(refreshUris.indexOf(uri), 1);
484-
}, null, context.subscriptions)
485-
486-
vscode.workspace.onDidChangeTextDocument(event => {
487-
const uri = event.document.uri.toString();
488-
if (!(uri in trees))
489-
return;
490-
if (event.contentChanges.length < 1)
491-
return;
492-
updateTree(event.document);
493-
}, null, context.subscriptions);
494-
495-
496-
// Enumerate already visible editors
497-
visibleEditors = vscode.window.visibleTextEditors;
498-
visibleUris = [];
499-
for (const e of visibleEditors) {
500-
const uri = e.document.uri.toString();
501-
if (!visibleUris.includes(uri))
502-
visibleUris.push(uri);
503-
}
504-
505-
vscode.window.onDidChangeVisibleTextEditors(editors => {
506-
// Flag refresh for new editors
507-
let needUpdate = false;
508-
for (const e of editors) {
509-
const uri = e.document.uri.toString();
510-
if (visibleEditors.includes(e))
511-
continue;
512-
if (!refreshUris.includes(uri))
513-
refreshUris.push(uri);
514-
if (uri in trees)
515-
needUpdate = true;
516-
}
517-
518-
// Set visible editors
519-
visibleEditors = editors;
520-
visibleUris = [];
521-
for (const e of visibleEditors) {
522-
const uri = e.document.uri.toString();
523-
if (!visibleUris.includes(uri))
524-
visibleUris.push(uri);
525-
}
526-
527-
// Enqueue refresh if required
528-
if (needUpdate)
529-
enqueueDecorUpdate();
530-
}, null, context.subscriptions);
336+
context.subscriptions.push(
337+
vscode.languages.registerDocumentSemanticTokensProvider(
338+
supportedLangs, new TokensProvider(), legend));
531339

532340
// Register debug hover providers
533341
// Very useful tool for implementation and fixing of grammars
534342
if (vscode.workspace.getConfiguration("syntax").get("debugHover"))
535343
for (const lang of supportedLangs)
536344
vscode.languages.registerHoverProvider(lang, { provideHover: scopeInfo });
537-
538345
}

0 commit comments

Comments
 (0)