-
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
Draft
JannikLassahn
wants to merge
1
commit into
angular:main
Choose a base branch
from
JannikLassahn:feature/semantic-tokens
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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. |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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