11const TreeSitter = require ( 'tree-sitter' ) ;
22const Python = require ( 'tree-sitter-python' ) ;
3+ const TypeScript = require ( 'tree-sitter-typescript' ) ;
4+ const JavaScript = require ( 'tree-sitter-javascript' ) ;
35const fs = require ( 'fs' ) ;
46
57const args = JSON . parse ( process . argv [ 2 ] )
68
79// Load the Python parser
810const parser = new TreeSitter ( ) ;
911// Set the language to the parser
10- parser . setLanguage ( Python ) ;
12+ const ext = args . path . split ( '.' ) . pop ( ) ;
13+
14+ let language
15+
16+ if ( ext === 'py' ) {
17+ language = Python
18+ }
19+ else if ( ext === 'ts' ) {
20+ language = TypeScript . typescript
21+ }
22+ else if ( ext === 'tsx' ) {
23+ language = TypeScript . tsx
24+ }
25+ else if ( ext === 'js' || ext === 'jsx' ) {
26+ language = JavaScript
27+ }
28+
29+ parser . setLanguage ( language ) ;
30+
1131// Read the code file content
1232const codeContent = fs . readFileSync ( args . path , 'utf8' ) ;
1333// Parse the code using the chosen parser
1434const parsed = parser . parse ( codeContent ) ;
1535
1636const line_to_grab = args . line
1737
38+ if ( line_to_grab === undefined ) {
39+ console . log ( 'No line number provided' )
40+ process . exit ( 1 )
41+ }
42+
43+ if ( line_to_grab > codeContent . split ( '\n' ) . length ) {
44+ console . log ( 'Line number provided is greater than the number of lines in the file' )
45+ process . exit ( 1 )
46+ }
47+
1848// Look for node where node.startPosition.row and node.endPosition.row are equal to line_to_grab
1949const search_node = ( node ) => {
20- if ( node . startPosition . row === line_to_grab && node . endPosition . row === line_to_grab ) {
50+ if ( node . startPosition . row === line_to_grab - 1 && node . endPosition . row === line_to_grab - 1 ) {
2151 return node
2252 }
53+
2354 for ( const child of node . children ) {
2455 const result = search_node ( child )
2556 if ( result ) {
@@ -31,19 +62,52 @@ const search_node = (node) => {
3162
3263const line_node = search_node ( parsed . rootNode )
3364
34- const parent = line_node . parent
65+ if ( ! line_node ) {
66+ console . log ( 'No node found for line' , line_to_grab )
67+ console . log ( 'Code:' , codeContent . split ( '\n' ) . slice ( line_to_grab - 5 , line_to_grab + 5 ) . join ( '\n' ) )
68+ process . exit ( 1 )
69+ }
3570
36- if ( parent ) {
37- const start_line = parent . startPosition . row
38- const end_line = parent . endPosition . row
39- // Return codeContent from start_line to end_line
40- const lines = codeContent . split ( '\n' ) . slice ( start_line , end_line + 1 )
41- parent . content = lines . join ( '\n' )
71+ const parseParent = ( childNode ) => {
72+ const parent = childNode . parent
73+ if ( ! parent ) {
74+ return childNode
75+ }
76+ const MIN_CONTEXT_LENGTH = 700
77+ const MAX_CONTEXT_LENGTH = 1500
78+ if ( parent . text . length > MAX_CONTEXT_LENGTH ) {
79+ console . log ( 'Parent node text is too long, truncating' )
80+ const index = parent . text . indexOf ( line_node . text )
81+ const start = index - MAX_CONTEXT_LENGTH / 2
82+ const end = index + MAX_CONTEXT_LENGTH / 2
83+ parent . text = parent . text . slice ( start , end )
84+ return parent
85+ }
86+ if ( parent . text . length < MIN_CONTEXT_LENGTH ) {
87+ if ( parent . parent ) {
88+ return parseParent ( parent . parent )
89+ }
90+ else {
91+ return parent
92+ }
93+ }
94+ return parent
4295}
4396
97+ const ancestor = parseParent ( line_node )
98+
99+
100+ const siblings = line_node . parent ? line_node . parent . children : [ line_node ]
101+
102+ const offending_line = siblings . map ( sibling => {
103+ if ( sibling . startPosition . row === line_to_grab - 1 && sibling . endPosition . row === line_to_grab - 1 ) {
104+ return sibling . text
105+ }
106+ } ) . join ( '' )
107+
44108console . log ( {
45- offending_line : line_node . text ,
46- line_node : line_node ,
47- parent : parent ,
48- parent_text : parent . text
109+ offending_line,
110+ line_node,
111+ ancestor ,
112+ ancestor_text : ancestor . text
49113} )
0 commit comments