Skip to content

Commit

Permalink
Bugfix: Clicking the 'x' doesn't close editors (onivim#306)
Browse files Browse the repository at this point in the history
* Fix close command

* Formatting

* Remove logging

* Remove now-unused code

* Factor 'filterMap' utility out

* Fix list reversal

* Get metadata
  • Loading branch information
bryphe authored May 8, 2019
1 parent 69865cd commit 30ff587
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 73 deletions.
1 change: 0 additions & 1 deletion src/editor/Core/Tab.re

This file was deleted.

14 changes: 14 additions & 0 deletions src/editor/Core/Utility.re
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,17 @@ let escapeSpaces = str => {
let whitespace = Str.regexp(" ");
Str.global_replace(whitespace, "\\ ", str);
};

let filterMap = (f, l) => {
let rec inner = l =>
switch (l) {
| [] => []
| [hd, ...tail] =>
switch (f(hd)) {
| Some(v) => [v, ...inner(tail)]
| None => inner(tail)
}
};

inner(l);
};
7 changes: 7 additions & 0 deletions src/editor/Model/EditorGroup.re
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ let _getAdjacentEditor = (editor: int, reverseTabOrder: list(int)) => {
};
};

let isActiveEditor = (state, editorId) => {
switch (state.activeEditorId) {
| None => false
| Some(v) => v == editorId
};
};

let removeEditorById = (state, editorId) => {
switch (IntMap.find_opt(editorId, state.editors)) {
| None => state
Expand Down
1 change: 0 additions & 1 deletion src/editor/Model/Oni_Model.re
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module Selectors = Selectors;
module StatusBarModel = StatusBarModel;
module State = State;
module SyntaxHighlighting = SyntaxHighlighting;
module Tab = Tab;
module WhitespaceTokenFilter = WhitespaceTokenFilter;
module Wildmenu = Wildmenu;
module WindowManager = WindowManager;
Expand Down
26 changes: 0 additions & 26 deletions src/editor/Model/Selectors.re
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,3 @@ let getActiveBuffer = (state: State.t) => {
| None => None
};
};

let getTabs = (state: State.t, editorGroup: EditorGroup.t) => {
let activeEditorId = editorGroup.activeEditorId;
let f = (editorId: int) => {
let editor = EditorGroup.getEditorById(editorId, editorGroup);

let buffer =
switch (editor) {
| None => None
| Some(v) => getBufferById(state, v.bufferId)
};

let active =
switch (activeEditorId) {
| None => false
| Some(v) => v == editorId
};

switch (buffer) {
| Some(v) => Tab.ofBuffer(~buffer=v, ~active, ())
| None => Tab.create(~id=-1, ~title="Unknown", ())
};
};

editorGroup.reverseTabOrder |> List.rev |> List.map(f);
};
34 changes: 0 additions & 34 deletions src/editor/Model/Tab.re

This file was deleted.

47 changes: 36 additions & 11 deletions src/editor/UI/EditorGroupView.re
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

open Revery.UI;
open Oni_Core;
open Oni_Model;
module Model = Oni_Model;

Expand All @@ -28,19 +29,43 @@ let editorViewStyle = (background, foreground) =>
flexDirection(`Column),
];

let toUiTabs = (tabs: list(Model.Tab.t)) => {
let f = (t: Model.Tab.t) => {
let ret: Tabs.tabInfo = {
title: t.title,
modified: t.modified,
active: t.active,
onClick: () => GlobalContext.current().openEditorById(t.id),
onClose: () => GlobalContext.current().closeEditorById(t.id),
let truncateFilepath = path =>
switch (path) {
| Some(p) => Filename.basename(p)
| None => "untitled"
};

let getBufferMetadata = (buffer: option(Buffer.t)) => {
switch (buffer) {
| None => (false, "untitled")
| Some(v) =>
open Types.BufferMetadata;
let {filePath, modified, _} = Buffer.getMetadata(v);

let title = filePath |> truncateFilepath;
(modified, title);
};
};

let toUiTabs = (editorGroup: Model.EditorGroup.t, buffers: Model.Buffers.t) => {
let f = (id: int) => {
switch (Model.EditorGroup.getEditorById(id, editorGroup)) {
| None => None
| Some(v) =>
let (modified, title) =
Model.Buffers.getBuffer(id, buffers) |> getBufferMetadata;
let ret: Tabs.tabInfo = {
title,
modified,
active: EditorGroup.isActiveEditor(editorGroup, v.id),
onClick: () => GlobalContext.current().openEditorById(v.id),
onClose: () => GlobalContext.current().closeEditorById(v.id),
};
Some(ret);
};
ret;
};

List.map(f, tabs);
Utility.filterMap(f, editorGroup.reverseTabOrder) |> List.rev;
};

let createElement = (~state: State.t, ~editorGroupId: int, ~children as _, ()) =>
Expand All @@ -58,7 +83,7 @@ let createElement = (~state: State.t, ~editorGroupId: int, ~children as _, ()) =
| Some(v) =>
let editor =
Selectors.getActiveEditorGroup(state) |> Selectors.getActiveEditor;
let tabs = Model.Selectors.getTabs(state, v) |> toUiTabs;
let tabs = toUiTabs(v, state.buffers);
let uiFont = state.uiFont;

let metrics = v.metrics;
Expand Down

0 comments on commit 30ff587

Please sign in to comment.