| 1 | import { DiffLine } from "../.."; |
| 2 | |
| 3 | /** |
| 4 | * Given a diff of two editable regions, get the offset position at the last new line inside the editable region. |
| 5 | * @param diffLines Result of myersDiff. |
| 6 | * @param lineContentAtCursorPos Content of the line at cursor position. |
| 7 | * @param lineOffsetAtCursorPos Offset of the line at cursor position compared to the start of the editable region. |
| 8 | * @returns Offset position at last new line inside the editable region. |
| 9 | */ |
| 10 | export function getOffsetPositionAtLastNewLine( |
| 11 | diffLines: DiffLine[], |
| 12 | lineContentAtCursorPos: string, |
| 13 | lineOffsetAtCursorPos: number, |
| 14 | ): { |
| 15 | line: number; |
| 16 | character: number; |
| 17 | } { |
| 18 | let lastNewLineContent = ""; |
| 19 | let lineOffset = -1; |
| 20 | let currentResultLine = 0; |
| 21 | let hasChanges = false; |
| 22 | |
| 23 | // Build the string while tracking line numbers in the result |
| 24 | diffLines.reduce((acc, curr, i) => { |
| 25 | // Add the current line to our result |
| 26 | acc += curr.line; |
| 27 | |
| 28 | // Add newline if not the last line |
| 29 | if (i < diffLines.length - 1) { |
| 30 | acc += "\n"; |
| 31 | } |
| 32 | |
| 33 | // If this is a "new" or "same" line, it will be part of the result |
| 34 | if (curr.type === "new" || curr.type === "same") { |
| 35 | if (curr.type === "new") { |
| 36 | // If it's a new line, update our tracking |
| 37 | lastNewLineContent = curr.line; |
| 38 | lineOffset = currentResultLine; |
| 39 | hasChanges = true; |
| 40 | } |
| 41 | // Increment our position in the result |
| 42 | currentResultLine++; |
| 43 | } |
| 44 | |
| 45 | return acc; |
| 46 | }, ""); |
| 47 | |
| 48 | // If nothing has changed, return the original position |
| 49 | if (!hasChanges) { |
| 50 | lineOffset = lineOffsetAtCursorPos; |
| 51 | lastNewLineContent = lineContentAtCursorPos; |
| 52 | } |
| 53 | // Calculate the character position for the end of the last relevant line |
| 54 | const endOfCharPos = lastNewLineContent.length; |
| 55 | return { |
| 56 | line: lineOffset, |
| 57 | character: endOfCharPos, |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | export function getRenderableDiffWithGutterAnnotations( |
| 62 | diffLines: DiffLine[], |
| 63 | lineContentAtCursorPos: string, |
| 64 | lineOffsetAtCursorPos: number, |
| 65 | ): { |
| 66 | offset: { |
| 67 | line: number; |
| 68 | character: number; |
| 69 | }; |
| 70 | } { |
| 71 | let lastNewLineContent = ""; |
| 72 | let lineOffset = -1; |
| 73 | let currentResultLine = 0; |
| 74 | let hasChanges = false; |
| 75 | |
| 76 | // Build the string while tracking line numbers in the result |
| 77 | diffLines.reduce((acc, curr, i) => { |
| 78 | // Add the current line to our result |
| 79 | acc += curr.line; |
| 80 | |
| 81 | // Add newline if not the last line |
| 82 | if (i < diffLines.length - 1) { |
| 83 | acc += "\n"; |
| 84 | } |
| 85 | |
| 86 | // If this is a "new" or "same" line, it will be part of the result |
| 87 | if (curr.type === "new" || curr.type === "same") { |
| 88 | if (curr.type === "new") { |
| 89 | // If it's a new line, update our tracking |
| 90 | lastNewLineContent = curr.line; |
| 91 | lineOffset = currentResultLine; |
| 92 | hasChanges = true; |
| 93 | } |
| 94 | // Increment our position in the result |
| 95 | currentResultLine++; |
| 96 | } |
| 97 | |
| 98 | return acc; |
| 99 | }, ""); |
| 100 | |
| 101 | // If nothing has changed, return the original position |
| 102 | if (!hasChanges) { |
| 103 | lineOffset = lineOffsetAtCursorPos; |
| 104 | lastNewLineContent = lineContentAtCursorPos; |
| 105 | } |
| 106 | // Calculate the character position for the end of the last relevant line |
| 107 | const endOfCharPos = lastNewLineContent.length; |
| 108 | return { |
| 109 | offset: { |
| 110 | line: lineOffset, |
| 111 | character: endOfCharPos, |
| 112 | }, |
| 113 | }; |
| 114 | } |