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

Limit the amount of horizontal splits you can do #4642

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4258,11 +4258,19 @@ fn split(cx: &mut Context, action: Action) {
}

fn hsplit(cx: &mut Context) {
let view_id = view!(cx.editor).id;

split(cx, Action::HorizontalSplit);

typed::hsplit_limit_check(view_id, cx.editor);
}

fn hsplit_new(cx: &mut Context) {
let view_id = view!(cx.editor).id;

cx.editor.new_file(Action::HorizontalSplit);

typed::hsplit_limit_check(view_id, cx.editor);
}

fn vsplit(cx: &mut Context) {
Expand Down
23 changes: 22 additions & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,8 +1221,8 @@ fn hsplit(
if event != PromptEvent::Validate {
return Ok(());
}

let id = view!(cx.editor).doc;
let view_id = view!(cx.editor).id;

if args.is_empty() {
cx.editor.switch(id, Action::HorizontalSplit);
Expand All @@ -1233,6 +1233,8 @@ fn hsplit(
}
}

hsplit_limit_check(view_id, cx.editor);

Ok(())
}

Expand All @@ -1258,12 +1260,31 @@ fn hsplit_new(
if event != PromptEvent::Validate {
return Ok(());
}
let view_id = view!(cx.editor).id;

cx.editor.new_file(Action::HorizontalSplit);

hsplit_limit_check(view_id, cx.editor);

Ok(())
}

pub fn hsplit_limit_check(view_id: ViewId, editor: &mut Editor) {
// check if there are views with height equal to 1
// if there are, close the newly created view
if editor
.tree
.views()
.any(|(view, _focused)| view.area.height == 1 && editor.tree.is_child(&view.id))
{
editor.set_error("Max number of splits reached");
editor.close(view!(editor).id);

// focus the view from which the split was called
editor.focus(view_id);
}
}

fn debug_eval(
cx: &mut compositor::Context,
args: &[Cow<str>],
Expand Down
14 changes: 14 additions & 0 deletions helix-view/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ impl Tree {
node
}

pub fn is_child(&self, child_id: &ViewId) -> bool {
let focus = self.focus;
let parent = self.nodes[focus].parent;

let container_children: Vec<ViewId> = match &self.nodes[parent] {
Node {
content: Content::Container(container),
..
} => container.children.clone(),
_ => unimplemented!(),
};
container_children.contains(child_id)
}

pub fn remove(&mut self, index: ViewId) {
let mut stack = Vec::new();

Expand Down