From 66f009b873f693ba3f2e2169065b161fc2045a9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikodem=20Rabuli=C5=84ski?= Date: Sat, 1 Jul 2023 14:53:54 +0200 Subject: [PATCH] Add style configuration for tabline, make tabline compatible with bufferline --- helix-term/src/ui/editor.rs | 40 ++++++++++++++++++++++++++++--------- helix-view/src/editor.rs | 14 +++++++++++++ helix-view/src/tabs.rs | 10 ++++++++++ 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index cf9a35df9..5e8ee22eb 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -583,18 +583,21 @@ impl EditorView { viewport, editor .theme - .try_get("ui.bufferline.background") + .try_get("ui.tabline.background") + .or_else(|| editor.theme.try_get("ui.bufferline.background")) .unwrap_or_else(|| editor.theme.get("ui.statusline")), ); - let bufferline_active = editor + let tabline_active = editor .theme - .try_get("ui.bufferline.active") + .try_get("ui.tabline.active") + .or_else(|| editor.theme.try_get("ui.bufferline.active")) .unwrap_or_else(|| editor.theme.get("ui.statusline.active")); - let bufferline_inactive = editor + let tabline_inactive = editor .theme - .try_get("ui.bufferline") + .try_get("ui.tabline") + .or_else(|| editor.theme.try_get("ui.bufferline")) .unwrap_or_else(|| editor.theme.get("ui.statusline.inactive")); let mut x = viewport.x; @@ -602,9 +605,9 @@ impl EditorView { let current_tab = editor.tabs.focus; for (id, tab) in editor.tabs.iter_tabs() { let style = if current_tab == id { - bufferline_active + tabline_active } else { - bufferline_inactive + tabline_inactive }; let text = format!(" {} ", tab.name); @@ -1511,6 +1514,14 @@ impl Component for EditorView { surface.set_style(area, cx.editor.theme.get("ui.background")); let config = cx.editor.config(); + // check if tabline should be rendered + use helix_view::editor::TabLine; + let use_tabline = if let TabLine::Multiple = config.tabline { + cx.editor.tabs.len() > 1 + } else { + true + }; + // check if bufferline should be rendered use helix_view::editor::BufferLine; let use_bufferline = match config.bufferline { @@ -1519,8 +1530,15 @@ impl Component for EditorView { _ => false, }; - // -1 for commandline and -1 for bufferline + // -1 for commandline and -1 for tabline + let mut bufferline_area = area; let mut editor_area = area.clip_bottom(1); + if use_tabline { + editor_area = editor_area.clip_top(1); + bufferline_area = bufferline_area.clip_top(1); + } + + // -1 for bufferline if use_bufferline { editor_area = editor_area.clip_top(1); } @@ -1528,10 +1546,14 @@ impl Component for EditorView { // if the terminal size suddenly changed, we need to trigger a resize cx.editor.resize(editor_area); - if use_bufferline { + if use_tabline { Self::render_tabline(cx.editor, area.with_height(1), surface); } + if use_bufferline { + Self::render_bufferline(cx.editor, bufferline_area.with_height(1), surface); + } + for (view, is_focused) in cx.editor.tabs.curr_tree().views() { let doc = cx.editor.document(view.doc).unwrap(); self.render_view(cx.editor, doc, view, area, surface, is_focused); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 2023dbede..7a4f058d5 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -311,6 +311,8 @@ pub struct Config { pub rulers: Vec, #[serde(default)] pub whitespace: WhitespaceConfig, + /// Display open tabs along the top + pub tabline: TabLine, /// Persistently display open buffers along the top pub bufferline: BufferLine, /// Vertical indent width guides. @@ -625,6 +627,17 @@ impl Default for CursorShapeConfig { } } +/// tabline render modes +#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum TabLine { + /// Always render + Always, + /// Only if multiple tabs are open + #[default] + Multiple, +} + /// bufferline render modes #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] @@ -898,6 +911,7 @@ impl Default for Config { terminal: get_terminal_provider(), rulers: Vec::new(), whitespace: WhitespaceConfig::default(), + tabline: TabLine::default(), bufferline: BufferLine::default(), indent_guides: IndentGuidesConfig::default(), color_modes: false, diff --git a/helix-view/src/tabs.rs b/helix-view/src/tabs.rs index a33dad415..25aa22081 100644 --- a/helix-view/src/tabs.rs +++ b/helix-view/src/tabs.rs @@ -76,4 +76,14 @@ impl Tabs { self.focus = id; id } + + #[inline] + pub fn len(&self) -> usize { + self.tabs.len() + } + + #[inline] + pub fn is_empty(&self) -> bool { + self.tabs.is_empty() + } }