oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package markup
2
3import "strings"
4
5// Settling a merge keeps bytes (level-up §6.4, D13). The current
6// revision ("theirs") is the anchor: its blocks, and the bytes between
7// two of its blocks that are still neighbours, are emitted as they are.
8// A block taken from mine brings its own source, and the bytes between
9// two of mine's neighbours come with them. Only a seam where the two
10// sides meet follows Join's rule. The result is parsed again: if it does
11// not give the settled blocks, Settle falls back to Join, which is
12// correct but tidies spacing.
13
14// placed is one settled block and its place in the side it came from.
15type placed struct {
16 block Block
17 from Side
18 at int
19}
20
21// Settle joins the hunks into a body, taking each conflict's choice in
22// order. It reports false if any conflict has no valid choice.
23func Settle(hunks []Hunk, choices []Choice) (string, bool) {
24 var pieces []placed
25 var texts *sides
26 next := 0
27 for _, hunk := range hunks {
28 texts = firstSides(texts, hunk.texts)
29 if !hunk.Conflict {
30 pieces = appendPieces(pieces, hunk.Blocks, hunk.From, hunk.At)
31 continue
32 }
33 if next >= len(choices) {
34 return "", false
35 }
36 chosen, ok := hunk.chosen(pieces, choices[next])
37 if !ok {
38 return "", false
39 }
40 pieces = chosen
41 next++
42 }
43 return texts.join(pieces), next == len(choices)
44}
45
46func firstSides(found, candidate *sides) *sides {
47 if found != nil {
48 return found
49 }
50 return candidate
51}
52
53// chosen is pieces with a conflict's chosen side appended.
54func (hunk Hunk) chosen(pieces []placed, choice Choice) ([]placed, bool) {
55 switch choice {
56 case KeepMine:
57 return appendPieces(pieces, hunk.Mine, FromMine, hunk.MineAt), true
58 case KeepTheirs:
59 return appendPieces(pieces, hunk.Theirs, FromTheirs,
60 hunk.TheirAt), true
61 case KeepBoth:
62 pieces = appendPieces(pieces, hunk.Mine, FromMine, hunk.MineAt)
63 return appendPieces(pieces, hunk.Theirs, FromTheirs,
64 hunk.TheirAt), true
65 }
66 return pieces, false
67}
68
69func appendPieces(pieces []placed, blocks []Block, from Side,
70 at int) []placed {
71 for step, block := range blocks {
72 pieces = append(pieces, placed{block: block, from: from,
73 at: at + step})
74 }
75 return pieces
76}
77
78// join is the settled body: the sides' own bytes where they hold, and
79// Join where the result would not parse back to the settled blocks.
80func (texts *sides) join(pieces []placed) string {
81 blocks := make([]Block, len(pieces))
82 for at, settled := range pieces {
83 blocks[at] = settled.block
84 }
85 if texts == nil || len(pieces) == 0 {
86 return Join(blocks)
87 }
88 kept := texts.keep(pieces)
89 if sameStructure(Parse(kept), blocks) {
90 return kept
91 }
92 return Join(blocks)
93}
94
95// keep writes the pieces with their sides' bytes between neighbours.
96func (texts *sides) keep(pieces []placed) string {
97 var out strings.Builder
98 if first := pieces[0]; first.at == 0 {
99 out.WriteString(texts.text(first.from)[:first.block.Start])
100 }
101 for at, settled := range pieces {
102 if at > 0 {
103 out.WriteString(texts.seam(pieces[at-1], settled))
104 }
105 out.WriteString(settled.block.Source)
106 }
107 return out.String()
108}
109
110// seam is the bytes between two settled blocks: their side's own, when
111// they were neighbours there, and Join's rule anywhere else.
112func (texts *sides) seam(before, after placed) string {
113 if before.from == after.from && after.at == before.at+1 {
114 blocks := texts.blocks(before.from)
115 start, end := blocks[before.at].End, blocks[after.at].Start
116 return texts.text(before.from)[start:end]
117 }
118 return seam(before.block, after.block)
119}
120
121func (texts *sides) text(from Side) string {
122 if from == FromMine {
123 return texts.mine
124 }
125 return texts.theirs
126}
127
128func (texts *sides) blocks(from Side) []Block {
129 if from == FromMine {
130 return texts.mineBlocks
131 }
132 return texts.theirsBlocks
133}
134
135// sameStructure reports whether parsed is blocks: the same number, and
136// each of the same kind, level and source.
137func sameStructure(parsed, blocks []Block) bool {
138 if len(parsed) != len(blocks) {
139 return false
140 }
141 for at := range parsed {
142 one, other := parsed[at], blocks[at]
143 // A layout opener's Level is its column count, a position, as
144 // Column is: neither is compared (columns and icons spec §1.4).
145 level := one.Kind == Columns || one.Level == other.Level
146 if one.Kind != other.Kind || !level ||
147 one.Ordered != other.Ordered || one.Source != other.Source {
148 return false
149 }
150 }
151 return true
152}
153
154// Sources is a conflict's three sides as Markdown, each the exact bytes
155// of its body from the hunk's first block to its last ("" for a side
156// with no blocks). The editor's inline choice shows them (§6.5).
157func (hunk Hunk) Sources() (base, mine, theirs string) {
158 if hunk.texts == nil {
159 return Join(hunk.Base), Join(hunk.Mine), Join(hunk.Theirs)
160 }
161 texts := hunk.texts
162 return span(texts.base, hunk.Base), span(texts.mine, hunk.Mine),
163 span(texts.theirs, hunk.Theirs)
164}
165
166// span is the bytes of text that blocks, a run of its own, cover.
167func span(text string, blocks []Block) string {
168 if len(blocks) == 0 {
169 return ""
170 }
171 return text[blocks[0].Start:blocks[len(blocks)-1].End]
172}
173