-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added actions: indent, dedent, comment (#116)
* Added actions: indent, dedent, comment * Renames indent classes * Update Types.ts * Update index.ts * Update Comment.ts * Update index.ts * Update Types.ts * Update CommandAction.ts Co-authored-by: Pokey Rule <pokey.rule@gmail.com>
- Loading branch information
1 parent
63b330a
commit d95a990
Showing
9 changed files
with
142 additions
and
3 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,109 @@ | ||
import { commands, window, Selection, TextEditor } from "vscode"; | ||
import { | ||
Action, | ||
ActionPreferences, | ||
ActionReturnValue, | ||
Graph, | ||
TypedSelection, | ||
SelectionWithEditor, | ||
} from "./Types"; | ||
import displayPendingEditDecorations from "./editDisplayUtils"; | ||
import { runOnTargetsForEachEditor } from "./targetUtils"; | ||
import { focusEditor } from "./setSelectionsAndFocusEditor"; | ||
import { flatten } from "lodash"; | ||
|
||
export default class CommandAction implements Action { | ||
targetPreferences: ActionPreferences[] = [{ insideOutsideType: "inside" }]; | ||
|
||
constructor(private graph: Graph, private command: string) { | ||
this.run = this.run.bind(this); | ||
} | ||
|
||
async run([targets]: [ | ||
TypedSelection[], | ||
TypedSelection[] | ||
]): Promise<ActionReturnValue> { | ||
await displayPendingEditDecorations( | ||
targets, | ||
this.graph.editStyles.referenced, | ||
this.graph.editStyles.referencedLine | ||
); | ||
|
||
const originalEditor = window.activeTextEditor; | ||
|
||
const thatMark = flatten( | ||
await runOnTargetsForEachEditor<SelectionWithEditor[]>( | ||
targets, | ||
async (editor, selections) => { | ||
// For command to the work we have to have the correct editor focused | ||
if (editor !== window.activeTextEditor) { | ||
await focusEditor(editor); | ||
} | ||
|
||
const originalSelections = editor.selections; | ||
|
||
const newSelections = selections.map( | ||
(selection) => selection.selection.selection | ||
); | ||
|
||
editor.selections = newSelections; | ||
|
||
await commands.executeCommand(this.command); | ||
|
||
// Map of line numbers mapping to the delta in character index for that line. | ||
const lineDeltas = calculateLineChanges(editor, newSelections); | ||
|
||
const updateSelection = (selection: Selection) => | ||
new Selection( | ||
selection.start.line, | ||
selection.start.character + | ||
(lineDeltas[selection.start.line] ?? 0), | ||
selection.end.line, | ||
selection.end.character + (lineDeltas[selection.end.line] ?? 0) | ||
); | ||
|
||
const thatMark = newSelections.map((selection) => ({ | ||
editor, | ||
selection: updateSelection(selection), | ||
})); | ||
|
||
// Reset original selections | ||
editor.selections = originalSelections.map(updateSelection); | ||
|
||
return thatMark; | ||
} | ||
) | ||
); | ||
|
||
// If necessary focus back original editor | ||
if (originalEditor != null && originalEditor !== window.activeTextEditor) { | ||
await focusEditor(originalEditor); | ||
} | ||
|
||
return { returnValue: null, thatMark }; | ||
} | ||
} | ||
|
||
function calculateLineChanges( | ||
editor: TextEditor, | ||
selectionsBefore: Selection[] | ||
) { | ||
const lineDeltas: { [key: number]: number } = {}; | ||
editor.selections.forEach((selectionAfter, index) => { | ||
const selectionBefore = selectionsBefore[index]; | ||
const deltaStart = | ||
selectionAfter.start.character - selectionBefore.start.character; | ||
const deltaEnd = | ||
selectionAfter.end.character - selectionBefore.end.character; | ||
const delta = | ||
Math.abs(deltaStart) > Math.abs(deltaEnd) ? deltaStart : deltaEnd; | ||
for ( | ||
let i = selectionBefore.start.line; | ||
i <= selectionBefore.end.line; | ||
++i | ||
) { | ||
lineDeltas[i] = delta; | ||
} | ||
}); | ||
return lineDeltas; | ||
} |
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,8 @@ | ||
import { Graph } from "../Types"; | ||
import CommandAction from "../CommandAction"; | ||
|
||
export class CommentLines extends CommandAction { | ||
constructor(graph: Graph) { | ||
super(graph, "editor.action.commentLine"); | ||
} | ||
} |
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,14 @@ | ||
import { Graph } from "../Types"; | ||
import CommandAction from "../CommandAction"; | ||
|
||
export class IndentLines extends CommandAction { | ||
constructor(graph: Graph) { | ||
super(graph, "editor.action.indentLines"); | ||
} | ||
} | ||
|
||
export class OutdentLines extends CommandAction { | ||
constructor(graph: Graph) { | ||
super(graph, "editor.action.outdentLines"); | ||
} | ||
} |
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
File renamed without changes.