Skip to content

Commit

Permalink
Bugfix: ':q' doesn't work as expected (onivim#391)
Browse files Browse the repository at this point in the history
* Add quit buffer action

* Add modified helpers

* Hook up q

* Formatting
  • Loading branch information
bryphe authored Jul 10, 2019
1 parent a9ba3e6 commit c0a77a7
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/editor/Model/Actions.re
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ type t =
| RemoveSplit(int)
| OpenConfigFile(string)
| QuickOpen
| Quit
| QuitBuffer(Vim.Buffer.t, bool)
| Quit(bool)
| RegisterQuitCleanup(unit => unit)
| SearchClearMatchingPair(int)
| SearchSetMatchingPair(int, Position.t, Position.t)
Expand Down
2 changes: 2 additions & 0 deletions src/editor/Model/Buffer.re
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ let getId = (buffer: t) => buffer.metadata.id;

let getLine = (buffer: t, line: int) => buffer.lines[line];

let isModified = (buffer: t) => buffer.metadata.modified;

let getUri = (buffer: t) => {
let getUriFromMetadata = (metadata: Vim.BufferMetadata.t) => {
switch (metadata.filePath) {
Expand Down
2 changes: 1 addition & 1 deletion src/editor/Model/Buffer.rei
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ let getLineLength: (t, int) => int;
let getMetadata: t => Vim.BufferMetadata.t;
let getUri: t => Uri.t;
let getId: t => int;

let getNumberOfLines: t => int;
let isModified: t => bool;

let isIndentationSet: t => bool;
let setIndentation: (IndentationSettings.t, t) => t;
Expand Down
8 changes: 8 additions & 0 deletions src/editor/Model/Buffers.re
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ let log = (m: t) =>

let getBuffer = (id, map) => IntMap.find_opt(id, map);

let anyModified = (buffers: t) => {
IntMap.fold(
(_key, v, prev) => Buffer.isModified(v) || prev,
buffers,
false,
);
};

let getBufferMetadata = (id, map) =>
switch (getBuffer(id, map)) {
| Some(v) => Some(Buffer.getMetadata(v))
Expand Down
41 changes: 34 additions & 7 deletions src/editor/Store/LifecycleStoreConnector.re
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,48 @@ module Core = Oni_Core;
module Model = Oni_Model;

let start = () => {
let quitEffect = handlers =>
Isolinear.Effect.create(~name="lifecycle.quit", () => {
List.iter(h => h(), handlers);
App.quit(0);
let (stream, dispatch) = Isolinear.Stream.create();

let quitAllEffect = (state: Model.State.t, force) => {
let handlers = state.lifecycle.onQuitFunctions;

let anyModified = Model.Buffers.anyModified(state.buffers);
let canClose = force || !anyModified;

Isolinear.Effect.create(~name="lifecycle.quit", () =>
if (canClose) {
List.iter(h => h(), handlers);
App.quit(0);
}
);
};

let quitBufferEffect = (state: Model.State.t, buffer: Vim.Buffer.t, force) => {
Isolinear.Effect.create(~name="lifecycle.quitBuffer", () => {
let editorGroup = Model.Selectors.getActiveEditorGroup(state);
switch (Model.Selectors.getActiveEditor(editorGroup)) {
| None => ()
| Some(editor) =>
let bufferMeta = Vim.BufferMetadata.ofBuffer(buffer);
if (editor.bufferId == bufferMeta.id) {
if (force || !bufferMeta.modified) {
dispatch(Model.Actions.ViewCloseEditor(editor.editorId));
};
};
};
});
};

let updater = (state: Model.State.t, action) => {
switch (action) {
| Model.Actions.Quit => (
| Model.Actions.QuitBuffer(buffer, force) => (
state,
quitEffect(state.lifecycle.onQuitFunctions),
quitBufferEffect(state, buffer, force),
)
| Model.Actions.Quit(force) => (state, quitAllEffect(state, force))
| _ => (state, Isolinear.Effect.none)
};
};

updater;
(updater, stream);
};
3 changes: 2 additions & 1 deletion src/editor/Store/StoreThread.re
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let start = (~setup: Core.Setup.t, ~executingDirectory, ~onStateChanged, ()) =>
let (fileExplorerUpdater, explorerStream) =
FileExplorerStoreConnector.start();

let lifecycleUpdater = LifecycleStoreConnector.start();
let (lifecycleUpdater, lifecycleStream) = LifecycleStoreConnector.start();
let indentationUpdater = IndentationStoreConnector.start();

let (storeDispatch, storeStream) =
Expand Down Expand Up @@ -124,6 +124,7 @@ let start = (~setup: Core.Setup.t, ~executingDirectory, ~onStateChanged, ()) =>
/* Isolinear.Stream.connect(dispatch, extHostStream); */
Isolinear.Stream.connect(dispatch, menuStream);
Isolinear.Stream.connect(dispatch, explorerStream);
Isolinear.Stream.connect(dispatch, lifecycleStream);

dispatch(Model.Actions.SetLanguageInfo(languageInfo));

Expand Down
8 changes: 8 additions & 0 deletions src/editor/Store/VimStoreConnector.re
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ let start = (getState: unit => Model.State.t) => {
};
});

let _ =
Vim.onQuit((quitType, force) =>
switch (quitType) {
| QuitAll => dispatch(Quit(force))
| QuitOne(buf) => dispatch(QuitBuffer(buf, force))
}
);

let _ =
Vim.Visual.onRangeChanged(vr => {
open Vim.Range;
Expand Down

0 comments on commit c0a77a7

Please sign in to comment.