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 vertical splits you can do #4726

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4270,11 +4270,15 @@ fn hsplit_new(cx: &mut Context) {
}

fn vsplit(cx: &mut Context) {
split(cx, Action::VerticalSplit);
if typed::can_do_vsplit(cx.editor) {
split(cx, Action::VerticalSplit);
}
}

fn vsplit_new(cx: &mut Context) {
cx.editor.new_file(Action::VerticalSplit);
if typed::can_do_vsplit(cx.editor) {
cx.editor.new_file(Action::VerticalSplit);
}
}

fn wclose(cx: &mut Context) {
Expand Down
38 changes: 27 additions & 11 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,15 +1198,16 @@ fn vsplit(
if event != PromptEvent::Validate {
return Ok(());
}
if can_do_vsplit(cx.editor) {
let id = view!(cx.editor).doc;

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

if args.is_empty() {
cx.editor.switch(id, Action::VerticalSplit);
} else {
for arg in args {
cx.editor
.open(&PathBuf::from(arg.as_ref()), Action::VerticalSplit)?;
if args.is_empty() {
cx.editor.switch(id, Action::VerticalSplit);
} else {
for arg in args {
cx.editor
.open(&PathBuf::from(arg.as_ref()), Action::VerticalSplit)?;
}
}
}

Expand Down Expand Up @@ -1244,8 +1245,9 @@ fn vsplit_new(
if event != PromptEvent::Validate {
return Ok(());
}

cx.editor.new_file(Action::VerticalSplit);
if can_do_vsplit(cx.editor) {
cx.editor.new_file(Action::VerticalSplit);
}

Ok(())
}
Expand All @@ -1258,12 +1260,26 @@ fn hsplit_new(
if event != PromptEvent::Validate {
return Ok(());
}

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

Ok(())
}

pub fn can_do_vsplit(editor: &mut Editor) -> bool {
// check if there are views with an inner area of 1 (1 character per line gets drawn)
// if there are, it's not feasible to create any more vertical splits
let current = view!(editor).doc;
if editor.tree.views().any(|(view, _focused)| {
view.inner_area(editor.document(current).unwrap()).width == 1
&& editor.tree.is_child(&view.id)
}) {
editor.set_error("Max number of splits reached");
false
} else {
true
}
}

fn debug_eval(
cx: &mut compositor::Context,
args: &[Cow<str>],
Expand Down
23 changes: 18 additions & 5 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!(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ => unimplemented!(),
_ => unreachable!(),

This should be unreachable since we're looking at a parent node in the tree

};
container_children.contains(child_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid the cloning by doing the contains check inside of the match

match &self.nodes[parent] {
    Node {
        content: Content::Container(container),
        ..
    } => container.children.contains(child_id),
    _ => unreachable!(),
}

}

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

Expand Down Expand Up @@ -385,22 +399,21 @@ impl Tree {
}
Layout::Vertical => {
let len = container.children.len();
let inner_gap = 1u16;

let width = area.width / len as u16;

let inner_gap = 1u16;
// let total_gap = inner_gap * (len as u16 - 1);

let mut child_x = area.x;

for (i, child) in container.children.iter().enumerate() {
let mut area = Rect::new(
child_x,
container.area.y,
width,
// we subtract inner gap here to accommodate for the width that inner gaps take
width - inner_gap,
container.area.height,
);
child_x += width + inner_gap;
child_x += width;

// last child takes the remaining width because we can get uneven
// space from rounding
Expand Down