From ddfcefa036553cf8370847e3b3a1da3874fc95f9 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 10 Dec 2024 20:34:21 -0800 Subject: [PATCH] path -> ide part 243243 --- core/indexing/chunk/chunk.test.ts | 8 ++++---- core/indexing/chunk/chunk.ts | 16 +++++++--------- .../vscode/src/autocomplete/recentlyEdited.ts | 7 ++----- extensions/vscode/src/commands.ts | 5 ++--- .../vscode/src/extension/VsCodeMessenger.ts | 5 ++--- .../src/quickEdit/QuickEditQuickPick.ts | 7 +++---- .../src/test/test-suites/ideUtils.test.ts | 19 ++++++++----------- extensions/vscode/src/util/util.ts | 10 ---------- 8 files changed, 28 insertions(+), 49 deletions(-) diff --git a/core/indexing/chunk/chunk.test.ts b/core/indexing/chunk/chunk.test.ts index 85683a51e..7a8ce6f76 100644 --- a/core/indexing/chunk/chunk.test.ts +++ b/core/indexing/chunk/chunk.test.ts @@ -6,25 +6,25 @@ describe("shouldChunk", () => { test("should chunk a typescript file", () => { const filePath = path.join("directory", "file.ts"); const fileContent = generateString(10000); - expect(shouldChunk(path.sep, filePath, fileContent)).toBe(true); + expect(shouldChunk(filePath, fileContent)).toBe(true); }); test("should not chunk a large typescript file", () => { const filePath = path.join("directory", "file.ts"); const fileContent = generateString(1500000); - expect(shouldChunk(path.sep, filePath, fileContent)).toBe(false); + expect(shouldChunk(filePath, fileContent)).toBe(false); }); test("should not chunk an empty file", () => { const filePath = path.join("directory", "file.ts"); const fileContent = generateString(0); - expect(shouldChunk(path.sep, filePath, fileContent)).toBe(false); + expect(shouldChunk(filePath, fileContent)).toBe(false); }); test("should not chunk a file without extension", () => { const filePath = path.join("directory", "with.dot", "filename"); const fileContent = generateString(10000); - expect(shouldChunk(path.sep, filePath, fileContent)).toBe(false); + expect(shouldChunk(filePath, fileContent)).toBe(false); }); }); diff --git a/core/indexing/chunk/chunk.ts b/core/indexing/chunk/chunk.ts index 142f75b28..20a2a19b0 100644 --- a/core/indexing/chunk/chunk.ts +++ b/core/indexing/chunk/chunk.ts @@ -3,7 +3,7 @@ import { countTokensAsync } from "../../llm/countTokens.js"; import { extractMinimalStackTraceInfo } from "../../util/extractMinimalStackTraceInfo.js"; import { Telemetry } from "../../util/posthog.js"; import { supportedLanguages } from "../../util/treeSitter.js"; -import { getUriPathBasename } from "../../util/uri.js"; +import { getUriFileExtension, getUriPathBasename } from "../../util/uri.js"; import { basicChunker } from "./basic.js"; import { codeChunker } from "./code.js"; @@ -16,25 +16,23 @@ export type ChunkDocumentParam = { }; async function* chunkDocumentWithoutId( - filepath: string, + fileUri: string, contents: string, maxChunkSize: number, ): AsyncGenerator { if (contents.trim() === "") { return; } - - const segs = filepath.split("."); - const ext = segs[segs.length - 1]; - if (ext in supportedLanguages) { + const extension = getUriFileExtension(fileUri); + if (extension in supportedLanguages) { try { - for await (const chunk of codeChunker(filepath, contents, maxChunkSize)) { + for await (const chunk of codeChunker(fileUri, contents, maxChunkSize)) { yield chunk; } return; } catch (e: any) { - Telemetry.capture("code_chunker_error", { - fileExtension: ext, + void Telemetry.capture("code_chunker_error", { + fileExtension: extension, stack: extractMinimalStackTraceInfo(e.stack), }); // falls back to basicChunker diff --git a/extensions/vscode/src/autocomplete/recentlyEdited.ts b/extensions/vscode/src/autocomplete/recentlyEdited.ts index 221d7df66..6ab614c90 100644 --- a/extensions/vscode/src/autocomplete/recentlyEdited.ts +++ b/extensions/vscode/src/autocomplete/recentlyEdited.ts @@ -23,9 +23,6 @@ export class RecentlyEditedTracker { constructor() { vscode.workspace.onDidChangeTextDocument((event) => { - if (event.document.uri.scheme !== "file") { - return; - } event.contentChanges.forEach((change) => { const editedRange = { uri: event.document.uri, @@ -123,7 +120,7 @@ export class RecentlyEditedTracker { return this.recentlyEditedRanges.map((entry) => { return { ...entry, - filepath: entry.uri.fsPath, + filepath: entry.uri.toString(), }; }); } @@ -140,7 +137,7 @@ export class RecentlyEditedTracker { const lines = contents.split("\n"); return { - filepath: entry.uri.fsPath, + filepath: entry.uri.toString(), contents, range: { start: { line: 0, character: 0 }, diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index 3706cf79a..bdc63ddec 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -32,7 +32,6 @@ import { VerticalDiffManager } from "./diff/vertical/manager"; import EditDecorationManager from "./quickEdit/EditDecorationManager"; import { QuickEdit, QuickEditShowParams } from "./quickEdit/QuickEditQuickPick"; import { Battery } from "./util/battery"; -import { getFullyQualifiedPath } from "./util/util"; import { VsCodeIde } from "./VsCodeIde"; import type { VsCodeWebviewProtocol } from "./webviewProtocol"; @@ -231,7 +230,7 @@ async function processDiff( diffManager: DiffManager, ide: VsCodeIde, verticalDiffManager: VerticalDiffManager, - newFilepath?: string | vscode.Uri, + newFilepath?: vscode.Uri, streamId?: string, ) { captureCommandTelemetry(`${action}Diff`); @@ -241,7 +240,7 @@ async function processDiff( if (fullPath instanceof vscode.Uri) { fullPath = fullPath.fsPath; } else if (fullPath) { - fullPath = getFullyQualifiedPath(ide, fullPath); + fullPath = resolveF(ide, fullPath); } else { const curFile = await ide.getCurrentFile(); fullPath = curFile?.path; diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index 3f201f016..244777fa8 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -16,7 +16,6 @@ import { CORE_TO_WEBVIEW_PASS_THROUGH, WEBVIEW_TO_CORE_PASS_THROUGH, } from "core/protocol/passThrough"; -import { getBasename } from "core/util"; import { InProcessMessenger, Message } from "core/protocol/messenger"; import * as vscode from "vscode"; @@ -27,10 +26,10 @@ import { getControlPlaneSessionInfo, WorkOsAuthProvider, } from "../stubs/WorkOsAuthProvider"; -import { getFullyQualifiedPath } from "../util/util"; import { getExtensionUri } from "../util/vscode"; import { VsCodeIde } from "../VsCodeIde"; import { VsCodeWebviewProtocol } from "../webviewProtocol"; +import { getUriPathBasename } from "core/util/uri"; /** * A shared messenger class between Core and Webview @@ -246,7 +245,7 @@ export class VsCodeMessenger { const [instant, diffLines] = await applyCodeBlock( editor.document.getText(), data.text, - getBasename(editor.document.fileName), + getUriPathBasename(editor.document.uri.toString()), llm, fastLlm, ); diff --git a/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts b/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts index 4b59dcbda..ce4a1b645 100644 --- a/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts +++ b/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts @@ -17,7 +17,6 @@ import { getModelQuickPickVal } from "./ModelSelectionQuickPick"; // @ts-ignore - error finding typings // @ts-ignore - /** * Used to track what action to take after a user interacts * with the initial Quick Pick @@ -133,11 +132,11 @@ export class QuickEdit { } const hasChanges = !!this.verticalDiffManager.getHandlerForFile( - editor.document.uri.fsPath, + editor.document.uri.toString(), ); if (hasChanges) { - this.openAcceptRejectMenu("", editor.document.uri.fsPath); + this.openAcceptRejectMenu("", editor.document.uri.toString()); } else { await this.initiateNewQuickPick(editor, params); } @@ -185,7 +184,7 @@ export class QuickEdit { }); if (prompt) { - await this.handleUserPrompt(prompt, editor.document.uri.fsPath); + await this.handleUserPrompt(prompt, editor.document.uri.toString()); } } diff --git a/extensions/vscode/src/test/test-suites/ideUtils.test.ts b/extensions/vscode/src/test/test-suites/ideUtils.test.ts index 3a289a079..37ba5cc33 100644 --- a/extensions/vscode/src/test/test-suites/ideUtils.test.ts +++ b/extensions/vscode/src/test/test-suites/ideUtils.test.ts @@ -10,6 +10,9 @@ import { testWorkspacePath } from "../runner/runTestOnVSCodeHost"; const util = require("node:util"); const asyncExec = util.promisify(require("node:child_process").exec); +/* + TODO check uri => path assumptions, will only work in some environments. +*/ describe("IDE Utils", () => { const utils = new VsCodeIdeUtils(); const testPyPath = path.join(testWorkspacePath, "test.py"); @@ -17,22 +20,16 @@ describe("IDE Utils", () => { test("getWorkspaceDirectories", async () => { const [dir] = utils.getWorkspaceDirectories(); - assert(dir.endsWith("test-workspace")); + assert(dir.toString().endsWith("test-workspace")); }); test("fileExists", async () => { const exists2 = await utils.fileExists( - path.join(testWorkspacePath, "test.py"), + vscode.Uri.file(path.join(testWorkspacePath, "test.py")), ); assert(exists2); }); - test("getAbsolutePath", async () => { - const groundTruth = path.join(testWorkspacePath, "test.py"); - assert(utils.getAbsolutePath("test.py") === groundTruth); - assert(utils.getAbsolutePath(groundTruth) === groundTruth); - }); - test("getOpenFiles", async () => { let openFiles = utils.getOpenFiles(); assert(openFiles.length === 0); @@ -43,7 +40,7 @@ describe("IDE Utils", () => { }); openFiles = utils.getOpenFiles(); assert(openFiles.length === 1); - assert(openFiles[0] === testPyPath); + assert(openFiles[0].fsPath === testPyPath); document = await vscode.workspace.openTextDocument(testJsPath); await vscode.window.showTextDocument(document, { @@ -51,8 +48,8 @@ describe("IDE Utils", () => { }); openFiles = utils.getOpenFiles(); assert(openFiles.length === 2); - assert(openFiles.includes(testPyPath)); - assert(openFiles.includes(testJsPath)); + assert(openFiles.find((f) => f.fsPath === testPyPath)); + assert(openFiles.find((f) => f.fsPath === testJsPath)); }); test("getUniqueId", async () => { diff --git a/extensions/vscode/src/util/util.ts b/extensions/vscode/src/util/util.ts index 48777fa07..e8a6cd0b4 100644 --- a/extensions/vscode/src/util/util.ts +++ b/extensions/vscode/src/util/util.ts @@ -124,13 +124,3 @@ export function getExtensionVersion(): string { const extension = vscode.extensions.getExtension("continue.continue"); return extension?.packageJSON.version || "0.1.0"; } - -export function getFullyQualifiedPath(ide: VsCodeIde, filepath: string) { - if (ide.ideUtils.path.isAbsolute(filepath)) {return filepath;} - - const workspaceFolders = vscode.workspace.workspaceFolders; - - if (workspaceFolders && workspaceFolders.length > 0) { - return ide.ideUtils.path.join(workspaceFolders[0].uri.fsPath, filepath); - } -}