-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(web): clicking a composer mention chip opens the file #6299
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
Open
Brechard
wants to merge
3
commits into
pingdotgg:main
Choose a base branch
from
Brechard:feat/composer-mention-chip-opens-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { resolveComposerMentionFileTarget } from "./composerMentionFileTarget"; | ||
|
|
||
| const ROOT = "/Users/dev/t3code"; | ||
|
|
||
| describe("resolveComposerMentionFileTarget", () => { | ||
| it("passes workspace-relative mentions straight through", () => { | ||
| expect(resolveComposerMentionFileTarget("AGENTS.md", ROOT)).toEqual({ | ||
| relativePath: "AGENTS.md", | ||
| }); | ||
| expect(resolveComposerMentionFileTarget("apps/web/src/main.tsx", ROOT)).toEqual({ | ||
| relativePath: "apps/web/src/main.tsx", | ||
| }); | ||
| }); | ||
|
|
||
| it("relativizes absolute mentions inside the workspace", () => { | ||
| expect(resolveComposerMentionFileTarget(`${ROOT}/apps/web/src/main.tsx`, ROOT)).toEqual({ | ||
| relativePath: "apps/web/src/main.tsx", | ||
| }); | ||
| }); | ||
|
|
||
| it("keeps extensionless filenames, which markdown link heuristics reject", () => { | ||
| expect(resolveComposerMentionFileTarget("Makefile", ROOT)).toEqual({ | ||
| relativePath: "Makefile", | ||
| }); | ||
| }); | ||
|
|
||
| it("collapses dot segments", () => { | ||
| expect(resolveComposerMentionFileTarget("./docs/../AGENTS.md", ROOT)).toEqual({ | ||
| relativePath: "AGENTS.md", | ||
| }); | ||
| }); | ||
|
|
||
| it("carries a :line suffix over as the reveal line", () => { | ||
| expect(resolveComposerMentionFileTarget("apps/web/src/main.tsx:42", ROOT)).toEqual({ | ||
| relativePath: "apps/web/src/main.tsx", | ||
| line: 42, | ||
| }); | ||
| expect(resolveComposerMentionFileTarget("apps/web/src/main.tsx:42:7", ROOT)).toEqual({ | ||
| relativePath: "apps/web/src/main.tsx", | ||
| line: 42, | ||
| }); | ||
| }); | ||
|
|
||
| it("normalizes windows separators", () => { | ||
| expect(resolveComposerMentionFileTarget("C:\\repo\\apps\\web\\main.tsx", "C:\\repo")).toEqual({ | ||
| relativePath: "apps/web/main.tsx", | ||
| }); | ||
| }); | ||
|
|
||
| it("works for a workspace rooted at the filesystem root", () => { | ||
| expect(resolveComposerMentionFileTarget("/srv/app/main.ts", "/")).toEqual({ | ||
| relativePath: "srv/app/main.ts", | ||
| }); | ||
| }); | ||
|
|
||
| // Folding case here would accept a path from outside the workspace on a | ||
| // case-sensitive filesystem. | ||
| it("does not accept a root whose case does not match", () => { | ||
| expect(resolveComposerMentionFileTarget("/users/dev/t3code/Other.ts", ROOT)).toBeNull(); | ||
| }); | ||
|
|
||
| // The filesystem behind a drive root does not distinguish these. | ||
| it("matches a windows root case-insensitively", () => { | ||
| expect(resolveComposerMentionFileTarget("C:\\Repo\\src\\file.ts", "C:\\repo")).toEqual({ | ||
| relativePath: "src/file.ts", | ||
| }); | ||
| }); | ||
|
|
||
| it("still matches a windows root whose drive letter case differs", () => { | ||
| expect(resolveComposerMentionFileTarget("c:\\repo\\apps\\main.tsx", "C:\\repo")).toEqual({ | ||
| relativePath: "apps/main.tsx", | ||
| }); | ||
| }); | ||
|
|
||
| // `/..` is `/`, so climbing past an absolute root cannot leave `..` behind | ||
| // for the file surface to walk. | ||
| it("does not let a path climb above the root it resolves against", () => { | ||
| expect(resolveComposerMentionFileTarget("../a/secret.ts", "/")).toEqual({ | ||
| relativePath: "a/secret.ts", | ||
| }); | ||
| expect(resolveComposerMentionFileTarget("/x/../../a/secret.ts", "/")).toEqual({ | ||
| relativePath: "a/secret.ts", | ||
| }); | ||
| }); | ||
|
|
||
| it("returns null outside the workspace, and without one", () => { | ||
| expect(resolveComposerMentionFileTarget("/etc/hosts", ROOT)).toBeNull(); | ||
| expect(resolveComposerMentionFileTarget("../sibling/AGENTS.md", ROOT)).toBeNull(); | ||
| expect(resolveComposerMentionFileTarget("AGENTS.md", undefined)).toBeNull(); | ||
| expect(resolveComposerMentionFileTarget(" ", ROOT)).toBeNull(); | ||
| }); | ||
| }); |
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,64 @@ | ||
| import { resolvePathLinkTarget, splitPathAndPosition } from "./terminal-links"; | ||
|
|
||
| export interface ComposerMentionFileTarget { | ||
| readonly relativePath: string; | ||
| readonly line?: number; | ||
| } | ||
|
|
||
| function toPosixPath(path: string): string { | ||
| const normalized = path.replaceAll("\\", "/"); | ||
| // Windows drive paths pick up a leading slash on the way through URL-ish | ||
| // plumbing (`/C:/repo`); the workspace root never carries one. | ||
| return /^\/[A-Za-z]:\//.test(normalized) ? normalized.slice(1) : normalized; | ||
| } | ||
|
|
||
| const WINDOWS_DRIVE_ROOT_PATTERN = /^[A-Za-z]:\//; | ||
|
|
||
| function collapseDotSegments(path: string): string { | ||
| const isAbsolute = path.startsWith("/"); | ||
| const segments: string[] = []; | ||
| for (const segment of path.split("/")) { | ||
| if (segment === "" || segment === ".") continue; | ||
| if (segment === "..") { | ||
| if (segments.length > 0 && segments[segments.length - 1] !== "..") { | ||
| segments.pop(); | ||
| continue; | ||
| } | ||
| // `/..` is `/`; a surviving `..` would walk out of the workspace. | ||
| if (isAbsolute) continue; | ||
| } | ||
| segments.push(segment); | ||
| } | ||
| const joined = segments.join("/"); | ||
| return isAbsolute ? `/${joined}` : joined; | ||
| } | ||
|
|
||
| /** Null for anything outside the workspace, which leaves the chip inert. */ | ||
| export function resolveComposerMentionFileTarget( | ||
| mentionPath: string, | ||
| workspaceRoot: string | undefined, | ||
| ): ComposerMentionFileTarget | null { | ||
| const trimmed = mentionPath.trim(); | ||
| if (!trimmed || !workspaceRoot) return null; | ||
|
|
||
| const { path, line } = splitPathAndPosition(trimmed); | ||
| if (!path) return null; | ||
|
|
||
| const absolute = collapseDotSegments(toPosixPath(resolvePathLinkTarget(path, workspaceRoot))); | ||
| const normalizedRoot = collapseDotSegments(toPosixPath(workspaceRoot)); | ||
| // A `/` root strips to an empty prefix, which is what makes `/x` read as `x`. | ||
| const root = normalizedRoot === "/" ? "" : normalizedRoot.replace(/\/+$/, ""); | ||
| if (!root && normalizedRoot !== "/") return null; | ||
| // Compared the way the root's filesystem would: exact for POSIX, folded for | ||
| // a Windows drive. | ||
| const rootIsCaseInsensitive = WINDOWS_DRIVE_ROOT_PATTERN.test(normalizedRoot); | ||
| const comparableAbsolute = rootIsCaseInsensitive ? absolute.toLowerCase() : absolute; | ||
| const comparableRoot = rootIsCaseInsensitive ? root.toLowerCase() : root; | ||
| if (!comparableAbsolute.startsWith(`${comparableRoot}/`)) return null; | ||
|
|
||
| const relativePath = absolute.slice(root.length + 1); | ||
| if (!relativePath) return null; | ||
|
|
||
| const parsedLine = line ? Number.parseInt(line, 10) : Number.NaN; | ||
| return Number.isFinite(parsedLine) ? { relativePath, line: parsedLine } : { relativePath }; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.