-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added increment and decrement actions (#2236)
Fixes #2192 ## Checklist - [x] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [x] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [x] I have not broken the cheatsheet --------- Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com>
- Loading branch information
1 parent
585c852
commit aaf645a
Showing
12 changed files
with
564 additions
and
1 deletion.
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
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
162 changes: 162 additions & 0 deletions
162
packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/actions/decrementFile.yml
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,162 @@ | ||
languageId: plaintext | ||
command: | ||
version: 6 | ||
spokenForm: decrement file | ||
action: | ||
name: decrement | ||
target: | ||
type: primitive | ||
modifiers: | ||
- type: containingScope | ||
scopeType: {type: document} | ||
usePrePhraseSnapshot: true | ||
initialState: | ||
documentContents: |- | ||
foo | ||
0 | ||
1 | ||
0.5 | ||
1.5 | ||
-0 | ||
-1 | ||
-0.5 | ||
-1.5 | ||
0rem | ||
1rem | ||
0.5rem | ||
1.5rem | ||
-0rem | ||
-1rem | ||
-0.5rem | ||
-1.5rem | ||
selections: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 0} | ||
marks: {} | ||
finalState: | ||
documentContents: |- | ||
foo | ||
-1 | ||
0 | ||
0.4 | ||
0.5 | ||
-1 | ||
-2 | ||
-0.6 | ||
-2.5 | ||
-1rem | ||
0rem | ||
0.4rem | ||
0.5rem | ||
-1rem | ||
-2rem | ||
-0.6rem | ||
-2.5rem | ||
selections: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 0} | ||
thatMark: | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 2, character: 0} | ||
end: {line: 2, character: 2} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 3, character: 0} | ||
end: {line: 3, character: 1} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 4, character: 0} | ||
end: {line: 4, character: 3} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 5, character: 0} | ||
end: {line: 5, character: 3} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 7, character: 0} | ||
end: {line: 7, character: 2} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 8, character: 0} | ||
end: {line: 8, character: 2} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 9, character: 0} | ||
end: {line: 9, character: 4} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 10, character: 0} | ||
end: {line: 10, character: 4} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 12, character: 0} | ||
end: {line: 12, character: 2} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 13, character: 0} | ||
end: {line: 13, character: 1} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 14, character: 0} | ||
end: {line: 14, character: 3} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 15, character: 0} | ||
end: {line: 15, character: 3} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 17, character: 0} | ||
end: {line: 17, character: 2} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 18, character: 0} | ||
end: {line: 18, character: 2} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 19, character: 0} | ||
end: {line: 19, character: 4} | ||
isReversed: false | ||
hasExplicitRange: true | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 20, character: 0} | ||
end: {line: 20, character: 4} | ||
isReversed: false | ||
hasExplicitRange: true |
Oops, something went wrong.
aaf645a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have pulled latest main and I can do incrementing and decrementing but it's not in the cursorless cheatsheet
aaf645a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strange; it's in my cheatsheet. This is when you say "cursorless cheatsheet"? Are you on latest cursorless-talon?
aaf645a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah it regenerates the sheet when you run the command! I've just had the cheatsheet.html bookmarked and opened it that way. It's there after running the 'cursorless cheatsheet' command, thank you!