wsb-daemon 0.275.7622

wsb's daemon: the workbench, the GitHub mirror's schedule and peer continuity, for every workspace on the machine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<script lang="ts">
  import { onMount } from "svelte";
  import { contextMenu as placeMenu } from "../../utils/menu.js";
  import MonacoEditor from "../layout/MonacoEditor.svelte";
  import MarkdownFileViewer from "./MarkdownFileViewer.svelte";
  import AppIcon from "../layout/AppIcon.svelte";
  import { explorer as explorerStore } from "../../stores/explorer.svelte.js";
  import { workbench } from "../../stores/workbench.svelte.js";
  import { notifications } from "../../stores/notifications.svelte.js";
  import { scoped, viewWorktree } from "../../stores/worktree-scope.svelte.js";
  const explorer = scoped(explorerStore);

  interface Props {
    path: string;
    /** The line to open at, when whoever opened this named one. */
    line?: number | null;
    column?: number | null;
  }

  let { path, line = null, column = null }: Props = $props();
  /** The worktree this tab's file is of, as it was when the tab opened. */
  const worktree = viewWorktree("the file editor");
  let file = $derived(explorer.files.get(path));
  let unsupportedContent = $derived(isUnsupportedTextContentError(file?.error));
  let editorContext = $derived(explorer.editorContextForPath(path));
  let titleMenuItems = $derived(explorer.menuCommands("editor/title", editorContext));
  let contextMenuItems = $derived(explorer.menuCommands("editor/context", editorContext));
  let isMarkdown = $derived.by(() => {
    return explorer.languageIdForPath(path) === "markdown";
  });
  let isDirty = $derived(file?.editedContent !== null && file?.editedContent !== file?.content);
  let contextMenu = $state<{ x: number; y: number } | null>(null);

  function isUnsupportedTextContentError(error: string | null | undefined): boolean {
    if (!error) {
      return false;
    }
    return error.includes("is not valid UTF-8 text");
  }

  onMount(() => {
    void explorer.ensureWorkbenchModel();
  });

  async function handleSave() {
    try {
      await explorer.saveFile(path);
    } catch (error) {
      notifications.upsert(
        `file-save:${path}`,
        "error",
        `Failed to save ${path.split("/").pop() ?? path}`,
        error instanceof Error ? error.message : String(error),
        "explorer",
      );
    }
  }

  async function handleLoadFromDisk() {
    try {
      await explorer.loadFile(path);
    } catch (error) {
      notifications.upsert(
        `file-reload:${path}`,
        "error",
        `Failed to reload ${path.split("/").pop() ?? path}`,
        error instanceof Error ? error.message : String(error),
        "explorer",
      );
    }
  }

  async function handleOverwriteDisk() {
    const file = explorer.files.get(path);
    if (!file) return;
    // Force-save by updating the expected mtime to the disk mtime so the
    // server accepts the write even though the disk changed.
    // We do this by reloading the mtime from a fresh HEAD then saving.
    // The simplest approach: temporarily clear diskChanged and force-save.
    // The backend's mtime check prevents races — if the disk changed *again*
    // between our reload and this save, the server returns 409 and we surface it.
    try {
      // Get the current on-disk mtime so our expected_mtime matches.
      const fresh = await explorer.loadFile(path);
      // Restore edited content after reload.
      if (file.editedContent !== null) {
        explorer.setEditedContent(path, file.editedContent);
      }
      await explorer.saveFile(path);
    } catch (error) {
      notifications.upsert(
        `file-overwrite:${path}`,
        "error",
        `Failed to overwrite ${path.split("/").pop() ?? path}`,
        error instanceof Error ? error.message : String(error),
        "explorer",
      );
    }
  }

  async function openContributedCommand(commandId: string) {
    const command = explorer.commandById(commandId);
    try {
      const execution = await explorer.executeCommand(commandId);
      const panel = execution.panels[0];
      if (!panel) {
        throw new Error(`extension command '${commandId}' completed without creating a supported workbench panel`);
      }
      workbench.openWebviewPanel(command.extension_id, panel, worktree);
    } catch (error) {
      notifications.upsert(
        `extension-command:${commandId}`,
        "error",
        `Extension command failed`,
        error instanceof Error ? error.message : String(error),
        "extensions",
      );
    } finally {
      contextMenu = null;
    }
  }

  function handleEditorContextMenu(event: MouseEvent) {
    if (contextMenuItems.length === 0) {
      return;
    }
    event.preventDefault();
    contextMenu = { x: event.clientX, y: event.clientY };
  }

  function handleKeyDown(event: KeyboardEvent) {
    if ((event.metaKey || event.ctrlKey) && event.key === "s") {
      event.preventDefault();
      void handleSave();
    }
  }

  $effect(() => {
    if (!contextMenu) return;
    function handleDismiss() {
      contextMenu = null;
    }
    const id = setTimeout(() => {
      document.addEventListener("click", handleDismiss, { once: true });
      document.addEventListener("contextmenu", handleDismiss, { once: true });
    }, 0);
    return () => {
      clearTimeout(id);
      document.removeEventListener("click", handleDismiss);
      document.removeEventListener("contextmenu", handleDismiss);
    };
  });

  $effect(() => {
    if (!contextMenu) return;
    function handleEscape(event: KeyboardEvent) {
      if (event.key === "Escape") {
        contextMenu = null;
      }
    }
    document.addEventListener("keydown", handleEscape);
    return () => document.removeEventListener("keydown", handleEscape);
  });
