diff --git a/src/actions/Fold.ts b/src/actions/Fold.ts index 622e78536c..fe8d783bae 100644 --- a/src/actions/Fold.ts +++ b/src/actions/Fold.ts @@ -1,4 +1,4 @@ -import { commands } from "vscode"; +import { commands, window } from "vscode"; import { Action, ActionPreferences, @@ -6,10 +6,13 @@ import { Graph, TypedSelection, } from "../typings/Types"; +import { focusEditor } from "../util/setSelectionsAndFocusEditor"; import { ensureSingleEditor } from "../util/targetUtils"; class FoldAction implements Action { - getTargetPreferences: () => ActionPreferences[] = () => [{ insideOutsideType: "outside" }]; + getTargetPreferences: () => ActionPreferences[] = () => [ + { insideOutsideType: "inside" }, + ]; constructor(private command: string) { this.run = this.run.bind(this); @@ -19,16 +22,26 @@ class FoldAction implements Action { TypedSelection[], TypedSelection[] ]): Promise { - ensureSingleEditor(targets); + const originalEditor = window.activeTextEditor; + const editor = ensureSingleEditor(targets); + + if (originalEditor !== editor) { + await focusEditor(editor); + } await commands.executeCommand(this.command, { levels: 1, direction: "down", - selectionLines: targets - .filter((target) => !target.selection.selection.isSingleLine) - .map((target) => target.selection.selection.start.line), + selectionLines: targets.map( + (target) => target.selection.selection.start.line + ), }); + // If necessary focus back original editor + if (originalEditor != null && originalEditor !== window.activeTextEditor) { + await focusEditor(originalEditor); + } + return { thatMark: targets.map((target) => target.selection), };