Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/desktop/src/renderer/lib/clickPolicy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export { LinkHoverHint } from "./components/LinkHoverHint";
export { ShadowClickHint } from "./components/ShadowClickHint";
export { buildHint, unboundHint } from "./hint";
export { modifierLabel } from "./modifierLabel";
export {
FILE_LINK_PLAIN_DEFAULT,
FOLDER_LINK_PLAIN_DEFAULT,
} from "./policies/agentOutputLinks";
export {
buildChangesSidebarFileHint,
type ChangesSidebarFileIntent,
Expand Down Expand Up @@ -43,7 +47,7 @@ export {
type UrlLinkAction,
useUrlLinkAction,
} from "./policies/useUrlLinkAction";
export { tierFor } from "./tiers";
export { tierFor, withPlainDefault } from "./tiers";
export type {
LinkAction,
LinkTier,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { LinkAction } from "../types";
import type { FolderLinkAction } from "./folderPolicy";

/**
* File and folder paths in agent output (terminal, chat) open on a plain
* click, unless the user bound plain click to another action.
*/
export const FILE_LINK_PLAIN_DEFAULT: LinkAction = "pane";
export const FOLDER_LINK_PLAIN_DEFAULT: FolderLinkAction = "reveal";
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
FolderTierMap,
} from "renderer/routes/_authenticated/providers/CollectionsProvider/dashboardSidebarLocal/schema";
import { tierFor } from "../tiers";
import type { ModifierEvent } from "../types";
import type { ModifierEvent, TierMode } from "../types";

export type { FolderLinkAction, FolderTierMap };

Expand All @@ -15,7 +15,7 @@ export type { FolderLinkAction, FolderTierMap };
* links): each click tier maps to reveal-in-sidebar, external editor,
* Finder, or unbound. Defaults:
*
* plain → null (caller decides — toggle/hint)
* plain → null (reveals in sidebar, see agentOutputLinks)
* shift → open in Finder
* meta / ctrl → reveal in sidebar
* meta+shift → open in external editor
Expand All @@ -25,8 +25,9 @@ export type FolderIntent = FolderLinkAction | null;
export function folderIntentForMap(
event: ModifierEvent,
map: FolderTierMap,
mode: TierMode = "4-tier",
): FolderIntent {
return map[tierFor(event, "4-tier")];
return map[tierFor(event, mode)];
}

