Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/web-config-view-defaults.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hunkdiff": patch
---

The browser review page now follows three more config options: `tab_width` sets its tab stops (it drew tabs two columns wide before, whatever the config said, and now starts at the terminal's default of 4), `auto_split_min_width` sets where `auto` layout switches between split and unified, and `sidebar` opens or closes the files sidebar when a browser first opens the review. A choice made in the page still wins in that browser. A running daemon from an older Hunk build must be restarted (`hunk daemon restart`) before a review from this build can register with it.
6 changes: 3 additions & 3 deletions docs/module-boundaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ Other workspaces are bounded units rather than one tier in that stack:
- `packages/hunk/src/web` is the browser review client, bundled separately by
`scripts/build/build-web.ts` and served by the daemon as static assets. It reaches only
`core/review` and the browser-safe session modules (`web-client-stays-separate`, plus the exact
closure in `scripts/quality/source-boundaries.test.ts`), plus two import-free presentation
closure in `scripts/quality/source-boundaries.test.ts`), plus three import-free presentation
leaves it shares with the terminal (`core/run/commandInputs.ts` for the layout vocabulary and
the auto-split threshold, `core/documents/highlightRenderOptions.ts` for Pierre's highlight
options), and the command vocabulary on a closed import set (`core/run/commandCatalog.ts`,
the auto-split threshold, `core/run/tabWidth.ts` for the default tab width,
`core/documents/highlightRenderOptions.ts` for Pierre's highlight options), and the command vocabulary on a closed import set (`core/run/commandCatalog.ts`,
`core/run/webLocalCommandCatalog.ts` for the page's own `hunk.web.*` commands, which the host
reads to resolve their `[keybindings]`, `core/run/keyChordLabels.ts`, and the chord grammar in
`extension-api/keys.ts`), and the
Expand Down
22 changes: 22 additions & 0 deletions docs/web-review-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,28 @@ imports; a page fetches only the languages and themes it shows. The largest sing
code highlights with its base Shiki theme, or GitHub's for its appearance, because Pierre
takes a highlight theme by name or as a whole Shiki theme, so the theme's `syntaxColors` and
scope overrides are not applied in the browser.
- **Config options.** `core/run/webConfigSupport.ts` classifies every key of
`CONFIG_REFERENCE_OPTIONS` in one record keyed by `ConfigOptionKey`, so a new option fails to
typecheck until the page decides about it, as the command table does for commands. A
`published` option names the presentation field it lands in; the host's `view` block must
carry exactly the published `view.*` fields (a `satisfies` check in `app/review/sharedState.ts`)
and `sharedState.test.ts` compares the built defaults with the record both ways.

| Class | Options |
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Published | `mode`, `theme`, `wrap_lines`, `line_numbers`, `tab_width`, `auto_split_min_width`, `sidebar`; `hunk_headers` is sent but not applied (header rows need the renderer) |
| Host-side | `vcs`, `watch`, `exclude_untracked`, `agent_notes` (seeds the shared note visibility) |
| Terminal-only | `cursor_line`, `scroll_off`, `file_gap`, `hunk_gap`, `wheel_scroll_lines`, `menu_bar`, `animations` (the page follows `prefers-reduced-motion`), `copy_decorations`, `prompt_save_view_preferences`, `transparent_background` |
| Deferred | `color_moved`: the marks are sent, but Pierre has no per-line decoration hook to paint them |

The page applies `tab_width` as Pierre's `--diffs-tab-size` on the review root, uses
`auto_split_min_width` as the column threshold for `auto` layout, and opens the files sidebar
for `sidebar = true`, closes it for `false`, and leaves `"auto"` to the viewport. A pager
session publishes `sidebar = false`, as the terminal opens it. Until the shared state arrives
the page uses the terminal's built-in defaults (tab width 4, threshold 160). These view fields
are required by the strict shared-state parser; `HUNK_SESSION_DAEMON_VERSION` (23) keeps the
daemon, the page it serves, and every registered window on one shape.

