-
Notifications
You must be signed in to change notification settings - Fork 123
feat: add semantic tokens #2191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import * as ts from 'typescript/lib/tsserverlibrary'; | ||
import * as lsp from 'vscode-languageserver/node'; | ||
|
||
export enum TokenEncodingConsts { | ||
typeOffset = 8, | ||
modifierMask = (1 << typeOffset) - 1, | ||
} | ||
|
||
export function getSemanticTokens( | ||
classifications: ts.Classifications, script: ts.server.ScriptInfo): lsp.SemanticTokens { | ||
const spans = classifications.spans; | ||
const builder = new lsp.SemanticTokensBuilder(); | ||
|
||
for (let i = 0; i < spans.length;) { | ||
const offset = spans[i++]; | ||
const length = spans[i++]; | ||
const tsClassification = spans[i++]; | ||
|
||
const tokenType = getTokenTypeFromClassification(tsClassification); | ||
if (tokenType === undefined) { | ||
continue; | ||
} | ||
|
||
const tokenModifiers = getTokenModifierFromClassification(tsClassification); | ||
|
||
const startPos = script.positionToLineOffset(offset); | ||
startPos.line -= 1; | ||
startPos.offset -= 1; | ||
|
||
const endPos = script.positionToLineOffset(offset + length); | ||
endPos.line -= 1; | ||
endPos.offset -= 1; | ||
|
||
for (let line = startPos.line; line <= endPos.line; line++) { | ||
const startCharacter = line === startPos.line ? startPos.offset : 0; | ||
const endCharacter = | ||
line === endPos.line ? endPos.offset : script.lineToTextSpan(line - 1).length; | ||
builder.push(line, startCharacter, endCharacter - startCharacter, tokenType, tokenModifiers); | ||
} | ||
} | ||
|
||
return builder.build(); | ||
} | ||
|
||
function getTokenTypeFromClassification(tsClassification: number): number|undefined { | ||
if (tsClassification > TokenEncodingConsts.modifierMask) { | ||
return (tsClassification >> TokenEncodingConsts.typeOffset) - 1; | ||
} | ||
return undefined; | ||
} | ||
|
||
function getTokenModifierFromClassification(tsClassification: number) { | ||
return tsClassification & TokenEncodingConsts.modifierMask; | ||
} | ||
Comment on lines
+53
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I wonder if this should really be something that the language service provides since it's so highly dependent on the offset and modifier mask actually matching what the language service returns. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used the integrated Typescript Server from VSCode as a reference. They don't have a "single source of truth" either, most likely because Typescript doesn't include these definitions in the public API. If I understand you correctly, we could add these methods to Let me know if I should move this code to the language service! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good to move them over to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind linking to the TS definitions like you did in angular/angular#60260? Context is pretty lost between the two sometimes