| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {RecordOf} from 'immutable'; |
| b69ab31 | | | 9 | import type {FlattenLine, LineIdx} from '../linelog'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {Set as ImSet, List, Record} from 'immutable'; |
| b69ab31 | | | 12 | import {LRU, cachedMethod} from 'shared/LRU'; |
| b69ab31 | | | 13 | import {SelfUpdate} from 'shared/immutableExt'; |
| b69ab31 | | | 14 | import {FileStackState} from './fileStackState'; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | /** |
| b69ab31 | | | 17 | * Represents selections of changes between 2 texts (`a` and `b`), optionally |
| b69ab31 | | | 18 | * the selection text can be edited free-form. |
| b69ab31 | | | 19 | * |
| b69ab31 | | | 20 | * Based on a 3-rev `FlattenLine`s representation: |
| b69ab31 | | | 21 | * - Rev 0: The `a` side (not editable by this `ChunkSelectState`). |
| b69ab31 | | | 22 | * - Rev 1: The selection (editable by this `ChunkSelectState`). |
| b69ab31 | | | 23 | * - Rev 2: The `b` side (not editable by this `ChunkSelectState`). |
| b69ab31 | | | 24 | * |
| b69ab31 | | | 25 | * Support operations: |
| b69ab31 | | | 26 | * - getLines: Obtain lines for rendering. See `SelectLine` for details. |
| b69ab31 | | | 27 | * - setSelectedLines: Set line selections. Only added or removed lines can be selected. |
| b69ab31 | | | 28 | * - getSelectedText: Obtain selected or edited text. |
| b69ab31 | | | 29 | * - setSelectedText: Set edited text. Useful for free-form editing. |
| b69ab31 | | | 30 | * |
| b69ab31 | | | 31 | * With free-from editing, there are two "special" cases: |
| b69ab31 | | | 32 | * |
| b69ab31 | | | 33 | * revs | meaning |
| b69ab31 | | | 34 | * ----------------------------- |
| b69ab31 | | | 35 | * {0,1,2} | unchanged |
| b69ab31 | | | 36 | * {0,1} | deletion; not selected |
| b69ab31 | | | 37 | * {0,2} | special: extra deletion [*] |
| b69ab31 | | | 38 | * {0} | deletion; selected |
| b69ab31 | | | 39 | * {1,2} | insertion; selected |
| b69ab31 | | | 40 | * {1} | special: extra insertion |
| b69ab31 | | | 41 | * {2} | insertion; not selected |
| b69ab31 | | | 42 | * |
| b69ab31 | | | 43 | * [*]: LineLog.flatten() never produces {0,2}. It generates {0} and {2} as |
| b69ab31 | | | 44 | * separate lines. But `getLines()` post-processing might produce {0,2} lines. |
| b69ab31 | | | 45 | * |
| b69ab31 | | | 46 | * The callsite might want to treat the "special: extra insertion" like an |
| b69ab31 | | | 47 | * insertion with a different highlighting. |
| b69ab31 | | | 48 | * |
| b69ab31 | | | 49 | * This state does not care about collapsing unmodified lines, "context lines" |
| b69ab31 | | | 50 | * or "code expansion" states. They do not affect editing state, and belong to |
| b69ab31 | | | 51 | * the UI components. |
| b69ab31 | | | 52 | */ |
| b69ab31 | | | 53 | export class ChunkSelectState extends SelfUpdate<ChunkSelectRecord> { |
| b69ab31 | | | 54 | /** |
| b69ab31 | | | 55 | * Initialize ChunkSelectState from text `a` and `b`. |
| b69ab31 | | | 56 | * |
| b69ab31 | | | 57 | * If `selected` is `true`, then all changes are selected, selection result is `b`. |
| b69ab31 | | | 58 | * If `selected` is `false`, none of the changes are selected, selection result is `a`. |
| b69ab31 | | | 59 | * If `selected` is a string, then the selections are "inferred" from the string. |
| b69ab31 | | | 60 | * |
| b69ab31 | | | 61 | * If `normalize` is `true`, drop changes in `selected` that is not in `a` or `b`. |
| b69ab31 | | | 62 | */ |
| b69ab31 | | | 63 | static fromText( |
| b69ab31 | | | 64 | a: string, |
| b69ab31 | | | 65 | b: string, |
| b69ab31 | | | 66 | selected: boolean | string, |
| b69ab31 | | | 67 | normalize = false, |
| b69ab31 | | | 68 | ): ChunkSelectState { |
| b69ab31 | | | 69 | const mid = selected === true ? b : selected === false ? a : selected; |
| b69ab31 | | | 70 | const fileStack = new FileStackState([a, mid, b]); |
| b69ab31 | | | 71 | let lines = fileStack.convertToFlattenLines().map(l => toLineBits(l)); |
| b69ab31 | | | 72 | if (normalize) { |
| b69ab31 | | | 73 | lines = lines.filter(l => l.bits !== 0b101 && l.bits !== 0b010 && l.bits !== 0b000); |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | return new ChunkSelectState(ChunkSelectRecord({a, b, lines})); |
| b69ab31 | | | 76 | } |
| b69ab31 | | | 77 | |
| b69ab31 | | | 78 | /** Get the text of the "a" side. */ |
| b69ab31 | | | 79 | get a(): string { |
| b69ab31 | | | 80 | return this.inner.a; |
| b69ab31 | | | 81 | } |
| b69ab31 | | | 82 | |
| b69ab31 | | | 83 | /** Get the text of the "b" side. */ |
| b69ab31 | | | 84 | get b(): string { |
| b69ab31 | | | 85 | return this.inner.b; |
| b69ab31 | | | 86 | } |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | /** |
| b69ab31 | | | 89 | * Get the `SelectLine`s. Useful for rendering the lines. |
| b69ab31 | | | 90 | * See `SelectLine` for details. |
| b69ab31 | | | 91 | */ |
| b69ab31 | | | 92 | getLines = cachedMethod(this.getLinesImpl, {cache: getLinesCache}); |
| b69ab31 | | | 93 | private getLinesImpl(): Readonly<SelectLine[]> { |
| b69ab31 | | | 94 | let nextALine = 1; |
| b69ab31 | | | 95 | let nextBLine = 1; |
| b69ab31 | | | 96 | let nextSelLine = 1; |
| b69ab31 | | | 97 | let result: SelectLine[] = []; |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | // Modified lines to sort before appending to `result`. |
| b69ab31 | | | 100 | let buffer: SelectLine[] = []; |
| b69ab31 | | | 101 | const pushBuffer = () => { |
| b69ab31 | | | 102 | buffer.sort((a, b) => { |
| b69ab31 | | | 103 | // In this order: Deletion, insertion. |
| b69ab31 | | | 104 | const aOrder = bitsToOrder[a.bits]; |
| b69ab31 | | | 105 | const bOrder = bitsToOrder[b.bits]; |
| b69ab31 | | | 106 | if (aOrder !== bOrder) { |
| b69ab31 | | | 107 | return aOrder - bOrder; |
| b69ab31 | | | 108 | } |
| b69ab31 | | | 109 | return a.rawIndex - b.rawIndex; |
| b69ab31 | | | 110 | }); |
| b69ab31 | | | 111 | // Merge "selected deletion + deselected insertion" with |
| b69ab31 | | | 112 | // the same content into "unselectable deletion". |
| b69ab31 | | | 113 | let nextDelIndex = 0; |
| b69ab31 | | | 114 | buffer.forEach((line, i) => { |
| b69ab31 | | | 115 | if (line.bits === 0b001 /* deselected insertion */) { |
| b69ab31 | | | 116 | // Try to find the matched "selected deletion" line. |
| b69ab31 | | | 117 | while (nextDelIndex < i) { |
| b69ab31 | | | 118 | const otherLine = buffer[nextDelIndex]; |
| b69ab31 | | | 119 | if (otherLine.data === line.data && otherLine.bits === 0b100) { |
| b69ab31 | | | 120 | // Change otherLine to "unselectable deletion", |
| b69ab31 | | | 121 | // then remove this line. |
| b69ab31 | | | 122 | otherLine.bits = 0b101; |
| b69ab31 | | | 123 | otherLine.selected = null; |
| b69ab31 | | | 124 | otherLine.bLine = line.bLine; |
| b69ab31 | | | 125 | otherLine.sign = '!-'; |
| b69ab31 | | | 126 | line.bits = 0; |
| b69ab31 | | | 127 | break; |
| b69ab31 | | | 128 | } |
| b69ab31 | | | 129 | nextDelIndex += 1; |
| b69ab31 | | | 130 | } |
| b69ab31 | | | 131 | } |
| b69ab31 | | | 132 | }); |
| b69ab31 | | | 133 | buffer = buffer.filter(line => line.bits !== 0); |
| b69ab31 | | | 134 | result = result.concat(buffer); |
| b69ab31 | | | 135 | buffer = []; |
| b69ab31 | | | 136 | }; |
| b69ab31 | | | 137 | |
| b69ab31 | | | 138 | this.inner.lines.forEach((line, rawIndex) => { |
| b69ab31 | | | 139 | const bits = line.bits; |
| b69ab31 | | | 140 | let sign: Sign = ''; |
| b69ab31 | | | 141 | let selected: boolean | null = null; |
| b69ab31 | | | 142 | let aLine: LineIdx | null = null; |
| b69ab31 | | | 143 | let bLine: LineIdx | null = null; |
| b69ab31 | | | 144 | let selLine: LineIdx | null = null; |
| b69ab31 | | | 145 | // eslint-disable-next-line no-bitwise |
| b69ab31 | | | 146 | if (bits >> 2 !== 0) { |
| b69ab31 | | | 147 | aLine = nextALine; |
| b69ab31 | | | 148 | nextALine += 1; |
| b69ab31 | | | 149 | } |
| b69ab31 | | | 150 | // eslint-disable-next-line no-bitwise |
| b69ab31 | | | 151 | if ((bits & 1) !== 0) { |
| b69ab31 | | | 152 | bLine = nextBLine; |
| b69ab31 | | | 153 | nextBLine += 1; |
| b69ab31 | | | 154 | } |
| b69ab31 | | | 155 | // eslint-disable-next-line no-bitwise |
| b69ab31 | | | 156 | if ((bits & 2) !== 0) { |
| b69ab31 | | | 157 | selLine = nextSelLine; |
| b69ab31 | | | 158 | nextSelLine += 1; |
| b69ab31 | | | 159 | } |
| b69ab31 | | | 160 | switch (bits) { |
| b69ab31 | | | 161 | case 0b001: |
| b69ab31 | | | 162 | sign = '+'; |
| b69ab31 | | | 163 | selected = false; |
| b69ab31 | | | 164 | break; |
| b69ab31 | | | 165 | case 0b010: |
| b69ab31 | | | 166 | sign = '!+'; |
| b69ab31 | | | 167 | break; |
| b69ab31 | | | 168 | case 0b011: |
| b69ab31 | | | 169 | sign = '+'; |
| b69ab31 | | | 170 | selected = true; |
| b69ab31 | | | 171 | break; |
| b69ab31 | | | 172 | case 0b100: |
| b69ab31 | | | 173 | sign = '-'; |
| b69ab31 | | | 174 | selected = true; |
| b69ab31 | | | 175 | break; |
| b69ab31 | | | 176 | case 0b101: |
| b69ab31 | | | 177 | sign = '!-'; |
| b69ab31 | | | 178 | break; |
| b69ab31 | | | 179 | case 0b110: |
| b69ab31 | | | 180 | sign = '-'; |
| b69ab31 | | | 181 | selected = false; |
| b69ab31 | | | 182 | break; |
| b69ab31 | | | 183 | case 0b111: |
| b69ab31 | | | 184 | break; |
| b69ab31 | | | 185 | } |
| b69ab31 | | | 186 | const selectLine: SelectLine = { |
| b69ab31 | | | 187 | rawIndex, |
| b69ab31 | | | 188 | aLine, |
| b69ab31 | | | 189 | bLine, |
| b69ab31 | | | 190 | selLine, |
| b69ab31 | | | 191 | sign, |
| b69ab31 | | | 192 | selected, |
| b69ab31 | | | 193 | bits, |
| b69ab31 | | | 194 | data: line.data, |
| b69ab31 | | | 195 | }; |
| b69ab31 | | | 196 | if (sign === '') { |
| b69ab31 | | | 197 | pushBuffer(); |
| b69ab31 | | | 198 | result.push(selectLine); |
| b69ab31 | | | 199 | } else { |
| b69ab31 | | | 200 | buffer.push(selectLine); |
| b69ab31 | | | 201 | } |
| b69ab31 | | | 202 | }); |
| b69ab31 | | | 203 | pushBuffer(); |
| b69ab31 | | | 204 | return result; |
| b69ab31 | | | 205 | } |
| b69ab31 | | | 206 | |
| b69ab31 | | | 207 | /** |
| b69ab31 | | | 208 | * Get the line regions. By default, unchanged lines are collapsed. |
| b69ab31 | | | 209 | * |
| b69ab31 | | | 210 | * `config.contextLines` sets how many lines to expand around |
| b69ab31 | | | 211 | * changed or current lines. |
| b69ab31 | | | 212 | * |
| b69ab31 | | | 213 | * `config.expanded` and `config.caretLine` specify lines to |
| b69ab31 | | | 214 | * expanded. |
| b69ab31 | | | 215 | */ |
| b69ab31 | | | 216 | getLineRegions(config?: { |
| b69ab31 | | | 217 | contextLines?: number; |
| b69ab31 | | | 218 | /** Line numbers on the "A" side to expand. */ |
| b69ab31 | | | 219 | expandedALines: ImSet<number>; |
| b69ab31 | | | 220 | /** Line number on the "M" (selection) side to expand. */ |
| b69ab31 | | | 221 | expandedSelLine?: number; |
| b69ab31 | | | 222 | }): Readonly<LineRegion[]> { |
| b69ab31 | | | 223 | const contextLines = config?.contextLines ?? 2; |
| b69ab31 | | | 224 | const lines = this.getLines(); |
| b69ab31 | | | 225 | const expandedSelLine = config?.expandedSelLine ?? -1; |
| b69ab31 | | | 226 | const expandedALines = config?.expandedALines ?? ImSet(); |
| b69ab31 | | | 227 | const regions: LineRegion[] = []; |
| b69ab31 | | | 228 | |
| b69ab31 | | | 229 | // Figure out indexes of `lines` to collapse (skip). |
| b69ab31 | | | 230 | const collapsedLines = Array<boolean>(lines.length + contextLines).fill(true); |
| b69ab31 | | | 231 | lines.forEach((line, i) => { |
| b69ab31 | | | 232 | if ( |
| b69ab31 | | | 233 | line.bits !== 0b111 || |
| b69ab31 | | | 234 | expandedALines.has(line.aLine ?? -1) || |
| b69ab31 | | | 235 | line.selLine === expandedSelLine |
| b69ab31 | | | 236 | ) { |
| b69ab31 | | | 237 | for (let j = i + contextLines; j >= 0 && j >= i - contextLines && collapsedLines[j]; j--) { |
| b69ab31 | | | 238 | collapsedLines[j] = false; |
| b69ab31 | | | 239 | } |
| b69ab31 | | | 240 | } |
| b69ab31 | | | 241 | }); |
| b69ab31 | | | 242 | |
| b69ab31 | | | 243 | // Scan through regions. |
| b69ab31 | | | 244 | let currentRegion: LineRegion | null = null; |
| b69ab31 | | | 245 | lines.forEach((line, i) => { |
| b69ab31 | | | 246 | const same = line.bits === 0b111; |
| b69ab31 | | | 247 | const collapsed = collapsedLines[i]; |
| b69ab31 | | | 248 | if (currentRegion?.same === same && currentRegion?.collapsed === collapsed) { |
| b69ab31 | | | 249 | currentRegion.lines.push(line); |
| b69ab31 | | | 250 | } else { |
| b69ab31 | | | 251 | if (currentRegion !== null) { |
| b69ab31 | | | 252 | regions.push(currentRegion); |
| b69ab31 | | | 253 | } |
| b69ab31 | | | 254 | currentRegion = {lines: [line], same, collapsed}; |
| b69ab31 | | | 255 | } |
| b69ab31 | | | 256 | }); |
| b69ab31 | | | 257 | if (currentRegion !== null) { |
| b69ab31 | | | 258 | regions.push(currentRegion); |
| b69ab31 | | | 259 | } |
| b69ab31 | | | 260 | |
| b69ab31 | | | 261 | return regions; |
| b69ab31 | | | 262 | } |
| b69ab31 | | | 263 | |
| b69ab31 | | | 264 | /** |
| b69ab31 | | | 265 | * Get the text of selected lines. It is the editing result. |
| b69ab31 | | | 266 | * |
| b69ab31 | | | 267 | * Note: passing `getSelectedText` to `fromText` does not maintain the selection |
| b69ab31 | | | 268 | * state. For example, from an empty text to `1\n1\n1\n`. The user might select |
| b69ab31 | | | 269 | * the 1st, 2nd, or 3rd line. That's 3 different ways of selections with the same |
| b69ab31 | | | 270 | * `getSelectedText` output. |
| b69ab31 | | | 271 | */ |
| b69ab31 | | | 272 | getSelectedText = cachedMethod(this.getSelectedTextImpl, {cache: getSelectedTextCache}); |
| b69ab31 | | | 273 | private getSelectedTextImpl(): string { |
| b69ab31 | | | 274 | return ( |
| b69ab31 | | | 275 | this.inner.lines |
| b69ab31 | | | 276 | // eslint-disable-next-line no-bitwise |
| b69ab31 | | | 277 | .filter(l => (l.bits & 0b010) !== 0) |
| b69ab31 | | | 278 | .map(l => l.data) |
| b69ab31 | | | 279 | .join('') |
| b69ab31 | | | 280 | ); |
| b69ab31 | | | 281 | } |
| b69ab31 | | | 282 | |
| b69ab31 | | | 283 | /** |
| b69ab31 | | | 284 | * Calculate the "inverse" of selected text. Useful for `revert -i` or "Discard". |
| b69ab31 | | | 285 | * |
| b69ab31 | | | 286 | * A Selected B | Inverse Note |
| b69ab31 | | | 287 | * 0 0 1 | 1 + not selected, preserve B |
| b69ab31 | | | 288 | * 0 1 0 | 0 = preserve B |
| b69ab31 | | | 289 | * 0 1 1 | 0 + selected, drop B, preserve A |
| b69ab31 | | | 290 | * 1 0 0 | 1 - selected, drop B, preserve A |
| b69ab31 | | | 291 | * 1 0 1 | 1 = preserve B |
| b69ab31 | | | 292 | * 1 1 0 | 0 - not selected, preserve B |
| b69ab31 | | | 293 | * 1 1 1 | 1 = preserve B |
| b69ab31 | | | 294 | */ |
| b69ab31 | | | 295 | getInverseText(): string { |
| b69ab31 | | | 296 | return this.inner.lines |
| b69ab31 | | | 297 | .filter(l => [0b001, 0b100, 0b101, 0b111].includes(l.bits)) |
| b69ab31 | | | 298 | .map(l => l.data) |
| b69ab31 | | | 299 | .join(''); |
| b69ab31 | | | 300 | } |
| b69ab31 | | | 301 | |
| b69ab31 | | | 302 | /** |
| b69ab31 | | | 303 | * Select or deselect lines. |
| b69ab31 | | | 304 | * |
| b69ab31 | | | 305 | * `selects` is a list of tuples. Each tuple has a `rawIndex` and whether that |
| b69ab31 | | | 306 | * line is selected or not. |
| b69ab31 | | | 307 | * Note if a line is deleted (sign is '-'), then selected means deleting that line. |
| b69ab31 | | | 308 | * |
| b69ab31 | | | 309 | * Note all lines are editable. Lines that are not editable are silently ignored. |
| b69ab31 | | | 310 | */ |
| b69ab31 | | | 311 | setSelectedLines(selects: Array<[LineIdx, boolean]>): ChunkSelectState { |
| b69ab31 | | | 312 | const newLines = this.inner.lines.withMutations(mutLines => { |
| b69ab31 | | | 313 | let lines = mutLines; |
| b69ab31 | | | 314 | selects.forEach(([idx, selected]) => { |
| b69ab31 | | | 315 | const line = lines.get(idx); |
| b69ab31 | | | 316 | if (line === undefined) { |
| b69ab31 | | | 317 | return; |
| b69ab31 | | | 318 | } |
| b69ab31 | | | 319 | const {bits} = line; |
| b69ab31 | | | 320 | // eslint-disable-next-line no-bitwise |
| b69ab31 | | | 321 | const bits101 = bits & 0b101; |
| b69ab31 | | | 322 | if (bits101 === 0 || bits101 === 0b101) { |
| b69ab31 | | | 323 | // Not changed in v0 and v2 - ignore editing. |
| b69ab31 | | | 324 | return; |
| b69ab31 | | | 325 | } |
| b69ab31 | | | 326 | const oldSelected: boolean = |
| b69ab31 | | | 327 | // eslint-disable-next-line no-bitwise |
| b69ab31 | | | 328 | bits101 === 0b100 ? (bits & 0b010) === 0 : (bits & 0b010) !== 0; |
| b69ab31 | | | 329 | if (oldSelected !== selected) { |
| b69ab31 | | | 330 | // Update selection by toggling (xor) rev 1. |
| b69ab31 | | | 331 | // eslint-disable-next-line no-bitwise |
| b69ab31 | | | 332 | const newBits = bits ^ 0b010; |
| b69ab31 | | | 333 | const newLine = line.set('bits', newBits as Bits); |
| b69ab31 | | | 334 | lines = lines.set(idx, newLine); |
| b69ab31 | | | 335 | } |
| b69ab31 | | | 336 | }); |
| b69ab31 | | | 337 | return lines; |
| b69ab31 | | | 338 | }); |
| b69ab31 | | | 339 | return new ChunkSelectState(this.inner.set('lines', newLines)); |
| b69ab31 | | | 340 | } |
| b69ab31 | | | 341 | |
| b69ab31 | | | 342 | /** |
| b69ab31 | | | 343 | * Free-form edit selected text. |
| b69ab31 | | | 344 | * |
| b69ab31 | | | 345 | * Runs analysis to mark lines as selected. Consider only calling this once |
| b69ab31 | | | 346 | * when switching from free-form editing to line selections. |
| b69ab31 | | | 347 | */ |
| b69ab31 | | | 348 | setSelectedText(text: string): ChunkSelectState { |
| b69ab31 | | | 349 | const {a, b} = this.inner; |
| b69ab31 | | | 350 | return ChunkSelectState.fromText(a, b, text); |
| b69ab31 | | | 351 | } |
| b69ab31 | | | 352 | |
| b69ab31 | | | 353 | /** |
| b69ab31 | | | 354 | * The constructor is for internal use only. |
| b69ab31 | | | 355 | * Use static methods to construct `ChunkSelectState`. |
| b69ab31 | | | 356 | */ |
| b69ab31 | | | 357 | constructor(record: ChunkSelectRecord) { |
| b69ab31 | | | 358 | super(record); |
| b69ab31 | | | 359 | } |
| b69ab31 | | | 360 | } |
| b69ab31 | | | 361 | |
| b69ab31 | | | 362 | const getLinesCache = new LRU(1000); |
| b69ab31 | | | 363 | const getSelectedTextCache = new LRU(1000); |
| b69ab31 | | | 364 | |
| b69ab31 | | | 365 | /** A line and its position on both sides, and selection state. */ |
| b69ab31 | | | 366 | export type SelectLine = { |
| b69ab31 | | | 367 | /** Index in the `lines` internal state. Starting from 0. */ |
| b69ab31 | | | 368 | rawIndex: LineIdx; |
| b69ab31 | | | 369 | |
| b69ab31 | | | 370 | /** Line index on the `a` side for rendering, or `null` if the line does not exist on the `a` side. */ |
| b69ab31 | | | 371 | aLine: LineIdx | null; |
| b69ab31 | | | 372 | |
| b69ab31 | | | 373 | /** Line index on the `b` side for rendering, or `null` if the line does not exist on the `b` side. */ |
| b69ab31 | | | 374 | bLine: LineIdx | null; |
| b69ab31 | | | 375 | |
| b69ab31 | | | 376 | /** Line index for "selected" lines, for rendering, or `null` if the line is not in the "selected" side. */ |
| b69ab31 | | | 377 | selLine: LineIdx | null; |
| b69ab31 | | | 378 | |
| b69ab31 | | | 379 | /** See `Sign` for description. */ |
| b69ab31 | | | 380 | sign: Sign; |
| b69ab31 | | | 381 | |
| b69ab31 | | | 382 | /** |
| b69ab31 | | | 383 | * Whether the line is selected or not. Only used when sign is '-' or '+'. |
| b69ab31 | | | 384 | * Note if a line is deleted (sign is '-'), then selected means deleting that line. |
| b69ab31 | | | 385 | */ |
| b69ab31 | | | 386 | selected: boolean | null; |
| b69ab31 | | | 387 | |
| b69ab31 | | | 388 | /** Line selection bits. */ |
| b69ab31 | | | 389 | bits: Bits; |
| b69ab31 | | | 390 | |
| b69ab31 | | | 391 | /** Content of the line. */ |
| b69ab31 | | | 392 | data: string; |
| b69ab31 | | | 393 | }; |
| b69ab31 | | | 394 | |
| b69ab31 | | | 395 | /** |
| b69ab31 | | | 396 | * A contiguous range of lines that share same properties. |
| b69ab31 | | | 397 | * Properties include: "same for all version", "collapsed". |
| b69ab31 | | | 398 | */ |
| b69ab31 | | | 399 | export type LineRegion = { |
| b69ab31 | | | 400 | /** Lines in the region. */ |
| b69ab31 | | | 401 | lines: SelectLine[]; |
| b69ab31 | | | 402 | |
| b69ab31 | | | 403 | /** If the region has the same content for all versions. */ |
| b69ab31 | | | 404 | same: boolean; |
| b69ab31 | | | 405 | |
| b69ab31 | | | 406 | /** If the region is collapsed. */ |
| b69ab31 | | | 407 | collapsed: boolean; |
| b69ab31 | | | 408 | }; |
| b69ab31 | | | 409 | |
| b69ab31 | | | 410 | /** '-': deletion; '+': insertion; '': unchanged; '!+', '!-': forced insertion or deletion, not selectable. */ |
| b69ab31 | | | 411 | type Sign = '' | '+' | '-' | '!+' | '!-'; |
| b69ab31 | | | 412 | |
| b69ab31 | | | 413 | type ChunkSelectProps = { |
| b69ab31 | | | 414 | a: string; |
| b69ab31 | | | 415 | b: string; |
| b69ab31 | | | 416 | lines: List<LineBitsRecord>; |
| b69ab31 | | | 417 | }; |
| b69ab31 | | | 418 | const ChunkSelectRecord = Record<ChunkSelectProps>({a: '', b: '', lines: List()}); |
| b69ab31 | | | 419 | type ChunkSelectRecord = RecordOf<ChunkSelectProps>; |
| b69ab31 | | | 420 | |
| b69ab31 | | | 421 | type Bits = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7; |
| b69ab31 | | | 422 | |
| b69ab31 | | | 423 | /** Similar to `FlattenLine` but compress Set<Rev> into 3 bits for easier access. */ |
| b69ab31 | | | 424 | type LineBitsProps = { |
| b69ab31 | | | 425 | /** Line content including `\n` EOL. */ |
| b69ab31 | | | 426 | data: string; |
| b69ab31 | | | 427 | /** Bitset. 0b100: left side; 0b001: right side; 0b010: editing / selecting text. */ |
| b69ab31 | | | 428 | bits: Bits; |
| b69ab31 | | | 429 | }; |
| b69ab31 | | | 430 | const LineBitsRecord = Record<LineBitsProps>({data: '', bits: 0}); |
| b69ab31 | | | 431 | type LineBitsRecord = RecordOf<LineBitsProps>; |
| b69ab31 | | | 432 | |
| b69ab31 | | | 433 | /** |
| b69ab31 | | | 434 | * Converts `FlattenLine` to `LineBits`. |
| b69ab31 | | | 435 | * `line.revs` (`ImSet<Rev>`) is converted to 3 bits. 0b100: rev 0; 0b010: rev 1; 0b001: rev 2 |
| b69ab31 | | | 436 | */ |
| b69ab31 | | | 437 | function toLineBits(line: FlattenLine): LineBitsRecord { |
| b69ab31 | | | 438 | // eslint-disable-next-line no-bitwise |
| b69ab31 | | | 439 | const bits = line.revs.reduce((acc, rev) => acc | (4 >> rev), 0); |
| b69ab31 | | | 440 | return LineBitsRecord({data: line.data, bits: bits as Bits}); |
| b69ab31 | | | 441 | } |
| b69ab31 | | | 442 | |
| b69ab31 | | | 443 | const bitsToOrder = [ |
| b69ab31 | | | 444 | 0, // 0b000: unused |
| b69ab31 | | | 445 | 2, // 0b001: normal insertion |
| b69ab31 | | | 446 | 2, // 0b010: unselectable insertion |
| b69ab31 | | | 447 | 2, // 0b011: normal insertion |
| b69ab31 | | | 448 | 1, // 0b100: normal deletion |
| b69ab31 | | | 449 | 1, // 0b101: unselectable deletion |
| b69ab31 | | | 450 | 1, // 0b110: normal deletion |
| b69ab31 | | | 451 | 0, // 0b111: normal |
| b69ab31 | | | 452 | ]; |