Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor/#3325): Move lines via Alt+J/Alt+K #3351

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGES_CURRENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- #3284 - Definition: Add 'editor.action.revealDefinitionAside' command (fixes #3261)
- #2881 - Extensions: Initial rename support
- #3351 - Editor: Add Alt+J/Alt+K keybinding to move lines (fixes #3325)

### Bug Fixes

Expand Down
179 changes: 177 additions & 2 deletions src/Feature/Vim/Feature_Vim.re
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,16 @@ let moveMarkers = (~newBuffer, ~markerUpdate, model) => {

// MSG

[@deriving show]
type command =
| MoveSelectionUpward
| MoveSelectionDownward
| CopySelectionUpward
| CopySelectionDownward;

[@deriving show]
type msg =
| Command(command)
| ModeChanged({
allowAnimation: bool,
mode: [@opaque] Vim.Mode.t,
Expand Down Expand Up @@ -154,10 +162,69 @@ module Effects = {
);
});
};

let command = cmd => {
Isolinear.Effect.createWithDispatch(
~name="Feature_Vim.Effect.command", dispatch => {
let context = Vim.Context.current();
let (newContext, effects) = Vim.command(~context, cmd);

dispatch(
ModeChanged({
allowAnimation: false,
mode: newContext.mode,
subMode: newContext.subMode,
effects,
}),
);
});
};
};

