-
-
Notifications
You must be signed in to change notification settings - Fork 249
(feat) Semantic tokens #752
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b257b00
first crack at typescript semantic tokens
jasonlyu123 cc58c1b
use index mapping for legend instead
jasonlyu123 d5e6b7f
reanme label => legend
jasonlyu123 82cf633
test
jasonlyu123 07601ed
test for partial semantic token
jasonlyu123 1e459ef
config
jasonlyu123 8b03695
remove unused helper function
jasonlyu123 27a331c
Apply suggestions from code review
jasonlyu123 dd86ec0
change execute mode to first not null
jasonlyu123 9f31368
short when error
jasonlyu123 c587140
group test data by tokens to better distinguish assertion result
jasonlyu123 03d256a
remove unused code
dummdidumm 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
78 changes: 78 additions & 0 deletions
78
packages/language-server/src/lib/semanticToken/semanticTokenLegend.ts
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,78 @@ | ||
| import { | ||
| SemanticTokensLegend, | ||
| SemanticTokenModifiers, | ||
| SemanticTokenTypes | ||
| } from 'vscode-languageserver'; | ||
|
|
||
| /** | ||
| * extended from https://github.com/microsoft/TypeScript/blob/35c8df04ad959224fad9037e340c1e50f0540a49/src/services/classifier2020.ts#L9 | ||
| * so that we don't have to map it into our own legend | ||
| */ | ||
| export const enum TokenType { | ||
| class, | ||
| enum, | ||
| interface, | ||
| namespace, | ||
| typeParameter, | ||
| type, | ||
| parameter, | ||
| variable, | ||
| enumMember, | ||
| property, | ||
| function, | ||
| member, | ||
|
|
||
| // svelte | ||
| event | ||
| } | ||
|
|
||
| /** | ||
| * adopted from https://github.com/microsoft/TypeScript/blob/35c8df04ad959224fad9037e340c1e50f0540a49/src/services/classifier2020.ts#L13 | ||
| * so that we don't have to map it into our own legend | ||
| */ | ||
| export const enum TokenModifier { | ||
| declaration, | ||
| static, | ||
| async, | ||
| readonly, | ||
| defaultLibrary, | ||
| local | ||
| } | ||
|
|
||
| export function getSemanticTokenLegends(): SemanticTokensLegend { | ||
| const tokenModifiers: string[] = []; | ||
|
|
||
| ([ | ||
| [TokenModifier.declaration, SemanticTokenModifiers.declaration], | ||
| [TokenModifier.static, SemanticTokenModifiers.static], | ||
| [TokenModifier.async, SemanticTokenModifiers.async], | ||
| [TokenModifier.readonly, SemanticTokenModifiers.readonly], | ||
| [TokenModifier.defaultLibrary, SemanticTokenModifiers.defaultLibrary], | ||
| [TokenModifier.local, 'local'] | ||
| ] as const).forEach(([tsModifier, legend]) => (tokenModifiers[tsModifier] = legend)); | ||
|
|
||
| const tokenTypes: string[] = []; | ||
|
|
||
| ([ | ||
| [TokenType.class, SemanticTokenTypes.class], | ||
| [TokenType.enum, SemanticTokenTypes.enum], | ||
| [TokenType.interface, SemanticTokenTypes.interface], | ||
| [TokenType.namespace, SemanticTokenTypes.namespace], | ||
| [TokenType.typeParameter, SemanticTokenTypes.typeParameter], | ||
| [TokenType.type, SemanticTokenTypes.type], | ||
| [TokenType.parameter, SemanticTokenTypes.parameter], | ||
| [TokenType.variable, SemanticTokenTypes.variable], | ||
| [TokenType.enumMember, SemanticTokenTypes.enumMember], | ||
| [TokenType.property, SemanticTokenTypes.property], | ||
| [TokenType.function, SemanticTokenTypes.function], | ||
|
|
||
| // member is renamed to method in vscode codebase to match LSP default | ||
| [TokenType.member, SemanticTokenTypes.method], | ||
| [TokenType.event, SemanticTokenTypes.event] | ||
| ] as const).forEach(([tokenType, legend]) => (tokenTypes[tokenType] = legend)); | ||
|
|
||
| return { | ||
| tokenModifiers, | ||
| tokenTypes | ||
| }; | ||
| } |
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
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
107 changes: 107 additions & 0 deletions
107
packages/language-server/src/plugins/typescript/features/SemanticTokensProvider.ts
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,107 @@ | ||
| import ts from 'typescript'; | ||
| import { | ||
| Range, | ||
| SemanticTokens, | ||
| SemanticTokensBuilder | ||
| } from 'vscode-languageserver'; | ||
| import { Document } from '../../../lib/documents'; | ||
| import { SemanticTokensProvider } from '../../interfaces'; | ||
| import { SnapshotFragment } from '../DocumentSnapshot'; | ||
| import { LSAndTSDocResolver } from '../LSAndTSDocResolver'; | ||
| import { convertToTextSpan } from '../utils'; | ||
|
|
||
| export class SemanticTokensProviderImpl implements SemanticTokensProvider { | ||
| constructor(private readonly lsAndTsDocResolver: LSAndTSDocResolver) {} | ||
|
|
||
| async getSemanticTokens(textDocument: Document, range?: Range): Promise<SemanticTokens> { | ||
| const { lang, tsDoc } = this.lsAndTsDocResolver.getLSAndTSDoc(textDocument); | ||
| const fragment = await tsDoc.getFragment(); | ||
| const textSpan = range | ||
| ? convertToTextSpan(range, fragment) | ||
| : { | ||
| start: 0, | ||
| length: fragment.text.length | ||
| }; | ||
|
jasonlyu123 marked this conversation as resolved.
|
||
|
|
||
| const { spans } = lang.getEncodedSemanticClassifications( | ||
| tsDoc.filePath, | ||
| textSpan, | ||
| ts.SemanticClassificationFormat.TwentyTwenty | ||
| ); | ||
|
|
||
| const builder = new SemanticTokensBuilder(); | ||
| let index = 0; | ||
|
|
||
| while (index < spans.length) { | ||
| // [start, length, encodedClassification, start2, length2, encodedClassification2] | ||
| const generatedOffset = spans[index++]; | ||
| const generatedLength = spans[index++]; | ||
| const encodedClassification = spans[index++]; | ||
| const classificationType = this.getTokenTypeFromClassification(encodedClassification); | ||
| if (classificationType < 0) { | ||
| continue; | ||
| } | ||
|
|
||
| const originalPosition = this.mapToOrigin( | ||
| textDocument, | ||
| fragment, | ||
| generatedOffset, | ||
| generatedLength | ||
| ); | ||
| if (!originalPosition) { | ||
| continue; | ||
| } | ||
|
|
||
| const [line, character, length] = originalPosition; | ||
|
|
||
| // remove identifers whose start and end mapped to the same location | ||
| // like the svelte2tsx inserted render function | ||
| if (!length) { | ||
| continue; | ||
| } | ||
|
|
||
| const modifier = this.getTokenModifierFromClassification(encodedClassification); | ||
|
|
||
| builder.push(line, character, length, classificationType , modifier); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| private mapToOrigin( | ||
| document: Document, | ||
| fragment: SnapshotFragment, | ||
| generatedOffset: number, | ||
| generatedLength: number | ||
| ): [line: number, character: number, length: number] | undefined { | ||
| const startPosition = fragment.getOriginalPosition(fragment.positionAt(generatedOffset)); | ||
|
|
||
| if (startPosition.line < 0) { | ||
| return; | ||
| } | ||
|
|
||
| const endPosition = fragment.getOriginalPosition( | ||
| fragment.positionAt(generatedOffset + generatedLength) | ||
| ); | ||
| const startOffset = document.offsetAt(startPosition); | ||
| const endOffset = document.offsetAt(endPosition); | ||
|
|
||
| return [startPosition.line, startPosition.character, endOffset - startOffset]; | ||
| } | ||
|
|
||
| /** | ||
| * TSClassification = (TokenType + 1) << TokenEncodingConsts.typeOffset + TokenModifier | ||
| */ | ||
| private getTokenTypeFromClassification(tsClassification: number): number { | ||
| return (tsClassification >> TokenEncodingConsts.typeOffset) - 1; | ||
| } | ||
|
|
||
| private getTokenModifierFromClassification(tsClassification: number) { | ||
| return tsClassification & TokenEncodingConsts.modifierMask; | ||
| } | ||
| } | ||
|
|
||
| const enum TokenEncodingConsts { | ||
| typeOffset = 8, | ||
| modifierMask = (1 << typeOffset) - 1 | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.