Elffuss BraidHub

kikocis/continue public

main · 3.3 KiBRawPermalink
1import { Chunk, IDE } from "core";
2import { languageForFilepath } from "core/autocomplete/constants/AutocompleteLanguageInfo";
3import { DEFAULT_IGNORE_DIRS } from "core/indexing/ignore";
4import { deduplicateArray } from "core/util";
5import { getParserForFile } from "core/util/treeSitter";
6import * as vscode from "vscode";
7
8import { getDefinitionsForNode } from "../autocomplete/lsp";
9
10import type { SyntaxNode } from "web-tree-sitter";
11
12export async function expandSnippet(
13 fileUri: string,
14 startLine: number,
15 endLine: number,
16 ide: IDE,
17): Promise<Chunk[]> {
18 const parser = await getParserForFile(fileUri);
19 if (!parser) {
20 return [];
21 }
22
23 const fullFileContents = await ide.readFile(fileUri);
24 const root: SyntaxNode = parser.parse(fullFileContents).rootNode;
25
26 // Find all nodes contained in the range
27 const containedInRange: SyntaxNode[] = [];
28 const toExplore: SyntaxNode[] = [root];
29 while (toExplore.length > 0) {
30 const node = toExplore.pop()!;
31 for (const child of node.namedChildren) {
32 if (
33 child.startPosition.row >= startLine &&
34 child.endPosition.row <= endLine
35 ) {
36 // Fully contained in range
37 containedInRange.push(child);
38 toExplore.push(child);
39 } else if (
40 child.startPosition.row >= startLine ||
41 child.endPosition.row <= endLine
42 ) {
43 // Overlaps, children may be contained in range
44 toExplore.push(child);
45 }
46 }
47 }
48
49 // Find all call expressions
50 const callExpressions = containedInRange.filter(
51 (node) => node.type === "call_expression",
52 );
53 let callExpressionDefinitions = (
54 await Promise.all(
55 callExpressions.map(async (node) => {
56 return getDefinitionsForNode(
57 vscode.Uri.parse(fileUri),
58 node,
59 ide,
60 languageForFilepath(fileUri),
61 );
62 }),
63 )
64 ).flat();
65
66 // De-duplicate the definitions
67 callExpressionDefinitions = deduplicateArray(
68 callExpressionDefinitions,
69 (a, b) => {
70 return (
71 a.filepath === b.filepath &&
72 a.range.start.line === b.range.start.line &&
73 a.range.end.line === b.range.end.line &&
74 a.range.start.character === b.range.start.character &&
75 a.range.end.character === b.range.end.character
76 );
77 },
78 );
79
80 // Filter out definitions already in selected range
81 callExpressionDefinitions = callExpressionDefinitions.filter((def) => {
82 return !(
83 def.filepath === fileUri &&
84 def.range.start.line >= startLine &&
85 def.range.end.line <= endLine
86 );
87 });
88
89 // Filter out defintions not under workspace directories
90 const workspaceDirectories = await ide.getWorkspaceDirs();
91 callExpressionDefinitions = callExpressionDefinitions.filter((def) => {
92 return (
93 workspaceDirectories.some((dir) => def.filepath.startsWith(dir)) &&
94 !DEFAULT_IGNORE_DIRS.some(
95 (dir) =>
96 def.filepath.includes(`/${dir}/`) ||
97 def.filepath.includes(`\\${dir}\\`),
98 )
99 );
100 });
101
102 const chunks = await Promise.all(
103 callExpressionDefinitions.map(async (def) => {
104 return {
105 filepath: def.filepath,
106 startLine: def.range.start.line,
107 endLine: def.range.end.line,
108 digest: "",
109 index: 0,
110 content: await ide.readRangeInFile(def.filepath, def.range),
111 };
112 }),
113 );
114 return chunks;
115}