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