|
| 1 | +import { Range, TextEditor } from "@cursorless/common"; |
| 2 | +import { PlainTarget } from "../processTargets/targets"; |
| 3 | +import { SelectionWithEditor } from "../typings/Types"; |
| 4 | +import { Destination, Target } from "../typings/target.types"; |
| 5 | +import { MatchedText, matchText } from "../util/regex"; |
| 6 | +import { runForEachEditor } from "../util/targetUtils"; |
| 7 | +import { Actions } from "./Actions"; |
| 8 | +import { ActionReturnValue } from "./actions.types"; |
| 9 | + |
| 10 | +const REGEX = /-?\d+(\.\d+)?/g; |
| 11 | + |
| 12 | +class IncrementDecrement { |
| 13 | + constructor( |
| 14 | + private actions: Actions, |
| 15 | + private isIncrement: boolean, |
| 16 | + ) { |
| 17 | + this.run = this.run.bind(this); |
| 18 | + } |
| 19 | + |
| 20 | + async run(targets: Target[]): Promise<ActionReturnValue> { |
| 21 | + const thatSelections: SelectionWithEditor[] = []; |
| 22 | + |
| 23 | + await runForEachEditor( |
| 24 | + targets, |
| 25 | + (target) => target.editor, |
| 26 | + async (editor, targets) => { |
| 27 | + const selections = await this.runOnEditor(editor, targets); |
| 28 | + thatSelections.push(...selections); |
| 29 | + }, |
| 30 | + ); |
| 31 | + |
| 32 | + return { thatSelections }; |
| 33 | + } |
| 34 | + |
| 35 | + private async runOnEditor( |
| 36 | + editor: TextEditor, |
| 37 | + targets: Target[], |
| 38 | + ): Promise<SelectionWithEditor[]> { |
| 39 | + const { document } = editor; |
| 40 | + const destinations: Destination[] = []; |
| 41 | + const replaceWith: string[] = []; |
| 42 | + |
| 43 | + for (const target of targets) { |
| 44 | + const offset = document.offsetAt(target.contentRange.start); |
| 45 | + const text = target.contentText; |
| 46 | + const matches = matchText(text, REGEX); |
| 47 | + |
| 48 | + for (const match of matches) { |
| 49 | + destinations.push(createDestination(editor, offset, match)); |
| 50 | + replaceWith.push(updateNumber(this.isIncrement, match.text)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + const { thatSelections } = await this.actions.replace.run( |
| 55 | + destinations, |
| 56 | + replaceWith, |
| 57 | + ); |
| 58 | + |
| 59 | + return thatSelections!; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export class Increment extends IncrementDecrement { |
| 64 | + constructor(actions: Actions) { |
| 65 | + super(actions, true); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +export class Decrement extends IncrementDecrement { |
| 70 | + constructor(actions: Actions) { |
| 71 | + super(actions, false); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +function createDestination( |
| 76 | + editor: TextEditor, |
| 77 | + offset: number, |
| 78 | + match: MatchedText, |
| 79 | +): Destination { |
| 80 | + const target = new PlainTarget({ |
| 81 | + editor, |
| 82 | + isReversed: false, |
| 83 | + contentRange: new Range( |
| 84 | + editor.document.positionAt(offset + match.index), |
| 85 | + editor.document.positionAt(offset + match.index + match.text.length), |
| 86 | + ), |
| 87 | + }); |
| 88 | + return target.toDestination("to"); |
| 89 | +} |
| 90 | + |
| 91 | +function updateNumber(isIncrement: boolean, text: string): string { |
| 92 | + return text.includes(".") |
| 93 | + ? updateFloat(isIncrement, text).toString() |
| 94 | + : updateInteger(isIncrement, text).toString(); |
| 95 | +} |
| 96 | + |
| 97 | +function updateInteger(isIncrement: boolean, text: string): number { |
| 98 | + const original = parseInt(text); |
| 99 | + const diff = 1; |
| 100 | + return original + (isIncrement ? diff : -diff); |
| 101 | +} |
| 102 | + |
| 103 | +function updateFloat(isIncrement: boolean, text: string): number { |
| 104 | + const original = parseFloat(text); |
| 105 | + const isPercentage = Math.abs(original) <= 1.0; |
| 106 | + const diff = isPercentage ? 0.1 : 1; |
| 107 | + const updated = original + (isIncrement ? diff : -diff); |
| 108 | + // Remove precision problems that would add a lot of extra digits |
| 109 | + return parseFloat(updated.toPrecision(15)) / 1; |
| 110 | +} |
0 commit comments