forked from abierbaum/vscode-file-peek
-
-
Notifications
You must be signed in to change notification settings - Fork 36
feat(client): respect .gitignore + better exclude defaults (#145) #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { workspace as Workspace, Uri } from "vscode"; | ||
|
|
||
| /** | ||
| * Convert a single `.gitignore` pattern line into one or more VS Code glob | ||
| * patterns. | ||
| * | ||
| * Lines explicitly skipped (returns an empty array): | ||
| * - blank lines (after trimming) | ||
| * - comments (lines starting with `#`) | ||
| * - negation lines (lines starting with `!`) | ||
| * - lines that reduce to an empty pattern after stripping a leading `/` | ||
| * or trailing `/` | ||
| * | ||
| * All other lines are passed through with minimal rewriting (anchoring, | ||
| * directory-vs-file expansion, bare-name -> `**\/name` lift). This is a | ||
| * best-effort conversion covering the common cases (directory names, file | ||
| * names, simple `*` globs). When the line could match either a file or a | ||
| * directory, we emit both globs since VS Code globs distinguish files from | ||
| * folders. | ||
| * | ||
| * Unsupported gitignore constructs are NOT detected — they are passed | ||
| * through verbatim and may produce wrong or best-effort matches: | ||
| * - character classes (e.g. `[abc]`, `[!a-z]`) | ||
| * - escape sequences (`\#`, `\!`, `\ `) | ||
| * - `**` in positions where gitignore semantics differ from VS Code's | ||
| * glob semantics (common shapes like `foo/**` and `**\/foo` work; | ||
| * exotic placements may not) | ||
| */ | ||
| export function gitignoreLineToGlob(rawLine: string): string[] { | ||
| const line = rawLine.trim(); | ||
|
|
||
| if (line.length === 0 || line.startsWith("#") || line.startsWith("!")) { | ||
| return []; | ||
| } | ||
|
|
||
| let pattern = line; | ||
|
|
||
| const isDirectoryPattern = pattern.endsWith("/"); | ||
| if (isDirectoryPattern) { | ||
| pattern = pattern.slice(0, -1); | ||
| } | ||
|
|
||
| const isAnchored = pattern.startsWith("/"); | ||
| if (isAnchored) { | ||
| pattern = pattern.slice(1); | ||
| } | ||
|
|
||
| if (pattern.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| // Patterns with a slash are workspace-root-relative; bare names match | ||
| // anywhere in the tree. | ||
| const base = isAnchored || pattern.includes("/") ? pattern : `**/${pattern}`; | ||
|
|
||
| // A directory marker (`foo/`) only matches the directory and its contents. | ||
| // Without it, gitignore matches both files and directories — emit both. | ||
| if (isDirectoryPattern) { | ||
| return [`${base}/**`]; | ||
| } | ||
| return [base, `${base}/**`]; | ||
| } | ||
|
|
||
| /** | ||
| * Read the workspace root's `.gitignore` and return a set of VS Code glob | ||
| * patterns that approximate its semantics. | ||
| * | ||
| * Uses `vscode.workspace.fs` (rather than Node `fs`) so the reader itself | ||
| * is not tied to local disk. Note: the extension as a whole does not yet | ||
| * declare virtual-workspace support — see `capabilities.virtualWorkspaces` | ||
| * in `package.json` and the server's direct `fs` usage. Returns an empty | ||
| * array if the file does not exist or cannot be read. | ||
| */ | ||
| export async function readGitignoreGlobs( | ||
| workspaceRoot: Uri | ||
| ): Promise<string[]> { | ||
| const gitignoreUri = Uri.joinPath(workspaceRoot, ".gitignore"); | ||
|
|
||
| let bytes: Uint8Array; | ||
| try { | ||
| bytes = await Workspace.fs.readFile(gitignoreUri); | ||
| } catch { | ||
| return []; | ||
| } | ||
|
|
||
| const content = Buffer.from(bytes).toString("utf-8"); | ||
| const globs = new Set<string>(); | ||
| for (const line of content.split(/\r?\n/)) { | ||
| for (const glob of gitignoreLineToGlob(line)) { | ||
| globs.add(glob); | ||
| } | ||
| } | ||
| return Array.from(globs); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Fixture .gitignore used by gitignore.test.ts to verify | ||
| # that readGitignoreGlobs parses a workspace-root .gitignore. | ||
| # These paths intentionally do not match any real fixture file. | ||
| excluded.css | ||
| /anchored-build | ||
| dist/ | ||
| !unignore.css |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import * as assert from "assert"; | ||
| import * as path from "path"; | ||
| import * as vscode from "vscode"; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const gitignoreMod = require("../../client/out/gitignore") as { | ||
| gitignoreLineToGlob: (line: string) => string[]; | ||
| readGitignoreGlobs: (uri: vscode.Uri) => Promise<string[]>; | ||
| }; | ||
| const { gitignoreLineToGlob, readGitignoreGlobs } = gitignoreMod; | ||
|
|
||
| suite("gitignoreLineToGlob", () => { | ||
| test("skips blank lines and comments", () => { | ||
| assert.deepStrictEqual(gitignoreLineToGlob(""), []); | ||
| assert.deepStrictEqual(gitignoreLineToGlob(" "), []); | ||
| assert.deepStrictEqual(gitignoreLineToGlob("# comment"), []); | ||
| }); | ||
|
|
||
| test("skips negation lines", () => { | ||
| assert.deepStrictEqual(gitignoreLineToGlob("!keep.css"), []); | ||
| }); | ||
|
|
||
| test("unanchored bare names match both files and directories anywhere", () => { | ||
| assert.deepStrictEqual(gitignoreLineToGlob("node_modules"), [ | ||
| "**/node_modules", | ||
| "**/node_modules/**", | ||
| ]); | ||
| assert.deepStrictEqual(gitignoreLineToGlob("excluded.css"), [ | ||
| "**/excluded.css", | ||
| "**/excluded.css/**", | ||
| ]); | ||
| }); | ||
|
|
||
| test("trailing slash matches directories only", () => { | ||
| assert.deepStrictEqual(gitignoreLineToGlob("dist/"), ["**/dist/**"]); | ||
| }); | ||
|
|
||
| test("leading slash anchors to workspace root", () => { | ||
| assert.deepStrictEqual(gitignoreLineToGlob("/build"), [ | ||
| "build", | ||
| "build/**", | ||
| ]); | ||
| assert.deepStrictEqual(gitignoreLineToGlob("/build/"), ["build/**"]); | ||
| }); | ||
|
|
||
| test("nested paths are relative to workspace root", () => { | ||
| assert.deepStrictEqual(gitignoreLineToGlob("packages/foo/dist"), [ | ||
| "packages/foo/dist", | ||
| "packages/foo/dist/**", | ||
| ]); | ||
| }); | ||
|
|
||
| test("trims whitespace", () => { | ||
| assert.deepStrictEqual(gitignoreLineToGlob(" coverage "), [ | ||
| "**/coverage", | ||
| "**/coverage/**", | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| suite("readGitignoreGlobs", () => { | ||
| test("returns empty array when .gitignore is missing", async () => { | ||
| const tmpRoot = vscode.Uri.file( | ||
| path.join(__dirname, `__missing_gitignore_${Date.now()}`) | ||
| ); | ||
| const globs = await readGitignoreGlobs(tmpRoot); | ||
| assert.deepStrictEqual(globs, []); | ||
| }); | ||
|
|
||
| test("parses .gitignore from the workspace root", async () => { | ||
| const root = vscode.workspace.workspaceFolders![0].uri; | ||
| const globs = await readGitignoreGlobs(root); | ||
| // .gitignore in the test fixture contains `excluded.css`. | ||
| assert.ok( | ||
| globs.includes("**/excluded.css"), | ||
| `expected file glob; got ${JSON.stringify(globs)}` | ||
| ); | ||
| assert.ok( | ||
| globs.includes("**/excluded.css/**"), | ||
| `expected dir glob; got ${JSON.stringify(globs)}` | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — fixed in 98aa492. I removed the virtual-workspace claim from the PR body and from the
readGitignoreGlobsdoc comment, and now explicitly note thatcapabilities.virtualWorkspaces.supported: falseand the server's Nodefsusage still gate full virtual-workspace support. The.gitignorereader usesvscode.workspace.fsso it isn't the blocker, but I'm not claiming the extension supports virtual workspaces today.