@deepseek-ai/dsh-experimental-client-ui-forge 0.2.2-alpha.3 → 0.2.2-rc.2
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.
- package/README.i18n.yaml +8 -8
- package/README.md +41 -14
- package/README.zh.md +40 -13
- package/lib/client.js +13 -11800
- package/lib/client.js.map +1 -0
- package/lib/index.js +1340 -115
- package/lib/typert.host.js +1715 -212
- package/lib/typert.remote-client.d.ts +42 -5
- package/lib/typert.remote-client.d.ts.map +1 -0
- package/lib/typert.remote-client.js +1523 -244
- package/lib/types/client/ActionToast.d.ts.map +1 -0
- package/lib/types/client/ActionToast.js.map +1 -0
- package/lib/types/client/BaseMenu.d.ts +30 -0
- package/lib/types/client/BaseMenu.d.ts.map +1 -0
- package/lib/types/client/BaseMenu.js.map +1 -0
- package/lib/types/client/ChangeRequestCard.d.ts +9 -5
- package/lib/types/client/ChangeRequestCard.d.ts.map +1 -0
- package/lib/types/client/ChangeRequestCard.js.map +1 -0
- package/lib/types/client/ChangeRequestOverview.d.ts +124 -0
- package/lib/types/client/ChangeRequestOverview.d.ts.map +1 -0
- package/lib/types/client/ChangeRequestOverview.js.map +1 -0
- package/lib/types/client/ChangedFiles.d.ts +2 -0
- package/lib/types/client/ChangedFiles.d.ts.map +1 -0
- package/lib/types/client/ChangedFiles.js.map +1 -0
- package/lib/types/client/ChangesTab.d.ts +30 -5
- package/lib/types/client/ChangesTab.d.ts.map +1 -0
- package/lib/types/client/ChangesTab.js.map +1 -0
- package/lib/types/client/ChangesTabTitle.d.ts.map +1 -0
- package/lib/types/client/ChangesTabTitle.js.map +1 -0
- package/lib/types/client/CommitMenu.d.ts +41 -0
- package/lib/types/client/CommitMenu.d.ts.map +1 -0
- package/lib/types/client/CommitMenu.js.map +1 -0
- package/lib/types/client/DiffFile.d.ts +47 -15
- package/lib/types/client/DiffFile.d.ts.map +1 -0
- package/lib/types/client/DiffFile.js.map +1 -0
- package/lib/types/client/FileLabel.d.ts.map +1 -0
- package/lib/types/client/FileLabel.js.map +1 -0
- package/lib/types/client/FileTreePane.d.ts +4 -2
- package/lib/types/client/FileTreePane.d.ts.map +1 -0
- package/lib/types/client/FileTreePane.js.map +1 -0
- package/lib/types/client/ForgeTab.d.ts +9 -1
- package/lib/types/client/ForgeTab.d.ts.map +1 -0
- package/lib/types/client/ForgeTab.js.map +1 -0
- package/lib/types/client/ForgeTabTitle.d.ts.map +1 -0
- package/lib/types/client/ForgeTabTitle.js.map +1 -0
- package/lib/types/client/HeaderButton.d.ts.map +1 -0
- package/lib/types/client/HeaderButton.js.map +1 -0
- package/lib/types/client/LocalChangesCard.d.ts.map +1 -0
- package/lib/types/client/LocalChangesCard.js.map +1 -0
- package/lib/types/client/RefreshGlyph.d.ts.map +1 -0
- package/lib/types/client/RefreshGlyph.js.map +1 -0
- package/lib/types/client/ReviewComposer.d.ts +20 -6
- package/lib/types/client/ReviewComposer.d.ts.map +1 -0
- package/lib/types/client/ReviewComposer.js.map +1 -0
- package/lib/types/client/ReviewControls.d.ts +79 -0
- package/lib/types/client/ReviewControls.d.ts.map +1 -0
- package/lib/types/client/ReviewControls.js.map +1 -0
- package/lib/types/client/ReviewThread.d.ts +4 -4
- package/lib/types/client/ReviewThread.d.ts.map +1 -0
- package/lib/types/client/ReviewThread.js.map +1 -0
- package/lib/types/client/ReviewThreads.d.ts +108 -0
- package/lib/types/client/ReviewThreads.d.ts.map +1 -0
- package/lib/types/client/ReviewThreads.js.map +1 -0
- package/lib/types/client/SessionHoverForge.d.ts.map +1 -0
- package/lib/types/client/SessionHoverForge.js.map +1 -0
- package/lib/types/client/SessionRowForge.d.ts +1 -1
- package/lib/types/client/SessionRowForge.d.ts.map +1 -0
- package/lib/types/client/SessionRowForge.js.map +1 -0
- package/lib/types/client/SubmitReview.d.ts +12 -4
- package/lib/types/client/SubmitReview.d.ts.map +1 -0
- package/lib/types/client/SubmitReview.js.map +1 -0
- package/lib/types/client/ThreadList.d.ts +43 -0
- package/lib/types/client/ThreadList.d.ts.map +1 -0
- package/lib/types/client/ThreadList.js.map +1 -0
- package/lib/types/client/ViewOptionsMenu.d.ts +31 -0
- package/lib/types/client/ViewOptionsMenu.d.ts.map +1 -0
- package/lib/types/client/ViewOptionsMenu.js.map +1 -0
- package/lib/types/client/WorktreeProgress.d.ts.map +1 -0
- package/lib/types/client/WorktreeProgress.js.map +1 -0
- package/lib/types/client/WorktreeSettings.d.ts +12 -0
- package/lib/types/client/WorktreeSettings.d.ts.map +1 -0
- package/lib/types/client/WorktreeSettings.js.map +1 -0
- package/lib/types/client/WorktreeToggle.d.ts.map +1 -0
- package/lib/types/client/WorktreeToggle.js.map +1 -0
- package/lib/types/client/changes-address.d.ts.map +1 -0
- package/lib/types/client/changes-address.js.map +1 -0
- package/lib/types/client/changes-store.d.ts +20 -5
- package/lib/types/client/changes-store.d.ts.map +1 -0
- package/lib/types/client/changes-store.js.map +1 -0
- package/lib/types/client/checkout.d.ts.map +1 -0
- package/lib/types/client/checkout.js.map +1 -0
- package/lib/types/client/code-selection.d.ts.map +1 -0
- package/lib/types/client/code-selection.js.map +1 -0
- package/lib/types/client/diff-rows.d.ts.map +1 -0
- package/lib/types/client/diff-rows.js.map +1 -0
- package/lib/types/client/file-tree.d.ts.map +1 -0
- package/lib/types/client/file-tree.js.map +1 -0
- package/lib/types/client/format.d.ts.map +1 -0
- package/lib/types/client/format.js.map +1 -0
- package/lib/types/client/glyphs.d.ts +4 -2
- package/lib/types/client/glyphs.d.ts.map +1 -0
- package/lib/types/client/glyphs.js.map +1 -0
- package/lib/types/client/host-viewed.d.ts +84 -0
- package/lib/types/client/host-viewed.d.ts.map +1 -0
- package/lib/types/client/host-viewed.js.map +1 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/index.js.map +1 -0
- package/lib/types/client/locales.d.ts +175 -3
- package/lib/types/client/locales.d.ts.map +1 -0
- package/lib/types/client/locales.js.map +1 -0
- package/lib/types/client/menu-search.d.ts +10 -0
- package/lib/types/client/menu-search.d.ts.map +1 -0
- package/lib/types/client/menu-search.js.map +1 -0
- package/lib/types/client/mount.d.ts +4 -0
- package/lib/types/client/mount.d.ts.map +1 -0
- package/lib/types/client/mount.js.map +1 -0
- package/lib/types/client/review-queue.d.ts +5 -8
- package/lib/types/client/review-queue.d.ts.map +1 -0
- package/lib/types/client/review-queue.js.map +1 -0
- package/lib/types/client/review-threads.d.ts +108 -0
- package/lib/types/client/review-threads.d.ts.map +1 -0
- package/lib/types/client/review-threads.js.map +1 -0
- package/lib/types/client/review.d.ts +31 -4
- package/lib/types/client/review.d.ts.map +1 -0
- package/lib/types/client/review.js.map +1 -0
- package/lib/types/client/section-reach.d.ts +26 -0
- package/lib/types/client/section-reach.d.ts.map +1 -0
- package/lib/types/client/section-reach.js.map +1 -0
- package/lib/types/client/session-list.d.ts.map +1 -0
- package/lib/types/client/session-list.js.map +1 -0
- package/lib/types/client/source.d.ts.map +1 -0
- package/lib/types/client/source.js.map +1 -0
- package/lib/types/client/status.d.ts.map +1 -0
- package/lib/types/client/status.js.map +1 -0
- package/lib/types/client/thread-state.d.ts +68 -0
- package/lib/types/client/thread-state.d.ts.map +1 -0
- package/lib/types/client/thread-state.js.map +1 -0
- package/lib/types/client/toolbar-fold.d.ts +19 -0
- package/lib/types/client/toolbar-fold.d.ts.map +1 -0
- package/lib/types/client/toolbar-fold.js.map +1 -0
- package/lib/types/client/viewed-files.d.ts +70 -0
- package/lib/types/client/viewed-files.d.ts.map +1 -0
- package/lib/types/client/viewed-files.js.map +1 -0
- package/lib/types/client/worktree-settings-form.d.ts +32 -0
- package/lib/types/client/worktree-settings-form.d.ts.map +1 -0
- package/lib/types/client/worktree-settings-form.js.map +1 -0
- package/lib/types/commit-range.d.ts +17 -0
- package/lib/types/commit-range.d.ts.map +1 -0
- package/lib/types/commit-range.js.map +1 -0
- package/lib/types/diff.d.ts +10 -2
- package/lib/types/diff.d.ts.map +1 -0
- package/lib/types/diff.js.map +1 -0
- package/lib/types/git.d.ts.map +1 -0
- package/lib/types/git.js.map +1 -0
- package/lib/types/index.d.ts +194 -14
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/layers.d.ts +66 -0
- package/lib/types/layers.d.ts.map +1 -0
- package/lib/types/layers.js.map +1 -0
- package/lib/types/local-diff.d.ts +54 -6
- package/lib/types/local-diff.d.ts.map +1 -0
- package/lib/types/local-diff.js.map +1 -0
- package/lib/types/panel.d.ts.map +1 -0
- package/lib/types/panel.js.map +1 -0
- package/lib/types/parse.d.ts +16 -0
- package/lib/types/parse.d.ts.map +1 -0
- package/lib/types/parse.js.map +1 -0
- package/lib/types/patch-cache.d.ts +12 -7
- package/lib/types/patch-cache.d.ts.map +1 -0
- package/lib/types/patch-cache.js.map +1 -0
- package/lib/types/review-cache.d.ts +54 -0
- package/lib/types/review-cache.d.ts.map +1 -0
- package/lib/types/review-cache.js.map +1 -0
- package/lib/types/states.d.ts.map +1 -0
- package/lib/types/states.js.map +1 -0
- package/lib/types/types.d.ts +113 -11
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js.map +1 -0
- package/lib/types/wildmatch.d.ts +14 -0
- package/lib/types/wildmatch.d.ts.map +1 -0
- package/lib/types/wildmatch.js.map +1 -0
- package/lib/types/worktree.d.ts +8 -6
- package/lib/types/worktree.d.ts.map +1 -0
- package/lib/types/worktree.js.map +1 -0
- package/package.json +59 -49
- package/src/client/ActionToast.tsx +38 -0
- package/src/client/BaseMenu.module.css +153 -0
- package/src/client/BaseMenu.tsx +180 -0
- package/src/client/ChangeRequestCard.tsx +439 -0
- package/src/client/ChangeRequestOverview.module.css +196 -0
- package/src/client/ChangeRequestOverview.tsx +620 -0
- package/src/client/ChangedFiles.module.css +26 -0
- package/src/client/ChangedFiles.tsx +144 -0
- package/src/client/ChangesTab.module.css +984 -0
- package/src/client/ChangesTab.tsx +1288 -0
- package/src/client/ChangesTabTitle.tsx +28 -0
- package/src/client/CommitMenu.module.css +139 -0
- package/src/client/CommitMenu.tsx +186 -0
- package/src/client/DiffFile.tsx +621 -0
- package/src/client/FileLabel.module.css +53 -0
- package/src/client/FileLabel.tsx +66 -0
- package/src/client/FileTreePane.module.css +10 -0
- package/src/client/FileTreePane.tsx +79 -0
- package/src/client/ForgeTab.module.css +627 -0
- package/src/client/ForgeTab.tsx +263 -0
- package/src/client/ForgeTabTitle.tsx +21 -0
- package/src/client/HeaderButton.module.css +29 -0
- package/src/client/HeaderButton.tsx +68 -0
- package/src/client/LocalChangesCard.tsx +83 -0
- package/src/client/RefreshGlyph.module.css +22 -0
- package/src/client/RefreshGlyph.tsx +35 -0
- package/src/client/ReviewComposer.module.css +37 -0
- package/src/client/ReviewComposer.tsx +128 -0
- package/src/client/ReviewControls.module.css +162 -0
- package/src/client/ReviewControls.tsx +191 -0
- package/src/client/ReviewThread.tsx +96 -0
- package/src/client/ReviewThreads.module.css +326 -0
- package/src/client/ReviewThreads.tsx +443 -0
- package/src/client/SessionHoverForge.tsx +72 -0
- package/src/client/SessionList.module.css +85 -0
- package/src/client/SessionRowForge.tsx +86 -0
- package/src/client/SubmitReview.module.css +26 -0
- package/src/client/SubmitReview.tsx +95 -0
- package/src/client/ThreadList.module.css +218 -0
- package/src/client/ThreadList.tsx +165 -0
- package/src/client/ViewOptionsMenu.tsx +79 -0
- package/src/client/WorktreeProgress.module.css +226 -0
- package/src/client/WorktreeProgress.tsx +280 -0
- package/src/client/WorktreeSettings.module.css +29 -0
- package/src/client/WorktreeSettings.tsx +42 -0
- package/src/client/WorktreeToggle.module.css +132 -0
- package/src/client/WorktreeToggle.tsx +165 -0
- package/src/client/changes-address.ts +110 -0
- package/src/client/changes-store.ts +155 -0
- package/src/client/checkout.ts +75 -0
- package/src/client/code-selection.ts +53 -0
- package/src/client/diff-rows.ts +199 -0
- package/src/client/file-tree.ts +86 -0
- package/src/client/format.ts +37 -0
- package/src/client/glyphs.tsx +151 -0
- package/src/client/host-viewed.ts +195 -0
- package/src/client/index.ts +31 -0
- package/src/client/locales.ts +3446 -0
- package/src/client/menu-search.ts +16 -0
- package/src/client/mount.ts +439 -0
- package/src/client/review-queue.ts +256 -0
- package/src/client/review-threads.ts +204 -0
- package/src/client/review.ts +120 -0
- package/src/client/section-reach.ts +61 -0
- package/src/client/session-list.ts +79 -0
- package/src/client/source.ts +274 -0
- package/src/client/status.ts +171 -0
- package/src/client/thread-state.ts +127 -0
- package/src/client/toolbar-fold.ts +70 -0
- package/src/client/viewed-files.ts +178 -0
- package/src/client/worktree-settings-form.ts +43 -0
- package/src/commit-range.ts +27 -0
- package/src/css-modules.d.ts +5 -0
- package/src/diff.ts +162 -0
- package/src/git.ts +293 -0
- package/src/index.ts +1162 -0
- package/src/layers.ts +163 -0
- package/src/local-diff.ts +438 -0
- package/src/panel.ts +422 -0
- package/src/parse.ts +137 -0
- package/src/patch-cache.ts +73 -0
- package/src/review-cache.ts +98 -0
- package/src/states.ts +350 -0
- package/src/types.ts +504 -0
- package/src/wildmatch.ts +121 -0
- package/src/worktree.ts +576 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-persisted review records, the review summary, comment drafts an earlier panel kept in the browser, and the
|
|
3
|
+
* write whose outcome still needs confirmation. New pull request comments go to the pending review on the code host.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { z } from 'zod'
|
|
7
|
+
import type { ForgeReviewRequest } from '@deepseek-ai/dsh-experimental-forge/types'
|
|
8
|
+
import type { ReviewSelection, ReviewSource } from './review.ts'
|
|
9
|
+
|
|
10
|
+
const line = z.object({ path: z.string(), side: z.enum(['L', 'R']), startLine: z.number().int().positive(), endLine: z.number().int().positive(), body: z.string() })
|
|
11
|
+
const commits = z.object({ from: z.string(), to: z.string() }).nullable().default(null)
|
|
12
|
+
const selection = z.object({
|
|
13
|
+
source: z.object({ kind: z.literal('change'), url: z.string(), headSha: z.string(), commits }), comment: line.extend({ hunk: z.string() }),
|
|
14
|
+
})
|
|
15
|
+
const recordedSelection = selection.extend({ source: z.discriminatedUnion('kind', [
|
|
16
|
+
selection.shape.source,
|
|
17
|
+
z.object({ kind: z.literal('local'), scope: z.string(), base: z.string().nullable().default(null), baseRef: z.string().nullable().default(null), commits }),
|
|
18
|
+
]) })
|
|
19
|
+
const record = z.discriminatedUnion('kind', [
|
|
20
|
+
z.object({ kind: z.literal('agent'), selection: recordedSelection }),
|
|
21
|
+
z.object({ kind: z.literal('published'), selection: recordedSelection, url: z.url({ protocol: /^https$/ }) }),
|
|
22
|
+
])
|
|
23
|
+
const event = z.enum(['comment', 'approve', 'request-changes'])
|
|
24
|
+
const request = z.discriminatedUnion('kind', [
|
|
25
|
+
z.object({ kind: z.literal('comment'), headSha: z.string(), comment: line }),
|
|
26
|
+
z.object({ kind: z.literal('review'), headSha: z.string(), event, body: z.string(), comments: z.array(line) }),
|
|
27
|
+
])
|
|
28
|
+
const saved = z.object({
|
|
29
|
+
comments: z.array(selection), records: z.array(record).default([]), body: z.string(), event, uncertain: request.nullable(),
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
/** A comment retained after adding its reference or receiving a publication receipt. */
|
|
33
|
+
export type ReviewRecord =
|
|
34
|
+
| { readonly kind: 'agent'; readonly selection: ReviewSelection }
|
|
35
|
+
| { readonly kind: 'published'; readonly selection: ReviewSelection; readonly url: string }
|
|
36
|
+
|
|
37
|
+
/** One inline record; its numeric key belongs only to the retained browser store. */
|
|
38
|
+
export type ReviewThread = { readonly key: number } & (ReviewRecord | { readonly kind: 'pending'; readonly selection: ReviewSelection })
|
|
39
|
+
|
|
40
|
+
/** File-scoped snapshot that does not change while unrelated comments or the summary are edited. */
|
|
41
|
+
export interface FileReviewState {
|
|
42
|
+
readonly threads: readonly ReviewThread[]
|
|
43
|
+
readonly locked: boolean
|
|
44
|
+
readonly error: ReviewQueueState['error']
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Stable snapshot for a file without a comment store. */
|
|
48
|
+
export const EMPTY_FILE_REVIEW: FileReviewState = { threads: [], locked: false, error: null }
|
|
49
|
+
|
|
50
|
+
/** Immutable queue snapshot; errors leave the original stored bytes untouched. */
|
|
51
|
+
export interface ReviewQueueState {
|
|
52
|
+
readonly comments: readonly ReviewSelection[]
|
|
53
|
+
readonly records: readonly ReviewRecord[]
|
|
54
|
+
readonly body: string
|
|
55
|
+
readonly event: 'comment' | 'approve' | 'request-changes'
|
|
56
|
+
readonly uncertain: ForgeReviewRequest | null
|
|
57
|
+
readonly error: 'load' | 'save' | null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Initial state of a newly opened review tab. */
|
|
61
|
+
export const EMPTY_REVIEW: ReviewQueueState = { comments: [], records: [], body: '', event: 'comment', uncertain: null, error: null }
|
|
62
|
+
|
|
63
|
+
/** One review tab's drafts and records, retained until its occurrence closes. */
|
|
64
|
+
export class ReviewQueue {
|
|
65
|
+
private closed = false
|
|
66
|
+
private state: ReviewQueueState = EMPTY_REVIEW
|
|
67
|
+
private readonly listeners = new Set<() => void>()
|
|
68
|
+
private readonly keys = new WeakMap<ReviewSelection, number>()
|
|
69
|
+
private nextKey = 0
|
|
70
|
+
private readonly files = new Map<string, FileReviewState>()
|
|
71
|
+
|
|
72
|
+
/** @param key - browser storage key including Session and tab identity. */
|
|
73
|
+
constructor(private readonly key: string) {
|
|
74
|
+
try {
|
|
75
|
+
const raw = localStorage.getItem(key)
|
|
76
|
+
if (raw !== null) this.state = { ...saved.parse(JSON.parse(raw)), error: null }
|
|
77
|
+
} catch (_error) {
|
|
78
|
+
this.state = { ...EMPTY_REVIEW, error: 'load' }
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Current queue snapshot, stable between edits and storage failures. */
|
|
83
|
+
readonly getSnapshot = (): ReviewQueueState => this.state
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Clear this tab's records and refuse later completions from its outgoing requests.
|
|
87
|
+
* @returns whether its browser storage was cleared; the in-memory state always closes.
|
|
88
|
+
*/
|
|
89
|
+
close(): boolean {
|
|
90
|
+
this.closed = true
|
|
91
|
+
this.state = EMPTY_REVIEW
|
|
92
|
+
this.files.clear()
|
|
93
|
+
this.emit()
|
|
94
|
+
this.listeners.clear()
|
|
95
|
+
try {
|
|
96
|
+
localStorage.removeItem(this.key)
|
|
97
|
+
} catch (_error) {
|
|
98
|
+
return false
|
|
99
|
+
}
|
|
100
|
+
return true
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Summary subscribed by the surrounding diff; editing draft text does not invalidate its file rows. */
|
|
104
|
+
readonly getStatus = (): string => `${this.state.comments.length}:${this.state.records.length}:${this.state.error ?? ''}:${this.state.uncertain === null ? 'idle' : 'pending'}`
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Read stable inline records for one path and review source.
|
|
108
|
+
* @param path - repository-relative file path.
|
|
109
|
+
* @param source - PR or local scope; older head commits remain available as outdated comments.
|
|
110
|
+
* @returns records and edit availability, reusing the snapshot when these values are unchanged.
|
|
111
|
+
*/
|
|
112
|
+
fileComments(path: string, source: ReviewSource): FileReviewState {
|
|
113
|
+
const matches = (item: ReviewSelection): boolean => item.comment.path === path && sameReviewSource(item.source, source)
|
|
114
|
+
const threads: ReviewThread[] = [
|
|
115
|
+
...this.state.records.filter(item => matches(item.selection)).map(item => ({ ...item, key: this.keyOf(item.selection) })),
|
|
116
|
+
...this.state.comments.filter(matches).map(item => ({ kind: 'pending' as const, selection: item, key: this.keyOf(item) })),
|
|
117
|
+
]
|
|
118
|
+
const key = JSON.stringify([path, source.kind, source.kind === 'local' ? [source.scope, source.baseRef] : source.url, source.commits])
|
|
119
|
+
const previous = this.files.get(key)
|
|
120
|
+
const locked = this.state.uncertain !== null
|
|
121
|
+
if (previous !== undefined && previous.locked === locked && previous.error === this.state.error
|
|
122
|
+
&& previous.threads.length === threads.length && previous.threads.every((item, index) => {
|
|
123
|
+
const next = threads[index]
|
|
124
|
+
return next?.selection === item.selection && next.kind === item.kind
|
|
125
|
+
})) return previous
|
|
126
|
+
const value = { threads, locked, error: this.state.error }
|
|
127
|
+
this.files.set(key, value)
|
|
128
|
+
return value
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Retain an Agent reference at its original code location.
|
|
133
|
+
* @param selection - reference already accepted by the main composer.
|
|
134
|
+
* @returns whether the record was saved.
|
|
135
|
+
*/
|
|
136
|
+
recordAgent(selection: ReviewSelection): boolean {
|
|
137
|
+
return this.save({ ...this.state, records: [...this.state.records, { kind: 'agent', selection }] })
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Retain a published comment and settle its outgoing write atomically in browser storage.
|
|
142
|
+
* @param selection - published comment's frozen code location.
|
|
143
|
+
* @param url - confirmed publication URL.
|
|
144
|
+
* @returns whether the receipt and comment were saved.
|
|
145
|
+
*/
|
|
146
|
+
published(selection: ReviewSelection, url: string): boolean {
|
|
147
|
+
return this.save({ ...this.state, uncertain: null, records: [...this.state.records, { kind: 'published', selection, url }] })
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Observe queue edits and storage failures.
|
|
152
|
+
* @param listener - snapshot invalidation.
|
|
153
|
+
* @returns unsubscribe.
|
|
154
|
+
*/
|
|
155
|
+
readonly subscribe = (listener: () => void): (() => void) => {
|
|
156
|
+
this.listeners.add(listener)
|
|
157
|
+
return () => { this.listeners.delete(listener) }
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Edit or remove a pending comment without changing its captured lines.
|
|
162
|
+
* @param index - comment position.
|
|
163
|
+
* @param body - replacement text, or null to remove.
|
|
164
|
+
* @returns whether saved.
|
|
165
|
+
*/
|
|
166
|
+
edit(index: number, body: string | null): boolean {
|
|
167
|
+
if (this.state.uncertain !== null) return false
|
|
168
|
+
return this.save({ ...this.state, comments: this.state.comments.flatMap((item, at) => {
|
|
169
|
+
if (at !== index) return [item]
|
|
170
|
+
if (body === null) return []
|
|
171
|
+
const next = { ...item, comment: { ...item.comment, body } }
|
|
172
|
+
this.keys.set(next, this.keyOf(item))
|
|
173
|
+
return [next]
|
|
174
|
+
}) })
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Edit the currently pending occurrence addressed by an inline record.
|
|
179
|
+
* @param key - browser-store identity from fileComments.
|
|
180
|
+
* @param body - replacement text, or null to remove.
|
|
181
|
+
* @returns whether the occurrence still exists and the edit was saved.
|
|
182
|
+
*/
|
|
183
|
+
editThread(key: number, body: string | null): boolean {
|
|
184
|
+
const index = this.state.comments.findIndex(item => this.keyOf(item) === key)
|
|
185
|
+
return index !== -1 && this.edit(index, body)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Save the summary and decision for the pending review.
|
|
190
|
+
* @param body - review summary.
|
|
191
|
+
* @param event - review decision.
|
|
192
|
+
* @returns whether saved.
|
|
193
|
+
*/
|
|
194
|
+
summarize(body: string, event: ReviewQueueState['event']): boolean {
|
|
195
|
+
if (this.state.uncertain !== null) return false
|
|
196
|
+
return this.save({ ...this.state, body, event })
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Persist the outgoing request before network dispatch.
|
|
201
|
+
* @param request - exact write.
|
|
202
|
+
* @returns whether dispatch may start.
|
|
203
|
+
*/
|
|
204
|
+
begin(request: ForgeReviewRequest): boolean {
|
|
205
|
+
if (this.state.uncertain !== null) return false
|
|
206
|
+
return this.save({ ...this.state, uncertain: request })
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Clear the write after a known result or explicit reconciliation.
|
|
211
|
+
* @param submitted - whether a batch review was published.
|
|
212
|
+
* @param url - confirmed publication URL when retaining submitted comments.
|
|
213
|
+
* @returns whether saved.
|
|
214
|
+
*/
|
|
215
|
+
finish(submitted: boolean, url?: string): boolean {
|
|
216
|
+
if (!submitted) return this.save({ ...this.state, uncertain: null })
|
|
217
|
+
const records = url === undefined ? this.state.records : [...this.state.records,
|
|
218
|
+
...this.state.comments.map(item => ({ kind: 'published' as const, selection: item, url }))]
|
|
219
|
+
return this.save({ ...EMPTY_REVIEW, records })
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private keyOf(selection: ReviewSelection): number {
|
|
223
|
+
let key = this.keys.get(selection)
|
|
224
|
+
if (key === undefined) { key = ++this.nextKey; this.keys.set(selection, key) }
|
|
225
|
+
return key
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private save(state: ReviewQueueState): boolean {
|
|
229
|
+
if (this.closed || this.state.error === 'load') return false
|
|
230
|
+
try {
|
|
231
|
+
const { error: _error, ...value } = state
|
|
232
|
+
localStorage.setItem(this.key, JSON.stringify(value))
|
|
233
|
+
} catch (_error) {
|
|
234
|
+
this.state = { ...this.state, error: 'save' }
|
|
235
|
+
this.emit()
|
|
236
|
+
return false
|
|
237
|
+
}
|
|
238
|
+
this.state = { ...state, error: null }
|
|
239
|
+
this.emit()
|
|
240
|
+
return true
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private emit(): void { for (const listener of this.listeners) listener() }
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Compare review identity while allowing records from an older PR head to remain visible.
|
|
248
|
+
* @param left - recorded source.
|
|
249
|
+
* @param right - displayed source.
|
|
250
|
+
* @returns whether the sources name the same PR, or the same local scope and base ref, with the same chosen commits.
|
|
251
|
+
*/
|
|
252
|
+
export function sameReviewSource(left: ReviewSource, right: ReviewSource): boolean {
|
|
253
|
+
if (left.commits?.from !== right.commits?.from || left.commits?.to !== right.commits?.to) return false
|
|
254
|
+
return left.kind === 'local' ? right.kind === 'local' && left.scope === right.scope && left.baseRef === right.baseRef
|
|
255
|
+
: right.kind === 'change' && left.url === right.url
|
|
256
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the changes view shows each review thread of a change request, the order a file shows them in, which threads
|
|
3
|
+
* each filter of the thread list keeps, and the composer quote of a whole thread.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { QuoteSummary } from '@deepseek-ai/dsh-client-ui-primitives'
|
|
7
|
+
import type { ForgeAnchor, ForgeReviewThread } from '@deepseek-ai/dsh-experimental-forge/types'
|
|
8
|
+
import { assertNever } from '@deepseek-ai/dsh-util-values'
|
|
9
|
+
import type { ForgeCommitRange, ForgeDiffFile, ForgeDiffHunk } from '../types.ts'
|
|
10
|
+
import { hunkRows } from './diff-rows.ts'
|
|
11
|
+
|
|
12
|
+
/** The changes a view shows, which decides where a thread can sit. */
|
|
13
|
+
export interface ThreadView {
|
|
14
|
+
/** Head commit the threads were read at. */
|
|
15
|
+
readonly head: string
|
|
16
|
+
/** Commit the shown changes end at: the head, or the last chosen commit. */
|
|
17
|
+
readonly end: string
|
|
18
|
+
/** Chosen consecutive commits, or null for the whole change request. */
|
|
19
|
+
readonly commits: ForgeCommitRange | null
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Where a thread shows in the changes view: on lines of one side, at the top of its file, in its file's outdated
|
|
24
|
+
* section, or nowhere in the file because the chosen commits do not hold the lines it is on.
|
|
25
|
+
*/
|
|
26
|
+
export type ThreadPlace =
|
|
27
|
+
| { readonly kind: 'line'; readonly side: 'L' | 'R'; readonly startLine: number; readonly endLine: number }
|
|
28
|
+
| { readonly kind: 'file' }
|
|
29
|
+
| { readonly kind: 'outdated' }
|
|
30
|
+
| { readonly kind: 'elsewhere' }
|
|
31
|
+
|
|
32
|
+
/** One filter of the thread list: the threads by whether they are resolved. */
|
|
33
|
+
export type ThreadFilter = 'unresolved' | 'resolved' | 'all'
|
|
34
|
+
|
|
35
|
+
/** Filters of the thread list in display order. */
|
|
36
|
+
export const THREAD_FILTERS: readonly ThreadFilter[] = ['unresolved', 'resolved', 'all']
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Place a thread in a view. The whole change request shows a thread at its position on the head. Chosen commits show
|
|
40
|
+
* only a new-side thread whose lines number the file at the last chosen commit: its position on the head when the
|
|
41
|
+
* commits end at the head, else the lines it was written on at that commit. The old side of chosen commits starts
|
|
42
|
+
* at the parent of the first commit, which no thread position numbers.
|
|
43
|
+
* @param thread - the thread.
|
|
44
|
+
* @param view - the shown changes.
|
|
45
|
+
* @returns where the thread shows.
|
|
46
|
+
*/
|
|
47
|
+
export function placeThread(thread: ForgeReviewThread, view: ThreadView): ThreadPlace {
|
|
48
|
+
if (thread.subject === 'file') return { kind: 'file' }
|
|
49
|
+
if (view.commits === null) return thread.current === null ? { kind: 'outdated' } : onLines(thread.side, thread.current)
|
|
50
|
+
if (thread.side === 'R' && thread.current !== null && view.end === view.head) return onLines('R', thread.current)
|
|
51
|
+
if (thread.side === 'R' && thread.original !== null && thread.original.commit === view.end) return onLines('R', thread.original)
|
|
52
|
+
return thread.current === null ? { kind: 'outdated' } : { kind: 'elsewhere' }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function onLines(side: 'L' | 'R', anchor: ForgeAnchor): ThreadPlace {
|
|
56
|
+
return { kind: 'line', side, startLine: anchor.startLine, endLine: anchor.endLine }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Name a side and lines, such as `R12` or `L3–5`.
|
|
61
|
+
* @param side - old or new side.
|
|
62
|
+
* @param anchor - the lines.
|
|
63
|
+
* @returns the range label.
|
|
64
|
+
*/
|
|
65
|
+
export function anchorRange(side: 'L' | 'R', anchor: Pick<ForgeAnchor, 'startLine' | 'endLine'>): string {
|
|
66
|
+
return `${side}${anchor.startLine}${anchor.endLine === anchor.startLine ? '' : `–${anchor.endLine}`}`
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Where a line of a diff file is: in the collapsed lines before a hunk or after the last one, or in a hunk. */
|
|
70
|
+
type LineSpot = { readonly kind: 'gap'; readonly index: number } | { readonly kind: 'hunk'; readonly index: number; readonly hunk: ForgeDiffHunk }
|
|
71
|
+
|
|
72
|
+
function spotOf(file: ForgeDiffFile, side: 'L' | 'R', line: number): LineSpot {
|
|
73
|
+
for (const [index, hunk] of file.hunks.entries()) {
|
|
74
|
+
const start = side === 'L' ? hunk.oldStart : hunk.newStart
|
|
75
|
+
const count = side === 'L' ? hunk.oldLines : hunk.newLines
|
|
76
|
+
if (line < start) return { kind: 'gap', index }
|
|
77
|
+
if (line < start + count) return { kind: 'hunk', index, hunk }
|
|
78
|
+
}
|
|
79
|
+
return { kind: 'gap', index: file.hunks.length }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Find the collapsed unchanged lines that hold a line: the index of the hunk they precede, or the hunk count after the last hunk.
|
|
84
|
+
* @param file - the diff file.
|
|
85
|
+
* @param side - side the line numbers.
|
|
86
|
+
* @param line - the line.
|
|
87
|
+
* @returns the gap index, or undefined when a hunk shows the line.
|
|
88
|
+
*/
|
|
89
|
+
export function gapOf(file: ForgeDiffFile, side: 'L' | 'R', line: number): number | undefined {
|
|
90
|
+
const spot = spotOf(file, side, line)
|
|
91
|
+
return spot.kind === 'gap' ? spot.index : undefined
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Order a file's threads as its section shows them: threads on the whole file, then threads on lines by the row of their
|
|
96
|
+
* last line, then outdated threads, then threads the chosen commits do not show. Threads on unchanged lines sort
|
|
97
|
+
* by the collapsed lines that hold them.
|
|
98
|
+
* @param items - threads of one file with their places.
|
|
99
|
+
* @param file - the file's diff, or undefined when the shown changes lack the file.
|
|
100
|
+
* @returns the threads in display order.
|
|
101
|
+
*/
|
|
102
|
+
export function inFileOrder<Item extends { readonly place: ThreadPlace }>(items: readonly Item[], file: ForgeDiffFile | undefined): Item[] {
|
|
103
|
+
const rank = ({ place }: Item): readonly [number, number, number] => {
|
|
104
|
+
switch (place.kind) {
|
|
105
|
+
case 'file': return [0, 0, 0]
|
|
106
|
+
case 'line': return [1, ...rowOf(file, place.side, place.endLine)]
|
|
107
|
+
case 'outdated': return [2, 0, 0]
|
|
108
|
+
case 'elsewhere': return [3, 0, 0]
|
|
109
|
+
/* v8 ignore next -- closed ThreadPlace union exhaustiveness guard */
|
|
110
|
+
default: return assertNever(place, 'ThreadPlace')
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const keyed = items.map(item => [rank(item), item] as const)
|
|
114
|
+
return keyed.toSorted(([a], [b]) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]).map(([, item]) => item)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Where a line draws: twice the index of the collapsed lines that hold it, or twice a hunk's index plus one with the
|
|
119
|
+
* line's row in that hunk, so rows of one hunk keep their patch order across both sides.
|
|
120
|
+
*/
|
|
121
|
+
function rowOf(file: ForgeDiffFile | undefined, side: 'L' | 'R', line: number): readonly [number, number] {
|
|
122
|
+
if (file === undefined) return [0, line]
|
|
123
|
+
const spot = spotOf(file, side, line)
|
|
124
|
+
if (spot.kind === 'gap') return [2 * spot.index, line]
|
|
125
|
+
return [2 * spot.index + 1, hunkRows(spot.hunk).findIndex(row => (side === 'L' ? row.old : row.new) === line)]
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Whether a thread passes a filter of the thread list.
|
|
130
|
+
* @param thread - the thread.
|
|
131
|
+
* @param filter - the filter.
|
|
132
|
+
* @returns whether the list shows the thread.
|
|
133
|
+
*/
|
|
134
|
+
export function passesFilter(thread: ForgeReviewThread, filter: ThreadFilter): boolean {
|
|
135
|
+
switch (filter) {
|
|
136
|
+
case 'unresolved': return !thread.resolved
|
|
137
|
+
case 'resolved': return thread.resolved
|
|
138
|
+
case 'all': return true
|
|
139
|
+
/* v8 ignore next -- closed ThreadFilter union exhaustiveness guard */
|
|
140
|
+
default: return assertNever(filter, 'ThreadFilter')
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Lines of a recorded diff hunk shown before its last line, beside the lines the thread is on. */
|
|
145
|
+
const EXCERPT_CONTEXT = 3
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Cut a recorded diff hunk to its header and the lines that end it: the thread's lines on its side plus a few before.
|
|
149
|
+
* GitHub records the hunk up to the thread's last line.
|
|
150
|
+
* @param hunk - the recorded hunk, header first.
|
|
151
|
+
* @param side - side the thread's lines number.
|
|
152
|
+
* @param lines - how many lines the thread covers.
|
|
153
|
+
* @returns the header and the kept lines.
|
|
154
|
+
*/
|
|
155
|
+
export function hunkExcerpt(hunk: string, side: 'L' | 'R', lines: number): { readonly header: string | null; readonly lines: readonly string[] } {
|
|
156
|
+
const header = hunk.startsWith('@@') ? hunk.split('\n', 1).join('') : null
|
|
157
|
+
const body = (header === null ? hunk : hunk.slice(header.length + 1)).split('\n')
|
|
158
|
+
const other = side === 'L' ? '+' : '-'
|
|
159
|
+
const kept: string[] = []
|
|
160
|
+
let wanted = lines + EXCERPT_CONTEXT
|
|
161
|
+
for (const line of body.toReversed()) {
|
|
162
|
+
if (wanted === 0) break
|
|
163
|
+
kept.push(line)
|
|
164
|
+
if (!line.startsWith(other)) wanted -= 1
|
|
165
|
+
}
|
|
166
|
+
return { header, lines: kept.reverse() }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Describe a composer quote of a thread by its file, range, and first comment.
|
|
171
|
+
* @param thread - the quoted thread.
|
|
172
|
+
* @param range - the thread's range label, or null for a file thread.
|
|
173
|
+
* @returns the composer's display of the quote.
|
|
174
|
+
*/
|
|
175
|
+
export function threadQuoteSummary(thread: ForgeReviewThread, range: string | null): QuoteSummary {
|
|
176
|
+
const name = thread.path.slice(thread.path.lastIndexOf('/') + 1)
|
|
177
|
+
return { label: range === null ? name : `${name} ${range}`, title: thread.path, detail: range ?? thread.path, body: thread.comments[0]?.body ?? '' }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Format a whole thread for the ordinary, logged user message. Comment bodies are quoted line by line, so their
|
|
182
|
+
* Markdown cannot add headings to the message.
|
|
183
|
+
* @param thread - the thread.
|
|
184
|
+
* @param url - web page of the change request.
|
|
185
|
+
* @param index - one-based thread number in the composer.
|
|
186
|
+
* @returns Markdown content for an atomic composer quote.
|
|
187
|
+
*/
|
|
188
|
+
export function formatThreadQuote(thread: ForgeReviewThread, url: string, index: number): string {
|
|
189
|
+
const anchor = thread.current ?? thread.original
|
|
190
|
+
const lines = anchor === null ? '' : anchor.startLine === anchor.endLine ? `${anchor.startLine}` : `${anchor.startLine}-${anchor.endLine}`
|
|
191
|
+
// Old-side lines number the file at the diff's base, so the quote names the commit only as where that diff ends.
|
|
192
|
+
const where = thread.subject === 'file' || anchor === null ? 'Lines: whole file'
|
|
193
|
+
: thread.side === 'R' ? `Side: R\nLines: ${lines}\nCommit: ${anchor.commit}`
|
|
194
|
+
: `Side: L (old side: the lines number the file before the pull request's changes)\nLines: ${lines}\nDiff head: ${anchor.commit}`
|
|
195
|
+
const state = [thread.resolved ? 'resolved' : 'unresolved', ...thread.subject === 'line' && thread.current === null ? ['outdated'] : []].join(', ')
|
|
196
|
+
const fence = thread.diffHunk === null ? '' : '`'.repeat(Math.max(3, ...Array.from(thread.diffHunk.matchAll(/`+/g), match => match[0].length + 1)))
|
|
197
|
+
const hunk = thread.diffHunk === null ? '' : `Diff hunk:\n${fence}diff\n${thread.diffHunk}\n${fence}\n`
|
|
198
|
+
// A blank comment line becomes a bare `>` and comments follow one another without blank lines, since the composer
|
|
199
|
+
// quotes every line of the reference again.
|
|
200
|
+
const comments = thread.comments.map(comment => `${comment.author === null ? 'Unknown author' : `@${comment.author.login}`} (${comment.createdAt}):\n`
|
|
201
|
+
+ comment.body.split('\n').map(line => line.trim() === '' ? '>' : `> ${line}`).join('\n')).join('\n')
|
|
202
|
+
const link = thread.comments[0]?.url ?? url
|
|
203
|
+
return `## Review Thread ${index}\nFile: ${thread.path}\n${where}\nState: ${state}\nSource: ${link}\n${hunk}Comments:\n${comments}`
|
|
204
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/** Frozen line selections shared by the diff UI, composer references, and review submission. */
|
|
2
|
+
|
|
3
|
+
import type { QuoteSummary } from '@deepseek-ai/dsh-client-ui-primitives'
|
|
4
|
+
import type { ForgeCommitRange, ForgeDiffFile } from '../types.ts'
|
|
5
|
+
|
|
6
|
+
/** A comment on consecutive lines of one side of one displayed hunk. */
|
|
7
|
+
export interface ReviewComment {
|
|
8
|
+
/** Repository-relative destination path, including for renamed files. */
|
|
9
|
+
readonly path: string
|
|
10
|
+
/** Old or new side of the displayed diff. */
|
|
11
|
+
readonly side: 'L' | 'R'
|
|
12
|
+
/** First selected line, inclusive. */
|
|
13
|
+
readonly startLine: number
|
|
14
|
+
/** Last selected line, inclusive. */
|
|
15
|
+
readonly endLine: number
|
|
16
|
+
/** Complete displayed hunk captured before subsequent file changes. */
|
|
17
|
+
readonly hunk: string
|
|
18
|
+
/** User's plain-text comment. */
|
|
19
|
+
readonly body: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The immutable source from which a comment was selected. `commits` names the consecutive commits shown instead of
|
|
24
|
+
* the whole change request or `branch` scope, or is null for all of them.
|
|
25
|
+
*/
|
|
26
|
+
export type ReviewSource =
|
|
27
|
+
/**
|
|
28
|
+
* Local changes in a scope. `base` names what the `branch` scope compared with for the reader, and is null for
|
|
29
|
+
* other scopes; `baseRef` identifies the comparison: the chosen ref, or null for the automatic fork point and other scopes.
|
|
30
|
+
*/
|
|
31
|
+
| {
|
|
32
|
+
readonly kind: 'local'
|
|
33
|
+
readonly scope: string
|
|
34
|
+
readonly base: string | null
|
|
35
|
+
readonly baseRef: string | null
|
|
36
|
+
readonly commits: ForgeCommitRange | null
|
|
37
|
+
}
|
|
38
|
+
/** A change request; `headSha` is the commit the shown changes end at: the head, or the last chosen commit. */
|
|
39
|
+
| { readonly kind: 'change'; readonly url: string; readonly headSha: string; readonly commits: ForgeCommitRange | null }
|
|
40
|
+
|
|
41
|
+
/** One selected range and the source the user saw. */
|
|
42
|
+
export interface ReviewSelection {
|
|
43
|
+
readonly source: ReviewSource
|
|
44
|
+
readonly comment: ReviewComment
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Capture a range only when both endpoints belong to the same displayed hunk.
|
|
49
|
+
* @param file - loaded diff file.
|
|
50
|
+
* @param side - selected side.
|
|
51
|
+
* @param from - first gesture endpoint.
|
|
52
|
+
* @param to - current gesture endpoint.
|
|
53
|
+
* @returns a frozen selection, or undefined for unchanged gaps or cross-hunk ranges.
|
|
54
|
+
*/
|
|
55
|
+
export function selectReviewLines(file: ForgeDiffFile, side: 'L' | 'R', from: number, to: number): ReviewComment | undefined {
|
|
56
|
+
const startLine = Math.min(from, to)
|
|
57
|
+
const endLine = Math.max(from, to)
|
|
58
|
+
const hunk = file.hunks.find((item) => {
|
|
59
|
+
const start = side === 'L' ? item.oldStart : item.newStart
|
|
60
|
+
const count = side === 'L' ? item.oldLines : item.newLines
|
|
61
|
+
return startLine >= start && endLine < start + count
|
|
62
|
+
})
|
|
63
|
+
if (hunk === undefined) return undefined
|
|
64
|
+
const oldStart = hunk.oldLines === 0 ? hunk.oldStart - 1 : hunk.oldStart
|
|
65
|
+
const newStart = hunk.newLines === 0 ? hunk.newStart - 1 : hunk.newStart
|
|
66
|
+
return {
|
|
67
|
+
path: file.path, side, startLine, endLine, body: '',
|
|
68
|
+
hunk: [`@@ -${oldStart},${hunk.oldLines} +${newStart},${hunk.newLines} @@`, ...hunk.lines].join('\n'),
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Name a comment's side and lines, such as `R12` or `L3–5`.
|
|
74
|
+
* @param comment - the commented range.
|
|
75
|
+
* @returns the range label shown beside comments and in composer quotes.
|
|
76
|
+
*/
|
|
77
|
+
export function reviewRange(comment: ReviewComment): string {
|
|
78
|
+
return `${comment.side}${comment.startLine}${comment.endLine === comment.startLine ? '' : `–${comment.endLine}`}`
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Describe a composer quote of a comment by its file, range, and text instead of the whole message reference.
|
|
83
|
+
* @param comment - the quoted comment.
|
|
84
|
+
* @returns the composer's display of the quote.
|
|
85
|
+
*/
|
|
86
|
+
export function reviewQuoteSummary(comment: ReviewComment): QuoteSummary {
|
|
87
|
+
const range = reviewRange(comment)
|
|
88
|
+
return { label: `${comment.path.slice(comment.path.lastIndexOf('/') + 1)} ${range}`, title: comment.path, detail: range, body: comment.body }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Format a frozen reference for the ordinary, logged user message.
|
|
93
|
+
* @param selection - source, range, displayed patch, and user comment.
|
|
94
|
+
* @param index - one-based reference number in the composer.
|
|
95
|
+
* @returns Markdown content for an atomic composer quote.
|
|
96
|
+
*/
|
|
97
|
+
export function formatReviewComment(selection: ReviewSelection, index: number): string {
|
|
98
|
+
const { source, comment } = selection
|
|
99
|
+
const range = comment.startLine === comment.endLine ? String(comment.startLine) : `${comment.startLine}-${comment.endLine}`
|
|
100
|
+
// A longer fence keeps code containing Markdown fences inside the quoted hunk.
|
|
101
|
+
const fence = '`'.repeat(Math.max(3, ...Array.from(comment.hunk.matchAll(/`+/g), match => match[0].length + 1)))
|
|
102
|
+
const commits = source.commits === null ? ''
|
|
103
|
+
: `\nCommits: ${source.commits.from === source.commits.to ? source.commits.from : `${source.commits.from}^..${source.commits.to}`}`
|
|
104
|
+
const origin = source.kind === 'change' ? `Source: ${source.url}\nCommit: ${source.headSha}${commits}`
|
|
105
|
+
: `Source: Local (${source.scope})${source.base === null ? '' : `\nBase: ${source.base}`}${commits}`
|
|
106
|
+
return `## User Comment ${index}\nFile: ${comment.path}\nSide: ${comment.side}\nLines: ${range}\n${origin}\nDiff hunk:\n${fence}diff\n${comment.hunk}\n${fence}\nComment:\n${comment.body}`
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Check whether the current diff still contains the captured range and hunk.
|
|
111
|
+
* @param selection - saved comment source and patch.
|
|
112
|
+
* @param source - currently displayed review source.
|
|
113
|
+
* @param file - current diff file.
|
|
114
|
+
* @returns whether an inline record can retain its original line placement.
|
|
115
|
+
*/
|
|
116
|
+
export function reviewMatches(selection: ReviewSelection, source: ReviewSource, file: ForgeDiffFile): boolean {
|
|
117
|
+
if (selection.source.kind === 'change' && (source.kind !== 'change' || source.headSha !== selection.source.headSha)) return false
|
|
118
|
+
const comment = selection.comment
|
|
119
|
+
return selectReviewLines(file, comment.side, comment.startLine, comment.endLine)?.hunk === comment.hunk
|
|
120
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reach of the changes view's file sections: a section's lines mount once the
|
|
3
|
+
* section first comes within a margin of the files scrollport, and stay
|
|
4
|
+
* mounted, so long diffs draw only what the reader approaches.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Distance beyond the scrollport's edges within which a section counts as reached. */
|
|
8
|
+
const REACH_MARGIN = '1200px 0px'
|
|
9
|
+
|
|
10
|
+
const noop = (): void => {}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Watches the file sections of one changes view against its scrollport. Without
|
|
14
|
+
* `IntersectionObserver` every section is reached at once.
|
|
15
|
+
*/
|
|
16
|
+
export class SectionReach {
|
|
17
|
+
private observer: IntersectionObserver | undefined
|
|
18
|
+
private readonly waiting = new Map<Element, () => void>()
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Measure sections against `root`, or stop measuring when it unmounts. Sections that are still waiting move to the new root.
|
|
22
|
+
* @param root - the files scrollport, or null.
|
|
23
|
+
*/
|
|
24
|
+
attach(root: HTMLElement | null): void {
|
|
25
|
+
this.observer?.disconnect()
|
|
26
|
+
this.observer = undefined
|
|
27
|
+
if (root === null || typeof IntersectionObserver === 'undefined') return
|
|
28
|
+
const observer = new IntersectionObserver((entries) => {
|
|
29
|
+
for (const entry of entries) {
|
|
30
|
+
if (!entry.isIntersecting) continue
|
|
31
|
+
const reached = this.waiting.get(entry.target)
|
|
32
|
+
/* v8 ignore next -- the observer reports only sections still waiting, which it stops watching on release */
|
|
33
|
+
if (reached === undefined) continue
|
|
34
|
+
this.waiting.delete(entry.target)
|
|
35
|
+
observer.unobserve(entry.target)
|
|
36
|
+
reached()
|
|
37
|
+
}
|
|
38
|
+
}, { root, rootMargin: REACH_MARGIN })
|
|
39
|
+
this.observer = observer
|
|
40
|
+
for (const element of this.waiting.keys()) observer.observe(element)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Call `reached` once when `element` comes within reach.
|
|
45
|
+
* @param element - a file section inside the scrollport.
|
|
46
|
+
* @param reached - runs once, from the observer's callback or at once without `IntersectionObserver`.
|
|
47
|
+
* @returns the disposer that stops watching before the section is reached.
|
|
48
|
+
*/
|
|
49
|
+
observe(element: Element, reached: () => void): () => void {
|
|
50
|
+
if (typeof IntersectionObserver === 'undefined') {
|
|
51
|
+
reached()
|
|
52
|
+
return noop
|
|
53
|
+
}
|
|
54
|
+
this.waiting.set(element, reached)
|
|
55
|
+
this.observer?.observe(element)
|
|
56
|
+
return () => {
|
|
57
|
+
this.waiting.delete(element)
|
|
58
|
+
this.observer?.unobserve(element)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|