let update = (msg, model: model) => {
let update = (~cursor, ~selections, msg, model: model) => {
switch (msg) {
| Command(command) =>
let (startLine, stopLine) =
switch (selections) {
| [visualRange, ..._] =>
let range = Oni_Core.VisualRange.(visualRange.range);
ByteRange.(range.start.line, range.stop.line);
| [] => BytePosition.(cursor.line, cursor.line)
};
let cmd =
switch (command) {
| MoveSelectionDownward
| MoveSelectionUpward => "m"

| CopySelectionDownward
| CopySelectionUpward => "t"
};

let destination =
EditorCoreTypes.(
switch (command) {
| MoveSelectionDownward => LineNumber.(stopLine + 1)
| MoveSelectionUpward => LineNumber.(startLine - 2)
| CopySelectionUpward => LineNumber.(startLine - 1)
| CopySelectionDownward => stopLine
}
);

let eff =
EditorCoreTypes.(
Effects.command(
Printf.sprintf(
"%s,%s%s%d",
startLine |> LineNumber.toOneBased |> string_of_int,
stopLine |> LineNumber.toOneBased |> string_of_int,
cmd,
destination |> LineNumber.toOneBased,
),
)
);

(model, Effect(eff));

| ModeChanged({allowAnimation, mode, effects, subMode}) => (
{...model, subMode} |> handleEffects(effects),
ModeDidChange({allowAnimation, mode, effects}),
Expand Down Expand Up @@ -263,6 +330,38 @@ let sub = (~buffer, ~topVisibleLine, ~bottomVisibleLine, model) => {
|> Option.value(~default=Isolinear.Sub.none);
};

module Commands = {
open Feature_Commands.Schema;

let moveLinesDown =
define(
~title="Move Line Down",
"editor.action.moveLinesDownAction",
Command(MoveSelectionDownward),
);

let moveLinesUp =
define(
~title="Move Line Up",
"editor.action.moveLinesUpAction",
Command(MoveSelectionUpward),
);

let copyLinesDown =
define(
~category="Copy Line Down",
"editor.action.copyLinesDownAction",
Command(CopySelectionDownward),
);

let copyLinesUp =
define(
~category="Copy Line Up",
"editor.action.copyLinesUpAction",
Command(CopySelectionUpward),
);
};

module Keybindings = {
open Feature_Input.Schema;
let controlSquareBracketRemap =
Expand All @@ -272,10 +371,86 @@ module Keybindings = {
~toKeys="<ESC>",
~condition=WhenExpr.Value(True),
);

let editCondition =
WhenExpr.parse(
"normalMode || visualMode || insertMode && editorTextFocus",
);

let moveLineDownwardJ =
bind(
~key="<A-j>",
~command=Commands.moveLinesDown.id,
~condition=editCondition,
);

let moveLineDownwardArrow =
bind(
~key="<A-Down>",
~command=Commands.moveLinesDown.id,
~condition=editCondition,
);

let moveLineUpwardK =
bind(
~key="<A-k>",
~command=Commands.moveLinesUp.id,
~condition=editCondition,
);

let moveLineUpwardArrow =
bind(
~key="<A-Up>",
~command=Commands.moveLinesUp.id,
~condition=editCondition,
);

let copyLineDownwardJ =
bind(
~key="<A-S-j>",
~command=Commands.copyLinesDown.id,
~condition=editCondition,
);

let copyLineDownwardArrow =
bind(
~key="<A-S-Down>",
~command=Commands.copyLinesDown.id,
~condition=editCondition,
);

let copyLineUpwardK =
bind(
~key="<A-S-k>",
~command=Commands.copyLinesUp.id,
~condition=editCondition,
);

let copyLineUpwardArrow =
bind(
~key="<A-S-Up>",
~command=Commands.copyLinesUp.id,
~condition=editCondition,
);
};

module Contributions = {
let keybindings = Keybindings.[controlSquareBracketRemap];
let commands =
Commands.[moveLinesDown, moveLinesUp, copyLinesDown, copyLinesUp];
let keybindings =
Keybindings.[
// Remaps
controlSquareBracketRemap,
// Bindings
moveLineDownwardJ,
moveLineDownwardArrow,
moveLineUpwardK,
moveLineUpwardArrow,
copyLineDownwardJ,
copyLineDownwardArrow,
copyLineUpwardArrow,
copyLineUpwardK,
];

let configuration = Configuration.[experimentalViml.spec];
};
14 changes: 13 additions & 1 deletion src/Feature/Vim/Feature_Vim.rei
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ let experimentalViml: model => list(string);

// MSG

[@deriving show]
type command;

[@deriving show]
type msg =
| Command(command)
| ModeChanged({
allowAnimation: bool,
mode: [@opaque] Vim.Mode.t,
Expand Down Expand Up @@ -53,7 +57,14 @@ type outmsg =

// UPDATE

let update: (msg, model) => (model, outmsg);
let update:
(
~cursor: BytePosition.t,
~selections: list(Oni_Core.VisualRange.t),
msg,
model
) =>
(model, outmsg);

let getSearchHighlightsByLine:
(~bufferId: int, ~line: LineNumber.t, model) => list(ByteRange.t);
Expand Down Expand Up @@ -103,6 +114,7 @@ module Configuration: {
};

module Contributions: {
let commands: list(Oni_Core.Command.t(msg));
let keybindings: list(Feature_Input.Schema.keybinding);
let configuration: list(Oni_Core.Config.Schema.spec);
};
3 changes: 3 additions & 0 deletions src/Model/CommandManager.re
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ let current = {
Feature_Help.Contributions.commands
|> Command.Lookup.fromList
|> Command.Lookup.map(msg => Actions.Help(msg)),
Feature_Vim.Contributions.commands
|> Command.Lookup.fromList
|> Command.Lookup.map(msg => Actions.Vim(msg)),
Feature_Zen.Contributions.commands
|> Command.Lookup.fromList
|> Command.Lookup.map(msg => Actions.Zen(msg)),
Expand Down
7 changes: 6 additions & 1 deletion src/Store/Features.re
Original file line number Diff line number Diff line change
Expand Up @@ -2251,7 +2251,12 @@ let update =

| Vim(msg) =>
let previousSubMode = state.vim |> Feature_Vim.subMode;
let (vim, outmsg) = Feature_Vim.update(msg, state.vim);
let editor = state.layout |> Feature_Layout.activeEditor;
let cursor = editor |> Feature_Editor.Editor.getPrimaryCursorByte;

let selections = editor |> Feature_Editor.Editor.selections;
let (vim, outmsg) =
Feature_Vim.update(~cursor, ~selections, msg, state.vim);
let newSubMode = state.vim |> Feature_Vim.subMode;

// If we've switched to, or from, insert literal,
Expand Down
5 changes: 5 additions & 0 deletions src/editor-core-types/BytePosition.re
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ let compare = (a, b) =>
} else {
LineNumber.toZeroBased(a.line) - LineNumber.toZeroBased(b.line);
};

let shiftLine = (~delta, bytePos) => {
...bytePos,
line: LineNumber.(bytePos.line + delta),
};
2 changes: 2 additions & 0 deletions src/editor-core-types/BytePosition.rei
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ let (==): (t, t) => bool;
* - a positive integer if a is greater than b
*/
let compare: (t, t) => int;

let shiftLine: (~delta: int, t) => t;