File tree Expand file tree Collapse file tree 2 files changed +24
-14
lines changed
Expand file tree Collapse file tree 2 files changed +24
-14
lines changed Original file line number Diff line number Diff line change @@ -4,14 +4,23 @@ import { initializeParser } from "../parser-loader/vite.ts";
44import { makeIterFunctions } from "./file-parsing.ts" ;
55export { getLanguage , fileTypes } from "./file-parsing.ts" ;
66
7- const parsers : { [ language in Language ] : Parser } = Object . fromEntries (
8- await ( async ( ) => {
9- const parsers = [ ] ;
10- for ( const language of supportedLanguages ) {
11- parsers . push ( [ language , await initializeParser ( language ) ] ) ;
12- }
13- return parsers ;
14- } ) ( ) ,
15- ) ;
7+ let parsers : { [ language in Language ] : Parser } | undefined ;
8+ export async function initParsers ( ) {
9+ if ( parsers ) return ;
10+ parsers = Object . fromEntries (
11+ await ( async ( ) => {
12+ const parsers = [ ] ;
13+ for ( const language of supportedLanguages ) {
14+ parsers . push ( [ language , await initializeParser ( language ) ] ) ;
15+ }
16+ return parsers ;
17+ } ) ( ) ,
18+ ) ;
19+ }
1620
17- export const iterFunctions = makeIterFunctions ( parsers ) ;
21+ export function iterFunctions ( code : string , language : Language ) : IterableIterator < Parser . SyntaxNode > {
22+ if ( ! parsers ) {
23+ throw new Error ( "Must initialize parsers by calling `initParsers()`" ) ;
24+ }
25+ return makeIterFunctions ( parsers ) ( code , language ) ;
26+ }
Original file line number Diff line number Diff line change 11<script lang =" ts" >
2- import { getLanguage , iterFunctions } from " ../../file-parsing/vite" ;
2+ import { getLanguage , initParsers , iterFunctions } from " ../../file-parsing/vite" ;
33 import type Parser from " web-tree-sitter" ;
44 import { type SyntaxNode } from " web-tree-sitter" ;
55 import { type Language , newCFGBuilder } from " ../../control-flow/cfg" ;
7171 * @param language Source code language
7272 * @param line Line number, 1-based.
7373 */
74- function getFunctionByLine(
74+ async function getFunctionByLine(
7575 code : string ,
7676 language : Language ,
7777 line : number ,
78- ): SyntaxNode | undefined {
78+ ): Promise <SyntaxNode | undefined > {
79+ await initParsers ();
7980 for (const func of iterFunctions (code , language )) {
8081 // GitHub lines are 1-based, TreeSitter rows are 0-based
8182 if (func .startPosition .row + 1 === line ) {
117118 // We assume that the raw URL always ends with the file extension
118119 const language = getLanguage (rawURL );
119120
120- const func = getFunctionByLine (code , language , line );
121+ const func = await getFunctionByLine (code , language , line );
121122 if (! func ) {
122123 throw new Error (` Unable to find function on line ${line } ` );
123124 }
You can’t perform that action at this time.
0 commit comments