/**
Expand Down
9 changes: 7 additions & 2 deletions apps/desktop/src/renderer/lib/clickPolicy/policies/policy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback, useMemo } from "react";
import { useUserPreferences } from "renderer/hooks/useUserPreferences";
import { buildHint } from "../hint";
import { tierFor } from "../tiers";
import { tierFor, withPlainDefault } from "../tiers";
import type {
LinkAction,
LinkTierMap,
Expand Down Expand Up @@ -65,9 +65,14 @@ export function usePolicy(
key: MapKey,
surface: Surface,
mode: TierMode,
plainDefault?: LinkAction,
): ClickPolicy {
const { preferences } = useUserPreferences();
const map = preferences[key];
const stored = preferences[key];
const map = useMemo(
() => (plainDefault ? withPlainDefault(stored, plainDefault) : stored),
[stored, plainDefault],
);
const resolve = useCallback(
(event: ModifierEvent): ResolvedClick => {
const tier = tierFor(event, mode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FILE_LINK_PLAIN_DEFAULT } from "./agentOutputLinks";
import { type ClickPolicy, usePolicy } from "./policy";

export function useInlineFilePolicy(): ClickPolicy {
return usePolicy("fileLinks", "file", "2-tier");
return usePolicy("fileLinks", "file", "2-tier", FILE_LINK_PLAIN_DEFAULT);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FILE_LINK_PLAIN_DEFAULT } from "./agentOutputLinks";
import { type ClickPolicy, usePolicy } from "./policy";

export function useTerminalFilePolicy(): ClickPolicy {
return usePolicy("fileLinks", "file", "4-tier");
return usePolicy("fileLinks", "file", "4-tier", FILE_LINK_PLAIN_DEFAULT);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useCallback } from "react";
import { useCallback, useMemo } from "react";
import { useUserPreferences } from "renderer/hooks/useUserPreferences";
import type { ModifierEvent } from "../types";
import { withPlainDefault } from "../tiers";
import type { ModifierEvent, TierMode } from "../types";
import { FOLDER_LINK_PLAIN_DEFAULT } from "./agentOutputLinks";
import {
type FolderIntent,
type FolderTierMap,
Expand All @@ -12,13 +14,19 @@ export interface FolderClickPolicy {
map: FolderTierMap;
}

/** Settings-driven click policy for folder links in terminal output. */
export function useTerminalFolderPolicy(): FolderClickPolicy {
/** Settings-driven click policy for folder links in terminal and chat output. */
export function useTerminalFolderPolicy(
mode: TierMode = "4-tier",
): FolderClickPolicy {
const { preferences } = useUserPreferences();
const map = preferences.folderLinks;
const stored = preferences.folderLinks;
const map = useMemo(
() => withPlainDefault(stored, FOLDER_LINK_PLAIN_DEFAULT),
[stored],
);
const getIntent = useCallback(
(event: ModifierEvent) => folderIntentForMap(event, map),
[map],
(event: ModifierEvent) => folderIntentForMap(event, map, mode),
[map, mode],
);
return { getIntent, map };
return useMemo(() => ({ getIntent, map }), [getIntent, map]);
}
18 changes: 17 additions & 1 deletion apps/desktop/src/renderer/lib/clickPolicy/tiers.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "bun:test";
import { tierFor } from "./tiers";
import { tierFor, withPlainDefault } from "./tiers";
import type { ModifierEvent } from "./types";

function event(init: Partial<ModifierEvent> = {}): ModifierEvent {
Expand Down Expand Up @@ -36,3 +36,19 @@ describe("tierFor 2-tier", () => {
);
});
});

describe("withPlainDefault", () => {
const unbound = { plain: null, shift: null, meta: "pane", metaShift: null };

it("fills an unbound plain click and leaves other tiers alone", () => {
expect(withPlainDefault(unbound, "newTab")).toEqual({
...unbound,
plain: "newTab",
});
});

it("keeps a plain click the user bound", () => {
const bound = { ...unbound, plain: "external" };
expect(withPlainDefault(bound, "newTab")).toBe(bound);
});
});
10 changes: 10 additions & 0 deletions apps/desktop/src/renderer/lib/clickPolicy/tiers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { LinkTier, ModifierEvent, TierMode } from "./types";

type TierMap<Action> = Record<LinkTier, Action | null>;

/**
* Resolve a click event to a tier under the given mode.
*
Expand All @@ -17,3 +19,11 @@ export function tierFor(event: ModifierEvent, mode: TierMode): LinkTier {
if (meta) return event.shiftKey ? "metaShift" : "meta";
return event.shiftKey ? "shift" : "plain";
}

/** A map where an unbound plain click runs `action`. Bound tiers are kept. */
export function withPlainDefault<Action>(
map: TierMap<Action>,
action: Action,
): TierMap<Action> {
return map.plain === null ? { ...map, plain: action } : map;
}
39 changes: 39 additions & 0 deletions apps/desktop/src/renderer/lib/fileLocation/fileLocation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect, test } from "bun:test";
import { splitFileLocation, splitLineAnchor } from "./fileLocation";

test("splits the line forms agents write", () => {
expect(splitFileLocation("src/app.ts")).toEqual({ path: "src/app.ts" });
expect(splitFileLocation("src/app.ts:12")).toEqual({
path: "src/app.ts",
row: 12,
});
expect(splitFileLocation("src/app.ts:12:3")).toEqual({
path: "src/app.ts",
row: 12,
col: 3,
});
expect(splitFileLocation("src/app.ts#L12C3")).toEqual({
path: "src/app.ts",
row: 12,
col: 3,
});
});

