@eliya-oss/agent-diff 0.1.0-alpha.1 → 0.1.0-alpha.100

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 (112) hide show
  1. package/.agents/adr/README.md +3 -0
  2. package/.agents/prd/README.md +2 -2
  3. package/CHANGELOG.md +666 -0
  4. package/README.md +37 -79
  5. package/bin/adiff.js +80 -2
  6. package/dist/main.js +17611 -0
  7. package/package.json +14 -10
  8. package/skills/adiff/SKILL.md +140 -26
  9. package/src/cli/catalog.ts +0 -178
  10. package/src/cli/commands.ts +0 -335
  11. package/src/cli/error.ts +0 -44
  12. package/src/cli/index.ts +0 -24
  13. package/src/cli/parse.ts +0 -31
  14. package/src/cli/report.ts +0 -96
  15. package/src/cli/story.ts +0 -223
  16. package/src/domain/narrative/coverage.ts +0 -54
  17. package/src/domain/narrative/index.ts +0 -4
  18. package/src/domain/narrative/model.ts +0 -46
  19. package/src/domain/narrative/version.ts +0 -63
  20. package/src/domain/patch/anchor.ts +0 -40
  21. package/src/domain/patch/index.ts +0 -7
  22. package/src/domain/patch/model.ts +0 -54
  23. package/src/domain/patch/parse.ts +0 -106
  24. package/src/domain/patch/render.ts +0 -48
  25. package/src/domain/patch/sticky.ts +0 -39
  26. package/src/domain/review/index.ts +0 -4
  27. package/src/domain/review/lifecycle.ts +0 -45
  28. package/src/domain/review/model.ts +0 -23
  29. package/src/domain/review/vouch.ts +0 -15
  30. package/src/main.ts +0 -186
  31. package/src/service/git/error.ts +0 -16
  32. package/src/service/git/index.ts +0 -3
  33. package/src/service/git/model.ts +0 -13
  34. package/src/service/git/run.ts +0 -27
  35. package/src/service/git/service.ts +0 -133
  36. package/src/service/store/error.ts +0 -11
  37. package/src/service/store/index.ts +0 -5
  38. package/src/service/store/model.ts +0 -33
  39. package/src/service/store/paths.ts +0 -25
  40. package/src/service/store/service.ts +0 -154
  41. package/src/testing/agent-surface.test.ts +0 -92
  42. package/src/testing/batch.test.ts +0 -93
  43. package/src/testing/branch-state.test.ts +0 -99
  44. package/src/testing/chrome-tone.test.ts +0 -57
  45. package/src/testing/comment-block.test.ts +0 -84
  46. package/src/testing/comment-walk.test.ts +0 -96
  47. package/src/testing/compose-box.test.ts +0 -97
  48. package/src/testing/compose.test.ts +0 -67
  49. package/src/testing/context.test.ts +0 -77
  50. package/src/testing/cursor.test.ts +0 -49
  51. package/src/testing/delivery.test.ts +0 -94
  52. package/src/testing/domains/agent/driver.ts +0 -50
  53. package/src/testing/domains/agent/index.ts +0 -2
  54. package/src/testing/domains/app/driver.ts +0 -168
  55. package/src/testing/domains/app/index.ts +0 -2
  56. package/src/testing/domains/branch/driver.ts +0 -51
  57. package/src/testing/domains/branch/index.ts +0 -3
  58. package/src/testing/domains/branch/model.ts +0 -39
  59. package/src/testing/domains/screen/driver.ts +0 -241
  60. package/src/testing/domains/screen/index.ts +0 -2
  61. package/src/testing/driver.ts +0 -31
  62. package/src/testing/escape.test.ts +0 -56
  63. package/src/testing/focus.test.ts +0 -104
  64. package/src/testing/footer-chips.test.ts +0 -65
  65. package/src/testing/footer.test.ts +0 -64
  66. package/src/testing/gaps.test.ts +0 -47
  67. package/src/testing/highlight.test.ts +0 -49
  68. package/src/testing/home-screen.test.ts +0 -51
  69. package/src/testing/index.ts +0 -4
  70. package/src/testing/inline-comments.test.ts +0 -131
  71. package/src/testing/marks.test.ts +0 -49
  72. package/src/testing/mouse.test.ts +0 -87
  73. package/src/testing/navigate.test.ts +0 -73
  74. package/src/testing/palette.test.ts +0 -68
  75. package/src/testing/pending.test.ts +0 -70
  76. package/src/testing/pin-align.test.ts +0 -37
  77. package/src/testing/progress.test.ts +0 -88
  78. package/src/testing/report.test.ts +0 -83
  79. package/src/testing/resume.test.ts +0 -39
  80. package/src/testing/review.test.ts +0 -181
  81. package/src/testing/roundtrip.test.ts +0 -70
  82. package/src/testing/scale.test.ts +0 -55
  83. package/src/testing/scroll-feel.test.ts +0 -81
  84. package/src/testing/sent-comments.test.ts +0 -106
  85. package/src/testing/state.ts +0 -55
  86. package/src/testing/sticky-removed.test.ts +0 -67
  87. package/src/testing/sticky.test.ts +0 -90
  88. package/src/testing/story-rail.test.ts +0 -122
  89. package/src/testing/story.test.ts +0 -183
  90. package/src/testing/stress.test.ts +0 -74
  91. package/src/testing/tree-collapse.test.ts +0 -108
  92. package/src/testing/tree-rows.test.ts +0 -66
  93. package/src/testing/tree.test.ts +0 -91
  94. package/src/testing/tui.test.ts +0 -73
  95. package/src/testing/typing.test.ts +0 -65
  96. package/src/testing/vouch-next.test.ts +0 -65
  97. package/src/testing/vouch-tui.test.ts +0 -77
  98. package/src/testing/wash.test.ts +0 -32
  99. package/src/testing/width.test.ts +0 -62
  100. package/src/tui/app.ts +0 -467
  101. package/src/tui/command.ts +0 -390
  102. package/src/tui/diffview.ts +0 -333
  103. package/src/tui/index.ts +0 -9
  104. package/src/tui/marks.ts +0 -102
  105. package/src/tui/match.ts +0 -34
  106. package/src/tui/model.ts +0 -310
  107. package/src/tui/reduce.ts +0 -305
  108. package/src/tui/render.ts +0 -897
  109. package/src/tui/report.ts +0 -74
  110. package/src/tui/session.ts +0 -32
  111. package/src/tui/theme.ts +0 -61
  112. package/src/tui/tree.ts +0 -149
