diff --git a/src/editor/lsp/fileReferences.test.ts b/src/editor/lsp/fileReferences.test.ts index 163a2ca3e4..fd55976e8a 100644 --- a/src/editor/lsp/fileReferences.test.ts +++ b/src/editor/lsp/fileReferences.test.ts @@ -10,6 +10,17 @@ describe("path completion context", () => { expect(pathContext("See [docs](guide/", "markdown")).toEqual({ prefix: "guide/", eager: true }); }); + it("completes bare path lines in ignore files", () => { + expect(pathContext("target/", "ignore")).toEqual({ prefix: "target/", eager: true }); + expect(pathContext("src/comp", "ignore")).toEqual({ prefix: "src/comp", eager: true }); + expect(pathContext("node_modules", "ignore")).toEqual({ prefix: "node_modules", eager: true }); + expect(pathContext("!node_modules", "ignore")).toEqual({ prefix: "node_modules", eager: true }); + expect(pathContext("/src-tauri/gen", "ignore")).toEqual({ prefix: "/src-tauri/gen", eager: true }); + expect(pathContext(" # a comment", "ignore")).toBeNull(); + expect(pathContext("*.log", "ignore")).toBeNull(); + expect(pathContext("src/*.ts", "ignore")).toBeNull(); + }); + it("completes string literals only once they look like paths", () => { expect(pathContext('open("data/x', "python")).toEqual({ prefix: "data/x", eager: false }); expect(pathLike("data/x") && pathLike("./") && pathLike("../a")).toBe(true); diff --git a/src/editor/lsp/fileReferences.ts b/src/editor/lsp/fileReferences.ts index b4c4732098..337e55c722 100644 --- a/src/editor/lsp/fileReferences.ts +++ b/src/editor/lsp/fileReferences.ts @@ -44,6 +44,14 @@ export const pathLike = (prefix: string) => /^\.{1,2}\/|\//.test(prefix); /** The path being typed at the end of `before` (the line up to the caret), if any. */ export function pathContext(before: string, language: string): PathContext | null { + if (language === "ignore") { + // Every non-comment line in a .gitignore-style file is a path pattern: + // complete it eagerly, but leave glob patterns to the user. + if (/^\s*#/.test(before)) return null; + const prefix = before.replace(/^\s*!?\s*/, ""); + if (/[*?[\]]/.test(prefix)) return null; + return { prefix, eager: true }; + } for (const re of [URL_ATTRIBUTE, CSS_URL, CSS_IMPORT, MARKDOWN_LINK]) { const m = re.exec(before); if (m) return { prefix: m[1]!, eager: true };