Skip to content

Commit

Permalink
Make mouse click extend selection in select mode
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI committed Jan 22, 2024
1 parent 9ed3dc5 commit cfef31b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
40 changes: 29 additions & 11 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,15 @@ impl EditorView {
if modifiers == KeyModifiers::ALT {
let selection = doc.selection(view_id).clone();
doc.set_selection(view_id, selection.push(Range::point(pos)));
} else if editor.mode == Mode::Select {
// Discards non-primary selections for consistent UX with normal mode
let primary = doc.selection(view_id).primary().put_cursor(
doc.text().slice(..),
pos,
true,
);
editor.mouse_down_range = Some(primary);
doc.set_selection(view_id, Selection::single(primary.anchor, primary.head));
} else {
doc.set_selection(view_id, Selection::point(pos));
}
Expand Down Expand Up @@ -1188,19 +1197,28 @@ impl EditorView {

let (view, doc) = current!(cxt.editor);

if doc
.selection(view.id)
.primary()
.slice(doc.text().slice(..))
.len_chars()
<= 1
{
return EventResult::Ignored(None);
}
let should_yank = match cxt.editor.mouse_down_range {
Some(down_range) => doc.selection(view.id).primary() != down_range,
None => {
// This should not happen under normal cases. We fall back to the original
// behavior of yanking on non-single-char selections.
doc.selection(view.id)
.primary()
.slice(doc.text().slice(..))
.len_chars()
> 1
}
};

commands::MappableCommand::yank_main_selection_to_primary_clipboard.execute(cxt);
cxt.editor.mouse_down_range = None;

EventResult::Consumed(None)
if should_yank {
commands::MappableCommand::yank_main_selection_to_primary_clipboard
.execute(cxt);
EventResult::Consumed(None)
} else {
EventResult::Ignored(None)
}
}

MouseEventKind::Up(MouseButton::Right) => {
Expand Down
7 changes: 5 additions & 2 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use helix_core::diagnostic::Severity;
use helix_core::{
auto_pairs::AutoPairs,
syntax::{self, AutoPairConfig, IndentationHeuristic, LanguageServerFeature, SoftWrap},
Change, LineEnding, Position, Selection, NATIVE_LINE_ENDING,
Change, LineEnding, Position, Range, Selection, NATIVE_LINE_ENDING,
};
use helix_dap as dap;
use helix_lsp::lsp;
Expand Down Expand Up @@ -960,6 +960,8 @@ pub struct Editor {
/// field is set and any old requests are automatically
/// canceled as a result
pub completion_request_handle: Option<oneshot::Sender<()>>,

pub mouse_down_range: Option<Range>,
}

pub type Motion = Box<dyn Fn(&mut Editor)>;
Expand Down Expand Up @@ -1072,6 +1074,7 @@ impl Editor {
needs_redraw: false,
cursor_cache: Cell::new(None),
completion_request_handle: None,
mouse_down_range: None,
}
}

Expand Down Expand Up @@ -1879,7 +1882,7 @@ impl Editor {

/// Switches the editor into normal mode.
pub fn enter_normal_mode(&mut self) {
use helix_core::{graphemes, Range};
use helix_core::graphemes;

if self.mode == Mode::Normal {
return;
Expand Down

0 comments on commit cfef31b

Please sign in to comment.