path -> uri: all files have been triaged

This commit is contained in:
Dallin Romney 2024-12-11 12:29:50 -08:00
parent fe47f45ddf
commit 3d37c1e1bf
8 changed files with 138 additions and 91 deletions

View file

@ -3,9 +3,9 @@ import path from "node:path";
import ignore from "ignore";
import { IDE } from "../..";
import { getBasename } from "../../util";
import { getConfigJsonPath } from "../../util/paths";
import { HelperVars } from "../util/HelperVars";
import { getRelativePath } from "../../util/uri";
async function isDisabledForFile(
currentFilepath: string,
@ -15,26 +15,11 @@ async function isDisabledForFile(
if (disableInFiles) {
// Relative path needed for `ignore`
const workspaceDirs = await ide.getWorkspaceDirs();
let filepath = currentFilepath;
for (const workspaceDir of workspaceDirs) {
const relativePath = path.relative(workspaceDir, filepath);
const relativePathBase = relativePath.split(path.sep).at(0);
const isInWorkspace =
!path.isAbsolute(relativePath) && relativePathBase !== "..";
if (isInWorkspace) {
filepath = path.relative(workspaceDir, filepath);
break;
}
}
// Worst case we can check filetype glob patterns
if (filepath === currentFilepath) {
filepath = getBasename(filepath);
}
const relativePath = getRelativePath(currentFilepath, workspaceDirs);
// @ts-ignore
const pattern = ignore.default().add(disableInFiles);
if (pattern.ignores(filepath)) {
if (pattern.ignores(relativePath)) {
return true;
}
}

20
core/index.d.ts vendored
View file

@ -611,7 +611,7 @@ export interface IDE {
getWorkspaceConfigs(): Promise<ContinueRcJson[]>;
fileExists(filepath: string): Promise<boolean>;
fileExists(fileUri: string): Promise<boolean>;
writeFile(path: string, contents: string): Promise<void>;
@ -625,20 +625,16 @@ export interface IDE {
runCommand(command: string): Promise<void>;
saveFile(filepath: string): Promise<void>;
saveFile(fileUri: string): Promise<void>;
readFile(filepath: string): Promise<string>;
readFile(fileUri: string): Promise<string>;
readRangeInFile(filepath: string, range: Range): Promise<string>;
readRangeInFile(fileUri: string, range: Range): Promise<string>;
showLines(
filepath: string,
startLine: number,
endLine: number,
): Promise<void>;
showLines(fileUri: string, startLine: number, endLine: number): Promise<void>;
showDiff(
filepath: string,
fileUri: string,
newContents: string,
stepIndex: number,
): Promise<void>;
@ -660,7 +656,7 @@ export interface IDE {
subprocess(command: string, cwd?: string): Promise<[string, string]>;
getProblems(filepath?: string | undefined): Promise<Problem[]>;
getProblems(fileUri?: string | undefined): Promise<Problem[]>;
getBranch(dir: string): Promise<string>;
@ -686,7 +682,7 @@ export interface IDE {
gotoDefinition(location: Location): Promise<RangeInFile[]>;
// Callbacks
onDidChangeActiveTextEditor(callback: (filepath: string) => void): void;
onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void;
}
// Slash Commands

View file

@ -27,16 +27,13 @@ export class MessageIde implements IDE {
) => void,
) {}
pathSep(): Promise<string> {
return this.request("pathSep", undefined);
}
fileExists(filepath: string): Promise<boolean> {
return this.request("fileExists", { filepath });
fileExists(fileUri: string): Promise<boolean> {
return this.request("fileExists", { filepath: fileUri });
}
async gotoDefinition(location: Location): Promise<RangeInFile[]> {
return this.request("gotoDefinition", { location });
}
onDidChangeActiveTextEditor(callback: (filepath: string) => void): void {
onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void {
this.on("didChangeActiveTextEditor", (data) => callback(data.filepath));
}
@ -126,11 +123,15 @@ export class MessageIde implements IDE {
}
async showLines(
filepath: string,
fileUri: string,
startLine: number,
endLine: number,
): Promise<void> {
return await this.request("showLines", { filepath, startLine, endLine });
return await this.request("showLines", {
filepath: fileUri,
startLine,
endLine,
});
}
async listFolders(): Promise<string[]> {
@ -148,16 +149,16 @@ export class MessageIde implements IDE {
return dir;
}
async writeFile(path: string, contents: string): Promise<void> {
await this.request("writeFile", { path, contents });
async writeFile(fileUri: string, contents: string): Promise<void> {
await this.request("writeFile", { path: fileUri, contents });
}
async showVirtualFile(title: string, contents: string): Promise<void> {
await this.request("showVirtualFile", { name: title, content: contents });
}
async openFile(path: string): Promise<void> {
await this.request("openFile", { path });
async openFile(fileUri: string): Promise<void> {
await this.request("openFile", { path: fileUri });
}
async openUrl(url: string): Promise<void> {
@ -168,18 +169,22 @@ export class MessageIde implements IDE {
await this.request("runCommand", { command });
}
async saveFile(filepath: string): Promise<void> {
await this.request("saveFile", { filepath });
async saveFile(fileUri: string): Promise<void> {
await this.request("saveFile", { filepath: fileUri });
}
async readFile(filepath: string): Promise<string> {
return await this.request("readFile", { filepath });
async readFile(fileUri: string): Promise<string> {
return await this.request("readFile", { filepath: fileUri });
}
async showDiff(
filepath: string,
fileUri: string,
newContents: string,
stepIndex: number,
): Promise<void> {
await this.request("showDiff", { filepath, newContents, stepIndex });
await this.request("showDiff", {
filepath: fileUri,
newContents,
stepIndex,
});
}
getOpenFiles(): Promise<string[]> {
@ -198,8 +203,8 @@ export class MessageIde implements IDE {
return this.request("getSearchResults", { query });
}
getProblems(filepath: string): Promise<Problem[]> {
return this.request("getProblems", { filepath });
getProblems(fileUri: string): Promise<Problem[]> {
return this.request("getProblems", { filepath: fileUri });
}
subprocess(command: string, cwd?: string): Promise<[string, string]> {

View file

@ -1,20 +1,27 @@
import fs from "fs";
import os from "os";
import path from "path";
import {
localPathOrUriToPath,
localPathToUri,
pathOrUriToUri,
} from "../util/uri";
import { fileURLToPath } from "url";
// Want this outside of the git repository so we can change branches in tests
export const TEST_DIR = path.join(os.tmpdir(), "testWorkspaceDir");
const TEST_DIR_PATH = path.join(os.tmpdir(), "testWorkspaceDir");
export const TEST_DIR = localPathToUri(TEST_DIR_PATH); // URI
export function setUpTestDir() {
if (fs.existsSync(TEST_DIR)) {
fs.rmSync(TEST_DIR, { recursive: true });
if (fs.existsSync(TEST_DIR_PATH)) {
fs.rmSync(TEST_DIR_PATH, { recursive: true });
}
fs.mkdirSync(TEST_DIR);
fs.mkdirSync(TEST_DIR_PATH);
}
export function tearDownTestDir() {
if (fs.existsSync(TEST_DIR)) {
fs.rmSync(TEST_DIR, { recursive: true });
if (fs.existsSync(TEST_DIR_PATH)) {
fs.rmSync(TEST_DIR_PATH, { recursive: true });
}
}
@ -24,9 +31,18 @@ export function tearDownTestDir() {
"index/index.ts" creates an empty index/index.ts
["index/index.ts", "hello"] creates index/index.ts with contents "hello"
*/
export function addToTestDir(paths: (string | string[])[]) {
export function addToTestDir(pathsOrUris: (string | string[])[]) {
// Allow tests to use URIs or local paths
const paths = pathsOrUris.map((val) => {
if (Array.isArray(val)) {
return [localPathOrUriToPath(val[0]), val[1]];
} else {
return localPathOrUriToPath(val);
}
});
for (const p of paths) {
const filepath = path.join(TEST_DIR, Array.isArray(p) ? p[0] : p);
const filepath = path.join(TEST_DIR_PATH, Array.isArray(p) ? p[0] : p);
fs.mkdirSync(path.dirname(filepath), { recursive: true });
if (Array.isArray(p)) {

View file

@ -1,9 +1,9 @@
import { writeFileSync } from "fs";
import fs from "fs";
import { getDevDataFilePath } from "./paths.js";
export function logDevData(tableName: string, data: any) {
const filepath: string = getDevDataFilePath(tableName);
const jsonLine = JSON.stringify(data);
writeFileSync(filepath, `${jsonLine}\n`, { flag: "a" });
fs.writeFileSync(filepath, `${jsonLine}\n`, { flag: "a" });
}

View file

@ -17,6 +17,7 @@ import {
import { GetGhTokenArgs } from "../protocol/ide.js";
import { getContinueGlobalPath } from "./paths.js";
import { fileURLToPath } from "node:url";
class FileSystemIde implements IDE {
constructor(private readonly workspaceDir: string) {}
@ -27,14 +28,15 @@ class FileSystemIde implements IDE {
): Promise<void> {
return Promise.resolve();
}
fileExists(filepath: string): Promise<boolean> {
fileExists(fileUri: string): Promise<boolean> {
const filepath = fileURLToPath(fileUri);
return Promise.resolve(fs.existsSync(filepath));
}
gotoDefinition(location: Location): Promise<RangeInFile[]> {
return Promise.resolve([]);
}
onDidChangeActiveTextEditor(callback: (filepath: string) => void): void {
onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void {
return;
}
@ -51,14 +53,17 @@ class FileSystemIde implements IDE {
async getGitHubAuthToken(args: GetGhTokenArgs): Promise<string | undefined> {
return undefined;
}
async getLastModified(files: string[]): Promise<{ [path: string]: number }> {
async getLastModified(
fileUris: string[],
): Promise<{ [path: string]: number }> {
const result: { [path: string]: number } = {};
for (const file of files) {
for (const uri of fileUris) {
try {
const stats = fs.statSync(file);
result[file] = stats.mtimeMs;
const filepath = fileURLToPath(uri);
const stats = fs.statSync(filepath);
result[uri] = stats.mtimeMs;
} catch (error) {
console.error(`Error getting last modified time for ${file}:`, error);
console.error(`Error getting last modified time for ${uri}:`, error);
}
}
return result;
@ -67,8 +72,9 @@ class FileSystemIde implements IDE {
return Promise.resolve(dir);
}
async listDir(dir: string): Promise<[string, FileType][]> {
const filepath = fileURLToPath(dir);
const all: [string, FileType][] = fs
.readdirSync(dir, { withFileTypes: true })
.readdirSync(filepath, { withFileTypes: true })
.map((dirent: any) => [
dirent.name,
dirent.isDirectory()
@ -105,7 +111,7 @@ class FileSystemIde implements IDE {
});
}
readRangeInFile(filepath: string, range: Range): Promise<string> {
readRangeInFile(fileUri: string, range: Range): Promise<string> {
return Promise.resolve("");
}
@ -149,7 +155,7 @@ class FileSystemIde implements IDE {
}
showLines(
filepath: string,
fileUri: string,
startLine: number,
endLine: number,
): Promise<void> {
@ -164,9 +170,10 @@ class FileSystemIde implements IDE {
return Promise.resolve([]);
}
writeFile(path: string, contents: string): Promise<void> {
writeFile(fileUri: string, contents: string): Promise<void> {
const filepath = fileURLToPath(fileUri);
return new Promise((resolve, reject) => {
fs.writeFile(path, contents, (err) => {
fs.writeFile(filepath, contents, (err) => {
if (err) {
reject(err);
}
@ -195,11 +202,12 @@ class FileSystemIde implements IDE {
return Promise.resolve();
}
saveFile(filepath: string): Promise<void> {
saveFile(fileUri: string): Promise<void> {
return Promise.resolve();
}
readFile(filepath: string): Promise<string> {
readFile(fileUri: string): Promise<string> {
const filepath = fileURLToPath(fileUri);
return new Promise((resolve, reject) => {
fs.readFile(filepath, "utf8", (err, contents) => {
if (err) {
@ -215,7 +223,7 @@ class FileSystemIde implements IDE {
}
showDiff(
filepath: string,
fileUri: string,
newContents: string,
stepIndex: number,
): Promise<void> {
@ -238,7 +246,7 @@ class FileSystemIde implements IDE {
return "";
}
async getProblems(filepath?: string | undefined): Promise<Problem[]> {
async getProblems(fileUri?: string | undefined): Promise<Problem[]> {
return Promise.resolve([]);
}

View file

@ -1,4 +1,5 @@
import URI from "uri-js";
import { fileURLToPath, pathToFileURL } from "url";
export function getFullPath(uri: string): string {
try {
@ -9,6 +10,11 @@ export function getFullPath(uri: string): string {
}
}
export function localPathToUri(path: string) {
const url = pathToFileURL(path);
return URI.normalize(url.toString());
}
export function pathToUriPathSegment(path: string) {
// Converts any OS path to cleaned up URI path segment format with no leading/trailing slashes
// e.g. \path\to\folder\ -> path/to/folder
@ -30,6 +36,37 @@ export function getRelativePath(
const fullPath = getFullPath(fileUri);
}
/*
*/
export function localPathOrUriToPath(localPathOrUri: string): string {
try {
return fileURLToPath(localPathOrUri);
} catch (e) {
console.log("Converted url to path");
return localPathOrUri;
}
}
/*
To smooth out the transition from path to URI will use this function to warn when path is used
*/
export function pathOrUriToUri(
pathOrUri: string,
workspaceDirUris: string[],
showTraceOnPath = true,
): string {
try {
// URI.parse(pathOrUri);
return pathOrUri;
} catch (e) {
if (showTraceOnPath) {
console.trace("Received relative path", e);
}
}
}
export function splitUriPath(uri: string): string[] {
let parts = path.includes("/") ? path.split("/") : path.split("\\");
if (withRoot !== undefined) {

View file

@ -265,9 +265,9 @@ class VsCodeIde implements IDE {
});
}
readRangeInFile(filepath: string, range: Range): Promise<string> {
readRangeInFile(fileUri: string, range: Range): Promise<string> {
return this.ideUtils.readRangeInFile(
vscode.Uri.parse(filepath),
vscode.Uri.parse(fileUri),
new vscode.Range(
new vscode.Position(range.start.line, range.start.character),
new vscode.Position(range.end.line, range.end.character),
@ -378,9 +378,9 @@ class VsCodeIde implements IDE {
return getContinueGlobalPath();
}
async writeFile(path: string, contents: string): Promise<void> {
async writeFile(fileUri: string, contents: string): Promise<void> {
await vscode.workspace.fs.writeFile(
vscode.Uri.parse(path),
vscode.Uri.parse(fileUri),
Buffer.from(contents),
);
}
@ -389,12 +389,12 @@ class VsCodeIde implements IDE {
this.ideUtils.showVirtualFile(title, contents);
}
async openFile(path: string): Promise<void> {
await this.ideUtils.openFile(vscode.Uri.parse(path));
async openFile(fileUri: string): Promise<void> {
await this.ideUtils.openFile(vscode.Uri.parse(fileUri));
}
async showLines(
filepath: string,
fileUri: string,
startLine: number,
endLine: number,
): Promise<void> {
@ -402,7 +402,7 @@ class VsCodeIde implements IDE {
new vscode.Position(startLine, 0),
new vscode.Position(endLine, 0),
);
openEditorAndRevealRange(vscode.Uri.parse(filepath), range).then(
openEditorAndRevealRange(vscode.Uri.parse(fileUri), range).then(
(editor) => {
// Select the lines
editor.selection = new vscode.Selection(
@ -426,8 +426,8 @@ class VsCodeIde implements IDE {
}
}
async saveFile(filepath: string): Promise<void> {
await this.ideUtils.saveFile(vscode.Uri.parse(filepath));
async saveFile(fileUri: string): Promise<void> {
await this.ideUtils.saveFile(vscode.Uri.parse(fileUri));
}
private static MAX_BYTES = 100000;
@ -482,11 +482,11 @@ class VsCodeIde implements IDE {
}
async showDiff(
filepath: string,
fileUri: string,
newContents: string,
stepIndex: number,
): Promise<void> {
await this.diffManager.writeDiff(filepath, newContents, stepIndex);
await this.diffManager.writeDiff(fileUri, newContents, stepIndex);
}
async getOpenFiles(): Promise<string[]> {
@ -563,9 +563,9 @@ class VsCodeIde implements IDE {
return results.join("\n\n");
}
async getProblems(filepath?: string | undefined): Promise<Problem[]> {
const uri = filepath
? vscode.Uri.file(filepath)
async getProblems(fileUri?: string | undefined): Promise<Problem[]> {
const uri = fileUri
? vscode.Uri.parse(fileUri)
: vscode.window.activeTextEditor?.document.uri;
if (!uri) {
return [];