Skip to content

Commit

Permalink
Implement the next/previous editor commands and keybindings
Browse files Browse the repository at this point in the history
This wraps around like VSCode.

Fixes onivim#778 and part of onivim#1423.
  • Loading branch information
leavengood committed Mar 8, 2020
1 parent 39b8de1 commit 6a9ac8e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Model/Actions.re
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ type t =
| StatusBarDisposeItem(int)
| StatusBar(StatusBarModel.action)
| ViewCloseEditor(int)
| ViewNextEditor
| ViewPreviousEditor
| ViewSetActiveEditor(int)
| EnableZenMode
| DisableZenMode
Expand Down
32 changes: 32 additions & 0 deletions src/Model/EditorGroup.re
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,38 @@ let _getAdjacentEditor = (editor: int, reverseTabOrder: list(int)) => {
};
};

let setActiveEditorByIndexDiff = (diff, model) => {
let tabs = model.reverseTabOrder;
let count = List.length(tabs);

if (count <= 1) {
// Nothing to change
model
} else {
switch (model.activeEditorId) {
| Some(activeEditorId) =>
switch (_getIndexOfElement(activeEditorId, tabs)) {
| (-1) => model
| idx =>
let newIndex =
switch (idx + diff) {
// Wrapping negative, go to end
| -1 => count - 1
// If this is past the end, go to zero, otherwise this index is fine
| i => i >= count ? 0 : i
};

{...model, activeEditorId: List.nth_opt(tabs, newIndex)};
}
| None => model
};
};
}

// The diff amounts are inverted because the list is in reverse order
let nextEditor = setActiveEditorByIndexDiff(-1);
let previousEditor = setActiveEditorByIndexDiff(1);

let isEmpty = model => IntMap.is_empty(model.editors);

let removeEditorById = (state, editorId) => {
Expand Down
2 changes: 2 additions & 0 deletions src/Model/EditorGroup.rei
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ let getActiveEditor: t => option(Feature_Editor.Editor.t);
let setActiveEditor: (t, int) => t;
let getEditorById: (int, t) => option(Feature_Editor.Editor.t);
let getOrCreateEditorForBuffer: (t, int) => (t, Feature_Editor.EditorId.t);
let nextEditor: t => t;
let previousEditor: t => t;
let removeEditorById: (t, int) => t;

let isEmpty: t => bool;
2 changes: 2 additions & 0 deletions src/Model/EditorGroupReducer.re
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ let reduce = (v: EditorGroup.t, action: Actions.t) => {
EditorGroup.getOrCreateEditorForBuffer(v, id);
{...newState, activeEditorId: Some(activeEditorId)};
| ViewCloseEditor(id) => EditorGroup.removeEditorById(v, id)
| ViewNextEditor => EditorGroup.nextEditor(v)
| ViewPreviousEditor => EditorGroup.previousEditor(v)
| ViewSetActiveEditor(id) =>
switch (IntMap.find_opt(id, v.editors)) {
| None => v
Expand Down
15 changes: 15 additions & 0 deletions src/Store/CommandStoreConnector.re
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ let createDefaultCommands = getState => {
~action=Command("view.closeEditor"),
(),
),
Command.create(
~category=Some("View"),
~name="Open Next Editor",
~action=Command("workbench.action.nextEditor"),
(),
),
Command.create(
~category=Some("View"),
~name="Open Previous Editor",
~action=Command("workbench.action.previousEditor"),
(),
),
Command.create(
~category=Some("View"),
~name="Toggle Problems (Errors, Warnings)",
Expand Down Expand Up @@ -314,11 +326,14 @@ let start = (getState, contributedCommands) => {
"workbench.action.closeQuickOpen",
_ => singleActionEffect(QuickmenuClose),
),

("list.focusDown", _ => singleActionEffect(ListFocusDown)),
("list.focusUp", _ => singleActionEffect(ListFocusUp)),
("list.select", _ => singleActionEffect(ListSelect)),
("list.selectBackground", _ => singleActionEffect(ListSelectBackground)),
("view.closeEditor", state => closeEditorEffect(state)),
("workbench.action.nextEditor", _ => singleActionEffect(ViewNextEditor)),
("workbench.action.previousEditor", _ => singleActionEffect(ViewPreviousEditor)),
("view.splitVertical", state => splitEditorEffect(state, Vertical)),
("view.splitHorizontal", state => splitEditorEffect(state, Horizontal)),
(
Expand Down
20 changes: 20 additions & 0 deletions src/Store/KeyBindingsStoreConnector.re
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,26 @@ let start = () => {
command: "view.closeEditor",
condition: WhenExpr.Value(True),
},
{
key: "<C-PAGEDOWN>",
command: "workbench.action.nextEditor",
condition: WhenExpr.Value(True),
},
{
key: "<D-S-]>",
command: "workbench.action.nextEditor",
condition: WhenExpr.Value(True),
},
{
key: "<C-PAGEUP>",
command: "workbench.action.previousEditor",
condition: WhenExpr.Value(True),
},
{
key: "<D-S-[>",
command: "workbench.action.previousEditor",
condition: WhenExpr.Value(True),
},
];

let reloadConfigOnWritePost = (~configPath, dispatch) => {
Expand Down
2 changes: 2 additions & 0 deletions src/Store/VimStoreConnector.re
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,8 @@ let start =
)
| ViewSetActiveEditor(_) => (state, synchronizeEditorEffect(state))
| ViewCloseEditor(_) => (state, synchronizeEditorEffect(state))
| ViewNextEditor => (state, synchronizeEditorEffect(state))
| ViewPreviousEditor => (state, synchronizeEditorEffect(state))
| KeyboardInput(s) => (state, inputEffect(s))
| CopyActiveFilepathToClipboard => (
state,
Expand Down

0 comments on commit 6a9ac8e

Please sign in to comment.