</script>

<!-- svelte-ignore a11y_no_static_element_interactions -->
<div class="worktree-file-editor" onkeydown={handleKeyDown}>
  <div class="file-toolbar">
    <div class="file-toolbar-title">
      {path.split("/").pop() ?? path}
      {#if isDirty}<span class="dirty-dot" title="Unsaved changes"></span>{/if}
    </div>
    <div class="file-toolbar-actions">
      {#if isDirty}
        <button
          class="file-toolbar-action save-icon-btn"
          onclick={handleSave}
          disabled={file?.saving}
          title={file?.saving ? "Saving…" : "Save (⌘S)"}
          aria-label="Save"
        >
          <AppIcon name="save" size={14} />
        </button>
      {/if}
      {#each titleMenuItems as item (`${item.command}:${item.group ?? ""}`)}
        <button class="file-toolbar-action" onclick={() => openContributedCommand(item.command)}>
          {item.title}
        </button>
      {/each}
    </div>
  </div>

  {#if file?.diskChanged}
    <div class="disk-changed-banner">
      <AppIcon name="triangle-alert" size={14} />
      <span>File changed on disk</span>
      <button class="disk-action" onclick={handleLoadFromDisk}>Load from disk</button>
      {#if isDirty}
        <button class="disk-action disk-action-overwrite" onclick={handleOverwriteDisk}>Overwrite disk</button>
      {/if}
    </div>
  {/if}

  {#if file?.loading && !file.content}
    <div class="file-state">Loading file...</div>
  {:else if unsupportedContent}
    <div class="file-unsupported">
      <div class="file-unsupported-icon">
        <AppIcon name="file-x" size={20} />
      </div>
      <span class="file-unsupported-label">Binary file</span>
      <span class="file-unsupported-sub">This file cannot be displayed in the text editor.</span>
    </div>
  {:else if file?.error}
    <div class="file-error">{file.error}</div>
  {:else if file?.content !== null}
    {#if isMarkdown}
      <MarkdownFileViewer
        content={file?.editedContent ?? file?.content ?? ""}
        onchange={(value) => explorer.setEditedContent(path, value)}
      />
    {:else}
      <div
        class="file-editor-shell"
        role="application"
        aria-label={`Editor for ${path}`}
        oncontextmenu={handleEditorContextMenu}
      >
        <MonacoEditor
          value={file?.editedContent ?? file?.content ?? ""}
          language={explorer.languageIdForPath(path)}
          readOnly={false}
          onchange={(value) => explorer.setEditedContent(path, value)}
          revealLine={line}
          revealColumn={column}
          height="100%"
          file={{ worktree, path }}
        />
      </div>
    {/if}
  {:else}
    <div class="file-state">Select a file from the explorer.</div>
  {/if}
</div>

{#if contextMenu}
  <div
    class="file-context-menu"
    use:placeMenu={contextMenu}
  >
    {#each contextMenuItems as item (`${item.command}:${item.group ?? ""}`)}
      <button class="file-context-menu-item" onclick={() => openContributedCommand(item.command)}>
        {item.title}
      </button>
    {/each}
  </div>
{/if}

<style>
  .worktree-file-editor {
    height: 100%;
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--vscode-tab-activeBackground);
  }
  .file-toolbar {
    height: var(--wb-control-size-40);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--wb-spacing-12);
    padding: 0 var(--wb-spacing-12);
    border-bottom: 1px solid var(--vscode-panel-border);
    background: color-mix(in srgb, var(--vscode-sideBar-background) 96%, black 4%);
    flex-shrink: 0;
  }
  .file-toolbar-title {
    min-width: 0;
    font-size: var(--wb-font-size-12);
    font-weight: 600;
    color: var(--vscode-editor-foreground);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    gap: var(--wb-spacing-6);
  }
  .dirty-dot {
    width: var(--wb-control-size-8);
    height: var(--wb-control-size-8);
    border-radius: 50%;
    background: var(--vscode-gitDecoration-modifiedResourceForeground, #e2c08d);
    flex-shrink: 0;
  }
  .save-icon-btn {
    width: var(--wb-control-size-26);
    height: var(--wb-control-size-26);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border-radius: var(--wsb-radius-sm);
    border: 1px solid transparent;
    background: transparent;
    color: var(--vscode-gitDecoration-modifiedResourceForeground, #e2c08d);
  }
  .save-icon-btn:hover:not(:disabled) {
    background: var(--vscode-list-hoverBackground);
    border-color: var(--vscode-widget-border);
  }
  .save-icon-btn:disabled {
    opacity: 0.4;
    cursor: default;
  }
  .disk-changed-banner {
    display: flex;
    align-items: center;
    gap: var(--wb-spacing-8);
    padding: var(--wb-spacing-6) var(--wb-spacing-12);
    font-size: var(--wb-font-size-12);
    background: color-mix(in srgb, var(--vscode-inputValidation-warningBackground, #352a05) 80%, transparent 20%);
    border-bottom: 1px solid var(--vscode-inputValidation-warningBorder, #b89500);
    color: var(--vscode-inputValidation-warningForeground, var(--vscode-editor-foreground));
    flex-shrink: 0;
  }
  .disk-action {
    padding: var(--wb-spacing-3) var(--wb-spacing-8);
    border-radius: var(--wsb-radius-sm);
    border: 1px solid currentColor;
    background: transparent;
    color: inherit;
    font-size: var(--wb-font-size-11);
    cursor: pointer;
  }
  .disk-action:hover {
    background: color-mix(in srgb, currentColor 15%, transparent 85%);
  }
  .disk-action-overwrite {
    color: var(--vscode-errorForeground);
    border-color: var(--vscode-errorForeground);
  }
  .file-toolbar-actions {
    display: flex;
    align-items: center;
    gap: var(--wb-spacing-8);
  }
  .file-toolbar-action {
    padding: var(--wb-spacing-6) var(--wb-spacing-10);
    border-radius: var(--wsb-radius-sm);
    border: 1px solid var(--vscode-panel-border);
    background: var(--vscode-tab-activeBackground);
    color: var(--vscode-editor-foreground);
    font-size: var(--wb-font-size-12);
  }
  .file-toolbar-action:hover {
    background: var(--vscode-list-hoverBackground);
  }
  .file-state,
  .file-error {
    padding: var(--wb-spacing-16);
    font-size: var(--wb-font-size-12);
  }
  .file-state {
    color: var(--vscode-disabledForeground);
  }
  .file-unsupported {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--wb-spacing-8);
  }
  .file-unsupported-icon {
    width: var(--wb-control-size-36);
    height: var(--wb-control-size-36);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--wb-corner-radius-8);
    background: var(--vscode-sideBar-background);
    border: 1px solid var(--vscode-widget-border);
    color: var(--vscode-disabledForeground);
    margin-bottom: var(--wb-spacing-4);
  }
  .file-unsupported-label {
    font-size: var(--wb-font-size-13);
    font-weight: 500;
    color: var(--vscode-foreground);
  }
  .file-unsupported-sub {
    font-size: var(--wb-font-size-12);
    color: var(--vscode-disabledForeground);
  }
  .file-error {
    color: var(--vscode-errorForeground);
  }
  .file-editor-shell {
    flex: 1;
    min-height: 0;
    position: relative;
  }
  .file-context-menu {
    position: fixed;
    min-width: 220px;
    padding: var(--wb-spacing-6);
    display: flex;
    flex-direction: column;
    gap: var(--wb-spacing-4);
    border-radius: var(--wsb-radius-md);
    border: 1px solid var(--vscode-panel-border);
    background: color-mix(in srgb, var(--vscode-sideBar-background) 97%, black 3%);
    box-shadow: var(--wb-shadow-md);
    z-index: 70;
  }
  .file-context-menu-item {
    width: 100%;
    padding: var(--wb-spacing-8) var(--wb-spacing-10);
    text-align: left;
    border-radius: var(--wsb-radius-sm);
    color: var(--vscode-editor-foreground);
    font-size: var(--wb-font-size-12);
  }
  .file-context-menu-item:hover {
    background: var(--vscode-list-hoverBackground);
  }
  :global(.worktree-file-editor .monaco-shell) {
    flex: 1;
    min-height: 0;
    border: none;
    border-radius: 0;
  }
</style>