-
-
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.
## Checklist - [/] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [/] 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) - [/] I have not broken the cheatsheet --------- Co-authored-by: Pokey Rule <[email protected]>
- Loading branch information
1 parent
62ceed4
commit d323e35
Showing
8 changed files
with
221 additions
and
8 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,30 @@ | ||
languageId: plaintext | ||
command: | ||
version: 7 | ||
spokenForm: dedent line | ||
action: | ||
name: outdentLine | ||
target: | ||
type: primitive | ||
modifiers: | ||
- type: containingScope | ||
scopeType: {type: line} | ||
usePrePhraseSnapshot: true | ||
initialState: | ||
documentContents: " foo" | ||
selections: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 0} | ||
marks: {} | ||
finalState: | ||
documentContents: foo | ||
selections: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 0} | ||
thatMark: | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 0, character: 0} | ||
end: {line: 0, character: 3} | ||
isReversed: false | ||
hasExplicitRange: true |
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,30 @@ | ||
languageId: plaintext | ||
command: | ||
version: 7 | ||
spokenForm: indent line | ||
action: | ||
name: indentLine | ||
target: | ||
type: primitive | ||
modifiers: | ||
- type: containingScope | ||
scopeType: {type: line} | ||
usePrePhraseSnapshot: true | ||
initialState: | ||
documentContents: foo | ||
selections: | ||
- anchor: {line: 0, character: 0} | ||
active: {line: 0, character: 0} | ||
marks: {} | ||
finalState: | ||
documentContents: " foo" | ||
selections: | ||
- anchor: {line: 0, character: 4} | ||
active: {line: 0, character: 4} | ||
thatMark: | ||
- type: UntypedTarget | ||
contentRange: | ||
start: {line: 0, character: 4} | ||
end: {line: 0, character: 7} | ||
isReversed: false | ||
hasExplicitRange: true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { FlashStyle, Range, type TextEditor } from "@cursorless/common"; | ||
import { flatten, zip } from "lodash-es"; | ||
import { selectionToStoredTarget } from "../core/commandRunner/selectionToStoredTarget"; | ||
import type { RangeUpdater } from "../core/updateSelections/RangeUpdater"; | ||
import { ide } from "../singletons/ide.singleton"; | ||
import { Target } from "../typings/target.types"; | ||
import { flashTargets, runOnTargetsForEachEditor } from "../util/targetUtils"; | ||
import { | ||
IndentLineSimpleAction, | ||
OutdentLineSimpleAction, | ||
} from "./SimpleIdeCommandActions"; | ||
import { ActionReturnValue } from "./actions.types"; | ||
import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections"; | ||
|
||
abstract class IndentLineBase { | ||
constructor( | ||
private rangeUpdater: RangeUpdater, | ||
private isIndent: boolean, | ||
) { | ||
this.run = this.run.bind(this); | ||
this.runForEditor = this.runForEditor.bind(this); | ||
} | ||
|
||
async run(targets: Target[]): Promise<ActionReturnValue> { | ||
if (this.hasCapability()) { | ||
return this.runSimpleCommandAction(targets); | ||
} | ||
|
||
await flashTargets(ide(), targets, FlashStyle.pendingModification0); | ||
|
||
const thatTargets = flatten( | ||
await runOnTargetsForEachEditor(targets, this.runForEditor), | ||
); | ||
|
||
return { thatTargets }; | ||
} | ||
|
||
private hasCapability() { | ||
return this.isIndent | ||
? ide().capabilities.commands.indentLine != null | ||
: ide().capabilities.commands.outdentLine != null; | ||
} | ||
|
||
private runSimpleCommandAction( | ||
targets: Target[], | ||
): Promise<ActionReturnValue> { | ||
const action = this.isIndent | ||
? new IndentLineSimpleAction(this.rangeUpdater) | ||
: new OutdentLineSimpleAction(this.rangeUpdater); | ||
return action.run(targets); | ||
} | ||
|
||
private async runForEditor(editor: TextEditor, targets: Target[]) { | ||
const edits = this.isIndent | ||
? getIndentEdits(editor, targets) | ||
: getOutdentEdits(editor, targets); | ||
|
||
const { targetSelections: updatedTargetSelections } = | ||
await performEditsAndUpdateSelections({ | ||
rangeUpdater: this.rangeUpdater, | ||
editor: ide().getEditableTextEditor(editor), | ||
edits, | ||
selections: { | ||
targetSelections: targets.map( | ||
({ contentSelection }) => contentSelection, | ||
), | ||
}, | ||
}); | ||
|
||
return zip(targets, updatedTargetSelections).map(([target, range]) => | ||
selectionToStoredTarget({ | ||
editor, | ||
selection: range!.toSelection(target!.isReversed), | ||
}), | ||
); | ||
} | ||
} | ||
|
||
export class IndentLine extends IndentLineBase { | ||
constructor(rangeUpdater: RangeUpdater) { | ||
super(rangeUpdater, true); | ||
} | ||
} | ||
|
||
export class OutdentLine extends IndentLineBase { | ||
constructor(rangeUpdater: RangeUpdater) { | ||
super(rangeUpdater, false); | ||
} | ||
} | ||
|
||
function getIndentEdits(editor: TextEditor, targets: Target[]) { | ||
const { document } = editor; | ||
const lineNumbers = getLineNumbers(targets); | ||
const indent = getIndent(editor); | ||
return lineNumbers.map((lineNumber) => { | ||
const line = document.lineAt(lineNumber); | ||
return { | ||
range: line.range.start.toEmptyRange(), | ||
text: indent, | ||
}; | ||
}); | ||
} | ||
|
||
function getOutdentEdits(editor: TextEditor, targets: Target[]) { | ||
const { document } = editor; | ||
const lineNumbers = getLineNumbers(targets); | ||
const regex = getRegex(editor); | ||
return lineNumbers.map((lineNumber) => { | ||
const line = document.lineAt(lineNumber); | ||
const match = line.text.match(regex); | ||
const { start } = line.range; | ||
const end = start.translate(undefined, match?.[0].length); | ||
return { | ||
range: new Range(start, end), | ||
text: "", | ||
}; | ||
}); | ||
} | ||
|
||
function getLineNumbers(targets: Target[]) { | ||
const lineNumbers = new Set<number>(); | ||
for (const target of targets) { | ||
const { start, end } = target.contentRange; | ||
for (let i = start.line; i <= end.line; ++i) { | ||
lineNumbers.add(i); | ||
} | ||
} | ||
return [...lineNumbers]; | ||
} | ||
|
||
function getIndent(editor: TextEditor) { | ||
if (editor.options.insertSpaces) { | ||
const tabSize = getTabSize(editor); | ||
return " ".repeat(tabSize); | ||
} | ||
return "\t"; | ||
} | ||
|
||
function getRegex(editor: TextEditor) { | ||
if (editor.options.insertSpaces) { | ||
const tabSize = getTabSize(editor); | ||
return new RegExp(`^[ ]{1,${tabSize}}`); | ||
} | ||
return /^\t/; | ||
} | ||
|
||
function getTabSize(editor: TextEditor): number { | ||
return typeof editor.options.tabSize === "number" | ||
? editor.options.tabSize | ||
: 4; | ||
} |
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