- **Shared filter.** Files the session's filter hides leave the stream and the sidebar, and the
planner walks the same visible stream.

Expand Down
110 changes: 110 additions & 0 deletions packages/hunk/src/app/review/sharedState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import {
reviewNoteByteLength,
} from "../../core/review/noteSize";
import { APP_COMMAND_CATALOG } from "../../core/run/commandCatalog";
import { DEFAULT_AUTO_SPLIT_MIN_WIDTH } from "../../core/run/commandInputs";
import { CONFIG_REFERENCE_OPTIONS } from "../../core/run/config";
import { DEFAULT_TAB_WIDTH } from "../../core/run/tabWidth";
import { WEB_CONFIG_SUPPORT } from "../../core/run/webConfigSupport";
import {
WEB_LOCAL_COMMAND_KEY_DEFAULTS,
WEB_LOCAL_FIXED_COMMAND_NAMES,
Expand Down Expand Up @@ -226,6 +230,9 @@ describe("buildReviewPresentationDefaults", () => {
wrapLines: true,
lineNumbers: true,
hunkHeaders: true,
tabWidth: DEFAULT_TAB_WIDTH,
autoSplitMinWidth: DEFAULT_AUTO_SPLIT_MIN_WIDTH,
sidebar: "auto",
});
expect(defaults.keymap["hunk.app.quit"]).toEqual(["ctrl+q"]);
expect(defaults.title).toBe("working tree");
Expand Down Expand Up @@ -295,6 +302,45 @@ describe("buildReviewPresentationDefaults", () => {
expect(keymap["hunk.web.toggleViewed"]).toEqual(["x"]);
});

test("publishes tab width, the auto-split width, and the sidebar as the launch resolved them", () => {
const defaults = buildReviewPresentationDefaults(
createTestSharedBootstrap([], {
input: { kind: "vcs", staged: false, options: { autoSplitMinWidth: 120 } },
initialTabWidth: 8,
initialSidebar: true,
}),
);

expect(defaults.view).toMatchObject({ tabWidth: 8, autoSplitMinWidth: 120, sidebar: true });
for (const sidebar of [false, "auto"] as const) {
expect(
buildReviewPresentationDefaults(createTestSharedBootstrap([], { initialSidebar: sidebar }))
.view.sidebar,
).toBe(sidebar);
}
});

test("falls back to the terminal's built-in defaults when the bootstrap leaves them unset", () => {
const { view } = buildReviewPresentationDefaults(createTestSharedBootstrap([]));

expect(view.tabWidth).toBe(DEFAULT_TAB_WIDTH);
expect(view.autoSplitMinWidth).toBe(DEFAULT_AUTO_SPLIT_MIN_WIDTH);
expect(view.sidebar).toBe("auto");
});

// Intent: the terminal opens a pager session with the files pane closed whatever `sidebar`
// says, so a page attached to that session starts the same way.
test("starts a pager session's sidebar closed", () => {
const { view } = buildReviewPresentationDefaults(
createTestSharedBootstrap([], {
input: { kind: "vcs", staged: false, options: { pager: true } },
initialSidebar: true,
}),
);

expect(view.sidebar).toBe(false);
});

test("carries a custom theme as resolved tokens", () => {
const defaults = buildReviewPresentationDefaults(
createTestSharedBootstrap([], {
Expand All @@ -310,6 +356,70 @@ describe("buildReviewPresentationDefaults", () => {
});
});

// Intent: every option classified `published` reaches the page, and nothing reaches the page
// that no option accounts for, so adding a view default means classifying the option behind it.
describe("WEB_CONFIG_SUPPORT against the published presentation defaults", () => {
const presentation = buildReviewPresentationDefaults(createTestSharedBootstrap([]));
const sentFields = [
"theme",
...Object.keys(presentation.view).map((field) => `view.${field}`),
].sort();
const publishedFields: string[] = Object.values(WEB_CONFIG_SUPPORT).flatMap((support) =>
support.kind === "published" ? [support.field] : [],
);

test("every published option names a field the host sends", () => {
const unsent = publishedFields.filter((field) => !sentFields.includes(field));
expect(unsent).toEqual([]);
});

test("every field the host sends is accounted for by exactly one published option", () => {
expect([...publishedFields].sort()).toEqual(sentFields);
});

test("the presentation block sends nothing config-derived beyond the theme and view", () => {
expect(Object.keys(presentation).sort()).toEqual([
"keymap",
"sourceLabel",
"theme",
"title",
"view",
]);
});

test("each published value follows the option it is classified from", () => {
const configured = buildReviewPresentationDefaults(
createTestSharedBootstrap([], {
input: { kind: "vcs", staged: false, options: { autoSplitMinWidth: 99 } },
initialMode: "unified",
initialTheme: "github-light-default",
initialWrapLines: true,
initialShowLineNumbers: false,
initialShowHunkHeaders: false,
initialTabWidth: 7,
initialSidebar: false,
}),
);
const view = configured.view as unknown as Record<string, unknown>;
const expectedByKey: Partial<Record<keyof typeof WEB_CONFIG_SUPPORT, unknown>> = {
mode: "unified",
wrap_lines: true,
line_numbers: false,
hunk_headers: false,
tab_width: 7,
auto_split_min_width: 99,
sidebar: false,
};
for (const option of CONFIG_REFERENCE_OPTIONS) {
const support = WEB_CONFIG_SUPPORT[option.key];
if (support.kind !== "published" || support.field === "theme") continue;
const sent = view[support.field.slice("view.".length)];
expect([option.key, sent]).toEqual([option.key, expectedByKey[option.key]]);
}
expect(configured.theme).toMatchObject({ id: "github-light-default" });
});
});

describe("ReviewProducer shared state", () => {
test("answers the current alias under the concrete revision and refuses a retired one", async () => {
const files = [createTestSharedFile("alpha")];
Expand Down
13 changes: 12 additions & 1 deletion packages/hunk/src/app/review/sharedState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import {
import { isRenderableStoredReviewNote } from "../../core/review/state";
import type { ReviewNote } from "../../core/review/types";
import { APP_COMMAND_CATALOG } from "../../core/run/commandCatalog";
import { DEFAULT_AUTO_SPLIT_MIN_WIDTH } from "../../core/run/commandInputs";
import { DOCUMENT_COMMAND_NAMES } from "../../core/run/documentCommandCatalog";
import { HISTORY_COMMAND_NAMES } from "../../core/run/historyCommandCatalog";
import { resolveCommandKeys } from "../../core/run/keymap";
import { STATUS_COMMAND_NAMES } from "../../core/run/statusCommandCatalog";
import { DEFAULT_TAB_WIDTH } from "../../core/run/tabWidth";
import type { WebPublishedViewField } from "../../core/run/webConfigSupport";
import {
WEB_LOCAL_COMMAND_KEY_DEFAULTS,
WEB_LOCAL_FIXED_COMMAND_NAMES,
Expand All @@ -34,6 +37,7 @@ import type {
ReviewSharedNote,
ReviewSharedState,
ReviewThemeDefaults,
ReviewViewDefaults,
} from "../../session/reviewSharedState";
import type { ReviewSessionHost } from "./reviewSessionHost";
import { indexReviewFiles } from "./reviewSessionProjections";
Expand Down Expand Up @@ -82,6 +86,10 @@ function themeDefaults(theme: AppTheme): ReviewThemeDefaults {
* fold, after the catalog, so a remap of one reaches the page and a chord the user gave one is
* taken from whatever held it by default there. Extension commands are not included: a browser
* cannot run them (F4).
*
* The view block carries exactly the options `WEB_CONFIG_SUPPORT` classifies as published to
* `view.*`, each as the terminal resolved it at launch. The files sidebar follows the terminal's
* launch rule too: a pager session opens with it closed whatever `sidebar` says.
*/
export function buildReviewPresentationDefaults(
bootstrap: AppBootstrap,
Expand Down Expand Up @@ -121,7 +129,10 @@ export function buildReviewPresentationDefaults(
wrapLines: bootstrap.initialWrapLines ?? false,
lineNumbers: bootstrap.initialShowLineNumbers ?? true,
hunkHeaders: bootstrap.initialShowHunkHeaders ?? true,
},
tabWidth: bootstrap.initialTabWidth ?? DEFAULT_TAB_WIDTH,
autoSplitMinWidth: bootstrap.input.options.autoSplitMinWidth ?? DEFAULT_AUTO_SPLIT_MIN_WIDTH,
sidebar: bootstrap.input.options.pager ? false : (bootstrap.initialSidebar ?? "auto"),
} satisfies ReviewViewDefaults & Record<WebPublishedViewField, unknown>,
keymap: Object.fromEntries(publishedIds.map((id) => [id, [...(keymap.keys.get(id) ?? [])]])),
};
}
Expand Down
15 changes: 13 additions & 2 deletions packages/hunk/src/core/run/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,11 @@ export interface ConfigReferenceOption {
/**
* Authoritative top-level preference catalog. Runtime parsing iterates this table, while the docs
* generator renders the same keys, accepted values, defaults, aliases, and descriptions.
*
* Declared as a literal table so its keys form `ConfigOptionKey`; a record keyed by that type
* (`webConfigSupport.ts`) fails to typecheck until it covers an option added here.
*/
export const CONFIG_REFERENCE_OPTIONS: readonly ConfigReferenceOption[] = [
const CONFIG_REFERENCE_OPTION_TABLE = [
{
key: "mode",
property: "mode",
Expand Down Expand Up @@ -572,7 +575,15 @@ export const CONFIG_REFERENCE_OPTIONS: readonly ConfigReferenceOption[] = [
description:
"Request moved-line coloring for Git reviews. `true` requests zebra coloring when Git has no diff.colorMoved setting; an explicit Git setting takes precedence, even when this preference is false.",
},
] as const;
] as const satisfies readonly ConfigReferenceOption[];

/** One top-level configuration key, e.g. `tab_width`. */
export type ConfigOptionKey = (typeof CONFIG_REFERENCE_OPTION_TABLE)[number]["key"];

/** The preference catalog with each entry's key narrowed to the literal key union. */
export const CONFIG_REFERENCE_OPTIONS: readonly (ConfigReferenceOption & {
readonly key: ConfigOptionKey;
})[] = CONFIG_REFERENCE_OPTION_TABLE;

/** Command-specific TOML tables accepted by the runtime resolver. */
export const CONFIG_COMMAND_SECTIONS = {
Expand Down
27 changes: 27 additions & 0 deletions packages/hunk/src/core/run/webConfigSupport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, test } from "bun:test";
import { CONFIG_REFERENCE_OPTIONS } from "./config";
import { WEB_CONFIG_SUPPORT } from "./webConfigSupport";

describe("WEB_CONFIG_SUPPORT", () => {
test("classifies exactly the options in the config reference", () => {
expect(Object.keys(WEB_CONFIG_SUPPORT).sort()).toEqual(
CONFIG_REFERENCE_OPTIONS.map((option) => option.key).sort(),
);
});

test("gives every option the page does not apply a one-sentence reason", () => {
for (const [key, support] of Object.entries(WEB_CONFIG_SUPPORT)) {
if (support.kind === "published" && support.applied) continue;
const reason = "reason" in support ? support.reason : "";
expect([key, /^[A-Z][^\n]*\.$/.test(reason)]).toEqual([key, true]);
expect([key, reason.split(/\.\s/).length]).toEqual([key, 1]);
}
});

test("lands each published option in its own presentation field", () => {
const fields = Object.values(WEB_CONFIG_SUPPORT).flatMap((support) =>
support.kind === "published" ? [support.field] : [],
);
expect(new Set(fields).size).toBe(fields.length);
});
});
Loading
Loading