Skip to content

Commit

Permalink
Merge pull request #15 from aabounegm/main
Browse files Browse the repository at this point in the history
Add support for semantic highlighting
  • Loading branch information
fizruk authored Jun 20, 2023
2 parents 9a2de6f + ba55065 commit 2606e6c
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.vscode-test/**
.DS_Store
*.vsix
node_modules
out
99 changes: 99 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"version": "0.2.6",
"repository": "https://github.com/fizruk/vscode-rzk/rzk-1-experimental-highlighting",
"publisher": "NikolaiKudasovfizruk",
"main": "./out/extension.js",
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc --watch -p ./"
},
"engines": {
"vscode": "^1.76.0"
},
Expand Down Expand Up @@ -141,6 +147,9 @@
}
},
"devDependencies": {
"js-yaml": "^4.1.0"
"@types/node": "^20.3.1",
"@types/vscode": "^1.79.1",
"js-yaml": "^4.1.0",
"typescript": "^5.1.3"
}
}
109 changes: 109 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import * as vscode from 'vscode';
import { spawnSync } from 'node:child_process';

// Inspired by https://github.com/microsoft/vscode-extension-samples/tree/main/semantic-tokens-sample

// https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide#standard-token-types-and-modifiers
const tokenTypes = [
'namespace',
'class',
'enum',
'interface',
'struct',
'typeParameter',
'type',
'parameter',
'variable',
'property',
'enumMember',
'decorator',
'event',
'function',
'method',
'macro',
'label',
'comment',
'string',
'keyword',
'number',
'regexp',
'operator',
] as const;

const tokenModifiers = [
'declaration',
'definition',
'readonly',
'static',
'deprecated',
'abstract',
'async',
'modification',
'documentation',
'defaultLibrary',
] as const;

// A workaround because a readonly string[] is not assignable to a normal string[]
type Writeable<T> = { -readonly [P in keyof T]: T[P] };
const legend = new vscode.SemanticTokensLegend(
tokenTypes as Writeable<typeof tokenTypes>,
tokenModifiers as Writeable<typeof tokenModifiers>
);

let output = vscode.window.createOutputChannel('Rzk');
export function activate(context: vscode.ExtensionContext) {
output.appendLine('Rzk extension activated.');
context.subscriptions.push(
vscode.languages.registerDocumentSemanticTokensProvider(
['rzk', 'literate rzk markdown'],
new DocumentSemanticTokensProvider(),
legend
)
);
}

type TokenType = (typeof tokenTypes)[number];
type TokenModifier = (typeof tokenModifiers)[number];

interface ParsedToken {
line: number;
startCharacter: number;
length: number;
tokenType: TokenType;
tokenModifiers: TokenModifier[];
}

class DocumentSemanticTokensProvider
implements vscode.DocumentSemanticTokensProvider
{
async provideDocumentSemanticTokens(
document: vscode.TextDocument,
token: vscode.CancellationToken
): Promise<vscode.SemanticTokens> {
output.appendLine(`Parsing file "${document.uri}"`);
const allTokens: ParsedToken[] = this._parseText(document.getText());
const builder = new vscode.SemanticTokensBuilder(legend);
allTokens.forEach((token) => {
builder.push(
new vscode.Range(
new vscode.Position(token.line, token.startCharacter),
new vscode.Position(token.line, token.startCharacter + token.length)
),
token.tokenType,
token.tokenModifiers
);
});
return builder.build();
}

private _parseText(doc: string): ParsedToken[] {
const processResult = spawnSync('rzk', ['tokenize'], { input: doc });
const stdout = processResult.stdout.toString();
output.appendLine(stdout);
try {
return JSON.parse(stdout);
} catch {
return [];
}
}
}
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2020",
"lib": ["es2020"],
"outDir": "out",
"sourceMap": true,
"rootDir": "src",
"strict": true
},
"exclude": ["node_modules", ".vscode-test"]
}

0 comments on commit 2606e6c

Please sign in to comment.