1
1
import { SyntaxNode } from "web-tree-sitter" ;
2
- import { ChunkWithoutID } from "../.." ;
3
- import { countTokens } from "../../llm/countTokens" ;
4
- import { getParserForFile } from "../../util/treeSitter" ;
2
+ import { ChunkWithoutID } from "../../index.js " ;
3
+ import { countTokens } from "../../llm/countTokens.js " ;
4
+ import { getParserForFile } from "../../util/treeSitter.js " ;
5
5
6
6
function collapsedReplacement ( node : SyntaxNode ) : string {
7
7
if ( node . type === "statement_block" ) {
8
8
return "{ ... }" ;
9
- } else {
10
- return "..." ;
11
9
}
10
+ return "..." ;
12
11
}
13
12
14
13
function firstChild (
@@ -19,9 +18,8 @@ function firstChild(
19
18
return (
20
19
node . children . find ( ( child ) => grammarName . includes ( child . type ) ) || null
21
20
) ;
22
- } else {
23
- return node . children . find ( ( child ) => child . type === grammarName ) || null ;
24
21
}
22
+ return node . children . find ( ( child ) => child . type === grammarName ) || null ;
25
23
}
26
24
27
25
function collapseChildren (
@@ -96,6 +94,14 @@ function collapseChildren(
96
94
return code ;
97
95
}
98
96
97
+ export const FUNCTION_BLOCK_NODE_TYPES = [ "block" , "statement_block" ] ;
98
+ export const FUNCTION_DECLARATION_NODE_TYPEs = [
99
+ "method_definition" ,
100
+ "function_definition" ,
101
+ "function_item" ,
102
+ "function_declaration" ,
103
+ ] ;
104
+
99
105
function constructClassDefinitionChunk (
100
106
node : SyntaxNode ,
101
107
code : string ,
@@ -105,8 +111,8 @@ function constructClassDefinitionChunk(
105
111
node ,
106
112
code ,
107
113
[ "block" , "class_body" , "declaration_list" ] ,
108
- [ "method_definition" , "function_definition" , "function_item" ] ,
109
- [ "block" , "statement_block" ] ,
114
+ FUNCTION_DECLARATION_NODE_TYPEs ,
115
+ FUNCTION_BLOCK_NODE_TYPES ,
110
116
maxChunkSize ,
111
117
) ;
112
118
}
@@ -130,12 +136,10 @@ function constructFunctionDefinitionChunk(
130
136
// If inside a class, include the class header
131
137
const classNode = node . parent . parent ;
132
138
const classBlock = node . parent ;
133
- return (
134
- code . slice ( classNode . startIndex , classBlock . startIndex ) +
135
- "...\n\n" +
136
- " " . repeat ( node . startPosition . column ) + // ...
137
- funcText
138
- ) ;
139
+ return `${ code . slice (
140
+ classNode . startIndex ,
141
+ classBlock . startIndex ,
142
+ ) } ...\n\n${ " " . repeat ( node . startPosition . column ) } ${ funcText } `;
139
143
}
140
144
return funcText ;
141
145
}
@@ -200,7 +204,7 @@ export async function* codeChunker(
200
204
return ;
201
205
}
202
206
203
- let parser = await getParserForFile ( filepath ) ;
207
+ const parser = await getParserForFile ( filepath ) ;
204
208
if ( parser === undefined ) {
205
209
console . warn ( `Failed to load parser for file ${ filepath } : ` ) ;
206
210
return ;
@@ -209,4 +213,4 @@ export async function* codeChunker(
209
213
const tree = parser . parse ( contents ) ;
210
214
211
215
yield * getSmartCollapsedChunks ( tree . rootNode , contents , maxChunkSize ) ;
212
- }
216
+ }
0 commit comments