From 07f975e820f46f4b10a85fede2b91e3f362cd22d Mon Sep 17 00:00:00 2001 From: yaozay <131705711+yaozay@users.noreply.github.com> Date: Sun, 24 Nov 2024 14:24:37 -0600 Subject: [PATCH] Update python-parser.ts --- src/context/language/python-parser.ts | 43 +++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/context/language/python-parser.ts b/src/context/language/python-parser.ts index 845e90b..93a74c0 100644 --- a/src/context/language/python-parser.ts +++ b/src/context/language/python-parser.ts @@ -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, + }; + } } }