heyquilt 0.3.174 → 0.3.176

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +6 -3
  2. package/RELEASES.md +30 -0
  3. package/bin/quilt.js +1 -75
  4. package/desktop/main.js +22 -9
  5. package/package.json +2 -3
  6. package/src/ai-vm.js +1 -1
  7. package/src/api/memory-store.js +1 -5
  8. package/src/api/server.js +4 -45
  9. package/src/api/supabase-store.js +0 -10
  10. package/src/autowork.js +7 -142
  11. package/src/base-pick.js +125 -12
  12. package/src/branch-view.js +1 -1
  13. package/src/connection.js +12 -37
  14. package/src/control.js +16 -93
  15. package/src/fsutil.js +1 -1
  16. package/src/git-aside.js +70 -0
  17. package/src/github-commit.js +1 -2
  18. package/src/gitstate.js +326 -0
  19. package/src/held.js +12 -53
  20. package/src/history.js +1 -25
  21. package/src/live-merge.js +42 -161
  22. package/src/mcp.js +6 -90
  23. package/src/merge3.js +21 -5
  24. package/src/pathrules.js +1 -1
  25. package/src/protocol.js +6 -11
  26. package/src/read-bases.js +3 -5
  27. package/src/relay-live-write.js +37 -44
  28. package/src/relay-mcp.js +30 -164
  29. package/src/relay-prune.js +3 -6
  30. package/src/relay-turn-sync.js +7 -11
  31. package/src/runner.js +2 -4
  32. package/src/server.js +61 -1717
  33. package/src/session.js +895 -844
  34. package/src/setup.js +11 -3
  35. package/src/status.js +2 -5
  36. package/src/swap-in.js +56 -13
  37. package/src/turn-sync-plan.js +2 -2
  38. package/src/turn-sync.js +1 -1
  39. package/src/ui/agent-guide.js +3 -1
  40. package/src/ui/app.css +4 -96
  41. package/src/ui/app.js +1 -1
  42. package/src/ui/chat.js +2 -6
  43. package/src/ui/cloud-ai-model.js +0 -13
  44. package/src/ui/cloud-ai.js +2 -10
  45. package/src/ui/connections.js +2 -1
  46. package/src/ui/fileview.js +7 -20
  47. package/src/ui/git-rule.js +2 -13
  48. package/src/ui/home.js +2 -8
  49. package/src/ui/session-words.js +27 -0
  50. package/src/ui/session.js +34 -407
  51. package/src/ui-server.js +7 -56
  52. package/src/api/routes/seam-connect.js +0 -103
  53. package/src/api/routes/session-migrate.js +0 -39
  54. package/src/force-date.js +0 -27
  55. package/src/quilt-default.js +0 -84
  56. package/src/quilt-history.js +0 -106
  57. package/src/quilt-remote.js +0 -78
  58. package/src/seam-access.js +0 -97
  59. package/src/seam-url.js +0 -15
  60. package/src/session-follow.js +0 -583
  61. package/src/session-layout3.js +0 -466
  62. package/src/session-migrate.js +0 -123
  63. package/src/session-quilt.js +0 -126
  64. package/src/session-seam.js +0 -312
  65. package/src/source-live/bring-in.js +0 -391
  66. package/src/source-live/land.js +0 -410
  67. package/src/source-live/live-block.js +0 -1166
  68. package/src/source-live/live-square.js +0 -116
  69. package/src/ui/force-words.js +0 -19
  70. package/src/ui/quiltview.js +0 -213
  71. package/src/ui/seam-model.js +0 -309
