Skip to content

Commit

Permalink
add helix-editor#6120 to main branch
Browse files Browse the repository at this point in the history
  • Loading branch information
akhenakh committed Jan 4, 2025
1 parent 128babf commit a1236b6
Showing 1 changed file with 51 additions and 9 deletions.
60 changes: 51 additions & 9 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use helix_view::{
graphics::{Color, CursorKind, Modifier, Rect, Style},
input::{KeyEvent, MouseButton, MouseEvent, MouseEventKind},
keyboard::{KeyCode, KeyModifiers},
Document, Editor, Theme, View,
Document, DocumentId, Editor, Theme, View,
};
use std::{mem::take, num::NonZeroUsize, path::PathBuf, rc::Rc, sync::Arc};

Expand All @@ -42,6 +42,7 @@ pub struct EditorView {
pub(crate) last_insert: (commands::MappableCommand, Vec<InsertEvent>),
pub(crate) completion: Option<Completion>,
spinners: ProgressSpinners,
bufferline_widgets: Vec<BufferLineWidget>,
/// Tracks if the terminal window is focused by reaction to terminal focus events
terminal_focused: bool,
}
Expand All @@ -57,6 +58,24 @@ pub enum InsertEvent {
RequestCompletion,
}

#[derive(Debug)]
struct BufferLineWidget {
columns: std::ops::Range<u16>,
document_id: DocumentId,
}

impl BufferLineWidget {
fn get_clicked(widgets: &[Self], column: u16) -> Option<&BufferLineWidget> {
widgets.iter().find(|w| w.columns.contains(&column))
}
}

impl Default for EditorView {
fn default() -> Self {
Self::new(Keymaps::default())
}
}

impl EditorView {
pub fn new(keymaps: Keymaps) -> Self {
Self {
Expand All @@ -66,6 +85,7 @@ impl EditorView {
last_insert: (commands::MappableCommand::normal_mode, Vec::new()),
completion: None,
spinners: ProgressSpinners::default(),
bufferline_widgets: Vec::new(),
terminal_focused: true,
}
}
Expand Down Expand Up @@ -608,7 +628,8 @@ impl EditorView {
}

/// Render bufferline at the top
pub fn render_bufferline(editor: &Editor, viewport: Rect, surface: &mut Surface) {
pub fn render_bufferline(&mut self, editor: &Editor, viewport: Rect, surface: &mut Surface) {
self.bufferline_widgets.clear();
let scratch = PathBuf::from(SCRATCH_BUFFER_NAME); // default filename to use for scratch buffer
surface.clear_with(
viewport,
Expand Down Expand Up @@ -650,6 +671,11 @@ impl EditorView {
let used_width = viewport.x.saturating_sub(x);
let rem_width = surface.area.width.saturating_sub(used_width);

self.bufferline_widgets.push(BufferLineWidget {
columns: x..x + text.len() as u16,
document_id: doc.id(),
});

x = surface
.set_stringn(x, viewport.y, text, rem_width as usize, style)
.0;
Expand Down Expand Up @@ -1173,6 +1199,16 @@ impl EditorView {
MouseEventKind::Down(MouseButton::Left) => {
let editor = &mut cxt.editor;

if is_bufferline_visible(editor) && row == 0 {
if let Some(widget) =
BufferLineWidget::get_clicked(&self.bufferline_widgets[..], column)
{
editor.switch(widget.document_id, helix_view::editor::Action::Replace);
}

return EventResult::Consumed(None);
}

if let Some((pos, view_id)) = pos_and_view(editor, row, column, true) {
let prev_view_id = view!(editor).id;
let doc = doc_mut!(editor, &view!(editor, view_id).doc);
Expand Down Expand Up @@ -1537,12 +1573,7 @@ impl Component for EditorView {
let config = cx.editor.config();

// check if bufferline should be rendered
use helix_view::editor::BufferLine;
let use_bufferline = match config.bufferline {
BufferLine::Always => true,
BufferLine::Multiple if cx.editor.documents.len() > 1 => true,
_ => false,
};
let use_bufferline = is_bufferline_visible(cx.editor);

// -1 for commandline and -1 for bufferline
let mut editor_area = area.clip_bottom(1);
Expand All @@ -1554,7 +1585,7 @@ impl Component for EditorView {
cx.editor.resize(editor_area);

if use_bufferline {
Self::render_bufferline(cx.editor, area.with_height(1), surface);
self.render_bufferline(cx.editor, area.with_height(1), surface);
}

for (view, is_focused) in cx.editor.tree.views() {
Expand Down Expand Up @@ -1649,6 +1680,17 @@ impl Component for EditorView {
}
}

fn is_bufferline_visible(editor: &Editor) -> bool {
use helix_view::editor::BufferLine;
let config = editor.config();

match config.bufferline {
BufferLine::Always => true,
BufferLine::Multiple if editor.documents.len() > 1 => true,
_ => false,
}
}

fn canonicalize_key(key: &mut KeyEvent) {
if let KeyEvent {
code: KeyCode::Char(_),
Expand Down

0 comments on commit a1236b6

Please sign in to comment.