test("keeps the start of a range", () => {
expect(splitFileLocation("src/app.ts:17-33")).toEqual({
path: "src/app.ts",
row: 17,
});
expect(splitFileLocation("src/app.ts#L12-L20")).toEqual({
path: "src/app.ts",
row: 12,
});
});

test("splitLineAnchor ignores colon suffixes", () => {
expect(splitLineAnchor("src/app.ts:12")).toBeNull();
expect(splitLineAnchor("src/app.ts#L12")).toEqual({
path: "src/app.ts",
row: 12,
});
});
26 changes: 26 additions & 0 deletions apps/desktop/src/renderer/lib/fileLocation/fileLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export interface FileLocation {
path: string;
row?: number;
col?: number;
}

const LINE_ANCHOR = /#L(\d+)(?:C(\d+))?(?:-L?\d+(?:C\d+)?)?$/;
const LINE_SUFFIX = /:(\d+)(?::(\d+))?(?:-\d+(?::\d+)?)?$/;

/**
* Splits `path:12`, `path:12:3`, `path:12-20`, `path#L12` and `path#L12-L20`
* into the path and where it points. A range keeps its start.
*/
export function splitFileLocation(text: string): FileLocation {
const match = text.match(LINE_ANCHOR) ?? text.match(LINE_SUFFIX);
if (!match) return { path: text };
return {
path: text.slice(0, match.index),
row: Number(match[1]),
...(match[2] ? { col: Number(match[2]) } : {}),
};
}

export function splitLineAnchor(text: string): FileLocation | null {
return LINE_ANCHOR.test(text) ? splitFileLocation(text) : null;
}
5 changes: 5 additions & 0 deletions apps/desktop/src/renderer/lib/fileLocation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
type FileLocation,
splitFileLocation,
splitLineAnchor,
} from "./fileLocation";
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { splitFileLocation } from "renderer/lib/fileLocation";

const MAX_PATH_TEXT_LENGTH = 240;
const LOCATION_SUFFIX = /(?::\d+(?::\d+)?|#L\d+(?:C\d+)?(?:-L?\d+(?:C\d+)?)?)$/;
const FILE_NAME = /^[\w%@+().-]+$/;
const EXTENSIONLESS_FILE_NAME = /^(dockerfile|makefile|gemfile|license)$/i;
const EXTENSION = /^[a-z][a-z0-9+-]{0,11}$/i;
const NON_FILE_SCHEME = /^(?!file:\/\/)[a-z][a-z0-9+.-]*:\/\//i;

export function stripLocation(text: string): string {
return text.replace(LOCATION_SUFFIX, "");
return splitFileLocation(text).path;
}

export function pathFileName(text: string): string | null {
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/renderer/lib/terminal/links/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export { type StatCallback, TerminalLinkResolver } from "./link-resolver";

export {
type DetectedLink,
type DetectedTerminalLink,
isWebLink,
LocalLinkDetector,
} from "./local-link-detector";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
getXtermLineContent,
getXtermRangesByAttr,
} from "./buffer-helpers";
import type { DetectedLink, LocalLinkDetector } from "./local-link-detector";
import type {
DetectedTerminalLink,
LocalLinkDetector,
} from "./local-link-detector";

/** Maximum characters of context to gather around the hovered line. */
const MAX_LINK_LENGTH = 500;
Expand Down Expand Up @@ -43,9 +46,12 @@ export class LinkDetectorAdapter implements ILinkProvider {
private readonly _detector: LocalLinkDetector,
private readonly _onActivate?: (
event: MouseEvent,
link: DetectedLink,
link: DetectedTerminalLink,
) => void,
private readonly _onHover?: (
event: MouseEvent,
link: DetectedTerminalLink,
) => void,
private readonly _onHover?: (event: MouseEvent, link: DetectedLink) => void,
private readonly _onLeave?: () => void,
) {}

Expand Down Expand Up @@ -208,7 +214,7 @@ export class LinkDetectorAdapter implements ILinkProvider {
}

private _mapDetectedLinks(
detectedLinks: DetectedLink[],
detectedLinks: DetectedTerminalLink[],
lines: IBufferLine[],
cols: number,
startLine: number,
Expand Down
Loading
Loading