Skip to content
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

fix semantic highlighting on names that are wrapped in parentheses across multiple lines #957

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@
"ruff.nativeServer": true,
"typescript.inlayHints.parameterNames.enabled": "literals",
"typescript.inlayHints.enumMemberValues.enabled": true,
"typescript.inlayHints.functionLikeReturnTypes.enabled": true,
"typescript.inlayHints.parameterTypes.enabled": true,
"typescript.inlayHints.propertyDeclarationTypes.enabled": true,
"typescript.inlayHints.variableTypes.enabled": true,
"jest.jestCommandLine": "npm run jest --",
"files.readonlyInclude": {
".pdm-build/**/*": true,
Expand Down
57 changes: 27 additions & 30 deletions packages/pyright-internal/src/analyzer/semanticTokensWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export class SemanticTokensWalker extends ParseTreeWalker {
super();
}
override visitClass(node: ClassNode): boolean {
this._addItem(node.d.name.start, node.d.name.length, SemanticTokenTypes.class, [
SemanticTokenModifiers.definition,
]);
this._addItemForNameNode(node.d.name, SemanticTokenTypes.class, [SemanticTokenModifiers.definition]);
return super.visitClass(node);
}

Expand All @@ -58,19 +56,17 @@ export class SemanticTokensWalker extends ParseTreeWalker {
}
//TODO: whats the correct type here
if ((node.a as any).declaration?.isMethod) {
this._addItem(node.d.name.start, node.d.name.length, SemanticTokenTypes.method, modifiers);
this._addItemForNameNode(node.d.name, SemanticTokenTypes.method, modifiers);
} else {
this._addItem(node.d.name.start, node.d.name.length, SemanticTokenTypes.function, modifiers);
this._addItemForNameNode(node.d.name, SemanticTokenTypes.function, modifiers);
}
// parameters & return type are covered by visitName
return super.visitFunction(node);
}

override visitParameter(node: ParameterNode): boolean {
if (node.d.name) {
this._addItem(node.d.name.start, node.d.name.length, SemanticTokenTypes.parameter, [
SemanticTokenModifiers.definition,
]);
this._addItemForNameNode(node.d.name, SemanticTokenTypes.parameter, [SemanticTokenModifiers.definition]);
}
return super.visitParameter(node);
}
Expand All @@ -82,17 +78,17 @@ export class SemanticTokensWalker extends ParseTreeWalker {
// as-is and their individual symbols are highlighted with their normal token type.
// see discussion in https://github.com/DetachHead/basedpyright/issues/278#issuecomment-2517502311
if (node.d.expr.nodeType === ParseNodeType.Name) {
this._addItem(node.d.expr.start, node.d.expr.length, SemanticTokenTypes.decorator, []);
this._addItemForNameNode(node.d.expr, SemanticTokenTypes.decorator, []);
}
return super.visitDecorator(node);
}

