From 4e10e1ec3d0ce6dae6592e7fae5bfaf7edcb56fd Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:58:07 -0400 Subject: [PATCH] Refactor tree-sitter --- README.md | 2 +- src/ClaudeDev.ts | 2 +- src/{parse-source-code => core/tree-sitter}/index.ts | 2 +- src/{parse-source-code => core/tree-sitter}/languageParser.ts | 0 src/{parse-source-code => core/tree-sitter}/queries/c-sharp.ts | 0 src/{parse-source-code => core/tree-sitter}/queries/c.ts | 0 src/{parse-source-code => core/tree-sitter}/queries/cpp.ts | 0 src/{parse-source-code => core/tree-sitter}/queries/go.ts | 0 src/{parse-source-code => core/tree-sitter}/queries/index.ts | 0 src/{parse-source-code => core/tree-sitter}/queries/java.ts | 0 .../tree-sitter}/queries/javascript.ts | 0 src/{parse-source-code => core/tree-sitter}/queries/php.ts | 0 src/{parse-source-code => core/tree-sitter}/queries/python.ts | 0 src/{parse-source-code => core/tree-sitter}/queries/ruby.ts | 0 src/{parse-source-code => core/tree-sitter}/queries/rust.ts | 0 src/{parse-source-code => core/tree-sitter}/queries/swift.ts | 0 .../tree-sitter}/queries/typescript.ts | 0 src/integrations/WorkspaceTracker.ts | 2 +- 18 files changed, 4 insertions(+), 4 deletions(-) rename src/{parse-source-code => core/tree-sitter}/index.ts (99%) rename src/{parse-source-code => core/tree-sitter}/languageParser.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/c-sharp.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/c.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/cpp.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/go.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/index.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/java.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/javascript.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/php.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/python.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/ruby.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/rust.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/swift.ts (100%) rename src/{parse-source-code => core/tree-sitter}/queries/typescript.ts (100%) diff --git a/README.md b/README.md index 42a99b9..0ec3a7f 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ When given a task in an existing project, Claude will look for the most relevant 1. **File Structure**: When a task is started, Claude is given an overview of your project's file structure. It turns out Claude 3.5 Sonnet is _really_ good at inferring what it needs to process further just from these file names alone. -2. **Source Code Definitions**: Claude may then use the `list_code_definition_names` tool on specific directories of interest. This tool uses [tree-sitter](https://github.com/tree-sitter/tree-sitter) to parse source code with custom tag queries that extract names of classes, functions, methods, and other definitions. It works by first identifying source code files that tree-sitter can parse (currently supports `python`, `javascript`, `typescript`, `ruby`, `go`, `java`, `php`, `rust`, `c`, `c++`, `c#`, `swift`), then parsing each file into an abstract syntax tree, and finally applying a language-specific query to extract definition names (you can see the exact query used for each language in `src/parse-source-code/queries`). The results are formatted into a concise & readable output that Claude can easily interpret to quickly understand the code's structure and purpose. +2. **Source Code Definitions**: Claude may then use the `list_code_definition_names` tool on specific directories of interest. This tool uses [tree-sitter](https://github.com/tree-sitter/tree-sitter) to parse source code with custom tag queries that extract names of classes, functions, methods, and other definitions. It works by first identifying source code files that tree-sitter can parse (currently supports `python`, `javascript`, `typescript`, `ruby`, `go`, `java`, `php`, `rust`, `c`, `c++`, `c#`, `swift`), then parsing each file into an abstract syntax tree, and finally applying a language-specific query to extract definition names. The results are formatted into a concise & readable output that Claude can easily interpret to quickly understand the code's structure and purpose. 3. **Search Files**: Claude can also use the `search_files` tool to search for specific patterns or content across multiple files. This tool uses [ripgrep](https://github.com/BurntSushi/ripgrep) to perform regex searches on files in a specified directory. The results are formatted into a concise & readable output that Claude can easily interpret to quickly understand the code's structure and purpose. This can be useful for tasks like refactoring function names, updating imports, addressing TODOs and FIXMEs, etc. diff --git a/src/ClaudeDev.ts b/src/ClaudeDev.ts index 8ed7887..dd18fc8 100644 --- a/src/ClaudeDev.ts +++ b/src/ClaudeDev.ts @@ -11,7 +11,7 @@ import { serializeError } from "serialize-error" import * as vscode from "vscode" import { ApiHandler, buildApiHandler } from "./api" import { TerminalManager } from "./integrations/TerminalManager" -import { listFiles, parseSourceCodeForDefinitionsTopLevel } from "./parse-source-code" +import { listFiles, parseSourceCodeForDefinitionsTopLevel } from "./core/tree-sitter" import { ClaudeDevProvider } from "./providers/ClaudeDevProvider" import { ApiConfiguration } from "./shared/api" import { ClaudeRequestResult } from "./shared/ClaudeRequestResult" diff --git a/src/parse-source-code/index.ts b/src/core/tree-sitter/index.ts similarity index 99% rename from src/parse-source-code/index.ts rename to src/core/tree-sitter/index.ts index 31f2988..036746a 100644 --- a/src/parse-source-code/index.ts +++ b/src/core/tree-sitter/index.ts @@ -3,7 +3,7 @@ import { globby, Options } from "globby" import os from "os" import * as path from "path" import { LanguageParser, loadRequiredLanguageParsers } from "./languageParser" -import { arePathsEqual } from "../utils/path-helpers" +import { arePathsEqual } from "../../utils/path-helpers" // TODO: implement caching behavior to avoid having to keep analyzing project for new tasks. export async function parseSourceCodeForDefinitionsTopLevel(dirPath: string): Promise { diff --git a/src/parse-source-code/languageParser.ts b/src/core/tree-sitter/languageParser.ts similarity index 100% rename from src/parse-source-code/languageParser.ts rename to src/core/tree-sitter/languageParser.ts diff --git a/src/parse-source-code/queries/c-sharp.ts b/src/core/tree-sitter/queries/c-sharp.ts similarity index 100% rename from src/parse-source-code/queries/c-sharp.ts rename to src/core/tree-sitter/queries/c-sharp.ts diff --git a/src/parse-source-code/queries/c.ts b/src/core/tree-sitter/queries/c.ts similarity index 100% rename from src/parse-source-code/queries/c.ts rename to src/core/tree-sitter/queries/c.ts diff --git a/src/parse-source-code/queries/cpp.ts b/src/core/tree-sitter/queries/cpp.ts similarity index 100% rename from src/parse-source-code/queries/cpp.ts rename to src/core/tree-sitter/queries/cpp.ts diff --git a/src/parse-source-code/queries/go.ts b/src/core/tree-sitter/queries/go.ts similarity index 100% rename from src/parse-source-code/queries/go.ts rename to src/core/tree-sitter/queries/go.ts diff --git a/src/parse-source-code/queries/index.ts b/src/core/tree-sitter/queries/index.ts similarity index 100% rename from src/parse-source-code/queries/index.ts rename to src/core/tree-sitter/queries/index.ts diff --git a/src/parse-source-code/queries/java.ts b/src/core/tree-sitter/queries/java.ts similarity index 100% rename from src/parse-source-code/queries/java.ts rename to src/core/tree-sitter/queries/java.ts diff --git a/src/parse-source-code/queries/javascript.ts b/src/core/tree-sitter/queries/javascript.ts similarity index 100% rename from src/parse-source-code/queries/javascript.ts rename to src/core/tree-sitter/queries/javascript.ts diff --git a/src/parse-source-code/queries/php.ts b/src/core/tree-sitter/queries/php.ts similarity index 100% rename from src/parse-source-code/queries/php.ts rename to src/core/tree-sitter/queries/php.ts diff --git a/src/parse-source-code/queries/python.ts b/src/core/tree-sitter/queries/python.ts similarity index 100% rename from src/parse-source-code/queries/python.ts rename to src/core/tree-sitter/queries/python.ts diff --git a/src/parse-source-code/queries/ruby.ts b/src/core/tree-sitter/queries/ruby.ts similarity index 100% rename from src/parse-source-code/queries/ruby.ts rename to src/core/tree-sitter/queries/ruby.ts diff --git a/src/parse-source-code/queries/rust.ts b/src/core/tree-sitter/queries/rust.ts similarity index 100% rename from src/parse-source-code/queries/rust.ts rename to src/core/tree-sitter/queries/rust.ts diff --git a/src/parse-source-code/queries/swift.ts b/src/core/tree-sitter/queries/swift.ts similarity index 100% rename from src/parse-source-code/queries/swift.ts rename to src/core/tree-sitter/queries/swift.ts diff --git a/src/parse-source-code/queries/typescript.ts b/src/core/tree-sitter/queries/typescript.ts similarity index 100% rename from src/parse-source-code/queries/typescript.ts rename to src/core/tree-sitter/queries/typescript.ts diff --git a/src/integrations/WorkspaceTracker.ts b/src/integrations/WorkspaceTracker.ts index 01e798b..3e82f0c 100644 --- a/src/integrations/WorkspaceTracker.ts +++ b/src/integrations/WorkspaceTracker.ts @@ -1,6 +1,6 @@ import * as vscode from "vscode" import * as path from "path" -import { listFiles } from "../parse-source-code/index" +import { listFiles } from "../core/tree-sitter/index" import { ClaudeDevProvider } from "../providers/ClaudeDevProvider" const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0)