Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
9ce18f8
remove dependency upon vscode.DecorationRangeBehavior
AndreasArvidsson Nov 17, 2022
03445b5
updated scroll
AndreasArvidsson Nov 17, 2022
1260530
make sure editable actions return promise
AndreasArvidsson Nov 17, 2022
12b040b
update paste action
AndreasArvidsson Nov 17, 2022
373db09
updated fold action
AndreasArvidsson Nov 17, 2022
e6580b0
Updated find in workspace action
AndreasArvidsson Nov 17, 2022
476f926
Updated insert snippet action
AndreasArvidsson Nov 17, 2022
6b1ddb0
Updated wrap with snippet action
AndreasArvidsson Nov 17, 2022
4051650
Updated the toggle break ponts action
AndreasArvidsson Nov 17, 2022
baab8cc
Use editor to insert snippet
AndreasArvidsson Nov 17, 2022
60321bc
Added open new text document to ide
AndreasArvidsson Nov 17, 2022
749290a
Added show input box on ide
AndreasArvidsson Nov 17, 2022
cbb1512
Added capabilities
AndreasArvidsson Nov 18, 2022
16fdf77
Added vscode capabilities
AndreasArvidsson Nov 18, 2022
9be062f
Started updating makeshift actions
AndreasArvidsson Nov 18, 2022
ab1f5b6
Updated all makeshift actions
AndreasArvidsson Nov 18, 2022
1f9014c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2022
212ced4
Updated execute command action
AndreasArvidsson Nov 18, 2022
baf1aa3
Made all editor action locations optional
AndreasArvidsson Nov 18, 2022
b2158a7
Merge branch 'main' into free_actions_from_vscode
AndreasArvidsson Nov 18, 2022
a440aa2
Updated edit new using insert new line
AndreasArvidsson Nov 20, 2022
b5e4a90
Updated insert notebook cell action
AndreasArvidsson Nov 20, 2022
94af99b
Merge branch 'main' into free_actions_from_vscode
AndreasArvidsson Nov 20, 2022
2b7919f
Merged with main
AndreasArvidsson Nov 20, 2022
ea5b29a
Cleanup
pokey Nov 23, 2022
a4089aa
More tweaks
pokey Nov 23, 2022
8bbe6cf
More tweaks
pokey Nov 23, 2022
876ccbe
Some renames
pokey Nov 23, 2022
e634021
Doc strings
pokey Nov 23, 2022
d328f66
More tweaks
pokey Nov 23, 2022
8441fb3
Minor tweaks
pokey Nov 23, 2022
0e8afc6
Cleanup capabilities
pokey Nov 23, 2022
10cb41c
More tweaks
pokey Nov 28, 2022
e8220a9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 1, 2022
32a0047
Fix import
pokey Dec 1, 2022
a4bea6a
Rename
pokey Dec 1, 2022
28fe23f
More rename
pokey Dec 1, 2022
67694bb
Move `executeCommand` to `ide`
pokey Dec 4, 2022
c27cd83
tweak
pokey Dec 4, 2022
2b8d7f9
Merge branch 'main' into free_actions_from_vscode
pokey Dec 4, 2022
e11f455
Rework breakpoint implementation
pokey Dec 7, 2022
bb874b3
Switch `DecorationRangeBehavior` to enum
pokey Dec 7, 2022
1d93c98
Rename `DecorationRangeBehavior` to `RangeExpansionBehavior`
pokey Dec 7, 2022
a0a0736
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 7, 2022
87828b7
Switch `RevealLineAt` to enum
pokey Dec 7, 2022
70b56a0
Fix
pokey Dec 7, 2022
a8fe83c
Merge branch 'main' into free_actions_from_vscode
pokey Dec 7, 2022
a8a363a
Merge branch 'main' into free_actions_from_vscode
AndreasArvidsson Dec 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/actions/Actions.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { Graph } from "../typings/Types";
import { ActionRecord } from "./actions.types";
import {
CommentLines,
ToggleLineComment,
ExtractVariable,
IndentLines,
OutdentLines,
IndentLine,
OutdentLine,
Rename,
RevealDefinition,
RevealTypeDefinition,
ShowDebugHover,
ShowHover,
ShowQuickFix,
ShowReferences,
} from "./MakeshiftActions";
CopyToClipboard,
Fold,
Unfold,
} from "./SimpleIdeCommandActions";
import { Bring, Move, Swap } from "./BringMoveSwap";
import Call from "./Call";
import Clear from "./Clear";
import { Copy, Cut } from "./CutCopy";
import { CutToClipboard } from "./CutToClipboard";
import Deselect from "./Deselect";
import { EditNew, EditNewAfter, EditNewBefore } from "./EditNew";
import ExecuteCommand from "./ExecuteCommand";
import { FindInFiles } from "./Find";
import { Fold, Unfold } from "./Fold";
import { FindInWorkspace } from "./Find";
import FollowLink from "./FollowLink";
import GenerateSnippet from "./GenerateSnippet";
import GetText from "./GetText";
Expand All @@ -36,7 +38,7 @@ import {
InsertEmptyLinesAround,
} from "./InsertEmptyLines";
import InsertSnippet from "./InsertSnippet";
import { Paste } from "./Paste";
import { PasteFromClipboard } from "./PasteFromClipboard";
import Remove from "./Remove";
import Replace from "./Replace";
import Rewrap from "./Rewrap";
Expand All @@ -56,30 +58,30 @@ class Actions implements ActionRecord {

callAsFunction = new Call(this.graph);
clearAndSetSelection = new Clear(this.graph);
copyToClipboard = new Copy(this.graph);
cutToClipboard = new Cut(this.graph);
copyToClipboard = new CopyToClipboard(this.graph);
cutToClipboard = new CutToClipboard(this.graph);
deselect = new Deselect(this.graph);
editNew = new EditNew(this.graph);
editNewLineAfter = new EditNewAfter(this.graph);
editNewLineBefore = new EditNewBefore(this.graph);
executeCommand = new ExecuteCommand(this.graph);
extractVariable = new ExtractVariable(this.graph);
findInWorkspace = new FindInFiles(this.graph);
findInWorkspace = new FindInWorkspace(this.graph);
foldRegion = new Fold(this.graph);
followLink = new FollowLink(this.graph);
generateSnippet = new GenerateSnippet(this.graph);
getText = new GetText(this.graph);
highlight = new Highlight(this.graph);
indentLine = new IndentLines(this.graph);
indentLine = new IndentLine(this.graph);
insertCopyAfter = new InsertCopyAfter(this.graph);
insertCopyBefore = new InsertCopyBefore(this.graph);
insertEmptyLineAfter = new InsertEmptyLineAfter(this.graph);
insertEmptyLineBefore = new InsertEmptyLineBefore(this.graph);
insertEmptyLinesAround = new InsertEmptyLinesAround(this.graph);
insertSnippet = new InsertSnippet(this.graph);
moveToTarget = new Move(this.graph);
outdentLine = new OutdentLines(this.graph);
pasteFromClipboard = new Paste(this.graph);
outdentLine = new OutdentLine(this.graph);
pasteFromClipboard = new PasteFromClipboard(this.graph);
randomizeTargets = new Random(this.graph);
remove = new Remove(this.graph);
rename = new Rename(this.graph);
Expand All @@ -102,7 +104,7 @@ class Actions implements ActionRecord {
sortTargets = new Sort(this.graph);
swapTargets = new Swap(this.graph);
toggleLineBreakpoint = new ToggleBreakpoint(this.graph);
toggleLineComment = new CommentLines(this.graph);
toggleLineComment = new ToggleLineComment(this.graph);
unfoldRegion = new Unfold(this.graph);
wrapWithPairedDelimiter = new Wrap(this.graph);
wrapWithSnippet = new WrapWithSnippet(this.graph);
Expand Down
11 changes: 7 additions & 4 deletions src/actions/BringMoveSwap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Selection, TextEditor } from "@cursorless/common";
import {
RangeExpansionBehavior,
Selection,
TextEditor,
} from "@cursorless/common";
import { flatten } from "lodash";
import { DecorationRangeBehavior } from "vscode";
import {
getSelectionInfo,
performEditsAndUpdateFullSelectionInfos,
Expand Down Expand Up @@ -169,15 +172,15 @@ class BringMoveSwap implements Action {
getSelectionInfo(
editor.document,
range.toSelection(originalTarget.isReversed),
DecorationRangeBehavior.OpenOpen,
RangeExpansionBehavior.openOpen,
),
);

const cursorSelectionInfos = editor.selections.map((selection) =>
getSelectionInfo(
editor.document,
selection,
DecorationRangeBehavior.ClosedClosed,
RangeExpansionBehavior.closedClosed,
),
);

Expand Down
139 changes: 139 additions & 0 deletions src/actions/CallbackAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { EditableTextEditor, TextEditor } from "@cursorless/common";
import { flatten } from "lodash";
import { selectionToThatTarget } from "../core/commandRunner/selectionToThatTarget";
import { callFunctionAndUpdateSelections } from "../core/updateSelections/updateSelections";
import ide from "../libs/cursorless-engine/singletons/ide.singleton";
import { Target } from "../typings/target.types";
import { Graph } from "../typings/Types";
import {
setSelectionsAndFocusEditor,
setSelectionsWithoutFocusingEditor,
} from "../util/setSelectionsAndFocusEditor";
import {
ensureSingleEditor,
ensureSingleTarget,
runOnTargetsForEachEditor,
runOnTargetsForEachEditorSequentially,
} from "../util/targetUtils";
import { Action, ActionReturnValue } from "./actions.types";

interface CallbackOptions {
callback: (editor: EditableTextEditor, targets: Target[]) => Promise<void>;
ensureSingleEditor: boolean;
ensureSingleTarget: boolean;
setSelection: boolean;
restoreSelection: boolean;
showDecorations: boolean;
}

/**
* This is a helper action that is used internally to implement various actions.
* It takes a {@link CallbackOptions.callback callback} that is called once for
* each editor, receiving all the targets that are in the given editor.
*/
export class CallbackAction implements Action {
constructor(private graph: Graph) {
this.run = this.run.bind(this);
}

async run(
[targets]: [Target[]],
options: CallbackOptions,
): Promise<ActionReturnValue> {
if (options.showDecorations) {
await this.graph.editStyles.displayPendingEditDecorations(
targets,
this.graph.editStyles.referenced,
);
}

if (options.ensureSingleEditor) {
ensureSingleEditor(targets);
}

if (options.ensureSingleTarget) {
ensureSingleTarget(targets);
}

const originalEditor = ide().activeEditableTextEditor;

// If we are relying on selections we have to wait for one editor to finish
// before moving the selection to the next
const runOnTargets = options.setSelection
? runOnTargetsForEachEditorSequentially
: runOnTargetsForEachEditor;

const thatTargets = flatten(
await runOnTargets(targets, (editor, targets) =>
this.runForEditor(options, editor, targets),
),
);

// If necessary focus back original editor
if (
options.setSelection &&
options.restoreSelection &&
originalEditor != null &&
!originalEditor.isActive
) {
// NB: We just do one editor focus at the end, instead of using
// setSelectionsAndFocusEditor because the command might operate on
// multiple editors, so we just do one focus at the end.
await originalEditor.focus();
}

return { thatTargets };
}

private async runForEditor(
options: CallbackOptions,
editor: TextEditor,
targets: Target[],
): Promise<Target[]> {
const editableEditor = ide().getEditableTextEditor(editor);
const originalSelections = editor.selections;
const originalEditorVersion = editor.document.version;
const targetSelections = targets.map((target) => target.contentSelection);

// For this callback/command to the work we have to have the correct editor focused
if (options.setSelection) {
await setSelectionsAndFocusEditor(
editableEditor,
targetSelections,
false,
);
}

const [updatedOriginalSelections, updatedTargetSelections] =
await callFunctionAndUpdateSelections(
this.graph.rangeUpdater,
() => options.callback(editableEditor, targets),
editor.document,
[originalSelections, targetSelections],
);

// Reset original selections
if (options.setSelection && options.restoreSelection) {
// NB: We don't focus the editor here because we'll do that at the
// very end. This code can run on multiple editors in the course of
// one command, so we want to avoid focusing the editor multiple
// times.
setSelectionsWithoutFocusingEditor(
editableEditor,
updatedOriginalSelections,
);
}

// If the document hasn't changed then we just return the original targets
// so that we preserve their rich types, but if it has changed then we
// just downgrade them to untyped targets
return editor.document.version === originalEditorVersion
? targets
: updatedTargetSelections.map((selection) =>
selectionToThatTarget({
editor,
selection,
}),
);
}
}
Loading