-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
139 changed files
with
1,589 additions
and
1,369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
tags: [enhancement] | ||
pullRequest: 2236 | ||
--- | ||
|
||
- Added increment action. Will increment a number. eg `"increment this"` to change `1` to `2`. | ||
|
||
- Added decrement action. Will decrement a number. eg `"decrement this"` to change `2` to `1`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/cursorless-engine/src/actions/incrementDecrement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { Range, TextEditor } from "@cursorless/common"; | ||
import { PlainTarget } from "../processTargets/targets"; | ||
import { SelectionWithEditor } from "../typings/Types"; | ||
import { Destination, Target } from "../typings/target.types"; | ||
import { MatchedText, matchText } from "../util/regex"; | ||
import { runForEachEditor } from "../util/targetUtils"; | ||
import { Actions } from "./Actions"; | ||
import { ActionReturnValue } from "./actions.types"; | ||
|
||
const REGEX = /-?\d+(\.\d+)?/g; | ||
|
||
class IncrementDecrement { | ||
constructor( | ||
private actions: Actions, | ||
private isIncrement: boolean, | ||
) { | ||
this.run = this.run.bind(this); | ||
} | ||
|
||
async run(targets: Target[]): Promise<ActionReturnValue> { | ||
const thatSelections: SelectionWithEditor[] = []; | ||
|
||
await runForEachEditor( | ||
targets, | ||
(target) => target.editor, | ||
async (editor, targets) => { | ||
const selections = await this.runOnEditor(editor, targets); | ||
thatSelections.push(...selections); | ||
}, | ||
); | ||
|
||
return { thatSelections }; | ||
} | ||
|
||
private async runOnEditor( | ||
editor: TextEditor, | ||
targets: Target[], | ||
): Promise<SelectionWithEditor[]> { | ||
const { document } = editor; | ||
const destinations: Destination[] = []; | ||
const replaceWith: string[] = []; | ||
|
||
for (const target of targets) { | ||
const offset = document.offsetAt(target.contentRange.start); | ||
const text = target.contentText; | ||
const matches = matchText(text, REGEX); | ||
|
||
for (const match of matches) { | ||
destinations.push(createDestination(editor, offset, match)); | ||
replaceWith.push(updateNumber(this.isIncrement, match.text)); | ||
} | ||
} | ||
|
||
const { thatSelections } = await this.actions.replace.run( | ||
destinations, | ||
replaceWith, | ||
); | ||
|
||
return thatSelections!; | ||
} | ||
} | ||
|
||
export class Increment extends IncrementDecrement { | ||
constructor(actions: Actions) { | ||
super(actions, true); | ||
} | ||
} | ||
|
||
export class Decrement extends IncrementDecrement { | ||
constructor(actions: Actions) { | ||
super(actions, false); | ||
} | ||
} | ||
|
||
function createDestination( | ||
editor: TextEditor, | ||
offset: number, | ||
match: MatchedText, | ||
): Destination { | ||
const target = new PlainTarget({ | ||
editor, | ||
isReversed: false, | ||
contentRange: new Range( | ||
editor.document.positionAt(offset + match.index), | ||
editor.document.positionAt(offset + match.index + match.text.length), | ||
), | ||
}); | ||
return target.toDestination("to"); | ||
} | ||
|
||
function updateNumber(isIncrement: boolean, text: string): string { | ||
return text.includes(".") | ||
? updateFloat(isIncrement, text).toString() | ||
: updateInteger(isIncrement, text).toString(); | ||
} | ||
|
||
function updateInteger(isIncrement: boolean, text: string): number { | ||
const original = parseInt(text); | ||
const diff = 1; | ||
return original + (isIncrement ? diff : -diff); | ||
} | ||
|
||
function updateFloat(isIncrement: boolean, text: string): number { | ||
const original = parseFloat(text); | ||
const isPercentage = Math.abs(original) <= 1.0; | ||
const diff = isPercentage ? 0.1 : 1; | ||
const updated = original + (isIncrement ? diff : -diff); | ||
// Remove precision problems that would add a lot of extra digits | ||
return parseFloat(updated.toPrecision(15)) / 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.