| 1 | package markup |
| 2 | |
| 3 | // The merge and column layouts (columns and icons spec §1.8). Inside a |
| 4 | // column, blocks merge as they always have. Two rules keep every result |
| 5 | // a valid body: a conflict never cuts a layout, and every result holds |
| 6 | // valid layouts, conflicts or not. Merge3 checks the first pass; when |
| 7 | // it would break a layout, it merges again with layouts as units. |
| 8 | |
| 9 | // inLayout is a fence, or a block inside a column. |
| 10 | func inLayout(block Block) bool { |
| 11 | return block.Column > 0 || IsFence(block.Kind) |
| 12 | } |
| 13 | |
| 14 | // anyFence reports whether any side holds a layout. |
| 15 | func (texts *sides) anyFence() bool { |
| 16 | for _, blocks := range [][]Block{texts.baseBlocks, texts.mineBlocks, |
| 17 | texts.theirsBlocks} { |
| 18 | for _, block := range blocks { |
| 19 | if IsFence(block.Kind) { |
| 20 | return true |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | return false |
| 25 | } |
| 26 | |
| 27 | // fenced reports whether the stretch from at to end holds a fence on |
| 28 | // any side. |
| 29 | func (texts *sides) fenced(at, end cursor) bool { |
| 30 | return holdsFence(texts.baseBlocks[at.base:end.base]) || |
| 31 | holdsFence(texts.mineBlocks[at.mine:end.mine]) || |
| 32 | holdsFence(texts.theirsBlocks[at.theirs:end.theirs]) |
| 33 | } |
| 34 | |
| 35 | func holdsFence(blocks []Block) bool { |
| 36 | for _, block := range blocks { |
| 37 | if IsFence(block.Kind) { |
| 38 | return true |
| 39 | } |
| 40 | } |
| 41 | return false |
| 42 | } |
| 43 | |
| 44 | // layoutsHold reports whether the first pass's hunks keep every layout |
| 45 | // valid whatever is chosen: no conflict holds a fence on any side (rule |
| 46 | // 1); the kinds in order are valid with each fence-free conflict there |
| 47 | // and gone; and the result, each conflict taken as mine and as theirs, |
| 48 | // parses back to those kinds (rule 2). |
| 49 | func (texts *sides) layoutsHold(hunks []Hunk) bool { |
| 50 | var present, absent []Block |
| 51 | for _, hunk := range hunks { |
| 52 | if !hunk.Conflict { |
| 53 | present = append(present, hunk.Blocks...) |
| 54 | absent = append(absent, hunk.Blocks...) |
| 55 | continue |
| 56 | } |
| 57 | if holdsFence(hunk.Base) || holdsFence(hunk.Mine) || |
| 58 | holdsFence(hunk.Theirs) { |
| 59 | return false |
| 60 | } |
| 61 | present = append(present, Block{Kind: Paragraph}) |
| 62 | } |
| 63 | if !ValidLayouts(present) || !ValidLayouts(absent) { |
| 64 | return false |
| 65 | } |
| 66 | return parsesBack(hunks, KeepMine) && parsesBack(hunks, KeepTheirs) |
| 67 | } |
| 68 | |
| 69 | // parsesBack reports whether settling every conflict as choice gives a |
| 70 | // body whose blocks are, kind for kind, the ones settled. |
| 71 | func parsesBack(hunks []Hunk, choice Choice) bool { |
| 72 | var want []Block |
| 73 | var choices []Choice |
| 74 | for _, hunk := range hunks { |
| 75 | switch { |
| 76 | case !hunk.Conflict: |
| 77 | want = append(want, hunk.Blocks...) |
| 78 | case choice == KeepMine: |
| 79 | want, choices = append(want, hunk.Mine...), append(choices, choice) |
| 80 | default: |
| 81 | want = append(want, hunk.Theirs...) |
| 82 | choices = append(choices, choice) |
| 83 | } |
| 84 | } |
| 85 | body, ok := Settle(hunks, choices) |
| 86 | if !ok { |
| 87 | return false |
| 88 | } |
| 89 | got := Parse(body) |
| 90 | if len(got) != len(want) { |
| 91 | return false |
| 92 | } |
| 93 | for at := range got { |
| 94 | if got[at].Kind != want[at].Kind || got[at].Source != want[at].Source { |
| 95 | return false |
| 96 | } |
| 97 | } |
| 98 | return true |
| 99 | } |
| 100 | |
| 101 | // ValidLayouts reports whether blocks, in order, hold only whole column |
| 102 | // layouts: an opener, two to four columns each opened and closed, no |
| 103 | // block between columns, and no fence anywhere else. Column numbers and |
| 104 | // the opener's count are positions; this is the check, not them. |
| 105 | func ValidLayouts(blocks []Block) bool { |
| 106 | state, count := "top", 0 |
| 107 | for _, block := range blocks { |
| 108 | switch { |
| 109 | case state == "top" && block.Kind == Columns: |
| 110 | state, count = "between", 0 |
| 111 | case state == "top" && !IsFence(block.Kind): |
| 112 | case state == "between" && block.Kind == Column: |
| 113 | state, count = "in", count+1 |
| 114 | case state == "between" && block.Kind == ColumnsEnd: |
| 115 | if count < 2 || count > 4 { |
| 116 | return false |
| 117 | } |
| 118 | state = "top" |
| 119 | case state == "in" && block.Kind == ColumnEnd: |
| 120 | state = "between" |
| 121 | case state == "in" && !IsFence(block.Kind): |
| 122 | default: |
| 123 | return false |
| 124 | } |
| 125 | } |
| 126 | return state == "top" |
| 127 | } |
| 128 | |
| 129 | // alignKeys is what align matches blocks by: their source, and for a |
| 130 | // column's fences, the source of the block inside them beside it too. |
| 131 | // Every column's fences read alike, so by source alone a column added |
| 132 | // before another would take the other's fences, and its edit would |
| 133 | // meet the addition (§1.8: edits inside columns merge as anywhere). |
| 134 | func alignKeys(blocks []Block) []string { |
| 135 | keys := make([]string, len(blocks)) |
| 136 | for at, block := range blocks { |
| 137 | keys[at] = block.Source |
| 138 | switch { |
| 139 | case block.Kind == Column && at+1 < len(blocks) && |
| 140 | !IsFence(blocks[at+1].Kind): |
| 141 | keys[at] += "\n" + blocks[at+1].Source |
| 142 | case block.Kind == ColumnEnd && at > 0 && !IsFence(blocks[at-1].Kind): |
| 143 | keys[at] += "\n" + blocks[at-1].Source |
| 144 | } |
| 145 | } |
| 146 | return keys |
| 147 | } |
| 148 | |
| 149 | // addedColumns splits a stretch in which one side only added whole |
| 150 | // columns at its start or its end and the other changed what follows or |
| 151 | // precedes them: Ana adds a column before Bo's, while Bo edits his. Every |
| 152 | // column's fences read alike, so without this the addition and the edit |
| 153 | // meet in one stretch and conflict (§1.8: they merge). |
| 154 | func (texts *sides) addedColumns(at, end cursor) ([]Hunk, bool) { |
| 155 | base := texts.baseBlocks[at.base:end.base] |
| 156 | for _, side := range []Side{FromMine, FromTheirs} { |
| 157 | start, stop := at.theirs, end.theirs |
| 158 | if side == FromMine { |
| 159 | start, stop = at.mine, end.mine |
| 160 | } |
| 161 | region := texts.blocks(side)[start:stop] |
| 162 | extra := len(region) - len(base) |
| 163 | if extra <= 0 { |
| 164 | continue |
| 165 | } |
| 166 | if sameBlocks(region[extra:], base) && wholeColumns(region[:extra]) { |
| 167 | return []Hunk{texts.settled(side, start, extra), |
| 168 | texts.resolve(moved(at, side, extra), end)}, true |
| 169 | } |
| 170 | if sameBlocks(region[:len(base)], base) && |
| 171 | wholeColumns(region[len(base):]) { |
| 172 | return []Hunk{texts.resolve(at, moved(end, side, -extra)), |
| 173 | texts.settled(side, stop-extra, extra)}, true |
| 174 | } |
| 175 | } |
| 176 | return nil, false |
| 177 | } |
| 178 | |
| 179 | // moved is a cursor with one side's position moved by count. |
| 180 | func moved(at cursor, side Side, count int) cursor { |
| 181 | if side == FromMine { |
| 182 | at.mine += count |
| 183 | } else { |
| 184 | at.theirs += count |
| 185 | } |
| 186 | return at |
| 187 | } |
| 188 | |
| 189 | // wholeColumns reports whether blocks are one or more whole columns. |
| 190 | func wholeColumns(blocks []Block) bool { |
| 191 | if len(blocks) < 2 || blocks[0].Kind != Column { |
| 192 | return false |
| 193 | } |
| 194 | inside := false |
| 195 | for _, block := range blocks { |
| 196 | switch { |
| 197 | case block.Kind == Column && !inside: |
| 198 | inside = true |
| 199 | case block.Kind == ColumnEnd && inside: |
| 200 | inside = false |
| 201 | case IsFence(block.Kind) || !inside: |
| 202 | return false |
| 203 | } |
| 204 | } |
| 205 | return !inside |
| 206 | } |
| 207 | |
| 208 | // WholeOrInside reports whether the picked blocks cut no column layout: |
| 209 | // each layout is picked whole, not at all, or only inside one column, |
| 210 | // fences left alone (columns and icons spec §1.10, Make team page). |
| 211 | func WholeOrInside(blocks []Block, picked map[int]bool) bool { |
| 212 | for start := 0; start < len(blocks); start++ { |
| 213 | if blocks[start].Kind != Columns { |
| 214 | continue |
| 215 | } |
| 216 | end := start |
| 217 | for end < len(blocks) && blocks[end].Kind != ColumnsEnd { |
| 218 | end++ |
| 219 | } |
| 220 | if !layoutPick(blocks[start:end+1], picked, start) { |
| 221 | return false |
| 222 | } |
| 223 | start = end |
| 224 | } |
| 225 | return true |
| 226 | } |
| 227 | |
| 228 | func layoutPick(layout []Block, picked map[int]bool, offset int) bool { |
| 229 | count, fences, columns := 0, 0, map[int]bool{} |
| 230 | for at, block := range layout { |
| 231 | if !picked[offset+at] { |
| 232 | continue |
| 233 | } |
| 234 | count++ |
| 235 | if IsFence(block.Kind) { |
| 236 | fences++ |
| 237 | } |
| 238 | columns[block.Column] = true |
| 239 | } |
| 240 | return count == 0 || count == len(layout) || |
| 241 | fences == 0 && len(columns) == 1 |
| 242 | } |
| 243 | |