Skip to content

Commit

Permalink
fix(parsers): sync upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fannheyward committed Jul 17, 2024
1 parent d4cfda2 commit 4280e33
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 60 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@types/minimatch": "^5.1.2",
"@types/node": "16",
"@types/which": "^3.0.4",
"@zzzen/pyright-internal": "^1.2.0-dev.20240707",
"@zzzen/pyright-internal": "^1.2.0-dev.20240714",
"coc.nvim": "^0.0.83-next.18",
"diff-match-patch": "^1.0.5",
"esbuild": "^0.23.0",
Expand Down
20 changes: 10 additions & 10 deletions src/parsers/inlayHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function isLeftSideOfAssignment(node: ParseNode): boolean {
if (node.parent?.nodeType !== ParseNodeType.Assignment) {
return false;
}
return node.start < (node.parent as AssignmentNode).rightExpression.start;
return node.start < (node.parent as AssignmentNode).d.rightExpr.start;
}

export class TypeInlayHintsWalker extends ParseTreeWalker {
Expand All @@ -38,7 +38,7 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
inlayHintType: 'variable',
startOffset: node.start,
endOffset: node.start + node.length - 1,
value: node.value,
value: node.d.value,
});
}
return super.visitName(node);
Expand All @@ -48,9 +48,9 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
if (isLeftSideOfAssignment(node)) {
this.featureItems.push({
inlayHintType: 'variable',
startOffset: node.memberName.start,
endOffset: node.memberName.start + node.memberName.length - 1,
value: node.memberName.value,
startOffset: node.d.member.start,
endOffset: node.d.member.start + node.d.member.length - 1,
value: node.d.member.d.value,
});
}
return super.visitMemberAccess(node);
Expand All @@ -62,7 +62,7 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
return false;
}
const result = getCallNodeAndActiveParameterIndex(node, node.start, this._parseResults.tokenizerOutput.tokens);
if (!result?.callNode || result.callNode.arguments[result.activeIndex].name) {
if (!result?.callNode || result.callNode.d.args[result.activeIndex].d.name) {
return false;
}
this.featureItems.push({
Expand All @@ -77,12 +77,12 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
override visitFunction(node: FunctionNode): boolean {
// If the code describes a type, do not add the item.
// Add item only if "node.returnTypeAnnotation" does not exist.
if (!node.returnTypeAnnotation) {
if (!node.d.returnAnnotation) {
this.featureItems.push({
inlayHintType: 'functionReturn',
startOffset: node.name.start,
endOffset: node.suite.start,
value: node.name.value,
startOffset: node.d.name.start,
endOffset: node.d.suite.start,
value: node.d.name.d.value,
});
}
return super.visitFunction(node);
Expand Down
92 changes: 50 additions & 42 deletions src/parsers/semanticTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type SemanticTokenItem = {
export class SemanticTokensWalker extends ParseTreeWalker {
public semanticItems: SemanticTokenItem[] = [];

private addItem(node: ParseNodeBase, type: string, modifiers: string[] = []) {
private addItem(node: ParseNodeBase<ParseNodeType>, type: string, modifiers: string[] = []) {
const item: SemanticTokenItem = { type, start: node.start, length: node.length, modifiers };
if (this.semanticItems.some((x) => x.type === item.type && x.start === item.start && x.length === item.length)) {
return;
Expand All @@ -48,24 +48,24 @@ export class SemanticTokensWalker extends ParseTreeWalker {

visitFor(node: ForNode): boolean {
if (node.nodeType === ParseNodeType.For) {
this.addItem(node.targetExpression, SemanticTokenTypes.variable);
this.addItem(node.d.targetExpr, SemanticTokenTypes.variable);
}
return super.visitFor(node);
}

visitFormatString(node: FormatStringNode): boolean {
node.fieldExpressions.map((f) => this.addItem(f, SemanticTokenTypes.variable));
node.d.fieldExprs.map((f) => this.addItem(f, SemanticTokenTypes.variable));
return super.visitFormatString(node);
}

visitCall(node: CallNode): boolean {
// TODO: hard-code, treat first-letter UpperCase as class
if (node.leftExpression.nodeType === 38) {
const value = node.leftExpression.value;
if (node.d.leftExpr.nodeType === 38) {
const value = node.d.leftExpr.d.value;
if (value[0] === value[0].toUpperCase()) {
this.addItem(node.leftExpression, SemanticTokenTypes.class);
this.addItem(node.d.leftExpr, SemanticTokenTypes.class);
} else {
this.addItem(node.leftExpression, SemanticTokenTypes.function);
this.addItem(node.d.leftExpr, SemanticTokenTypes.function);
}
}
return super.visitCall(node);
Expand All @@ -74,45 +74,45 @@ export class SemanticTokensWalker extends ParseTreeWalker {
visitClass(node: ClassNode): boolean {
// @ts-ignore
if (node.arguments.length === 1 && node.arguments[0].valueExpression.value === 'Enum') {
this.addItem(node.name, SemanticTokenTypes.enum);
this.addItem(node.d.name, SemanticTokenTypes.enum);

for (const m of node.suite.statements) {
for (const m of node.d.suite.d.statements) {
// @ts-ignore
this.addItem(m.statements[0].leftExpression, SemanticTokenTypes.enumMember);
}
return super.visitClass(node);
}

this.addItem(node.name, SemanticTokenTypes.class, [SemanticTokenModifiers.definition]);
this.addItem(node.d.name, SemanticTokenTypes.class, [SemanticTokenModifiers.definition]);
return super.visitClass(node);
}

visitMemberAccess(node: MemberAccessNode): boolean {
if (node.parent?.nodeType === ParseNodeType.Call) {
this.addItem(node.memberName, SemanticTokenTypes.function);
this.addItem(node.d.member, SemanticTokenTypes.function);
return super.visitMemberAccess(node);
}

this.addItem(node.memberName, SemanticTokenTypes.property);
this.addItem(node.d.member, SemanticTokenTypes.property);
return super.visitMemberAccess(node);
}

visitDecorator(node: DecoratorNode): boolean {
this.addItem(node.expression, SemanticTokenTypes.decorator);
this.addItem(node.d.expr, SemanticTokenTypes.decorator);
let nameNode: NameNode | undefined;
switch (node.expression.nodeType) {
switch (node.d.expr.nodeType) {
case ParseNodeType.Call:
if (node.expression.leftExpression.nodeType === ParseNodeType.MemberAccess) {
nameNode = node.expression.leftExpression.memberName;
} else if (node.expression.leftExpression.nodeType === ParseNodeType.Name) {
nameNode = node.expression.leftExpression;
if (node.d.expr.d.leftExpr.nodeType === ParseNodeType.MemberAccess) {
nameNode = node.d.expr.d.leftExpr.d.member;
} else if (node.d.expr.d.leftExpr.nodeType === ParseNodeType.Name) {
nameNode = node.d.expr.d.leftExpr;
}
break;
case ParseNodeType.MemberAccess:
nameNode = node.expression.memberName;
nameNode = node.d.expr.d.member;
break;
case ParseNodeType.Name:
nameNode = node.expression;
nameNode = node.d.expr;
break;
}
if (nameNode) {
Expand All @@ -122,68 +122,76 @@ export class SemanticTokensWalker extends ParseTreeWalker {
}

visitImport(node: ImportNode): boolean {
node.list.map((x) => this.addItem(x, SemanticTokenTypes.namespace));
for (const x of node.d.list) {
if (x.d.alias) {
this.addItem(x.d.alias, SemanticTokenTypes.namespace);
}
}
return super.visitImport(node);
}

visitImportAs(node: ImportAsNode): boolean {
if (node.alias?.value.length) {
this.addItem(node.alias, SemanticTokenTypes.namespace);
if (node.d.alias?.d.value.length) {
this.addItem(node.d.alias, SemanticTokenTypes.namespace);
}
node.module.nameParts.map((x) => this.addItem(x, SemanticTokenTypes.namespace));
node.d.module.d.nameParts.map((x) => this.addItem(x, SemanticTokenTypes.namespace));
return super.visitImportAs(node);
}

visitImportFrom(node: ImportFromNode): boolean {
node.module.nameParts.map((x) => this.addItem(x, SemanticTokenTypes.namespace));
node.imports.map((x) => this.addItem(x, SemanticTokenTypes.namespace));
node.d.module.d.nameParts.map((x) => this.addItem(x, SemanticTokenTypes.namespace));
for (const x of node.d.imports) {
if (x.d.alias) {
this.addItem(x.d.alias, SemanticTokenTypes.namespace);
}
}

return super.visitImportFrom(node);
}

visitImportFromAs(node: ImportFromAsNode): boolean {
if (node.alias?.value.length) {
this.addItem(node.alias, SemanticTokenTypes.namespace);
if (node.d.alias?.d.value.length) {
this.addItem(node.d.alias, SemanticTokenTypes.namespace);
}
return super.visitImportFromAs(node);
}

visitParameter(node: ParameterNode): boolean {
if (!node.name) return super.visitParameter(node);
if (!node.d.name) return super.visitParameter(node);

this.addItem(node.name, SemanticTokenTypes.parameter);
if (node.typeAnnotation) {
this.addItem(node.typeAnnotation, SemanticTokenTypes.typeParameter);
this.addItem(node.d.name, SemanticTokenTypes.parameter);
if (node.d.annotation) {
this.addItem(node.d.annotation, SemanticTokenTypes.typeParameter);
}
return super.visitParameter(node);
}

visitTypeParameter(node: TypeParameterNode): boolean {
this.addItem(node.name, SemanticTokenTypes.typeParameter);
this.addItem(node.d.name, SemanticTokenTypes.typeParameter);
return super.visitTypeParameter(node);
}

visitTypeAnnotation(node: TypeAnnotationNode): boolean {
if (node.typeAnnotation) {
this.addItem(node.typeAnnotation, SemanticTokenTypes.typeParameter);
if (node.d.annotation) {
this.addItem(node.d.annotation, SemanticTokenTypes.typeParameter);
}
return super.visitTypeAnnotation(node);
}

visitFunction(node: FunctionNode): boolean {
const modifiers = [SemanticTokenModifiers.definition];
if (node.isAsync) {
if (node.d.isAsync) {
modifiers.push(SemanticTokenModifiers.async);
}
const type = node.parent?.parent?.nodeType === 10 ? SemanticTokenTypes.method : SemanticTokenTypes.function;
this.addItem(node.name, type, modifiers);
this.addItem(node.d.name, type, modifiers);

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

this.addItem(p.name, SemanticTokenTypes.parameter);
if (p.typeAnnotation) {
this.addItem(p.typeAnnotation, SemanticTokenTypes.typeParameter);
this.addItem(p.d.name, SemanticTokenTypes.parameter);
if (p.d.annotation) {
this.addItem(p.d.annotation, SemanticTokenTypes.typeParameter);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/parsers/testFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class TestFrameworkWalker extends ParseTreeWalker {
}

override visitFunction(node: FunctionNode): boolean {
if (node.name.value.startsWith('test_')) {
if (node.d.name.d.value.startsWith('test_')) {
if (node.parent && printParseNodeType(node.parent.nodeType) === 'Suite') {
const parentSuiteNode = node.parent as SuiteNode;
if (parentSuiteNode.parent && printParseNodeType(parentSuiteNode.parent.nodeType) === 'Class') {
Expand All @@ -33,15 +33,15 @@ export class TestFrameworkWalker extends ParseTreeWalker {
}

this.featureItems.push({
value: classNode.name.value + combineString + node.name.value,
value: classNode.d.name.d.value + combineString + node.d.name.d.value,
startOffset: node.start,
endOffset: node.start + node.length - 1,
});
}
} else {
if (this.testFramework === 'pytest') {
this.featureItems.push({
value: node.name.value,
value: node.d.name.d.value,
startOffset: node.start,
endOffset: node.start + node.length - 1,
});
Expand Down

0 comments on commit 4280e33

Please sign in to comment.