feat(keymap-upgrader): Highlight changes

Updated the keymap upgrader to highlight which lines it changed as well
as indicate when nothing needed to be upgraded.

Also adjusted the line highlight colors to be more readable in both
light and dark color schemes.
This commit is contained in:
Joel Spadin
2024-01-21 19:31:55 -06:00
parent 84e056793b
commit 37fcf190e6
6 changed files with 109 additions and 45 deletions

View File

@@ -1,5 +1,5 @@
import { createParser } from "./parser";
import { applyEdits } from "./textedit";
import { applyEdits, Range } from "./textedit";
import { upgradeBehaviors } from "./behaviors";
import { upgradeHeaders } from "./headers";
@@ -23,3 +23,37 @@ export function upgradeKeymap(text: string) {
return applyEdits(text, edits);
}
export function rangesToLineNumbers(
text: string,
changedRanges: Range[]
): string {
const lineBreaks = getLineBreakPositions(text);
const changedLines = changedRanges.map((range) => {
const startLine = positionToLineNumber(range.startIndex, lineBreaks);
const endLine = positionToLineNumber(range.endIndex, lineBreaks);
return startLine === endLine ? `${startLine}` : `${startLine}-${endLine}`;
});
return `{${changedLines.join(",")}}`;
}
function getLineBreakPositions(text: string) {
const positions: number[] = [];
let index = 0;
while ((index = text.indexOf("\n", index)) >= 0) {
positions.push(index);
index++;
}
return positions;
}
function positionToLineNumber(position: number, lineBreaks: number[]) {
const line = lineBreaks.findIndex((lineBreak) => position <= lineBreak);
return line < 0 ? 0 : line + 1;
}