Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b15f55eccd | ||
|
|
dca0b9fb33 | ||
|
|
4437965135 | ||
|
|
5f94bfd5fe | ||
|
|
718a682782 |
No files matched your search
@@ -91,6 +91,14 @@ describe("server keybindings", () => {
|
||||
when: "terminalFocus && (",
|
||||
}),
|
||||
);
|
||||
|
||||
assert.isNull(
|
||||
compileResolvedKeybindingRule({
|
||||
key: "mod+d",
|
||||
command: "terminal.split",
|
||||
when: `${"!".repeat(300)}terminalFocus`,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("upserts custom keybindings to ~/.t3/keybindings.json", () => {
|
||||
|
||||
@@ -24,6 +24,8 @@ type WhenToken =
|
||||
| { type: "lparen" }
|
||||
| { type: "rparen" };
|
||||
|
||||
const MAX_WHEN_EXPRESSION_DEPTH = 256;
|
||||
|
||||
export const DEFAULT_KEYBINDINGS: ReadonlyArray<KeybindingRule> = [
|
||||
{ key: "mod+j", command: "terminal.toggle" },
|
||||
{ key: "mod+d", command: "terminal.split", when: "terminalFocus" },
|
||||
@@ -159,7 +161,10 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
|
||||
if (!tokens || tokens.length === 0) return null;
|
||||
let index = 0;
|
||||
|
||||
const parsePrimary = (): KeybindingWhenNode | null => {
|
||||
const parsePrimary = (depth: number): KeybindingWhenNode | null => {
|
||||
if (depth > MAX_WHEN_EXPRESSION_DEPTH) {
|
||||
return null;
|
||||
}
|
||||
const token = tokens[index];
|
||||
if (!token) return null;
|
||||
|
||||
@@ -170,7 +175,7 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
|
||||
|
||||
if (token.type === "lparen") {
|
||||
index += 1;
|
||||
const expressionNode = parseOr();
|
||||
const expressionNode = parseOr(depth + 1);
|
||||
const closeToken = tokens[index];
|
||||
if (!expressionNode || !closeToken || closeToken.type !== "rparen") {
|
||||
return null;
|
||||
@@ -182,24 +187,34 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
|
||||
return null;
|
||||
};
|
||||
|
||||
const parseUnary = (): KeybindingWhenNode | null => {
|
||||
const token = tokens[index];
|
||||
if (token?.type === "not") {
|
||||
const parseUnary = (depth: number): KeybindingWhenNode | null => {
|
||||
let notCount = 0;
|
||||
while (tokens[index]?.type === "not") {
|
||||
index += 1;
|
||||
const node = parseUnary();
|
||||
if (!node) return null;
|
||||
return { type: "not", node };
|
||||
notCount += 1;
|
||||
if (notCount > MAX_WHEN_EXPRESSION_DEPTH) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return parsePrimary();
|
||||
|
||||
let node = parsePrimary(depth);
|
||||
if (!node) return null;
|
||||
|
||||
while (notCount > 0) {
|
||||
node = { type: "not", node };
|
||||
notCount -= 1;
|
||||
}
|
||||
|
||||
return node;
|
||||
};
|
||||
|
||||
const parseAnd = (): KeybindingWhenNode | null => {
|
||||
let left = parseUnary();
|
||||
const parseAnd = (depth: number): KeybindingWhenNode | null => {
|
||||
let left = parseUnary(depth);
|
||||
if (!left) return null;
|
||||
|
||||
while (tokens[index]?.type === "and") {
|
||||
index += 1;
|
||||
const right = parseUnary();
|
||||
const right = parseUnary(depth);
|
||||
if (!right) return null;
|
||||
left = { type: "and", left, right };
|
||||
}
|
||||
@@ -207,13 +222,13 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
|
||||
return left;
|
||||
};
|
||||
|
||||
const parseOr = (): KeybindingWhenNode | null => {
|
||||
let left = parseAnd();
|
||||
const parseOr = (depth: number): KeybindingWhenNode | null => {
|
||||
let left = parseAnd(depth);
|
||||
if (!left) return null;
|
||||
|
||||
while (tokens[index]?.type === "or") {
|
||||
index += 1;
|
||||
const right = parseAnd();
|
||||
const right = parseAnd(depth);
|
||||
if (!right) return null;
|
||||
left = { type: "or", left, right };
|
||||
}
|
||||
@@ -221,7 +236,7 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
|
||||
return left;
|
||||
};
|
||||
|
||||
const ast = parseOr();
|
||||
const ast = parseOr(0);
|
||||
if (!ast || index !== tokens.length) return null;
|
||||
return ast;
|
||||
}
|
||||
@@ -328,6 +343,12 @@ function loadCustomKeybindingsConfig(
|
||||
return [];
|
||||
}
|
||||
if (options?.throwOnUnreadableConfig) {
|
||||
if (
|
||||
error instanceof Error &&
|
||||
error.message.startsWith("Unable to parse keybindings config at ")
|
||||
) {
|
||||
throw error;
|
||||
}
|
||||
throw new Error(
|
||||
`Unable to parse keybindings config at ${configPath}: ${
|
||||
error instanceof Error ? error.message : String(error)
|
||||
|
||||
@@ -739,6 +739,72 @@ export default function ChatView() {
|
||||
},
|
||||
[activeProject, activeThread, activeThreadId, api, dispatch, gitCwd],
|
||||
);
|
||||
const persistProjectScripts = useCallback(
|
||||
async (input: {
|
||||
projectId: string;
|
||||
projectCwd: string;
|
||||
previousScripts: ProjectScript[];
|
||||
nextScripts: ProjectScript[];
|
||||
keybinding?: string | null;
|
||||
keybindingCommand: string;
|
||||
}) => {
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
projectId: input.projectId,
|
||||
scripts: input.nextScripts,
|
||||
});
|
||||
|
||||
if (!isElectron || !api) return;
|
||||
|
||||
let scriptsPersisted = false;
|
||||
try {
|
||||
const updated = await api.projects.updateScripts({
|
||||
id: input.projectId,
|
||||
scripts: input.nextScripts,
|
||||
});
|
||||
scriptsPersisted = true;
|
||||
|
||||
if (input.keybinding) {
|
||||
const keybindingUpdate = await api.server.upsertKeybinding({
|
||||
key: input.keybinding,
|
||||
command: input.keybindingCommand,
|
||||
});
|
||||
queryClient.setQueryData(
|
||||
serverQueryKeys.config(),
|
||||
(current: { cwd: string; keybindings: ResolvedKeybindingsConfig } | undefined) =>
|
||||
current
|
||||
? { ...current, keybindings: keybindingUpdate.keybindings }
|
||||
: {
|
||||
cwd: input.projectCwd,
|
||||
keybindings: keybindingUpdate.keybindings,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
projectId: updated.project.id,
|
||||
scripts: updated.project.scripts,
|
||||
});
|
||||
} catch (error) {
|
||||
if (scriptsPersisted) {
|
||||
await api.projects
|
||||
.updateScripts({
|
||||
id: input.projectId,
|
||||
scripts: input.previousScripts,
|
||||
})
|
||||
.catch(() => undefined);
|
||||
}
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
projectId: input.projectId,
|
||||
scripts: input.previousScripts,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[api, dispatch, queryClient],
|
||||
);
|
||||
const saveProjectScript = useCallback(
|
||||
async (input: NewProjectScriptInput) => {
|
||||
if (!activeProject) return;
|
||||
@@ -762,61 +828,16 @@ export default function ChatView() {
|
||||
]
|
||||
: [...activeProject.scripts, nextScript];
|
||||
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
await persistProjectScripts({
|
||||
projectId: activeProject.id,
|
||||
scripts: nextScripts,
|
||||
projectCwd: activeProject.cwd,
|
||||
previousScripts: activeProject.scripts,
|
||||
nextScripts,
|
||||
keybinding: input.keybinding,
|
||||
keybindingCommand: commandForProjectScript(nextId),
|
||||
});
|
||||
|
||||
if (!isElectron || !api) return;
|
||||
let scriptsPersisted = false;
|
||||
try {
|
||||
const updated = await api.projects.updateScripts({
|
||||
id: activeProject.id,
|
||||
scripts: nextScripts,
|
||||
});
|
||||
scriptsPersisted = true;
|
||||
|
||||
if (input.keybinding) {
|
||||
const keybindingUpdate = await api.server.upsertKeybinding({
|
||||
key: input.keybinding,
|
||||
command: commandForProjectScript(nextId),
|
||||
});
|
||||
queryClient.setQueryData(
|
||||
serverQueryKeys.config(),
|
||||
(current: { cwd: string; keybindings: ResolvedKeybindingsConfig } | undefined) =>
|
||||
current
|
||||
? { ...current, keybindings: keybindingUpdate.keybindings }
|
||||
: {
|
||||
cwd: activeProject.cwd,
|
||||
keybindings: keybindingUpdate.keybindings,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
projectId: updated.project.id,
|
||||
scripts: updated.project.scripts,
|
||||
});
|
||||
} catch (error) {
|
||||
if (scriptsPersisted) {
|
||||
await api.projects
|
||||
.updateScripts({
|
||||
id: activeProject.id,
|
||||
scripts: activeProject.scripts,
|
||||
})
|
||||
.catch(() => undefined);
|
||||
}
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
projectId: activeProject.id,
|
||||
scripts: activeProject.scripts,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[activeProject, api, dispatch, queryClient],
|
||||
[activeProject, persistProjectScripts],
|
||||
);
|
||||
const updateProjectScript = useCallback(
|
||||
async (scriptId: string, input: NewProjectScriptInput) => {
|
||||
@@ -841,61 +862,16 @@ export default function ChatView() {
|
||||
: script,
|
||||
);
|
||||
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
await persistProjectScripts({
|
||||
projectId: activeProject.id,
|
||||
scripts: nextScripts,
|
||||
projectCwd: activeProject.cwd,
|
||||
previousScripts: activeProject.scripts,
|
||||
nextScripts,
|
||||
keybinding: input.keybinding,
|
||||
keybindingCommand: commandForProjectScript(scriptId),
|
||||
});
|
||||
|
||||
if (!isElectron || !api) return;
|
||||
let scriptsPersisted = false;
|
||||
try {
|
||||
const updated = await api.projects.updateScripts({
|
||||
id: activeProject.id,
|
||||
scripts: nextScripts,
|
||||
});
|
||||
scriptsPersisted = true;
|
||||
|
||||
if (input.keybinding) {
|
||||
const keybindingUpdate = await api.server.upsertKeybinding({
|
||||
key: input.keybinding,
|
||||
command: commandForProjectScript(scriptId),
|
||||
});
|
||||
queryClient.setQueryData(
|
||||
serverQueryKeys.config(),
|
||||
(current: { cwd: string; keybindings: ResolvedKeybindingsConfig } | undefined) =>
|
||||
current
|
||||
? { ...current, keybindings: keybindingUpdate.keybindings }
|
||||
: {
|
||||
cwd: activeProject.cwd,
|
||||
keybindings: keybindingUpdate.keybindings,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
projectId: updated.project.id,
|
||||
scripts: updated.project.scripts,
|
||||
});
|
||||
} catch (error) {
|
||||
if (scriptsPersisted) {
|
||||
await api.projects
|
||||
.updateScripts({
|
||||
id: activeProject.id,
|
||||
scripts: activeProject.scripts,
|
||||
})
|
||||
.catch(() => undefined);
|
||||
}
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
projectId: activeProject.id,
|
||||
scripts: activeProject.scripts,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[activeProject, api, dispatch, queryClient],
|
||||
[activeProject, persistProjectScripts],
|
||||
);
|
||||
|
||||
const handleRuntimeModeChange = async (mode: "approval-required" | "full-access") => {
|
||||
|
||||
@@ -292,7 +292,7 @@ export default function ProjectScriptsControl({
|
||||
>
|
||||
<ScriptIcon icon={script.icon} className="size-4" />
|
||||
<span className="truncate">
|
||||
{script.runOnWorktreeCreate ? `${script.name}` : script.name}
|
||||
{script.runOnWorktreeCreate ? `${script.name} (setup)` : script.name}
|
||||
</span>
|
||||
<span className="relative ms-auto flex h-6 min-w-6 items-center justify-end">
|
||||
{shortcutLabel && (
|
||||
|
||||
@@ -40,7 +40,7 @@ describe("projectScripts helpers", () => {
|
||||
{ T3CODE_PROJECT_ROOT: '%USERPROFILE%^"repo"' },
|
||||
"Win32",
|
||||
);
|
||||
expect(command).toBe('set "T3CODE_PROJECT_ROOT=%%USERPROFILE%%^^""repo""" && bun install');
|
||||
expect(command).toBe('set "T3CODE_PROJECT_ROOT=^%USERPROFILE^%^^""repo""" && bun install');
|
||||
});
|
||||
|
||||
it("resolves primary and setup scripts", () => {
|
||||
|
||||
@@ -48,6 +48,7 @@ export function nextProjectScriptId(name: string, existingIds: Iterable<string>)
|
||||
suffix += 1;
|
||||
}
|
||||
|
||||
// This last-resort fallback only triggers after exhausting thousands of suffixes.
|
||||
return `${baseId}-${Date.now()}`.slice(0, MAX_SCRIPT_ID_LENGTH);
|
||||
}
|
||||
|
||||
@@ -56,7 +57,7 @@ function shellEscapePosix(value: string): string {
|
||||
}
|
||||
|
||||
function shellEscapeWindowsSetValue(value: string): string {
|
||||
return value.replace(/\^/g, "^^").replace(/%/g, "%%").replace(/"/g, '""');
|
||||
return value.replace(/\^/g, "^^").replace(/%/g, "^%").replace(/"/g, '""');
|
||||
}
|
||||
|
||||
export function injectEnvIntoShellCommand(
|
||||
|
||||
Reference in new issue
Block a user