Skip to content
Open
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
43 changes: 40 additions & 3 deletions src/context/language/python-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,48 @@ export class PythonParser implements AbstractParser {
lineStart: number,
lineEnd: number
): EnclosingContext {
// TODO: Implement this method for Python
const tree = parser.parse(file);

let largestEnclosingContext: SyntaxNode | null = null;
let largestSize = 0;

// Traverse the syntax tree
const cursor = tree.walk();
do {
const node = cursor.currentNode;
if (
node.type === "function_definition" ||
node.type === "class_definition"
) {
({ largestSize, largestEnclosingContext } = processNode(
node,
lineStart,
lineEnd,
largestSize,
largestEnclosingContext
));
}
} while (cursor.gotoNextSibling() || cursor.gotoParent());

return {
enclosingContext: largestEnclosingContext,
} as EnclosingContext;
}

return null;
}
dryRun(file: string): { valid: boolean; error: string } {
// TODO: Implement this method for Python
return { valid: false, error: "Not implemented yet" };
try {
const tree = parser.parse(file);
return {
valid: true,
error: "",
};
} catch (exc) {
return {
valid: false,
error: exc,
};
}
}
}