override visitImportAs(node: ImportAsNode): boolean {
for (const part of node.d.module.d.nameParts) {
this._addItem(part.start, part.length, SemanticTokenTypes.namespace, []);
this._addItemForNameNode(part, SemanticTokenTypes.namespace, []);
}
if (node.d.alias) {
this._addItem(node.d.alias.start, node.d.alias.length, SemanticTokenTypes.namespace, []);
this._addItemForNameNode(node.d.alias, SemanticTokenTypes.namespace, []);
}
return super.visitImportAs(node);
}
Expand All @@ -104,7 +100,7 @@ export class SemanticTokensWalker extends ParseTreeWalker {

override visitImportFrom(node: ImportFromNode): boolean {
for (const part of node.d.module.d.nameParts) {
this._addItem(part.start, part.length, SemanticTokenTypes.namespace, []);
this._addItemForNameNode(part, SemanticTokenTypes.namespace, []);
}
return super.visitImportFrom(node);
}
Expand Down Expand Up @@ -133,9 +129,7 @@ export class SemanticTokensWalker extends ParseTreeWalker {
method: 'set',
});
if (declaredType && isClass(declaredType) && declaredType.shared.flags & ClassTypeFlags.PropertyClass) {
this._addItem(node.d.member.start, node.d.member.length, SemanticTokenTypes.variable, [
SemanticTokenModifiers.readonly,
]);
this._addItemForNameNode(node.d.member, SemanticTokenTypes.variable, [SemanticTokenModifiers.readonly]);
}
}
return super.visitMemberAccess(node);
Expand All @@ -146,41 +140,41 @@ export class SemanticTokensWalker extends ParseTreeWalker {
case TypeCategory.Function:
if (type.flags & TypeFlags.Instance) {
if ((type as FunctionType).shared.declaration?.isMethod) {
this._addItem(node.start, node.length, SemanticTokenTypes.method, []);
this._addItemForNameNode(node, SemanticTokenTypes.method, []);
} else {
const modifiers = this.builtinModules.has(type.shared.moduleName)
? [SemanticTokenModifiers.defaultLibrary, CustomSemanticTokenModifiers.builtin]
: [];
this._addItem(node.start, node.length, SemanticTokenTypes.function, modifiers);
this._addItemForNameNode(node, SemanticTokenTypes.function, modifiers);
}
} else {
// type alias to Callable
this._addItem(node.start, node.length, SemanticTokenTypes.type, []);
this._addItemForNameNode(node, SemanticTokenTypes.type, []);
}
return;
case TypeCategory.Overloaded:
if (type.flags & TypeFlags.Instance) {
const details = OverloadedType.getOverloads(type)[0].shared;
if (details.declaration?.isMethod) {
this._addItem(node.start, node.length, SemanticTokenTypes.method, []);
this._addItemForNameNode(node, SemanticTokenTypes.method, []);
} else {
const modifiers = this.builtinModules.has(details.moduleName)
? [SemanticTokenModifiers.defaultLibrary, CustomSemanticTokenModifiers.builtin]
: [];
this._addItem(node.start, node.length, SemanticTokenTypes.function, modifiers);
this._addItemForNameNode(node, SemanticTokenTypes.function, modifiers);
}
} else {
// dunno if this is possible but better safe than sorry!!!
this._addItem(node.start, node.length, SemanticTokenTypes.type, []);
this._addItemForNameNode(node, SemanticTokenTypes.type, []);
}
return;

case TypeCategory.Module:
this._addItem(node.start, node.length, SemanticTokenTypes.namespace, []);
this._addItemForNameNode(node, SemanticTokenTypes.namespace, []);
return;
case TypeCategory.Any:
if (type.props?.specialForm) {
this._addItem(node.start, node.length, SemanticTokenTypes.type, []);
this._addItemForNameNode(node, SemanticTokenTypes.type, []);
return;
}
// eslint-disable-next-line no-fallthrough -- intentional fallthrough. these are handled below
Expand All @@ -192,7 +186,7 @@ export class SemanticTokensWalker extends ParseTreeWalker {
return;
case TypeCategory.Union:
if (!(type.flags & TypeFlags.Instance)) {
this._addItem(node.start, node.length, SemanticTokenTypes.type, []);
this._addItemForNameNode(node, SemanticTokenTypes.type, []);
return;
}
break;
Expand Down Expand Up @@ -220,7 +214,7 @@ export class SemanticTokensWalker extends ParseTreeWalker {
const modifiers = isBuiltIn
? [SemanticTokenModifiers.defaultLibrary, CustomSemanticTokenModifiers.builtin]
: [];
this._addItem(node.start, node.length, SemanticTokenTypes.class, modifiers);
this._addItemForNameNode(node, SemanticTokenTypes.class, modifiers);
return;
}
}
Expand All @@ -238,7 +232,7 @@ export class SemanticTokensWalker extends ParseTreeWalker {
(typeResult.type.category === TypeCategory.Never && !typeResult.includesVariableDecl) ||
(getTypeAliasInfo(type) && !typeResult.includesIllegalTypeAliasDecl)
) {
this._addItem(node.start, node.length, SemanticTokenTypes.type, []);
this._addItemForNameNode(node, SemanticTokenTypes.type, []);
return;
}
}
Expand All @@ -247,26 +241,29 @@ export class SemanticTokensWalker extends ParseTreeWalker {
const parent = declarations[0].node.parent as FunctionNode | LambdaNode;
// Avoid duplicates for parameters visited by `visitParameter`
if (!parent.d.params.some((param) => param.d.name?.id === node.id)) {
this._addItem(node.start, node.length, SemanticTokenTypes.parameter, []);
this._addItemForNameNode(node, SemanticTokenTypes.parameter, []);
}
} else if (type?.category === TypeCategory.TypeVar && !(type.flags & TypeFlags.Instance)) {
// `cls` method parameter is treated as a TypeVar in some special methods (methods
// with @classmethod decorator, `__new__`, `__init_subclass__`, etc.) so we need to
// check first if it's a parameter before checking that it's a TypeVar
this._addItem(node.start, node.length, SemanticTokenTypes.typeParameter, []);
this._addItemForNameNode(node, SemanticTokenTypes.typeParameter, []);
return;
} else if (
(type?.category === TypeCategory.Unknown || type?.category === TypeCategory.Any) &&
(declarations === undefined || declarations.length === 0 || declarations.every(isAliasDeclaration))
) {
return;
} else if (isConstantName(node.d.value) || (symbol && this._evaluator.isFinalVariable(symbol))) {
this._addItem(node.start, node.length, SemanticTokenTypes.variable, [SemanticTokenModifiers.readonly]);
this._addItemForNameNode(node, SemanticTokenTypes.variable, [SemanticTokenModifiers.readonly]);
} else {
this._addItem(node.start, node.length, SemanticTokenTypes.variable, []);
this._addItemForNameNode(node, SemanticTokenTypes.variable, []);
}
}

private _addItemForNameNode = (node: NameNode, type: string, modifiers: string[]) =>
this._addItem(node.d.token.start, node.d.token.length, type, modifiers);

private _addItem(start: number, length: number, type: string, modifiers: string[]) {
this.items.push({ type, modifiers, start, length });
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
a = 1
foo = 1
bar = (
foo
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { semanticTokenizeSampleFile } from './testUtils';
if (process.platform !== 'win32' || !process.env['CI']) {
test('variable', () => {
const result = semanticTokenizeSampleFile('variable.py');
expect(result).toStrictEqual([{ type: 'variable', length: 1, start: 0, modifiers: [] }]);
expect(result).toStrictEqual([
{ type: 'variable', start: 0, length: 3, modifiers: [] },
{ type: 'variable', start: 8, length: 3, modifiers: [] },
{ type: 'variable', start: 20, length: 3, modifiers: [] },
]);
});

test('type annotation', () => {
Expand Down
Loading