package/src/cli/story.ts DELETED
@@ -1,223 +0,0 @@
1
- import { realpath } from "node:fs/promises"
2
- import { Effect, Option } from "effect"
3
- import {
4
- codeBlocks,
5
- statusOf,
6
- withFullCoverage,
7
- type Span,
8
- type Step,
9
- type Story,
10
- type StoryBlock,
11
- } from "../domain/narrative/index.ts"
12
- import type { Patch } from "../domain/patch/index.ts"
13
- import { Git, type Worktree } from "../service/git/index.ts"
14
- import { Store, type StoredStory } from "../service/store/index.ts"
15
- import { findBranch, patchesOf } from "./commands.ts"
16
- import { MalformedStory, NoStory, UnknownWorktree } from "./error.ts"
17
-
18
- export type StoryStep = {
19
- readonly title: string
20
- readonly files: ReadonlyArray<string>
21
- readonly spans: ReadonlyArray<Span>
22
- }
23
-
24
- export type StoryReport = {
25
- readonly version: number
26
- readonly parent: number | undefined
27
- readonly head: string
28
- readonly branchHead: string
29
- readonly stale: boolean
30
- readonly written: string
31
- readonly summary: string
32
- readonly covered: number
33
- readonly total: number
34
- readonly uncovered: ReadonlyArray<Span>
35
- readonly vanished: ReadonlyArray<string>
36
- readonly steps: ReadonlyArray<StoryStep>
37
- }
38
-
39
- type StepInput = {
40
- readonly title?: unknown
41
- readonly note?: unknown
42
- readonly blocks?: unknown
43
- readonly spans?: unknown
44
- }
45
-
46
- const unique = (paths: ReadonlyArray<string>): ReadonlyArray<string> => [...new Set(paths)]
47
-
48
- const spansOfStep = (step: Step): ReadonlyArray<Span> =>
49
- codeBlocks(step).map(({ path, start, end }) => ({ path, start, end }))
50
-
51
- const isSpan = (value: unknown): value is Span => {
52
- const span = value as Partial<Span> | undefined
53
- return (
54
- typeof span?.path === "string" &&
55
- typeof span.start === "number" &&
56
- typeof span.end === "number"
57
- )
58
- }
59
-
60
- const isBlock = (value: unknown): value is StoryBlock => {
61
- const block = value as { kind?: unknown; markdown?: unknown } | undefined
62
- if (block?.kind === "prose") return typeof block.markdown === "string"
63
- return block?.kind === "code" && isSpan(value)
64
- }
65
-
66
- const codeBlockOf = (span: Span): StoryBlock => ({
67
- kind: "code",
68
- path: span.path,
69
- start: span.start,
70
- end: span.end,
71
- })
72
-
73
- const blocksOf = (step: StepInput): ReadonlyArray<StoryBlock> => {
74
- if (Array.isArray(step.blocks)) return step.blocks.filter(isBlock)
75
- const note = typeof step.note === "string" ? [{ kind: "prose" as const, markdown: step.note }] : []
76
- const spans = Array.isArray(step.spans) ? step.spans.filter(isSpan) : []
77
- return [...note, ...spans.map(codeBlockOf)]
78
- }
79
-
80
- const stepOf = (value: unknown): Option.Option<Step> => {
81
- const step = value as StepInput | undefined
82
- const title = typeof step?.title === "string" ? step.title.trim() : ""
83
- if (step === undefined || title.length === 0) return Option.none()
84
- const blocks = blocksOf(step)
85
- return blocks.length === 0 ? Option.none() : Option.some({ title, blocks })
86
- }
87
-
88
- const parsed = (text: string): Option.Option<Record<string, unknown>> => {
89
- try {
90
- const value: unknown = JSON.parse(text)
91
- return typeof value === "object" && value !== null
92
- ? Option.some(value as Record<string, unknown>)
93
- : Option.none()
94
- } catch {
95
- return Option.none()
96
- }
97
- }
98
-
99
- export const readStory = Effect.fn("Cli.readStory")(function* (text: string) {
100
- const document = yield* Option.match(parsed(text), {
101
- onNone: () => new MalformedStory({ reason: "the story is not a JSON object" }),
102
- onSome: Effect.succeed,
103
- })
104
- const raw = document["steps"]
105
- const steps = (Array.isArray(raw) ? raw : []).flatMap((entry) =>
106
- Option.match(stepOf(entry), { onNone: (): ReadonlyArray<Step> => [], onSome: (step) => [step] }),
107
- )
108
- if (steps.length === 0) {
109
- return yield* new MalformedStory({
110
- reason: "every step needs a title and at least one span or block",
111
- })
112
- }
113
- const summary = document["summary"]
114
- return { summary: typeof summary === "string" ? summary : "", steps }
115
- })
116
-
117
- const fromStored = (stored: StoredStory): Story => ({
118
- ...stored,
119
- parent: Option.fromNullishOr(stored.parent),
120
- })
121
-
122
- const toStored = (story: Story): StoredStory => ({
123
- ...story,
124
- parent: Option.getOrUndefined(story.parent),
125
- })
126
-
127
- const stepsOf = (patches: ReadonlyArray<Patch>, story: Story): ReadonlyArray<StoryStep> => {
128
- const present = new Set(patches.map((patch) => patch.path))
129
- return withFullCoverage(patches, story).steps.map((step) => ({
130
- title: step.title,
131
- files: unique(spansOfStep(step).map((span) => span.path)).filter((path) => present.has(path)),
132
- spans: spansOfStep(step),
133
- }))
134
- }
135
-
136
- const reportOf = (
137
- patches: ReadonlyArray<Patch>,
138
- story: Story,
139
- branchHead: string,
140
- ): StoryReport => {
141
- const status = statusOf(patches, story, branchHead)
142
- return {
143
- version: status.version,
144
- parent: Option.getOrUndefined(status.parent),
145
- head: status.storyHead,
146
- branchHead: status.branchHead,
147
- stale: status.stale,
148
- written: story.written,
149
- summary: story.summary,
150
- covered: status.covered,
151
- total: status.total,
152
- uncovered: status.uncovered,
153
- vanished: status.vanished,
154
- steps: stepsOf(patches, story),
155
- }
156
- }
157
-
158
- const resolve = (path: string): Promise<string> => realpath(path).catch(() => path)
159
-
160
- const resolveAll = (paths: ReadonlyArray<string>): Promise<ReadonlyArray<string>> =>
161
- Promise.all(paths.map(resolve))
162
-
163
- const worktreeAt = Effect.fn("Cli.worktreeAt")(function* (worktreePath: string) {
164
- const git = yield* Git
165
- const asked = yield* Effect.promise(() => resolve(worktreePath))
166
- const worktrees = yield* git.worktrees(asked)
167
- const paths = worktrees.map((entry) => entry.path)
168
- const resolved = yield* Effect.promise(() => resolveAll(paths))
169
- const at = resolved.indexOf(asked)
170
- const found = worktrees[at]
171
- return yield* found === undefined
172
- ? new UnknownWorktree({ worktree: asked, known: worktrees.map((entry) => entry.path) })
173
- : Effect.succeed(found)
174
- })
175
-
176
- const storyOf = Effect.fn("Cli.storyOf")(function* (worktree: Worktree) {
177
- const store = yield* Store
178
- return Option.map(yield* store.story(worktree.path), fromStored)
179
- })
180
-
181
- export const setStory = Effect.fn("Cli.setStory")(function* (
182
- worktreePath: string,
183
- text: string,
184
- written: string,
185
- ) {
186
- const store = yield* Store
187
- const worktree = yield* worktreeAt(worktreePath)
188
- const input = yield* readStory(text)
189
- const patches = yield* patchesOf(worktree)
190
- const previous = yield* storyOf(worktree)
191
- const story: Story = {
192
- version: Option.match(previous, { onNone: () => 1, onSome: (old) => old.version + 1 }),
193
- parent: Option.map(previous, (old) => old.version),
194
- head: worktree.head,
195
- base: worktree.base,
196
- written,
197
- summary: input.summary,
198
- steps: input.steps,
199
- }
200
- yield* store.saveStory(worktree.path, toStored(story))
201
- return reportOf(patches, story, worktree.head)
202
- })
203
-
204
- export const showStory = Effect.fn("Cli.showStory")(function* (worktreePath: string) {
205
- const worktree = yield* worktreeAt(worktreePath)
206
- const patches = yield* patchesOf(worktree)
207
- const found = yield* storyOf(worktree)
208
- const story = yield* Option.match(found, {
209
- onNone: () => new NoStory({ worktree: worktree.path }),
210
- onSome: Effect.succeed,
211
- })
212
- return reportOf(patches, story, worktree.head)
213
- })
214
-
215
- export const listStorySteps = Effect.fn("Cli.listStorySteps")(function* (
216
- repo: string,
217
- branch: string,
218
- ) {
219
- const worktree = yield* findBranch(repo, branch)
220
- const found = yield* storyOf(worktree)
221
- if (Option.isNone(found)) return [] as ReadonlyArray<StoryStep>
222
- return stepsOf(yield* patchesOf(worktree), found.value)
223
- })
@@ -1,54 +0,0 @@
1
- import { Option } from "effect"
2
- import type { Hunk, Patch } from "../patch/index.ts"
3
- import { REMAINDER_TITLE, type Coverage, type Span, type Step, type Story, type StoryBlock } from "./model.ts"
4
-
5
- type CodeBlock = Extract<StoryBlock, { kind: "code" }>
6
-
7
- const codeBlockOf = (span: Span): CodeBlock => ({
8
- kind: "code",
9
- path: span.path,
10
- start: span.start,
11
- end: span.end,
12
- })
13
-
14
- export const codeBlocks = (step: Step): ReadonlyArray<CodeBlock> =>
15
- step.blocks.filter((block): block is CodeBlock => block.kind === "code")
16
-
17
- export const spansOf = (steps: ReadonlyArray<Step>): ReadonlyArray<Span> =>
18
- steps.flatMap((step) => codeBlocks(step).map(({ path, start, end }) => ({ path, start, end })))
19
-
20
- const spanOfHunk = (patch: Patch, hunk: Hunk): Option.Option<Span> => {
21
- const lines = hunk.rows.flatMap((row) =>
22
- Option.match(Option.orElse(row.newLine, () => row.oldLine), { onNone: () => [], onSome: (n) => [n] }),
23
- )
24
- return lines.length === 0
25
- ? Option.none()
26
- : Option.some({ path: patch.path, start: Math.min(...lines), end: Math.max(...lines) })
27
- }
28
-
29
- const overlaps = (a: Span, b: Span): boolean => a.path === b.path && a.start <= b.end && a.end >= b.start
30
-
31
- export const coverage = (patches: ReadonlyArray<Patch>, steps: ReadonlyArray<Step>): Coverage => {
32
- const claimed = spansOf(steps)
33
- const hunks = patches.flatMap((patch) =>
34
- patch.hunks.flatMap((hunk) => Option.match(spanOfHunk(patch, hunk), { onNone: () => [], onSome: (s) => [s] })),
35
- )
36
- const missing = hunks.filter((hunk) => !claimed.some((span) => overlaps(span, hunk)))
37
- return { total: hunks.length, covered: hunks.length - missing.length, missing }
38
- }
39
-
40
- export const withFullCoverage = (patches: ReadonlyArray<Patch>, story: Story): Story => {
41
- const gap = coverage(patches, story.steps)
42
- if (gap.missing.length === 0) return story
43
- const remainder: Step = {
44
- title: REMAINDER_TITLE,
45
- blocks: [
46
- {
47
- kind: "prose",
48
- markdown: `${gap.missing.length} hunks the agent did not account for.`,
49
- },
50
- ...gap.missing.map((span) => codeBlockOf(span)),
51
- ],
52
- }
53
- return { ...story, steps: [...story.steps, remainder] }
54
- }
@@ -1,4 +0,0 @@
1
- export { codeBlocks, coverage, spansOf, withFullCoverage } from "./coverage.ts"
2
- export { describeStatus, reviseFor, statusOf } from "./version.ts"
3
- export { REMAINDER_TITLE } from "./model.ts"
4
- export type { Coverage, Span, Step, Story, StoryBlock, StoryStatus } from "./model.ts"
@@ -1,46 +0,0 @@
1
- import type { Option } from "effect"
2
-
3
- export type StoryBlock =
4
- | { readonly kind: "prose"; readonly markdown: string }
5
- | { readonly kind: "code"; readonly path: string; readonly start: number; readonly end: number }
6
-
7
- export type Step = {
8
- readonly title: string
9
- readonly blocks: ReadonlyArray<StoryBlock>
10
- }
11
-
12
- export type Story = {
13
- readonly version: number
14
- readonly head: string
15
- readonly base: string
16
- readonly parent: Option.Option<number>
17
- readonly written: string
18
- readonly summary: string
19
- readonly steps: ReadonlyArray<Step>
20
- }
21
-
22
- export type Span = {
23
- readonly path: string
24
- readonly start: number
25
- readonly end: number
26
- }
27
-
28
- export type Coverage = {
29
- readonly total: number
30
- readonly covered: number
31
- readonly missing: ReadonlyArray<Span>
32
- }
33
-
34
- export type StoryStatus = {
35
- readonly version: number
36
- readonly parent: Option.Option<number>
37
- readonly stale: boolean
38
- readonly storyHead: string
39
- readonly branchHead: string
40
- readonly uncovered: ReadonlyArray<Span>
41
- readonly vanished: ReadonlyArray<string>
42
- readonly covered: number
43
- readonly total: number
44
- }
45
-
46
- export const REMAINDER_TITLE = "not in any step"
@@ -1,63 +0,0 @@
1
- import { Option } from "effect"
2
- import type { Patch } from "../patch/index.ts"
3
- import { codeBlocks, coverage, spansOf, withFullCoverage } from "./coverage.ts"
4
- import type { Step, Story, StoryStatus } from "./model.ts"
5
-
6
- export const statusOf = (
7
- patches: ReadonlyArray<Patch>,
8
- story: Story,
9
- branchHead: string,
10
- ): StoryStatus => {
11
- const gap = coverage(patches, story.steps)
12
- const present = new Set(patches.map((patch) => patch.path))
13
- const mentioned = new Set(spansOf(story.steps).map((span) => span.path))
14
- return {
15
- version: story.version,
16
- parent: story.parent,
17
- stale: story.head !== branchHead,
18
- storyHead: story.head,
19
- branchHead,
20
- uncovered: gap.missing,
21
- vanished: [...mentioned].filter((path) => !present.has(path)),
22
- covered: gap.covered,
23
- total: gap.total,
24
- }
25
- }
26
-
27
- export const describeStatus = (status: StoryStatus): { line: string; warn: boolean } => {
28
- if (status.stale) {
29
- return { line: `v${status.version} written for ${status.storyHead}, head is ${status.branchHead}`, warn: true }
30
- }
31
- if (status.uncovered.length > 0) {
32
- return { line: `v${status.version} covers ${status.covered}/${status.total} hunks`, warn: true }
33
- }
34
- const lineage = Option.match(status.parent, {
35
- onNone: () => `v${status.version}`,
36
- onSome: (parent) => `v${status.version}, revised from v${parent}`,
37
- })
38
- return { line: `${lineage} covers the whole diff`, warn: false }
39
- }
40
-
41
- export const reviseFor = (
42
- previous: Story,
43
- branchHead: string,
44
- patches: ReadonlyArray<Patch>,
45
- written: string,
46
- ): Story => {
47
- const present = new Set(patches.map((patch) => patch.path))
48
- const kept: ReadonlyArray<Step> = previous.steps
49
- .map((step) => ({
50
- ...step,
51
- blocks: step.blocks.filter((block) => block.kind === "prose" || present.has(block.path)),
52
- }))
53
- .filter((step) => codeBlocks(step).length > 0)
54
-
55
- return withFullCoverage(patches, {
56
- ...previous,
57
- version: previous.version + 1,
58
- parent: Option.some(previous.version),
59
- head: branchHead,
60
- written,
61
- steps: kept,
62
- })
63
- }
@@ -1,40 +0,0 @@
1
- import { Option } from "effect"
2
- import { lineOn, type Anchor, type Patch, type Range, type Row, type Side } from "./model.ts"
3
-
4
- const sideOf = (rows: ReadonlyArray<Row>): Side =>
5
- rows.some((row) => Option.isSome(row.newLine)) ? "new" : "old"
6
-
7
- const linesOn = (rows: ReadonlyArray<Row>, side: Side): ReadonlyArray<number> =>
8
- rows.flatMap((row) => Option.match(lineOn(row, side), { onNone: () => [], onSome: (n) => [n] }))
9
-
10
- export const anchorFor = (patch: Patch, from: number, to: number): Option.Option<Anchor> => {
11
- const [low, high] = from <= to ? [from, to] : [to, from]
12
- const rows = patch.rows.slice(low, high + 1)
13
- if (rows.length === 0) return Option.none()
14
-
15
- const side = sideOf(rows)
16
- const lines = linesOn(rows, side)
17
- if (lines.length === 0) return Option.none()
18
-
19
- return Option.some({
20
- path: patch.path,
21
- blob: patch.blob,
22
- side,
23
- start: Math.min(...lines),
24
- end: Math.max(...lines),
25
- snippet: rows.map((row) => row.text).join("\n"),
26
- })
27
- }
28
-
29
- export const rowsForRange = (patch: Patch, range: Range): Option.Option<readonly [number, number]> => {
30
- const covered = patch.rows.filter((row) =>
31
- Option.match(lineOn(row, range.side), {
32
- onNone: () => false,
33
- onSome: (line) => line >= range.start && line <= range.end,
34
- }),
35
- )
36
- const first = covered[0]
37
- const last = covered.at(-1)
38
- if (first === undefined || last === undefined) return Option.none()
39
- return Option.some([first.index, last.index] as const)
40
- }
@@ -1,7 +0,0 @@
1
- export { anchorFor, rowsForRange } from "./anchor.ts"
2
- export { parsePatches } from "./parse.ts"
3
- export { renderPatch, renderWindow, scopeAbove } from "./render.ts"
4
- export { stickyChain } from "./sticky.ts"
5
- export type { Rendered, RenderedHunk } from "./render.ts"
6
- export { lineOn } from "./model.ts"
7
- export type { Anchor, Blob, FilePath, Hunk, Patch, Range, Row, RowKind, Side } from "./model.ts"
@@ -1,54 +0,0 @@
1
- import { Schema } from "effect"
2
- import type { Option } from "effect"
3
-
4
- export const Blob = Schema.String.pipe(Schema.brand("@adiff/Blob"))
5
- export type Blob = typeof Blob.Type
6
-
7
- export const FilePath = Schema.String.pipe(Schema.brand("@adiff/FilePath"))
8
- export type FilePath = typeof FilePath.Type
9
-
10
- export type Side = "old" | "new"
11
-
12
- export type RowKind = "context" | "added" | "removed"
13
-
14
- export type Row = {
15
- readonly index: number
16
- readonly kind: RowKind
17
- readonly oldLine: Option.Option<number>
18
- readonly newLine: Option.Option<number>
19
- readonly text: string
20
- }
21
-
22
- export type Hunk = {
23
- readonly marker: string
24
- readonly scope: string
25
- readonly startRow: number
26
- readonly rows: ReadonlyArray<Row>
27
- readonly skipped: number
28
- }
29
-
30
- export type Patch = {
31
- readonly path: string
32
- readonly previousPath: string
33
- readonly blob: string
34
- readonly headerLines: ReadonlyArray<string>
35
- readonly hunks: ReadonlyArray<Hunk>
36
- readonly rows: ReadonlyArray<Row>
37
- readonly added: number
38
- readonly removed: number
39
- }
40
-
41
- export type Range = {
42
- readonly side: Side
43
- readonly start: number
44
- readonly end: number
45
- }
46
-
47
- export type Anchor = Range & {
48
- readonly path: string
49
- readonly blob: string
50
- readonly snippet: string
51
- }
52
-
53
- export const lineOn = (row: Row, side: Side): Option.Option<number> =>
54
- side === "new" ? row.newLine : row.oldLine
@@ -1,106 +0,0 @@
1
- import { Option } from "effect"
2
- import type { Hunk, Patch, Row, RowKind } from "./model.ts"
3
-
4
- const FILE_MARKER = "diff --git "
5
- const HUNK_MARKER = "@@"
6
- const BLOB_MARKER = "index "
7
- const OLD_PATH_MARKER = "--- "
8
- const NEW_PATH_MARKER = "+++ "
9
-
10
- const HUNK_HEADER = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@ ?(.*)$/
11
- const FILE_HEADER = / a\/(.+) b\/(.+)$/
12
-
13
- const SIGNS: Record<string, RowKind> = { " ": "context", "+": "added", "-": "removed" }
14
-
15
- type Draft = {
16
- path: string
17
- previousPath: string
18
- blob: string
19
- headerLines: Array<string>
20
- hunks: Array<Hunk & { rows: Array<Row> }>
21
- rows: Array<Row>
22
- added: number
23
- removed: number
24
- oldLine: number
25
- newLine: number
26
- }
27
-
28
- const draftFrom = (line: string): Draft => {
29
- const named = FILE_HEADER.exec(line)
30
- return {
31
- path: named?.[2] ?? "",
32
- previousPath: named?.[1] ?? "",
33
- blob: "",
34
- headerLines: [line],
35
- hunks: [],
36
- rows: [],
37
- added: 0,
38
- removed: 0,
39
- oldLine: 1,
40
- newLine: 1,
41
- }
42
- }
43
-
44
- const openHunk = (draft: Draft, line: string): void => {
45
- const header = HUNK_HEADER.exec(line)
46
- const previousNew = draft.newLine
47
- draft.oldLine = Number(header?.[1] ?? 1)
48
- draft.newLine = Number(header?.[2] ?? 1)
49
- draft.hunks.push({
50
- marker: line,
51
- scope: (header?.[3] ?? "").trim(),
52
- startRow: draft.rows.length,
53
- rows: [],
54
- skipped: draft.hunks.length === 0 ? 0 : Math.max(0, draft.newLine - previousNew),
55
- })
56
- }
57
-
58
- const appendRow = (draft: Draft, kind: RowKind, text: string): void => {
59
- const row: Row = {
60
- index: draft.rows.length,
61
- kind,
62
- oldLine: kind === "added" ? Option.none() : Option.some(draft.oldLine),
63
- newLine: kind === "removed" ? Option.none() : Option.some(draft.newLine),
64
- text,
65
- }
66
- if (kind !== "added") draft.oldLine += 1
67
- if (kind !== "removed") draft.newLine += 1
68
- if (kind === "added") draft.added += 1
69
- if (kind === "removed") draft.removed += 1
70
- draft.rows.push(row)
71
- draft.hunks.at(-1)?.rows.push(row)
72
- }
73
-
74
- const readBody = (draft: Draft, line: string): void => {
75
- const kind = SIGNS[line.slice(0, 1)]
76
- if (kind !== undefined) appendRow(draft, kind, line.slice(1))
77
- }
78
-
79
- const readHeader = (draft: Draft, line: string): boolean => {
80
- if (line.startsWith(BLOB_MARKER)) {
81
- draft.blob = line.slice(BLOB_MARKER.length).split("..")[1]?.split(" ")[0] ?? ""
82
- draft.headerLines.push(line)
83
- return true
84
- }
85
- if (line.startsWith(OLD_PATH_MARKER) || line.startsWith(NEW_PATH_MARKER)) {
86
- draft.headerLines.push(line)
87
- return true
88
- }
89
- return false
90
- }
91
-
92
- export const parsePatches = (diff: string): ReadonlyArray<Patch> => {
93
- const drafts: Array<Draft> = []
94
- for (const line of diff.split("\n")) {
95
- if (line.startsWith(FILE_MARKER)) {
96
- drafts.push(draftFrom(line))
97
- continue
98
- }
99
- const draft = drafts.at(-1)
100
- if (draft === undefined) continue
101
- if (readHeader(draft, line)) continue
102
- if (line.startsWith(HUNK_MARKER)) openHunk(draft, line)
103
- else readBody(draft, line)
104
- }
105
- return drafts
106
- }
@@ -1,48 +0,0 @@
1
- import { Option } from "effect"
2
- import { lineOn, type Patch, type Row, type RowKind } from "./model.ts"
3
-
4
- const SIGNS: Record<RowKind, string> = { context: " ", added: "+", removed: "-" }
5
-
6
- export type RenderedHunk = {
7
- readonly startRow: number
8
- readonly scope: string
9
- }
10
-
11
- export type Rendered = {
12
- readonly text: string
13
- readonly hunks: ReadonlyArray<RenderedHunk>
14
- }
15
-
16
- export const renderPatch = (patch: Patch): Rendered => {
17
- const lines: Array<string> = [...patch.headerLines]
18
- for (const hunk of patch.hunks) {
19
- lines.push(hunk.marker)
20
- for (const row of hunk.rows) lines.push(`${SIGNS[row.kind]}${row.text}`)
21
- }
22
- return {
23
- text: lines.join("\n"),
24
- hunks: patch.hunks.map((hunk) => ({ startRow: hunk.startRow, scope: hunk.scope })),
25
- }
26
- }
27
-
28
- export const scopeAbove = (rendered: Rendered, topRow: number): string => {
29
- const hunk = rendered.hunks.findLast((candidate) => candidate.startRow < topRow)
30
- return hunk === undefined ? "" : hunk.scope
31
- }
32
-
33
- const countOn = (rows: ReadonlyArray<Row>, side: "old" | "new"): number =>
34
- rows.filter((row) => Option.isSome(lineOn(row, side))).length
35
-
36
- const firstOn = (rows: ReadonlyArray<Row>, side: "old" | "new"): number => {
37
- const found = rows.find((row) => Option.isSome(lineOn(row, side)))
38
- return found === undefined ? 1 : Option.getOrElse(lineOn(found, side), () => 1)
39
- }
40
-
41
- export const renderWindow = (patch: Patch, from: number, count: number): string => {
42
- const rows = patch.rows.slice(from, from + count)
43
- if (rows.length === 0) return patch.headerLines.join("\n")
44
- const marker = `@@ -${firstOn(rows, "old")},${countOn(rows, "old")} +${firstOn(rows, "new")},${countOn(rows, "new")} @@`
45
- return [...patch.headerLines, marker, ...rows.map((row) => `${SIGNS[row.kind]}${row.text}`)].join(
46
- "\n",
47
- )
48
- }
@@ -1,39 +0,0 @@
1
- const CLOSER = /^[)\]}>;,]/
2
- const COMMENT = /^(\/\/|\/\*|\*|#|<!--)/
3
- const LOOKAHEAD = 5
4
-
5
- const indentOf = (text: string): number => text.length - text.trimStart().length
6
-
7
- const pinnable = (text: string): boolean => {
8
- const trimmed = text.trim()
9
- if (trimmed.length === 0) return false
10
- return !CLOSER.test(trimmed) && !COMMENT.test(trimmed)
11
- }
12
-
13
- const limitFrom = (source: ReadonlyArray<string>, index: number): number => {
14
- const here = source[index] ?? ""
15
- if (here.trim().length > 0) return indentOf(here)
16
- const ahead = source.slice(index, index + LOOKAHEAD).find((line) => line.trim().length > 0)
17
- return ahead === undefined ? 0 : indentOf(ahead)
18
- }
19
-
20
- export const stickyChain = (
21
- source: ReadonlyArray<string>,
22
- line: number,
23
- max: number,
24
- ): ReadonlyArray<string> => {
25
- const index = line - 1
26
- if (index <= 0 || index >= source.length) return []
27
- let limit = limitFrom(source, index)
28
- const chain: Array<string> = []
29
- for (let above = index - 1; above >= 0; above -= 1) {
30
- const text = source[above] ?? ""
31
- if (!pinnable(text)) continue
32
- const indent = indentOf(text)
33
- if (indent >= limit) continue
34
- chain.push(text)
35
- limit = indent
36
- if (indent === 0) break
37
- }
38
- return chain.toReversed().slice(0, max)
39
- }