From c2cb6f422a6fdaa2b34970b0dee76b659cbad1c1 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Mon, 6 May 2024 19:02:32 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20truncate=20function=20text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/indexing/chunk/code.ts | 12 +++- extensions/vscode/src/autocomplete/lsp.ts | 79 +++++++++++++++++++---- 2 files changed, 76 insertions(+), 15 deletions(-) diff --git a/core/indexing/chunk/code.ts b/core/indexing/chunk/code.ts index 558009de7..f76628132 100644 --- a/core/indexing/chunk/code.ts +++ b/core/indexing/chunk/code.ts @@ -96,6 +96,14 @@ function collapseChildren( return code; } +export const FUNCTION_BLOCK_NODE_TYPES = ["block", "statement_block"]; +export const FUNCTION_DECLARATION_NODE_TYPEs = [ + "method_definition", + "function_definition", + "function_item", + "function_declaration", +]; + function constructClassDefinitionChunk( node: SyntaxNode, code: string, @@ -105,8 +113,8 @@ function constructClassDefinitionChunk( node, code, ["block", "class_body", "declaration_list"], - ["method_definition", "function_definition", "function_item"], - ["block", "statement_block"], + FUNCTION_DECLARATION_NODE_TYPEs, + FUNCTION_BLOCK_NODE_TYPES, maxChunkSize, ); } diff --git a/extensions/vscode/src/autocomplete/lsp.ts b/extensions/vscode/src/autocomplete/lsp.ts index 796c0a023..b1c7f22cd 100644 --- a/extensions/vscode/src/autocomplete/lsp.ts +++ b/extensions/vscode/src/autocomplete/lsp.ts @@ -4,6 +4,10 @@ import { GetLspDefinitionsFunction } from "core/autocomplete/completionProvider" import { AutocompleteLanguageInfo } from "core/autocomplete/languages"; import { AutocompleteSnippet } from "core/autocomplete/ranking"; import { RangeInFileWithContents } from "core/commands/util"; +import { + FUNCTION_BLOCK_NODE_TYPES, + FUNCTION_DECLARATION_NODE_TYPEs, +} from "core/indexing/chunk/code"; import { intersection } from "core/util/ranges"; import * as vscode from "vscode"; import Parser from "web-tree-sitter"; @@ -48,9 +52,14 @@ function isRifWithContents( function findChildren( node: Parser.SyntaxNode, predicate: (n: Parser.SyntaxNode) => boolean, + firstN?: number, ): Parser.SyntaxNode[] { let matchingNodes: Parser.SyntaxNode[] = []; + if (firstN && firstN <= 0) { + return []; + } + // Check if the current node's type is in the list of types we're interested in if (predicate(node)) { matchingNodes.push(node); @@ -58,7 +67,13 @@ function findChildren( // Recursively search for matching types in all children of the current node for (const child of node.children) { - matchingNodes = matchingNodes.concat(findChildren(child, predicate)); + matchingNodes = matchingNodes.concat( + findChildren( + child, + predicate, + firstN ? firstN - matchingNodes.length : undefined, + ), + ); } return matchingNodes; @@ -78,9 +93,9 @@ function findTypeIdentifiers(node: Parser.SyntaxNode): Parser.SyntaxNode[] { async function crawlTypes( rif: RangeInFile | RangeInFileWithContents, ide: IDE, - depth: number, - results: RangeInFileWithContents[], - searchedLabels: Set, + depth: number = 1, + results: RangeInFileWithContents[] = [], + searchedLabels: Set = new Set(), ): Promise { // Get the file contents if not already attached const contents = isRifWithContents(rif) @@ -160,13 +175,57 @@ export async function getDefinitionsForNode( switch (node.type) { case "call_expression": // function call -> function definition - const funDefs = await executeGotoProvider( + const [funDef] = await executeGotoProvider( uri, node.startPosition.row, node.startPosition.column, "vscode.executeDefinitionProvider", ); - ranges.push(...funDefs); + if (!funDef) { + return []; + } + + // Don't display a function of more than 15 lines + // We can of course do something smarter here eventually + let funcText = await ide.readRangeInFile(funDef.filepath, funDef.range); + if (funcText.split("\n").length > 15) { + let truncated = false; + const funRootAst = await getAst(funDef.filepath, funcText); + if (funRootAst) { + const [funNode] = findChildren( + funRootAst?.rootNode, + (node) => FUNCTION_DECLARATION_NODE_TYPEs.includes(node.type), + 1, + ); + if (funNode) { + const [statementBlockNode] = findChildren( + funNode, + (node) => FUNCTION_BLOCK_NODE_TYPES.includes(node.type), + 1, + ); + if (statementBlockNode) { + funcText = funRootAst.rootNode.text + .slice(0, statementBlockNode.startIndex) + .trim(); + truncated = true; + } + } + } + if (!truncated) { + funcText = funcText.split("\n")[0]; + } + } + + ranges.push(funDef); + + const typeDefs = await crawlTypes( + { + ...funDef, + contents: funcText, + }, + ide, + ); + ranges.push(...typeDefs); break; case "variable_declarator": // variable assignment -> variable definition/type @@ -201,13 +260,7 @@ export async function getDefinitionsForNode( }${contents.trim()}`, }); - const definitions = await crawlTypes( - { ...classDef, contents }, - ide, - 1, - [], - new Set(), - ); + const definitions = await crawlTypes({ ...classDef, contents }, ide); ranges.push(...definitions.filter(Boolean)); break;