shouldignore working

This commit is contained in:
Dallin Romney 2025-03-07 13:01:37 -08:00
parent 66e4db7f7c
commit a2034e801d
3 changed files with 184 additions and 89 deletions

View file

@ -53,6 +53,7 @@ import {
type IndexingProgressUpdate,
} from ".";
import { shouldIgnore } from "./indexing/shouldIgnore";
import type { FromCoreProtocol, ToCoreProtocol } from "./protocol";
import type { IMessenger, Message } from "./protocol/messenger";
@ -890,30 +891,45 @@ export class Core {
uri.endsWith(".gitignore")
) {
// Reindex the workspaces
this.invoke("index/forceReIndex", undefined);
this.invoke("index/forceReIndex", {
shouldClearIndexes: true,
});
} else {
// Reindex the file
await this.refreshCodebaseIndexFiles([uri]);
const ignore = await shouldIgnore(uri, this.ide);
if (!ignore) {
await this.refreshCodebaseIndexFiles([uri]);
}
}
}
}
});
on("files/created", async ({ data }) => {
if (data?.uris?.length) {
const refreshIfNotIgnored = async (uris: string[]) => {
const toRefresh: string[] = [];
for (const uri of uris) {
const ignore = await shouldIgnore(uri, ide);
if (!ignore) {
toRefresh.push(uri);
}
}
if (toRefresh.length > 0) {
this.messenger.send("refreshSubmenuItems", {
providers: ["file"],
});
await this.refreshCodebaseIndexFiles(data.uris);
await this.refreshCodebaseIndexFiles(toRefresh);
}
};
on("files/created", async ({ data }) => {
if (data?.uris?.length) {
void refreshIfNotIgnored(data.uris);
}
});
on("files/deleted", async ({ data }) => {
if (data?.uris?.length) {
this.messenger.send("refreshSubmenuItems", {
providers: ["file"],
});
await this.refreshCodebaseIndexFiles(data.uris);
void refreshIfNotIgnored(data.uris);
}
});
on("files/opened", async ({ data }) => {

View file

@ -1,7 +1,5 @@
// Generated by continue
// Import modules
import { describe, expect, test } from "@jest/globals";
import { testIde } from "../test/fixtures";
import {
TEST_DIR,
@ -9,6 +7,7 @@ import {
setUpTestDir,
tearDownTestDir,
} from "../test/testDir";
import { shouldIgnore } from "./shouldIgnore";
// Test for the shouldIgnore function
@ -23,31 +22,105 @@ describe("shouldIgnore", () => {
test("should return true if a file is ignored by .gitignore", async () => {
addToTestDir([
["/ignored-file.txt", "content"],
["/kept-file.txt", "content"],
["/.gitignore", "ignored-file.txt\nignored-folder/"],
"/ignored-folder/",
["/ignored-folder/file.txt", "content"],
["ignored-file.txt", "content"],
[".gitignore", "ignored-file.txt"],
]);
const result1 = await shouldIgnore(
TEST_DIR + "/ignored-file.txt",
const result = await shouldIgnore(TEST_DIR + "/ignored-file.txt", testIde, [
TEST_DIR,
]);
expect(result).toBe(true);
});
test("should return true if a folder is ignored by .continueignore", async () => {
addToTestDir([
"ignored-folder/",
["ignored-folder/file.txt", "content"],
[".continueignore", "ignored-folder/"],
]);
const result = await shouldIgnore(
TEST_DIR + "/ignored-folder/file.txt",
testIde,
[TEST_DIR],
);
expect(result1).toBe(true);
expect(result).toBe(true);
});
test("should return false if a file is not ignored", async () => {
addToTestDir([["kept-file.txt", "contents"]]);
const result = await shouldIgnore(TEST_DIR + "/kept-file.txt", testIde, [
TEST_DIR,
]);
expect(result).toBe(false);
});
test("should return true if a file is ignored by .gitignore with spaces in file name", async () => {
addToTestDir([
["ignored file.txt", "content"],
[".gitignore", "ignored file.txt"],
]);
const result = await shouldIgnore(TEST_DIR + "/ignored file.txt", testIde, [
TEST_DIR,
]);
expect(result).toBe(true);
});
const result2 = await shouldIgnore(
TEST_DIR + "/ignored-folder",
TEST_DIR,
test("should return true if a .continueignore override ignores file", async () => {
addToTestDir([
["override-file.txt", "content"],
[".gitignore", "override-file.txt"],
[".continueignore", "!override-file.txt"],
]);
const result = await shouldIgnore(
TEST_DIR + "/override-file.txt",
testIde,
[TEST_DIR],
);
expect(result2).toBe(true);
expect(result).toBe(false);
});
const result3 = await shouldIgnore(
TEST_DIR + "/kept-file.txt",
TEST_DIR,
test("should return true for deeply nested file ignored by ignore files at multiple levels", async () => {
addToTestDir([
"level1/",
"level1/level2/",
"level1/level2/level3/",
["level1/level2/level3/ignored-file.txt", "content"],
["level1/.gitignore", "level2/"],
["level1/level2/.continueignore", "level3/"],
]);
const result = await shouldIgnore(
TEST_DIR + "/level1/level2/level3/ignored-file.txt",
testIde,
[TEST_DIR],
);
expect(result3).toBe(false);
expect(result).toBe(true);
});
test("should return false if a file with spaces is not ignored", async () => {
addToTestDir([["kept file.txt", "contents"]]);
const result = await shouldIgnore(TEST_DIR + "/kept file.txt", testIde, [
TEST_DIR,
]);
expect(result).toBe(false);
});
test("should handle multiple levels of directories with some files ignored", async () => {
addToTestDir([
"nested/",
"nested/dir1/",
"nested/dir2/",
["nested/dir1/.gitignore", "ignored.txt"],
["nested/dir1/ignored.txt", "content"],
["nested/dir2/kept.txt", "content"],
]);
const ignoredResult = await shouldIgnore(
TEST_DIR + "/nested/dir1/ignored.txt",
testIde,
[TEST_DIR],
);
expect(ignoredResult).toBe(true);
const keptResult = await shouldIgnore(
TEST_DIR + "/nested/dir2/kept.txt",
testIde,
[TEST_DIR],
);
expect(keptResult).toBe(false);
});
});

View file

@ -1,80 +1,86 @@
import ignore from "ignore";
import { FileType, IDE } from "../index";
import { getUriPathBasename } from "../util/uri";
import { getGlobalContinueIgArray, gitIgArrayFromFile } from "./ignore";
import type { FileType, IDE } from "../";
import { findUriInDirs } from "../util/uri";
import {
DEFAULT_IGNORE,
getGlobalContinueIgArray,
gitIgArrayFromFile,
} from "./ignore";
interface IgnoreContext {
ignore: ReturnType<typeof ignore>;
dirname: string;
}
/*
Process:
1. Check global/default ignores
2. Walk UP tree from file, checking ignores at each level
TODO there might be issues with symlinks here
*/
export async function shouldIgnore(
fileUri: string,
rootDirUri: string,
ide: IDE,
ignoreFiles: string[] = [".gitignore", ".continueignore"],
rootDirCandidates?: string[],
): Promise<boolean> {
let currentDir = fileUri;
const rootDirUris = rootDirCandidates ?? (await ide.getWorkspaceDirs());
const {
foundInDir: rootDir,
uri,
relativePathOrBasename,
} = findUriInDirs(fileUri, rootDirUris);
if (!rootDir) {
throw new Error("Should ignore: file uri not found in root dirs");
}
while (currentDir !== rootDirUri && currentDir.startsWith(rootDirUri)) {
// Global/default ignores
const globalPatterns = getGlobalContinueIgArray();
const rootIgnoreContext = ignore().add(globalPatterns).add(DEFAULT_IGNORE);
if (rootIgnoreContext.ignores(relativePathOrBasename)) {
return true;
}
let currentDir = uri;
while (currentDir !== rootDir) {
// Go to parent dir of file
const splitUri = currentDir.split("/");
splitUri.pop();
currentDir = splitUri.join("/");
// Get all files in the dir
const dirEntries = await ide.listDir(currentDir);
const dirs = dirEntries
.filter(([_, uriType]) => uriType === FileType.File)
.map(([uri, _]) => uri);
const dirFiles = dirEntries
.filter(([_, entryType]) => entryType === (1 as FileType.File))
.map(([name, _]) => name);
const ignoreContexts = await getIgnoreContextsInDir(
currentDir,
dirs,
ignoreFiles,
ide,
// Find ignore files and get ignore arrays from their contexts
const gitIgnoreFile = dirFiles.find((name) => name === ".gitignore");
const continueIgnoreFile = dirFiles.find(
(name) => name === ".continueignore",
);
const relativePath = fileUri.substring(currentDir.length + 1);
for (const context of ignoreContexts) {
if (context.ignore.ignores(relativePath)) {
return true;
const getGitIgnorePatterns = async () => {
if (gitIgnoreFile) {
const contents = await ide.readFile(`${currentDir}/.gitignore`);
return gitIgArrayFromFile(contents);
}
}
return [];
};
const getContinueIgnorePatterns = async () => {
if (continueIgnoreFile) {
const contents = await ide.readFile(`${currentDir}/.continueignore`);
return gitIgArrayFromFile(contents);
}
return [];
};
currentDir = getParentDir(currentDir);
const ignoreArrays = await Promise.all([
getGitIgnorePatterns(),
getContinueIgnorePatterns(),
]);
const ignoreContext = ignore().add(ignoreArrays[0]).add(ignoreArrays[1]);
const relativePath = uri.substring(currentDir.length + 1);
if (ignoreContext.ignores(relativePath)) {
return true;
}
}
return false;
}
async function getIgnoreContextsInDir(
dirUri: string,
entries: string[],
ignoreFiles: string[],
ide: IDE,
): Promise<IgnoreContext[]> {
const ignoreEntries = entries.filter((entryUri) =>
ignoreFiles.includes(getUriPathBasename(entryUri)),
);
const patterns = await Promise.all(
ignoreEntries.map(async (entryUri) => {
const fileContent = await ide.readFile(entryUri);
return gitIgArrayFromFile(fileContent);
}),
);
const globalPatterns = getGlobalContinueIgArray();
return [
...patterns.map((patterns) => ({
ignore: ignore().add(patterns),
dirname: dirUri,
})),
{
ignore: ignore().add(globalPatterns),
dirname: "/",
},
];
}
function getParentDir(uri: string): string {
const splitUri = uri.split("/");
splitUri.pop();
return splitUri.join("/");
}