@@ -1,116 +0,0 @@
1
- // Quilt's side of seam: live squares in a Quilt session. Seam (src/quilts/) never imports this.
2
- // A square's live document on the relay (Seam's plans/quilt-source-control/05, "The square's live
3
- // document and the block's mirror"). It is one of the relay's branch documents (branchdocs.js),
4
- // keyed `quilt/<square id>` instead of a git ref, so the store, the WebSocket protocol, saving,
5
- // unloading and size limits are the session's own. What it holds is the square's overlay: only
6
- // the paths changed since the square's head, not every file.
7
- //
8
- // files: Y.Map<path, Y.Text> text up to 2 MiB, typed live
9
- // blobs: Y.Map<path, { blob, size, mode }> binaries and big text, bytes already uploaded
10
- // gone: Y.Map<path, { by, ts }> deleted since head
11
- // modes: Y.Map<path, 'exec' | 'symlink'> a text entry's mode when it is not a plain file
12
- // clean: Y.Map<path, { at }> back to head's bytes; only the relay drops it
13
- // live: Y.Map head -> { commit, by, ts } the commit the overlay is relative to
14
- // writers: Y.Map<path, { by, ts }> who last wrote each path of the overlay (whose box it is on going back)
15
- //
16
- // Clients only add entries or mark them clean (05: a client deleting a Y.Text would lose a
17
- // concurrent insert into it). The relay removes entries, in `seal`: after a commit was made
18
- // from the overlay, one transaction moves `head` and drops every entry whose content is the one
19
- // that was committed. An entry typed into after the commit read it stays, against the new head.
20
- import * as Y from 'yjs'
21
- import { blobId } from '@heyquilt/seam/lib/quilts/records.js'
22
-
23
- export const LIVE_PREFIX = 'quilt/'
24
- export const liveKey = (squareId) => `${LIVE_PREFIX}${squareId}`
25
- // A square's live document is quilt/<square id>; an agent's task branch is quilt/<agent>/<task>, so
26
- // only a square id (a UUID) after the prefix makes a live key.
27
- const LIVE_KEY = /^quilt\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
28
- export const isLiveKey = (key) => typeof key === 'string' && LIVE_KEY.test(key)
29
-
30
- export const liveMaps = (doc) => ({
31
- files: doc.getMap('files'), blobs: doc.getMap('blobs'), gone: doc.getMap('gone'),
32
- modes: doc.getMap('modes'), clean: doc.getMap('clean'), live: doc.getMap('live'), writers: doc.getMap('writers')
33
- })
34
-
35
- /** Who last wrote each path of the overlay: path -> name (the member's, as its block wrote it). */
36
- export function lastWriters (doc) {
37
- const out = new Map()
38
- for (const [p, w] of liveMaps(doc).writers) if (w && typeof w.by === 'string' && w.by) out.set(p, w.by)
39
- return out
40
- }
41
-
42
- /** The commit the overlay is relative to (null before anyone set it). */
43
- export const liveHead = (doc) => (doc.getMap('live').get('head') || {}).commit || null
44
-
45
- /** What a path holds in the overlay: { op: 'put', bytes, mode } | { op: 'put', blob, size, mode } | { op: 'delete' } | null. */
46
- export function entryOf (doc, p) {
47
- const m = liveMaps(doc)
48
- if (m.clean.has(p)) return null
49
- if (m.gone.has(p)) return { op: 'delete' }
50
- if (m.blobs.has(p)) { const b = m.blobs.get(p); return { op: 'put', blob: b.blob, size: b.size, mode: b.mode || 'file' } }
51
- if (m.files.has(p)) return { op: 'put', bytes: Buffer.from(m.files.get(p).toString(), 'utf8'), mode: m.modes.get(p) || 'file' }
52
- return null
53
- }
54
-
55
- /** The id an entry seals to (null for a deletion): what `seal` compares. */
56
- export const entryId = (e) => !e || e.op === 'delete' ? null : e.blob || blobId(e.bytes)
57
-
58
- /** Every path in the overlay that is not clean, sorted, with its entry. O(uncommitted work). */
59
- export function overlay (doc) {
60
- const m = liveMaps(doc)
61
- const paths = new Set([...m.files.keys(), ...m.blobs.keys(), ...m.gone.keys()])
62
- const out = []
63
- for (const p of [...paths].sort()) { const e = entryOf(doc, p); if (e) out.push({ path: p, ...e }) }
64
- return out
65
- }
66
-
67
- /**
68
- * The relay's half of a commit (05, 07 "seal"). `prev` must be the document's head (a
69
- * compare-and-swap, so two seals of one head leave one), `head` the commit made from the
70
- * overlay, `sealed` path -> the id committed for it (null for a deletion). In one transaction:
71
- * the head moves and every entry that still holds what was committed leaves, as do clean marks
72
- * (a clean path equals the old head and the commit did not touch it, so it equals the new one).
73
- * Returns { ok, dropped, kept } or throws when the head moved.
74
- */
75
- export function seal (doc, { prev = null, head, sealed = {}, by = '' }) {
76
- if (typeof head !== 'string' || !/^[0-9a-f]{64}$/.test(head)) throw new Error('seal needs the new head commit id')
77
- const cur = liveHead(doc)
78
- if (cur !== (prev || null)) throw new Error(`the square's live head is ${cur ? cur.slice(0, 12) : 'not set'}, not ${prev ? prev.slice(0, 12) : 'empty'}: read it again`)
79
- const m = liveMaps(doc)
80
- const dropped = []
81
- const kept = []
82
- doc.transact(() => {
83
- for (const [p, id] of Object.entries(sealed)) {
84
- const e = entryOf(doc, p)
85
- const now = e ? entryId(e) : undefined
86
- if (e && now === (id ?? null)) { for (const k of ['files', 'blobs', 'gone', 'modes', 'clean', 'writers']) m[k].delete(p); dropped.push(p) } else kept.push(p)
87
- }
88
- for (const p of [...m.clean.keys()]) { if (!(p in sealed)) { for (const k of ['files', 'blobs', 'gone', 'modes', 'clean', 'writers']) m[k].delete(p); dropped.push(p) } }
89
- m.live.set('head', { commit: head, by, ts: Date.now() })
90
- }, 'seal')
91
- return { ok: true, head, dropped, kept }
92
- }
93
-
94
- /** A head set by the relay for a square no commit has sealed yet (the square's base or head at open). */
95
- export function startHead (doc, commit, by = '') {
96
- if (liveHead(doc)) return { ok: true, head: liveHead(doc), set: false }
97
- doc.transact(() => doc.getMap('live').set('head', { commit, by, ts: Date.now() }), 'seal')
98
- return { ok: true, head: commit, set: true }
99
- }
100
-
101
- /**
102
- * The relay's MSG_BRANCH ops for a live square's document (server.js asks this for `quilt/` keys
103
- * when QUILT_SOURCE is on): `start` sets the first head, `seal` is the commit's relay half.
104
- */
105
- export function liveRequest (doc, req, by) {
106
- if (req.op === 'start') {
107
- if (typeof req.head !== 'string' || !/^[0-9a-f]{64}$/.test(req.head)) throw new Error('start needs the square\'s head commit id')
108
- return startHead(doc, req.head, by)
109
- }
110
- if (req.op === 'seal') {
111
- const sealed = req.sealed && typeof req.sealed === 'object' ? req.sealed : {}
112
- return seal(doc, { prev: req.prev || null, head: req.head, sealed, by })
113
- }
114
- throw new Error('unknown live square request')
115
- }
116
-
@@ -1,19 +0,0 @@
1
- // What people hear about the forced date (Seam's plans/quilt-source-control/16, flag 9): QUILT_SOURCE_FORCE_AT
2
- // on the relay (src/force-date.js). Pure, loaded by the relay and the app alike.
3
-
4
- /** What the plain words say once a session's live sync stopped at the date (`at`, ISO). */
5
- export function syncStoppedWords (at) {
6
- const day = String(at || '').slice(0, 10)
7
- return `Live sync stopped${day ? ` on ${day}` : ''}: this session's folder is plain git now. Edits are no longer shared live; commit, pull and push with git as usual. Your git remote, clones and CI are untouched. Chat, tasks, the agent feed and the audit trail carry on in Quilt. The session's owner can still make it a quilt with its history (Settings, Session, Source control).`
8
- }
9
-
10
- /** What an owner hears before the date (16: on the release, seven days before, on the date): null when nothing applies. */
11
- export function forceAheadWords ({ at, now = Date.now(), remote = false }) {
12
- if (at == null || now >= at) return null
13
- const day = new Date(at).toISOString().slice(0, 10)
14
- const days = Math.ceil((at - now) / 86400000)
15
- const when = days <= 7 ? `in ${days} day${days === 1 ? '' : 's'} (${day})` : `on ${day}`
16
- return remote
17
- ? `On ${day} live file sync stops for sessions with a git remote that are not quilts yet: the folder becomes plain git (remote, clones and CI untouched), and chat and tasks carry on. Make this a quilt before then to keep editing live. That date is ${when}.`
18
- : `On ${day} this session becomes a quilt without history when you next open it: main as it is becomes its first commit, each branch a square. You can make it one now. That date is ${when}.`
19
- }
@@ -1,213 +0,0 @@
1
- // A file in a session linked to a quilt (Seam): its History (the live changes not committed yet,
2
- // then the patches that touched it, from the quilt's log, the chronology quilt_history gives
3
- // agents), its Blame (who wrote each line, in which patch), and code search over main. The data
4
- // comes through the relay (src/server.js quiltBlame, quiltSearch, quiltShow). A session that isn't
5
- // linked never shows any of it.
6
- import { esc, avatar, colorFor, I } from './common.js'
7
- import { diffLines } from './diffview.js'
8
-
9
- export const SEARCH_ICON = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg>'
10
-
11
- /** How long ago, short: now, 5m, 3h, 2d, 3w, 4mo, 2y. */
12
- export function shortAgo (ts, now = Date.now()) {
13
- const s = Math.max(0, (now - ts) / 1000)
14
- if (s < 60) return 'now'
15
- if (s < 3600) return `${Math.round(s / 60)}m`
16
- if (s < 86400) return `${Math.round(s / 3600)}h`
17
- if (s < 14 * 86400) return `${Math.round(s / 86400)}d`
18
- if (s < 60 * 86400) return `${Math.round(s / (7 * 86400))}w`
19
- if (s < 365 * 86400) return `${Math.round(s / (30 * 86400))}mo`
20
- return `${Math.round(s / (365 * 86400))}y`
21
- }
22
-
23
- /** The live changes, one row per person: what they changed in the file, summed, newest first. */
24
- export function liveByPerson (entries) {
25
- const by = new Map()
26
- for (const e of entries || []) {
27
- const p = by.get(e.by) || by.set(e.by, { by: e.by, ts: 0, added: 0, removed: 0, entries: [] }).get(e.by)
28
- p.ts = Math.max(p.ts, e.ts || 0)
29
- p.added += e.added || 0
30
- p.removed += e.removed || 0
31
- p.entries.push(e)
32
- }
33
- return [...by.values()].sort((a, b) => b.ts - a.ts)
34
- }
35
-
36
- /**
37
- * What a file view still has to read when it is drawn, so a tab put back after a reload (on any
38
- * segment) fetches its data instead of waiting for a click: 'history' (the file's changes, or in
39
- * a linked session its History) until it is read, and 'blame' on the Blame segment of a linked
40
- * file. `history`/`blame` are undefined until asked for; `busy` names reads already under way.
41
- */
42
- export function fileReadsNeeded ({ tab, linked = false, history, blame, busy = new Set() }) {
43
- const out = []
44
- if (history === undefined && !busy.has('history')) out.push('history')
45
- if (tab === 'blame' && linked && blame === undefined && !busy.has('blame')) out.push('blame')
46
- return out
47
- }
48
-
49
- /** How many rows History lists: a row per person changing the file now, and one per patch. */
50
- export const historyCount = (live, patches) => liveByPerson(live).length + (patches || []).length
51
-
52
- /** Whether a patch came in from git (the session shadows the quilt, or it is from before the cut-over) or was merged in Seam. */
53
- const fromGit = (p, quilt) => !!(quilt && (quilt.shadow || (quilt.at && p.ts < Date.parse(quilt.at))))
54
-
55
- /**
56
- * The History tab of a linked file.
57
- * @param {object} o
58
- * @param {string} o.path the file
59
- * @param {object[]} o.live the session's own changes not in the log yet, newest first
60
- * @param {object[]} o.patches the log's patches that touched the file, newest first (quilt-history.js entries)
61
- * @param {object} o.quilt the link ({ slug, shadow, at })
62
- * @param {string|null} o.open the row opened: a commit id, or `live:<name>`
63
- * @param {object|null} o.shown the opened patch: { files, diff }, { error }, or null while it loads
64
- * @param {string} [o.note] why the log is missing, when it is
65
- */
66
- export function patchesMarkup ({ path, live = [], patches = [], quilt = null, open = null, shown = null, note = '', who = (n) => n, colorOf = () => null }) {
67
- const people = liveByPerson(live)
68
- const head = note ? `<p class="hint qt-shadow">${esc(note)}</p>` : ''
69
- if (!people.length && !patches.length) return `${head}<div class="fv-note">No patch in ${esc((quilt && quilt.slug) || 'the quilt')} touched this file, and nobody is changing it now.</div>`
70
- const rows = people.map((p) => {
71
- const on = open === `live:${p.by}`
72
- const diffs = p.entries.filter((e) => e.diff).map((e) => diffLines(e.diff)).join('')
73
- return `<article class="chg-entry qt-patch qt-live${on ? ' on' : ''}">
74
- <button type="button" class="chg-row qt-row" data-qt-patch="live:${esc(p.by)}" aria-expanded="${on}">${avatar(p.by, colorFor(p.by, colorOf(p.by)))}<b>${esc(who(p.by))}</b><span class="qt-msg">${who(p.by) === 'you' ? 'are' : 'is'} changing <code>${esc(path)}</code>, not committed yet</span><span class="chg-kind">live</span><span class="spacer"></span><span class="hint">${esc(shortAgo(p.ts))}</span><span class="chg-delta"><span class="add">+${p.added}</span> <span class="del">−${p.removed}</span></span></button>
75
- ${on ? (diffs ? `<pre class="chg-diff">${diffs}</pre>` : '<p class="hint">No lines changed.</p>') : ''}
76
- </article>`
77
- })
78
- for (const p of patches) {
79
- const on = open === p.commit
80
- const mine = on && shown && shown.files ? shown.files.find((f) => f.path === path) : null
81
- rows.push(`<article class="chg-entry qt-patch${on ? ' on' : ''}">
82
- <button type="button" class="chg-row qt-row" data-qt-patch="${esc(p.commit)}" aria-expanded="${on}">${avatar(p.by, colorFor(p.by, colorOf(p.by)))}<b>${esc(who(p.by))}</b><span class="qt-no">${p.patch ? `#${esc(p.patch.no)}` : esc(p.short || '')}</span><span class="qt-msg">${esc(p.message)}</span>${fromGit(p, quilt) ? '<span class="chg-kind pulled">from git</span>' : '<span class="chg-kind">merged in Seam</span>'}<span class="spacer"></span><span class="hint">${esc(shortAgo(p.ts))}</span>${mine ? delta(mine) : ''}</button>
83
- ${on ? patchBody(shown) : ''}
84
- </article>`)
85
- }
86
- return head + rows.join('')
87
- }
88
-
89
- const delta = (f) => `<span class="chg-delta"><span class="add">+${f.added}</span> <span class="del">−${f.removed}</span></span>`
90
-
91
- function patchBody (shown) {
92
- if (!shown) return '<p class="hint">Reading the patch…</p>'
93
- if (shown.error) return `<p class="hint">${esc(shown.error)}</p>`
94
- const files = (shown.files || []).map((f) => `<button type="button" class="chg-file" data-qt-file="${esc(f.path)}"><code>${esc(f.path)}</code></button>${f.binary ? '<span class="hint">binary</span>' : delta(f)}`).join('')
95
- return `${files ? `<div class="qt-files">${files}</div>` : ''}${shown.diff ? `<pre class="chg-diff">${diffLines(shown.diff)}</pre>` : '<p class="hint">No lines of this file changed in it.</p>'}`
96
- }
97
-
98
- /**
99
- * For each line of `live` (the file now), the index of the same line in `base` (the file on
100
- * main, as blamed), or -1 for a line typed since and not committed: common start and end, then
101
- * the longest common subsequence of what is between.
102
- */
103
- export function alignLines (base, live) {
104
- const out = new Array(live.length).fill(-1)
105
- let s = 0
106
- while (s < base.length && s < live.length && base[s] === live[s]) { out[s] = s; s++ }
107
- let ea = base.length - 1
108
- let eb = live.length - 1
109
- while (ea >= s && eb >= s && base[ea] === live[eb]) { out[eb] = ea; ea--; eb-- }
110
- const a = base.slice(s, ea + 1)
111
- const b = live.slice(s, eb + 1)
112
- if (!a.length || !b.length) return out
113
- if (a.length * b.length > 4_000_000) return out // too far apart to line up: what differs is live
114
- const n = a.length
115
- const m = b.length
116
- const dp = Array.from({ length: n + 1 }, () => new Uint32Array(m + 1))
117
- for (let i = n - 1; i >= 0; i--) for (let j = m - 1; j >= 0; j--) dp[i][j] = a[i] === b[j] ? dp[i + 1][j + 1] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1])
118
- let i = 0
119
- let j = 0
120
- while (i < n && j < m) {
121
- if (a[i] === b[j]) { out[s + j] = s + i; i++; j++ } else if (dp[i + 1][j] >= dp[i][j + 1]) i++
122
- else j++
123
- }
124
- return out
125
- }
126
-
127
- const MAX_BLAME_LINES = 20000
128
-
129
- /**
130
- * The Blame tab: runs of lines, each headed by who wrote them, in which patch and how long ago.
131
- * Lines typed now and not committed are tinted and say "live".
132
- * @param {object} o
133
- * @param {string} o.text the file now
134
- * @param {object|null} o.blame the relay's answer: { blame }, { missing }, { working }, { error }, or null while it loads
135
- * @param {string} [o.liveBy] who is changing the file now (its live lines are theirs)
136
- */
137
- export function blameMarkup ({ text, blame, liveBy = '', who = (n) => n, colorOf = () => null, now = Date.now() }) {
138
- if (!blame) return '<div class="fv-note">Reading who wrote each line…</div>'
139
- if (blame.error) return `<div class="fv-note">${esc(blame.error)}</div>`
140
- if (blame.working) return '<div class="fv-note">Seam is working out who wrote each line. This takes a moment the first time.</div>'
141
- if (text == null) return '<div class="fv-note">Reading the file…</div>'
142
- const lines = String(text).split('\n')
143
- if (lines.length > 1 && lines[lines.length - 1] === '') lines.pop()
144
- const b = blame.blame || { who: [], lines: [] }
145
- const map = blame.missing ? lines.map(() => -1) : alignLines(b.lines.map((l) => l[0]), lines)
146
- let prev = null
147
- const rows = lines.slice(0, MAX_BLAME_LINES).map((l, i) => {
148
- const at = map[i]
149
- const w = at >= 0 ? b.who[b.lines[at][1]] || null : null
150
- const src = at < 0 ? 'live' : w ? w.commit : 'none'
151
- const run = src !== prev
152
- prev = src
153
- let gutter = ''
154
- if (run && src === 'live') {
155
- gutter = `${liveBy ? avatar(liveBy, colorFor(liveBy, colorOf(liveBy))) : ''}<span class="qt-bw">${esc(liveBy ? who(liveBy) : 'Not committed')}</span><span class="qt-bp">live</span><span class="qt-bt">now</span>`
156
- } else if (run && w) {
157
- gutter = `${avatar(w.by, colorFor(w.by, colorOf(w.by)))}<span class="qt-bw">${esc(who(w.by))}</span><button type="button" class="qt-bp" data-qt-blame="${esc(w.commit)}" title="Open this patch in History">${w.patch != null ? `#${esc(w.patch)}` : esc(w.short)}</button><span class="qt-bt">${esc(shortAgo(w.at, now))}</span>`
158
- }
159
- return `<tr class="${[run ? 'qt-run' : '', src === 'live' ? 'qt-live' : ''].filter(Boolean).join(' ')}"><td class="qt-bg">${gutter}</td><td class="ln">${i + 1}</td><td class="lt">${esc(l) || ' '}</td></tr>`
160
- })
161
- return `<table class="fv-code qt-blame"><tbody>${rows.join('')}</tbody></table>${lines.length > MAX_BLAME_LINES ? `<div class="fv-note">Showing the first ${MAX_BLAME_LINES.toLocaleString()} lines.</div>` : ''}`
162
- }
163
-
164
- /** The plain words of a Seam search, to mark in the hits (not path:, lang: or -excluded terms). */
165
- export function searchWords (q) {
166
- return (String(q).match(/"[^"]+"|\S+/g) || [])
167
- .filter((t) => !t.startsWith('-') && !/^[a-z]+:/i.test(t) && t !== 'OR' && !(t.startsWith('/') && t.endsWith('/') && t.length > 1))
168
- .map((t) => t.replace(/^"|"$/g, ''))
169
- .filter(Boolean)
170
- }
171
-
172
- function marked (text, words) {
173
- if (!words.length) return esc(text) || ' '
174
- const re = new RegExp(`(${words.map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|')})`, 'gi')
175
- return String(text).split(re).map((part, i) => i % 2 ? `<mark>${esc(part)}</mark>` : esc(part)).join('') || ' '
176
- }
177
-
178
- /**
179
- * A search tab: the field to search again, how much was found, and the hits grouped by file.
180
- * Each hit opens its file at its line.
181
- * @param {object} o
182
- * @param {string} o.q what was searched
183
- * @param {object|null} o.result the relay's answer: { search }, { working }, { error }, or null while it loads
184
- */
185
- export function searchMarkup ({ q, result }) {
186
- const form = `<form class="qt-sform" data-qt-search-form><span class="qt-sico">${SEARCH_ICON}</span><input class="input" name="q" value="${esc(q)}" aria-label="Search the code on main" autocomplete="off" spellcheck="false"></form>`
187
- let body
188
- if (!result) body = '<p class="hint qt-scount">Searching main…</p>'
189
- else if (result.error) body = `<p class="hint qt-scount">${esc(result.error)}</p>`
190
- else if (result.working) body = '<p class="hint qt-scount">Seam is still searching. It answers in a moment.</p>'
191
- else if (result.quilt === null) body = '<p class="hint qt-scount">This session is no longer linked to a quilt.</p>'
192
- else {
193
- const s = result.search
194
- const files = new Map()
195
- for (const h of s.hits) (files.get(h.path) || files.set(h.path, []).get(h.path)).push(h)
196
- const n = s.hits.filter((h) => h.line).length
197
- const words = searchWords(q)
198
- const count = s.hits.length
199
- ? `${n} line${n === 1 ? '' : 's'} in ${files.size} file${files.size === 1 ? '' : 's'} on main${s.more ? ', the first ones' : ''}`
200
- : 'Nothing on main matches.'
201
- body = `<p class="hint qt-scount">${count}</p>` + [...files].map(([p, hits]) => {
202
- const lines = hits.filter((h) => h.line)
203
- return `<section class="qt-hit"><div class="qt-hitpath"><button type="button" class="chg-file" data-qt-hit="${esc(p)}"><code>${esc(p)}</code></button><span class="hint">${lines.length ? `${lines.length} line${lines.length === 1 ? '' : 's'}` : 'its name'}</span></div>${lines.length ? `
204
- <table class="fv-code"><tbody>${lines.map((h) => `<tr data-qt-hit="${esc(p)}" data-line="${h.line}"><td class="ln">${h.line}</td><td class="lt">${h.symbol ? `<mark>${esc(h.symbol)}</mark>` : marked(h.text || '', words)}</td></tr>`).join('')}</tbody></table>` : ''}</section>`
205
- }).join('')
206
- }
207
- return `<div class="qt-search">${form}${body}</div>`
208
- }
209
-
210
- /** The search tab's label in the tab row. */
211
- export const searchTabMarkup = (q, on) => `<div class="ws-tab qt-tab${on ? ' on' : ''}" role="tab" aria-selected="${on}" tabindex="0" data-kind="search" data-tab="${esc(q)}" title="${esc(`Search: ${q}`)}">
212
- <span class="ico">${SEARCH_ICON}</span><span class="nm">Search: ${esc(q)}</span>
213
- <button class="x" data-kind="search" data-close="${esc(q)}" aria-label="Close search ${esc(q)}">${I.x}</button></div>`