From df48d9271b0ff908ae089ced8d9e55662f6579a7 Mon Sep 17 00:00:00 2001 From: Muu Date: Thu, 26 Nov 2026 23:31:47 +0800 Subject: [PATCH] feat(editor): complete paths on bare lines in ignore files (#6007) Path completion only triggered inside quoted strings, HTML attributes, url() and Markdown links, so it never fired in .gitignore-style files. Each non-comment line of an ignore file (.gitignore, .dockerignore, ...) is now a path prefix completed eagerly from the file system, after an optional `!`. Glob patterns and comment lines are left alone. Co-authored-by: muutot <92625353+muutot@users.noreply.github.com> --- src/editor/lsp/fileReferences.test.ts | 11 +++++++++++ src/editor/lsp/fileReferences.ts | 8 ++++++++ 2 files changed, 19 insertions(+) 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 };