-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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 #2103 from KapitanOczywisty/semanticTokens
Semantic tokens provider example
- Loading branch information
Showing
6 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
190 changes: 190 additions & 0 deletions
190
test/playground.generated/extending-language-services-semantic-tokens-provider-example.html
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,190 @@ | ||
<!DOCTYPE html> | ||
<!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples --> | ||
<html> | ||
<head> | ||
<base href=".."> | ||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> | ||
</head> | ||
<body> | ||
<style> | ||
/*----------------------------------------SAMPLE CSS START*/ | ||
|
||
|
||
|
||
/*----------------------------------------SAMPLE CSS END*/ | ||
</style> | ||
<a class="loading-opts" href="playground.generated/index.html">[<< BACK]</a> <br/> | ||
THIS IS A GENERATED FILE VIA gulp generate-test-samples | ||
|
||
<div id="bar" style="margin-bottom: 6px;"></div> | ||
|
||
<div style="clear:both"></div> | ||
<div id="outer-container" style="width:800px;height:450px;border: 1px solid grey"> | ||
<!-- ----------------------------------------SAMPLE HTML START--> | ||
|
||
<div id="container" style="height:100%;"></div> | ||
|
||
|
||
<!-- ----------------------------------------SAMPLE HTML END--> | ||
</div> | ||
<div style="clear:both"></div> | ||
|
||
<script src="../metadata.js"></script> | ||
<script src="dev-setup.js"></script> | ||
<script> | ||
loadEditor(function() { | ||
/*----------------------------------------SAMPLE JS START*/ | ||
|
||
/** @type {monaco.languages.SemanticTokensLegend} */ | ||
const legend = { | ||
tokenTypes: [ | ||
'comment', 'string', 'keyword', 'number', 'regexp', 'operator', 'namespace', | ||
'type', 'struct', 'class', 'interface', 'enum', 'typeParameter', 'function', | ||
'member', 'macro', 'variable', 'parameter', 'property', 'label' | ||
], | ||
tokenModifiers: [ | ||
'declaration', 'documentation', 'readonly', 'static', 'abstract', 'deprecated', | ||
'modification', 'async' | ||
] | ||
}; | ||
|
||
/** @type {(type: string)=>number} */ | ||
function getType(type) { | ||
return legend.tokenTypes.indexOf(type); | ||
} | ||
|
||
/** @type {(modifier: string[]|string|null)=>number} */ | ||
function getModifier(modifiers) { | ||
if (typeof modifiers === 'string') { | ||
modifiers = [modifiers]; | ||
} | ||
if (Array.isArray(modifiers)) { | ||
let nModifiers = 0; | ||
for (let modifier of modifiers) { | ||
nModifier = legend.tokenModifiers.indexOf(modifier); | ||
if (nModifier > -1) { | ||
nModifiers |= (1 << nModifier) >>> 0; | ||
} | ||
} | ||
return nModifiers; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
const tokenPattern = new RegExp('(?<=\\[)([a-zA-Z]+)((?:\\.[a-zA-Z]+)*)(?=\\])', 'g'); | ||
|
||
monaco.languages.registerDocumentSemanticTokensProvider('plaintext', { | ||
getLegend: function () { | ||
return legend; | ||
}, | ||
provideDocumentSemanticTokens: function (model, lastResultId, token) { | ||
const lines = model.getLinesContent(); | ||
|
||
/** @type {number[]} */ | ||
const data = []; | ||
|
||
let prevLine = 0; | ||
let prevChar = 0; | ||
|
||
for (let i = 0; i < lines.length; i++) { | ||
const line = lines[i]; | ||
|
||
for (let match = null; match = tokenPattern.exec(line);) { | ||
// translate token and modifiers to number representations | ||
let type = getType(match[1]); | ||
if (type === -1) { | ||
continue; | ||
} | ||
let modifier = match[2].length | ||
? getModifier(match[2].split('.').slice(1)) | ||
: 0; | ||
|
||
data.push( | ||
// translate line to deltaLine | ||
i - prevLine, | ||
// for the same line, translate start to deltaStart | ||
prevLine === i ? match.index - prevChar : match.index, | ||
match[0].length, | ||
type, | ||
modifier | ||
); | ||
|
||
prevLine = i; | ||
prevChar = match.index; | ||
} | ||
} | ||
return { | ||
data: new Uint32Array(data), | ||
resultId: null | ||
}; | ||
}, | ||
releaseDocumentSemanticTokens: function (resultId) { } | ||
}); | ||
|
||
// add some missing tokens | ||
monaco.editor.defineTheme('myCustomTheme', { | ||
base: 'vs', | ||
inherit: true, | ||
rules: [ | ||
{ token: 'comment', foreground: 'aaaaaa', fontStyle: 'italic' }, | ||
{ token: 'keyword', foreground: 'ce63eb' }, | ||
{ token: 'operator', foreground: '000000' }, | ||
{ token: 'namespace', foreground: '66afce' }, | ||
|
||
{ token: 'type', foreground: '1db010' }, | ||
{ token: 'struct', foreground: '0000ff' }, | ||
{ token: 'class', foreground: '0000ff', fontStyle: 'bold' }, | ||
{ token: 'interface', foreground: '007700', fontStyle: 'bold' }, | ||
{ token: 'enum', foreground: '0077ff', fontStyle: 'bold' }, | ||
{ token: 'typeParameter', foreground: '1db010' }, | ||
{ token: 'function', foreground: '94763a' }, | ||
|
||
{ token: 'member', foreground: '94763a' }, | ||
{ token: 'macro', foreground: '615a60' }, | ||
{ token: 'variable', foreground: '3e5bbf' }, | ||
{ token: 'parameter', foreground: '3e5bbf' }, | ||
{ token: 'property', foreground: '3e5bbf' }, | ||
{ token: 'label', foreground: '615a60' }, | ||
|
||
{ token: 'type.static', fontStyle: 'bold' }, | ||
{ token: 'class.static', foreground: 'ff0000', fontStyle: 'bold' } | ||
] | ||
}); | ||
|
||
const editor = monaco.editor.create(document.getElementById("container"), { | ||
value: [ | ||
'Available token types:', | ||
' [comment] [string] [keyword] [number] [regexp] [operator] [namespace]', | ||
' [type] [struct] [class] [interface] [enum] [typeParameter] [function]', | ||
' [member] [macro] [variable] [parameter] [property] [label]', | ||
'', | ||
'Available token modifiers:', | ||
' [type.declaration] [type.documentation] [type.member] [type.static]', | ||
' [type.abstract] [type.deprecated] [type.modification] [type.async]', | ||
'', | ||
'Some examples:', | ||
' [class.static.token] [type.static.abstract]', | ||
' [class.static.token] [type.static]', | ||
'', | ||
' [struct]', | ||
'', | ||
' [function.private]', | ||
'', | ||
'An error case:', | ||
' [notInLegend]' | ||
].join('\n'), | ||
language: "plaintext", | ||
theme: 'myCustomTheme', | ||
// semantic tokens provider is disabled by default | ||
'semanticHighlighting.enabled': true | ||
}); | ||
|
||
|
||
|
||
|
||
/*----------------------------------------SAMPLE JS END*/ | ||
}); | ||
</script> | ||
</body> | ||
</html> |
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
Empty file.
1 change: 1 addition & 0 deletions
1
...ound/new-samples/extending-language-services/semantic-tokens-provider-example/sample.html
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 @@ | ||
<div id="container" style="height:100%;"></div> |
146 changes: 146 additions & 0 deletions
146
...ground/new-samples/extending-language-services/semantic-tokens-provider-example/sample.js
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,146 @@ | ||
/** @type {monaco.languages.SemanticTokensLegend} */ | ||
const legend = { | ||
tokenTypes: [ | ||
'comment', 'string', 'keyword', 'number', 'regexp', 'operator', 'namespace', | ||
'type', 'struct', 'class', 'interface', 'enum', 'typeParameter', 'function', | ||
'member', 'macro', 'variable', 'parameter', 'property', 'label' | ||
], | ||
tokenModifiers: [ | ||
'declaration', 'documentation', 'readonly', 'static', 'abstract', 'deprecated', | ||
'modification', 'async' | ||
] | ||
}; | ||
|
||
/** @type {(type: string)=>number} */ | ||
function getType(type) { | ||
return legend.tokenTypes.indexOf(type); | ||
} | ||
|
||
/** @type {(modifier: string[]|string|null)=>number} */ | ||
function getModifier(modifiers) { | ||
if (typeof modifiers === 'string') { | ||
modifiers = [modifiers]; | ||
} | ||
if (Array.isArray(modifiers)) { | ||
let nModifiers = 0; | ||
for (let modifier of modifiers) { | ||
nModifier = legend.tokenModifiers.indexOf(modifier); | ||
if (nModifier > -1) { | ||
nModifiers |= (1 << nModifier) >>> 0; | ||
} | ||
} | ||
return nModifiers; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
const tokenPattern = new RegExp('(?<=\\[)([a-zA-Z]+)((?:\\.[a-zA-Z]+)*)(?=\\])', 'g'); | ||
|
||
monaco.languages.registerDocumentSemanticTokensProvider('plaintext', { | ||
getLegend: function () { | ||
return legend; | ||
}, | ||
provideDocumentSemanticTokens: function (model, lastResultId, token) { | ||
const lines = model.getLinesContent(); | ||
|
||
/** @type {number[]} */ | ||
const data = []; | ||
|
||
let prevLine = 0; | ||
let prevChar = 0; | ||
|
||
for (let i = 0; i < lines.length; i++) { | ||
const line = lines[i]; | ||
|
||
for (let match = null; match = tokenPattern.exec(line);) { | ||
// translate token and modifiers to number representations | ||
let type = getType(match[1]); | ||
if (type === -1) { | ||
continue; | ||
} | ||
let modifier = match[2].length | ||
? getModifier(match[2].split('.').slice(1)) | ||
: 0; | ||
|
||
data.push( | ||
// translate line to deltaLine | ||
i - prevLine, | ||
// for the same line, translate start to deltaStart | ||
prevLine === i ? match.index - prevChar : match.index, | ||
match[0].length, | ||
type, | ||
modifier | ||
); | ||
|
||
prevLine = i; | ||
prevChar = match.index; | ||
} | ||
} | ||
return { | ||
data: new Uint32Array(data), | ||
resultId: null | ||
}; | ||
}, | ||
releaseDocumentSemanticTokens: function (resultId) { } | ||
}); | ||
|
||
// add some missing tokens | ||
monaco.editor.defineTheme('myCustomTheme', { | ||
base: 'vs', | ||
inherit: true, | ||
rules: [ | ||
{ token: 'comment', foreground: 'aaaaaa', fontStyle: 'italic' }, | ||
{ token: 'keyword', foreground: 'ce63eb' }, | ||
{ token: 'operator', foreground: '000000' }, | ||
{ token: 'namespace', foreground: '66afce' }, | ||
|
||
{ token: 'type', foreground: '1db010' }, | ||
{ token: 'struct', foreground: '0000ff' }, | ||
{ token: 'class', foreground: '0000ff', fontStyle: 'bold' }, | ||
{ token: 'interface', foreground: '007700', fontStyle: 'bold' }, | ||
{ token: 'enum', foreground: '0077ff', fontStyle: 'bold' }, | ||
{ token: 'typeParameter', foreground: '1db010' }, | ||
{ token: 'function', foreground: '94763a' }, | ||
|
||
{ token: 'member', foreground: '94763a' }, | ||
{ token: 'macro', foreground: '615a60' }, | ||
{ token: 'variable', foreground: '3e5bbf' }, | ||
{ token: 'parameter', foreground: '3e5bbf' }, | ||
{ token: 'property', foreground: '3e5bbf' }, | ||
{ token: 'label', foreground: '615a60' }, | ||
|
||
{ token: 'type.static', fontStyle: 'bold' }, | ||
{ token: 'class.static', foreground: 'ff0000', fontStyle: 'bold' } | ||
] | ||
}); | ||
|
||
const editor = monaco.editor.create(document.getElementById("container"), { | ||
value: [ | ||
'Available token types:', | ||
' [comment] [string] [keyword] [number] [regexp] [operator] [namespace]', | ||
' [type] [struct] [class] [interface] [enum] [typeParameter] [function]', | ||
' [member] [macro] [variable] [parameter] [property] [label]', | ||
'', | ||
'Available token modifiers:', | ||
' [type.declaration] [type.documentation] [type.member] [type.static]', | ||
' [type.abstract] [type.deprecated] [type.modification] [type.async]', | ||
'', | ||
'Some examples:', | ||
' [class.static.token] [type.static.abstract]', | ||
' [class.static.token] [type.static]', | ||
'', | ||
' [struct]', | ||
'', | ||
' [function.private]', | ||
'', | ||
'An error case:', | ||
' [notInLegend]' | ||
].join('\n'), | ||
language: "plaintext", | ||
theme: 'myCustomTheme', | ||
// semantic tokens provider is disabled by default | ||
'semanticHighlighting.enabled': true | ||
}); | ||
|
||
|