| 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 | import type {FileRev} from './common'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {List, Record} from 'immutable'; |
| b69ab31 | | | 13 | import {SelfUpdate} from 'shared/immutableExt'; |
| b69ab31 | | | 14 | import {LineLog} from '../linelog'; |
| b69ab31 | | | 15 | import {next} from './revMath'; |
| b69ab31 | | | 16 | |
| b69ab31 | | | 17 | /** |
| b69ab31 | | | 18 | * A stack of file contents with stack editing features. |
| b69ab31 | | | 19 | */ |
| b69ab31 | | | 20 | export class FileStackState extends SelfUpdate<FileStackStateRecord> { |
| b69ab31 | | | 21 | constructor(value: Source | string[]) { |
| b69ab31 | | | 22 | if (Array.isArray(value)) { |
| b69ab31 | | | 23 | const contents: string[] = value; |
| b69ab31 | | | 24 | const source = Source({ |
| b69ab31 | | | 25 | type: 'plain', |
| b69ab31 | | | 26 | value: List(contents), |
| b69ab31 | | | 27 | revLength: contents.length as FileRev, |
| b69ab31 | | | 28 | }); |
| b69ab31 | | | 29 | super(FileStackStateRecord({source})); |
| b69ab31 | | | 30 | } else { |
| b69ab31 | | | 31 | super(FileStackStateRecord({source: value})); |
| b69ab31 | | | 32 | } |
| b69ab31 | | | 33 | } |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | get source(): Source { |
| b69ab31 | | | 36 | return this.inner.source; |
| b69ab31 | | | 37 | } |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | get revLength(): FileRev { |
| b69ab31 | | | 40 | return this.inner.source.revLength; |
| b69ab31 | | | 41 | } |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | fromLineLog(log: LineLog): FileStackState { |
| b69ab31 | | | 44 | return new FileStackState( |
| b69ab31 | | | 45 | Source({type: 'linelog', value: log, revLength: (Math.floor(log.maxRev) + 1) as FileRev}), |
| b69ab31 | | | 46 | ); |
| b69ab31 | | | 47 | } |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | fromFlattenLines(lines: List<FlattenLine>, revLength: number | undefined): FileStackState { |
| b69ab31 | | | 50 | const newRevLength = (revLength ?? lines.map(l => l.revs.max()).max() ?? 0) as FileRev; |
| b69ab31 | | | 51 | const source = Source({type: 'flatten', value: lines, revLength: newRevLength}); |
| b69ab31 | | | 52 | return new FileStackState(source); |
| b69ab31 | | | 53 | } |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | // Read operations. |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | /** |
| b69ab31 | | | 58 | * Obtain the content at the given revision. |
| b69ab31 | | | 59 | * 0 <= rev < this.revLength |
| b69ab31 | | | 60 | */ |
| b69ab31 | | | 61 | getRev(rev: FileRev): string { |
| b69ab31 | | | 62 | const type = this.source.type; |
| b69ab31 | | | 63 | if (type === 'linelog') { |
| b69ab31 | | | 64 | return this.source.value.checkOut(rev); |
| b69ab31 | | | 65 | } else if (type === 'flatten') { |
| b69ab31 | | | 66 | return this.source.value |
| b69ab31 | | | 67 | .filter(l => l.revs.has(rev)) |
| b69ab31 | | | 68 | .map(l => l.data) |
| b69ab31 | | | 69 | .join(''); |
| b69ab31 | | | 70 | } else if (type === 'plain') { |
| b69ab31 | | | 71 | return this.source.value.get(rev, ''); |
| b69ab31 | | | 72 | } |
| b69ab31 | | | 73 | throw new Error(`unexpected source type ${type}`); |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | /** Array of valid revisions. */ |
| b69ab31 | | | 77 | revs(): FileRev[] { |
| b69ab31 | | | 78 | return [...Array(this.source.revLength).keys()] as FileRev[]; |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | /** |
| b69ab31 | | | 82 | * Calculate the dependencies of revisions. |
| b69ab31 | | | 83 | * For example, `{5: [3, 1]}` means rev 5 depends on rev 3 and rev 1. |
| b69ab31 | | | 84 | */ |
| b69ab31 | | | 85 | calculateDepMap(): Map<FileRev, Set<FileRev>> { |
| b69ab31 | | | 86 | return this.convertToLineLog().calculateDepMap() as Map<FileRev, Set<FileRev>>; |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | |
| b69ab31 | | | 89 | /** Figure out which `rev` introduces the lines. */ |
| b69ab31 | | | 90 | blame(rev: FileRev): FileRev[] { |
| b69ab31 | | | 91 | const log = this.convertToLineLog(); |
| b69ab31 | | | 92 | const lines = log.checkOutLines(rev); |
| b69ab31 | | | 93 | // Skip the last 'END' line. |
| b69ab31 | | | 94 | return lines.slice(0, lines.length - 1).map(l => l.rev as FileRev); |
| b69ab31 | | | 95 | } |
| b69ab31 | | | 96 | |
| b69ab31 | | | 97 | // Write operations. |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | /** |
| b69ab31 | | | 100 | * Edit full text of a rev. |
| b69ab31 | | | 101 | * If `updateStack` is true, the rest of the stack will be updated |
| b69ab31 | | | 102 | * accordingly. Otherwise, no other revs are updated. |
| b69ab31 | | | 103 | */ |
| b69ab31 | | | 104 | editText(rev: FileRev, text: string, updateStack = true): FileStackState { |
| b69ab31 | | | 105 | const revLength = rev >= this.source.revLength ? next(rev) : this.source.revLength; |
| b69ab31 | | | 106 | let source = this.source; |
| b69ab31 | | | 107 | if (updateStack) { |
| b69ab31 | | | 108 | const log = this.convertToLineLog().recordText(text, rev); |
| b69ab31 | | | 109 | source = Source({type: 'linelog', value: log, revLength}); |
| b69ab31 | | | 110 | } else { |
| b69ab31 | | | 111 | const plain = this.convertToPlainText().set(rev, text); |
| b69ab31 | | | 112 | source = Source({type: 'plain', value: plain, revLength}); |
| b69ab31 | | | 113 | } |
| b69ab31 | | | 114 | return new FileStackState(source); |
| b69ab31 | | | 115 | } |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | /** |
| b69ab31 | | | 118 | * Replace line range `a1` to `a2` at `aRev` with `bLines` from `bRev`. |
| b69ab31 | | | 119 | * The rest of the stack will be updated accordingly. |
| b69ab31 | | | 120 | * |
| b69ab31 | | | 121 | * The `aRev` decides what `a1` and `a2` mean, since line indexes |
| b69ab31 | | | 122 | * from different revs are different. The `aRev` is not the rev that |
| b69ab31 | | | 123 | * makes the change. |
| b69ab31 | | | 124 | * |
| b69ab31 | | | 125 | * The `bRev` is the revision that makes the change (deletion, insertion). |
| b69ab31 | | | 126 | * |
| b69ab31 | | | 127 | * This is useful to implement absorb-like edits at chunk (not full text) |
| b69ab31 | | | 128 | * level. For example, absorb runs from the top of a stack, and calculates |
| b69ab31 | | | 129 | * the diff between the stack top commit and the current working copy. |
| b69ab31 | | | 130 | * So `aRev` is the stack top, since the diff uses line numbers in the |
| b69ab31 | | | 131 | * stack top. `bRev` is the revisions that each chunk blames to. |
| b69ab31 | | | 132 | */ |
| b69ab31 | | | 133 | editChunk( |
| b69ab31 | | | 134 | aRev: FileRev, |
| b69ab31 | | | 135 | a1: LineIdx, |
| b69ab31 | | | 136 | a2: LineIdx, |
| b69ab31 | | | 137 | bRev: FileRev, |
| b69ab31 | | | 138 | bLines: string[], |
| b69ab31 | | | 139 | ): FileStackState { |
| b69ab31 | | | 140 | const log = this.convertToLineLog().editChunk(aRev, a1, a2, bRev, bLines); |
| b69ab31 | | | 141 | return this.fromLineLog(log); |
| b69ab31 | | | 142 | } |
| b69ab31 | | | 143 | |
| b69ab31 | | | 144 | /** |
| b69ab31 | | | 145 | * Remap the revs. This can be useful for reordering, folding, |
| b69ab31 | | | 146 | * and insertion. The callsite is responsible for checking |
| b69ab31 | | | 147 | * `revDepMap` to ensure the reordering can be "conflict"-free. |
| b69ab31 | | | 148 | */ |
| b69ab31 | | | 149 | remapRevs(revMap: Map<FileRev, FileRev> | ((rev: FileRev) => FileRev)): FileStackState { |
| b69ab31 | | | 150 | const log = this.convertToLineLog().remapRevs( |
| b69ab31 | | | 151 | revMap as Map<number, number> | ((rev: number) => number), |
| b69ab31 | | | 152 | ); |
| b69ab31 | | | 153 | return this.fromLineLog(log); |
| b69ab31 | | | 154 | } |
| b69ab31 | | | 155 | |
| b69ab31 | | | 156 | /** |
| b69ab31 | | | 157 | * Move (or copy) line range `a1` to `a2` at `aRev` to other revs. |
| b69ab31 | | | 158 | * Those lines will be included by `includeRevs` and excluded by `excludeRevs`. |
| b69ab31 | | | 159 | * |
| b69ab31 | | | 160 | * PERF: It would be better to just use linelog to complete the edit. |
| b69ab31 | | | 161 | */ |
| b69ab31 | | | 162 | moveLines( |
| b69ab31 | | | 163 | aRev: FileRev, |
| b69ab31 | | | 164 | a1: LineIdx, |
| b69ab31 | | | 165 | a2: LineIdx, |
| b69ab31 | | | 166 | includeRevs?: FileRev[], |
| b69ab31 | | | 167 | excludeRevs?: FileRev[], |
| b69ab31 | | | 168 | ): FileStackState { |
| b69ab31 | | | 169 | let revLineIdx = 0; |
| b69ab31 | | | 170 | const editLine = (line: FlattenLine): FlattenLine => { |
| b69ab31 | | | 171 | let newLine = line; |
| b69ab31 | | | 172 | if (line.revs.has(aRev)) { |
| b69ab31 | | | 173 | if (revLineIdx >= a1 && revLineIdx < a2) { |
| b69ab31 | | | 174 | const newRevs = line.revs.withMutations(mutRevs => { |
| b69ab31 | | | 175 | let revs = mutRevs; |
| b69ab31 | | | 176 | if (includeRevs) { |
| b69ab31 | | | 177 | revs = revs.union(includeRevs); |
| b69ab31 | | | 178 | } |
| b69ab31 | | | 179 | if (excludeRevs) { |
| b69ab31 | | | 180 | revs = revs.subtract(excludeRevs); |
| b69ab31 | | | 181 | } |
| b69ab31 | | | 182 | return revs; |
| b69ab31 | | | 183 | }); |
| b69ab31 | | | 184 | newLine = line.set('revs', newRevs); |
| b69ab31 | | | 185 | } |
| b69ab31 | | | 186 | revLineIdx++; |
| b69ab31 | | | 187 | } |
| b69ab31 | | | 188 | return newLine; |
| b69ab31 | | | 189 | }; |
| b69ab31 | | | 190 | |
| b69ab31 | | | 191 | return this.mapAllLines(editLine); |
| b69ab31 | | | 192 | } |
| b69ab31 | | | 193 | |
| b69ab31 | | | 194 | /** |
| b69ab31 | | | 195 | * Edit lines for all revisions using a callback. |
| b69ab31 | | | 196 | * The return type can be an array (like flatMap), to insert or delete lines. |
| b69ab31 | | | 197 | */ |
| b69ab31 | | | 198 | mapAllLines( |
| b69ab31 | | | 199 | editLineFunc: (line: FlattenLine, i: number) => FlattenLine | FlattenLine[], |
| b69ab31 | | | 200 | ): FileStackState { |
| b69ab31 | | | 201 | const lines = this.convertToFlattenLines().flatMap((line, i) => { |
| b69ab31 | | | 202 | const mapped = editLineFunc(line, i); |
| b69ab31 | | | 203 | return Array.isArray(mapped) ? mapped : [mapped]; |
| b69ab31 | | | 204 | }); |
| b69ab31 | | | 205 | return this.fromFlattenLines(lines, this.revLength); |
| b69ab31 | | | 206 | } |
| b69ab31 | | | 207 | |
| b69ab31 | | | 208 | /** |
| b69ab31 | | | 209 | * Truncate the stack. Drop rev (inclusive) and higher revs. |
| b69ab31 | | | 210 | * Note: This is only implemented for linelog. |
| b69ab31 | | | 211 | */ |
| b69ab31 | | | 212 | truncate(rev: FileRev): FileStackState { |
| b69ab31 | | | 213 | const log = this.convertToLineLog().truncate(rev); |
| b69ab31 | | | 214 | return this.fromLineLog(log); |
| b69ab31 | | | 215 | } |
| b69ab31 | | | 216 | |
| b69ab31 | | | 217 | // Internal format conversions. |
| b69ab31 | | | 218 | |
| b69ab31 | | | 219 | /** Convert to LineLog representation on demand. */ |
| b69ab31 | | | 220 | convertToLineLog(): LineLog { |
| b69ab31 | | | 221 | const type = 'linelog'; |
| b69ab31 | | | 222 | if (this.source.type === type) { |
| b69ab31 | | | 223 | return this.source.value; |
| b69ab31 | | | 224 | } |
| b69ab31 | | | 225 | let log = new LineLog(); |
| b69ab31 | | | 226 | this.revs().forEach(rev => { |
| b69ab31 | | | 227 | const data = this.getRev(rev); |
| b69ab31 | | | 228 | log = log.recordText(data, rev); |
| b69ab31 | | | 229 | }); |
| b69ab31 | | | 230 | return log; |
| b69ab31 | | | 231 | } |
| b69ab31 | | | 232 | |
| b69ab31 | | | 233 | /** Convert to flatten representation on demand. */ |
| b69ab31 | | | 234 | convertToFlattenLines(): List<FlattenLine> { |
| b69ab31 | | | 235 | const type = 'flatten'; |
| b69ab31 | | | 236 | if (this.source.type === type) { |
| b69ab31 | | | 237 | return this.source.value; |
| b69ab31 | | | 238 | } |
| b69ab31 | | | 239 | const log = this.convertToLineLog(); |
| b69ab31 | | | 240 | const lines = log.flatten(); |
| b69ab31 | | | 241 | return List(lines); |
| b69ab31 | | | 242 | } |
| b69ab31 | | | 243 | |
| b69ab31 | | | 244 | /** Convert to plain representation on demand. */ |
| b69ab31 | | | 245 | convertToPlainText(): List<string> { |
| b69ab31 | | | 246 | const type = 'plain'; |
| b69ab31 | | | 247 | if (this.source.type === type) { |
| b69ab31 | | | 248 | return this.source.value; |
| b69ab31 | | | 249 | } |
| b69ab31 | | | 250 | const contents = this.revs().map(this.getRev.bind(this)); |
| b69ab31 | | | 251 | return List(contents); |
| b69ab31 | | | 252 | } |
| b69ab31 | | | 253 | } |
| b69ab31 | | | 254 | |
| b69ab31 | | | 255 | /** |
| b69ab31 | | | 256 | * Depending on the operation, there are different ways to represent the file contents: |
| b69ab31 | | | 257 | * - plain: Full text per revision. This is the initial representation. |
| b69ab31 | | | 258 | * - linelog: LineLog representation. This is useful for analysis (dependency, target |
| b69ab31 | | | 259 | * revs for absorb, line number offset calculation, etc), certain kinds of |
| b69ab31 | | | 260 | * editing, and generating the flatten representation. |
| b69ab31 | | | 261 | * - flatten: The flatten view of LineLog. This is useful for moving lines between |
| b69ab31 | | | 262 | * revisions more explicitly. |
| b69ab31 | | | 263 | */ |
| b69ab31 | | | 264 | type SourceProps = |
| b69ab31 | | | 265 | | { |
| b69ab31 | | | 266 | type: 'linelog'; |
| b69ab31 | | | 267 | value: LineLog; |
| b69ab31 | | | 268 | revLength: FileRev; |
| b69ab31 | | | 269 | } |
| b69ab31 | | | 270 | | { |
| b69ab31 | | | 271 | type: 'plain'; |
| b69ab31 | | | 272 | value: List<string>; |
| b69ab31 | | | 273 | revLength: FileRev; |
| b69ab31 | | | 274 | } |
| b69ab31 | | | 275 | | { |
| b69ab31 | | | 276 | type: 'flatten'; |
| b69ab31 | | | 277 | value: List<FlattenLine>; |
| b69ab31 | | | 278 | revLength: FileRev; |
| b69ab31 | | | 279 | }; |
| b69ab31 | | | 280 | |
| b69ab31 | | | 281 | export const Source = Record<SourceProps>({ |
| b69ab31 | | | 282 | type: 'plain', |
| b69ab31 | | | 283 | value: List([]), |
| b69ab31 | | | 284 | revLength: 0 as FileRev, |
| b69ab31 | | | 285 | }); |
| b69ab31 | | | 286 | type Source = RecordOf<SourceProps>; |
| b69ab31 | | | 287 | |
| b69ab31 | | | 288 | type FileStackStateProps = { |
| b69ab31 | | | 289 | source: Source; |
| b69ab31 | | | 290 | }; |
| b69ab31 | | | 291 | const FileStackStateRecord = Record<FileStackStateProps>({source: Source()}); |
| b69ab31 | | | 292 | type FileStackStateRecord = RecordOf<FileStackStateProps>; |
| b69ab31 | | | 293 | |
| b69ab31 | | | 294 | export type {FileRev}; |