Skip to content

Commit

Permalink
feat(semanticTokens): keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
fannheyward committed Nov 23, 2023
1 parent be8c5b4 commit c6ea405
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/features/semanticTokens.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { convertOffsetsToRange } from '@zzzen/pyright-internal/dist/common/positionUtils';
import { convertOffsetsToRange, convertTextRangeToRange } from '@zzzen/pyright-internal/dist/common/positionUtils';
import {
CancellationToken,
DocumentSemanticTokensProvider,
Expand All @@ -19,6 +19,7 @@ const tokenTypes: string[] = [
SemanticTokenTypes.enum,
SemanticTokenTypes.enumMember,
SemanticTokenTypes.function,
SemanticTokenTypes.keyword,
SemanticTokenTypes.method,
SemanticTokenTypes.namespace,
SemanticTokenTypes.parameter,
Expand Down Expand Up @@ -58,6 +59,14 @@ export class PythonSemanticTokensProvider implements DocumentSemanticTokensProvi
if (token && token.isCancellationRequested) return null;

const builder = new SemanticTokensBuilder(this.legend);
// @ts-ignore
for (const item of parsed.tokenizerOutput.tokens._items) {
if (item.type === 8 && item.keywordType) {
const range = convertTextRangeToRange(item, parsed.tokenizerOutput.lines);
builder.push(range.start.line, range.start.character, item.length, encodeTokenType(SemanticTokenTypes.keyword));
}
}

const walker = new SemanticTokensWalker();
walker.walk(parsed.parseTree);

Expand Down
5 changes: 2 additions & 3 deletions src/parsers/semanticTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,12 @@ export class SemanticTokensWalker extends ParseTreeWalker {
modifiers.push(SemanticTokenModifiers.async);
}
const type = node.parent?.parent?.nodeType === 10 ? SemanticTokenTypes.method : SemanticTokenTypes.function;
this.addItem(node.name, type);
this.addItem(node.name, type, modifiers);

for (const p of node.parameters) {
if (!p.name) continue;

const modifiers = [SemanticTokenModifiers.declaration];
this.addItem(p.name, SemanticTokenTypes.parameter, modifiers);
this.addItem(p.name, SemanticTokenTypes.parameter);
if (p.typeAnnotation) {
this.addItem(p.typeAnnotation, SemanticTokenTypes.typeParameter);
}
Expand Down

0 comments on commit c6ea405

Please sign in to comment.