-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from aabounegm/main
Add support for semantic highlighting
- Loading branch information
Showing
5 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.vscode-test/** | ||
.DS_Store | ||
*.vsix | ||
node_modules | ||
out |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |