Skip to content

Commit 8474b87

Browse files
committed
Initial implementation for #3325
1 parent 8244383 commit 8474b87

File tree

5 files changed

+79
-13
lines changed

5 files changed

+79
-13
lines changed

src/Feature/Vim/Feature_Vim.re

+58-11
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,68 @@ module Effects = {
162162
);
163163
});
164164
};
165+
166+
let command = cmd => {
167+
Isolinear.Effect.createWithDispatch(
168+
~name="Feature_Vim.Effect.command", dispatch => {
169+
let context = Vim.Context.current();
170+
let (newContext, effects) = Vim.command(~context, cmd);
171+
172+
dispatch(
173+
ModeChanged({
174+
allowAnimation: false,
175+
mode: newContext.mode,
176+
subMode: newContext.subMode,
177+
effects,
178+
}),
179+
);
180+
});
181+
};
165182
};
166183

167-
let update = (msg, model: model) => {
184+
let update = (~cursor, ~selections, msg, model: model) => {
168185
switch (msg) {
169186
| Command(command) =>
170-
prerr_endline("COMMAND: " ++ show_command(command));
171-
failwith("Done");
187+
let (startLine, stopLine) =
188+
switch (selections) {
189+
| [visualRange, ..._] =>
190+
let range = Oni_Core.VisualRange.(visualRange.range);
191+
ByteRange.(range.start.line, range.stop.line);
192+
| [] => BytePosition.(cursor.line, cursor.line)
193+
};
194+
let cmd =
195+
switch (command) {
196+
| MoveSelectionDownward
197+
| MoveSelectionUpward => "m"
198+
199+
| CopySelectionDownward
200+
| CopySelectionUpward => "t"
201+
};
202+
203+
let destination =
204+
EditorCoreTypes.(
205+
switch (command) {
206+
| MoveSelectionDownward => LineNumber.(stopLine + 1)
207+
| MoveSelectionUpward => LineNumber.(startLine - 2)
208+
| CopySelectionUpward => LineNumber.(startLine - 1)
209+
| CopySelectionDownward => stopLine
210+
}
211+
);
212+
213+
let eff =
214+
EditorCoreTypes.(
215+
Effects.command(
216+
Printf.sprintf(
217+
"%s,%s%s%d",
218+
startLine |> LineNumber.toOneBased |> string_of_int,
219+
stopLine |> LineNumber.toOneBased |> string_of_int,
220+
cmd,
221+
destination |> LineNumber.toOneBased,
222+
),
223+
)
224+
);
225+
226+
(model, Effect(eff));
172227

173228
| ModeChanged({allowAnimation, mode, effects, subMode}) => (
174229
{...model, subMode} |> handleEffects(effects),
@@ -278,14 +333,6 @@ let sub = (~buffer, ~topVisibleLine, ~bottomVisibleLine, model) => {
278333
module Commands = {
279334
open Feature_Commands.Schema;
280335

281-
let moveLinesDown =
282-
define(
283-
~category="Editor",
284-
~title="Move lines down",
285-
"editor.action.moveLinesDownAction",
286-
Command(MoveSelectionDownward),
287-
);
288-
289336
let moveLinesDown =
290337
define(
291338
~title="Move Line Down",

src/Feature/Vim/Feature_Vim.rei

+8-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ type outmsg =
5757

5858
// UPDATE
5959

60-
let update: (msg, model) => (model, outmsg);
60+
let update:
61+
(
62+
~cursor: BytePosition.t,
63+
~selections: list(Oni_Core.VisualRange.t),
64+
msg,
65+
model
66+
) =>
67+
(model, outmsg);
6168

6269
let getSearchHighlightsByLine:
6370
(~bufferId: int, ~line: LineNumber.t, model) => list(ByteRange.t);

src/Store/Features.re

+6-1
Original file line numberDiff line numberDiff line change
@@ -2243,7 +2243,12 @@ let update =
22432243

22442244
| Vim(msg) =>
22452245
let previousSubMode = state.vim |> Feature_Vim.subMode;
2246-
let (vim, outmsg) = Feature_Vim.update(msg, state.vim);
2246+
let editor = state.layout |> Feature_Layout.activeEditor;
2247+
let cursor = editor |> Feature_Editor.Editor.getPrimaryCursorByte;
2248+
2249+
let selections = editor |> Feature_Editor.Editor.selections;
2250+
let (vim, outmsg) =
2251+
Feature_Vim.update(~cursor, ~selections, msg, state.vim);
22472252
let newSubMode = state.vim |> Feature_Vim.subMode;
22482253

22492254
// If we've switched to, or from, insert literal,

src/editor-core-types/BytePosition.re

+5
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ let compare = (a, b) =>
2121
} else {
2222
LineNumber.toZeroBased(a.line) - LineNumber.toZeroBased(b.line);
2323
};
24+
25+
let shiftLine = (~delta, bytePos) => {
26+
...bytePos,
27+
line: LineNumber.(bytePos.line + delta),
28+
};

src/editor-core-types/BytePosition.rei

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ let (==): (t, t) => bool;
1919
* - a positive integer if a is greater than b
2020
*/
2121
let compare: (t, t) => int;
22+
23+
let shiftLine: (~delta: int, t) => t;

0 commit comments

Comments
 (0)