aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/commands.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-term/src/commands.rs')
-rw-r--r--helix-term/src/commands.rs186
1 files changed, 99 insertions, 87 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index e67708e7..3c337c50 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -10,7 +10,7 @@ use helix_core::{
use once_cell::sync::Lazy;
use crate::{
- compositor::Compositor,
+ compositor::{Callback, Compositor},
ui::{self, Popup, Prompt, PromptEvent},
};
@@ -41,8 +41,8 @@ impl<'a> Context<'a> {
}
#[inline]
- pub fn doc(&mut self) -> &mut Document {
- &mut self.editor.view_mut().doc
+ pub fn doc(&mut self) -> std::cell::RefMut<Document> {
+ self.editor.view().doc.borrow_mut()
}
/// Push a new component onto the compositor.
@@ -72,7 +72,7 @@ pub type Command = fn(cx: &mut Context);
pub fn move_char_left(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|range| {
movement::move_horizontally(
@@ -88,7 +88,7 @@ pub fn move_char_left(cx: &mut Context) {
pub fn move_char_right(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|range| {
movement::move_horizontally(
@@ -104,7 +104,7 @@ pub fn move_char_right(cx: &mut Context) {
pub fn move_line_up(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|range| {
movement::move_vertically(
@@ -120,7 +120,7 @@ pub fn move_line_up(cx: &mut Context) {
pub fn move_line_down(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|range| {
movement::move_vertically(
@@ -135,7 +135,7 @@ pub fn move_line_down(cx: &mut Context) {
}
pub fn move_line_end(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let lines = selection_lines(doc.text(), doc.selection());
let positions = lines
@@ -155,7 +155,7 @@ pub fn move_line_end(cx: &mut Context) {
}
pub fn move_line_start(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let lines = selection_lines(doc.text(), doc.selection());
let positions = lines
@@ -177,7 +177,7 @@ pub fn move_line_start(cx: &mut Context) {
pub fn move_next_word_start(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|range| {
@@ -190,7 +190,7 @@ pub fn move_next_word_start(cx: &mut Context) {
pub fn move_prev_word_start(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|range| {
@@ -203,7 +203,7 @@ pub fn move_prev_word_start(cx: &mut Context) {
pub fn move_next_word_end(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|range| {
@@ -215,14 +215,14 @@ pub fn move_next_word_end(cx: &mut Context) {
}
pub fn move_file_start(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
doc.set_selection(Selection::point(0));
doc.mode = Mode::Normal;
}
pub fn move_file_end(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text();
let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
doc.set_selection(Selection::point(last_line));
@@ -232,7 +232,7 @@ pub fn move_file_end(cx: &mut Context) {
pub fn extend_next_word_start(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|mut range| {
@@ -245,7 +245,7 @@ pub fn extend_next_word_start(cx: &mut Context) {
pub fn extend_prev_word_start(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|mut range| {
@@ -257,7 +257,7 @@ pub fn extend_prev_word_start(cx: &mut Context) {
pub fn extend_next_word_end(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|mut range| {
@@ -286,7 +286,7 @@ where
..
} = event
{
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|mut range| {
@@ -382,11 +382,13 @@ pub fn extend_prev_char(cx: &mut Context) {
fn scroll(view: &mut View, offset: usize, direction: Direction) {
use Direction::*;
- let text = view.doc.text().slice(..);
+ // we use short lived borrows since view's methods read from doc too
+ let doc = view.doc.borrow();
+ let text = doc.text().slice(..);
+ let cursor = coords_at_pos(doc.text().slice(..), doc.selection().cursor());
+ let doc_last_line = doc.text().len_lines() - 1;
let last_line = view.last_line();
- let cursor = coords_at_pos(text, view.doc.selection().cursor());
- let doc_last_line = text.len_lines() - 1;
if direction == Backward && view.first_line == 0
|| direction == Forward && last_line == doc_last_line
@@ -412,7 +414,8 @@ fn scroll(view: &mut View, offset: usize, direction: Direction) {
);
let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
- view.doc.set_selection(Selection::point(pos));
+ drop(doc); // upgrade to mutable borrow
+ view.doc.borrow_mut().set_selection(Selection::point(pos));
}
pub fn page_up(cx: &mut Context) {
@@ -437,7 +440,7 @@ pub fn half_page_down(cx: &mut Context) {
pub fn extend_char_left(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|range| {
movement::move_horizontally(
@@ -453,7 +456,7 @@ pub fn extend_char_left(cx: &mut Context) {
pub fn extend_char_right(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|range| {
movement::move_horizontally(
@@ -469,7 +472,7 @@ pub fn extend_char_right(cx: &mut Context) {
pub fn extend_line_up(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|range| {
movement::move_vertically(
@@ -485,7 +488,7 @@ pub fn extend_line_up(cx: &mut Context) {
pub fn extend_line_down(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let selection = doc.selection().transform(|range| {
movement::move_vertically(
@@ -500,12 +503,10 @@ pub fn extend_line_down(cx: &mut Context) {
}
pub fn select_all(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
- doc.set_selection(Selection::single(
- 0,
- doc.text().len_chars().saturating_sub(1),
- ))
+ let end = doc.text().len_chars().saturating_sub(1);
+ doc.set_selection(Selection::single(0, end))
}
pub fn select_regex(cx: &mut Context) {
@@ -530,7 +531,7 @@ pub fn split_selection(cx: &mut Context) {
}
pub fn split_selection_on_newline(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
// only compile the regex once
#[allow(clippy::trivial_regex)]
@@ -562,13 +563,14 @@ fn _search(doc: &mut Document, contents: &str, regex: &Regex) {
// TODO: use one function for search vs extend
pub fn search(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
// TODO: could probably share with select_on_matches?
// HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
// feed chunks into the regex yet
let contents = doc.text().slice(..).to_string();
+ drop(doc);
let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
let text = doc.text();
@@ -585,19 +587,20 @@ pub fn search(cx: &mut Context) {
pub fn search_next(cx: &mut Context) {
if let Some(query) = register::get('\\') {
let query = query.first().unwrap();
- let doc = cx.doc();
+ let mut doc = cx.doc();
let contents = doc.text().slice(..).to_string();
let regex = Regex::new(&query).unwrap();
- _search(doc, &contents, &regex);
+ _search(&mut doc, &contents, &regex);
}
}
pub fn search_selection(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let contents = doc.text().slice(..);
let query = doc.selection().primary().fragment(contents);
let regex = regex::escape(&query);
register::set('\\', vec![regex]);
+ drop(doc);
search_next(cx);
}
@@ -609,7 +612,7 @@ pub fn search_selection(cx: &mut Context) {
pub fn select_line(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let pos = doc.selection().primary();
let text = doc.text();
@@ -622,7 +625,7 @@ pub fn select_line(cx: &mut Context) {
}
pub fn extend_line(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let pos = doc.selection().primary();
let text = doc.text();
@@ -650,20 +653,21 @@ fn _delete_selection(doc: &mut Document) {
}
pub fn delete_selection(cx: &mut Context) {
- let doc = cx.doc();
- _delete_selection(doc);
+ let mut doc = cx.doc();
+ _delete_selection(&mut doc);
doc.append_changes_to_history();
}
pub fn change_selection(cx: &mut Context) {
- let doc = cx.doc();
- _delete_selection(doc);
+ let mut doc = cx.doc();
+ _delete_selection(&mut doc);
+ drop(doc);
insert_mode(cx);
}
pub fn collapse_selection(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let selection = doc
.selection()
.transform(|range| Range::new(range.head, range.head));
@@ -672,7 +676,7 @@ pub fn collapse_selection(cx: &mut Context) {
}
pub fn flip_selections(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let selection = doc
.selection()
.transform(|range| Range::new(range.head, range.anchor));
@@ -686,8 +690,8 @@ fn enter_insert_mode(doc: &mut Document) {
// inserts at the start of each selection
pub fn insert_mode(cx: &mut Context) {
- let doc = cx.doc();
- enter_insert_mode(doc);
+ let mut doc = cx.doc();
+ enter_insert_mode(&mut doc);
let selection = doc
.selection()
@@ -697,8 +701,8 @@ pub fn insert_mode(cx: &mut Context) {
// inserts at the end of each selection
pub fn append_mode(cx: &mut Context) {
- let doc = cx.doc();
- enter_insert_mode(doc);
+ let mut doc = cx.doc();
+ enter_insert_mode(&mut doc);
doc.restore_cursor = true;
let text = doc.text().slice(..);
@@ -765,7 +769,7 @@ pub fn command_mode(cx: &mut Context) {
}
["w"] | ["write"] => {
// TODO: non-blocking via save() command
- smol::block_on(editor.view_mut().doc.save());
+ smol::block_on(editor.view().doc.borrow().save());
}
_ => (),
@@ -800,16 +804,18 @@ fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
// I inserts at the start of each line with a selection
pub fn prepend_to_line(cx: &mut Context) {
- let doc = cx.doc();
- enter_insert_mode(doc);
+ let mut doc = cx.doc();
+ enter_insert_mode(&mut doc);
+ drop(doc);
move_line_start(cx);
}
// A inserts at the end of each line with a selection
pub fn append_to_line(cx: &mut Context) {
- let doc = cx.doc();
- enter_insert_mode(doc);
+ let mut doc = cx.doc();
+ enter_insert_mode(&mut doc);
+ drop(doc);
move_line_end(cx);
}
@@ -817,8 +823,8 @@ pub fn append_to_line(cx: &mut Context) {
// o inserts a new line after each line with a selection
pub fn open_below(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
- enter_insert_mode(doc);
+ let mut doc = cx.doc();
+ enter_insert_mode(&mut doc);
let lines = selection_lines(doc.text(), doc.selection());
@@ -875,7 +881,7 @@ pub fn open_below(cx: &mut Context) {
// O inserts a new line before each line with a selection
pub fn normal_mode(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
doc.mode = Mode::Normal;
@@ -909,14 +915,12 @@ pub fn exit_select_mode(cx: &mut Context) {
}
fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
- let doc = cx.doc();
-
- doc.mode = Mode::Normal;
+ cx.doc().mode = Mode::Normal;
match locations.as_slice() {
[location] => {
cx.editor.open(PathBuf::from(location.uri.path()));
- let doc = cx.doc();
+ let mut doc = cx.doc();
let definition_pos = location.range.start;
let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
doc.set_selection(Selection::point(new_pos));
@@ -932,7 +936,8 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
},
move |editor: &mut Editor, item| {
editor.open(PathBuf::from(item.uri.path()));
- let mut doc = &mut editor.view_mut().doc;
+ // TODO: issues with doc already being broo
+ let mut doc = &mut editor.view().doc.borrow_mut();
let definition_pos = item.range.start;
let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
doc.set_selection(Selection::point(new_pos));
@@ -944,7 +949,7 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
}
pub fn goto_definition(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let language_server = match doc.language_server() {
Some(language_server) => language_server,
None => return,
@@ -956,11 +961,12 @@ pub fn goto_definition(cx: &mut Context) {
// TODO: handle fails
let res =
smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
+ drop(doc);
goto(cx, res);
}
pub fn goto_type_definition(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let language_server = match doc.language_server() {
Some(language_server) => language_server,
None => return,
@@ -972,11 +978,12 @@ pub fn goto_type_definition(cx: &mut Context) {
// TODO: handle fails
let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
.unwrap_or_default();
+ drop(doc);
goto(cx, res);
}
pub fn goto_implementation(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let language_server = match doc.language_server() {
Some(language_server) => language_server,
None => return,
@@ -988,11 +995,12 @@ pub fn goto_implementation(cx: &mut Context) {
// TODO: handle fails
let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
.unwrap_or_default();
+ drop(doc);
goto(cx, res);
}
pub fn goto_reference(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let language_server = match doc.language_server() {
Some(language_server) => language_server,
None => return,
@@ -1004,6 +1012,7 @@ pub fn goto_reference(cx: &mut Context) {
// TODO: handle fails
let res =
smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
+ drop(doc);
goto(cx, res);
}
@@ -1017,7 +1026,7 @@ pub mod insert {
// TODO: insert means add text just before cursor, on exit we should be on the last letter.
pub fn insert_char(cx: &mut Context, c: char) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
// run through insert hooks, stopping on the first one that returns Some(t)
for hook in HOOKS {
@@ -1034,7 +1043,7 @@ pub mod insert {
}
pub fn insert_tab(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
// TODO: round out to nearest indentation level (for example a line with 3 spaces should
// indent by one to reach 4 spaces).
@@ -1044,7 +1053,7 @@ pub mod insert {
}
pub fn insert_newline(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
let indent_level =
@@ -1061,7 +1070,7 @@ pub mod insert {
// TODO: handle indent-aware delete
pub fn delete_char_backward(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
(
@@ -1075,7 +1084,7 @@ pub mod insert {
pub fn delete_char_forward(cx: &mut Context) {
let count = cx.count;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text().slice(..);
let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
(
@@ -1105,7 +1114,7 @@ pub fn redo(cx: &mut Context) {
pub fn yank(cx: &mut Context) {
// TODO: should selections be made end inclusive?
- let doc = cx.doc();
+ let mut doc = cx.doc();
let values = doc
.selection()
.fragments(doc.text().slice(..))
@@ -1146,7 +1155,7 @@ pub fn paste(cx: &mut Context) {
let mut values = values.into_iter().map(Tendril::from).chain(repeat);
- let doc = cx.doc();
+ let mut doc = cx.doc();
let transaction = if linewise {
// paste on the next line
@@ -1185,8 +1194,8 @@ fn get_lines(doc: &Document) -> Vec<usize> {
}
pub fn indent(cx: &mut Context) {
- let doc = cx.doc();
- let lines = get_lines(doc);
+ let mut doc = cx.doc();
+ let lines = get_lines(&mut doc);
// Indent by one level
let indent = Tendril::from(doc.indent_unit());
@@ -1203,8 +1212,8 @@ pub fn indent(cx: &mut Context) {
}
pub fn unindent(cx: &mut Context) {
- let doc = cx.doc();
- let lines = get_lines(doc);
+ let mut doc = cx.doc();
+ let lines = get_lines(&mut doc);
let mut changes = Vec::with_capacity(lines.len());
let tab_width = doc.tab_width();
@@ -1238,7 +1247,7 @@ pub fn unindent(cx: &mut Context) {
pub fn format_selections(cx: &mut Context) {
use helix_lsp::lsp;
- let doc = cx.doc();
+ let mut doc = cx.doc();
// via lsp if available
// else via tree-sitter indentation calculations
@@ -1276,7 +1285,7 @@ pub fn format_selections(cx: &mut Context) {
pub fn join_selections(cx: &mut Context) {
use movement::skip_over_next;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let text = doc.text();
let slice = doc.text().slice(..);
@@ -1319,7 +1328,6 @@ pub fn join_selections(cx: &mut Context) {
}
pub fn keep_selections(cx: &mut Context) {
- let doc = cx.doc();
// keep selections matching regex
let prompt = ui::regex_prompt(cx, "keep:".to_string(), |doc, regex| {
let text = doc.text().slice(..);
@@ -1333,7 +1341,7 @@ pub fn keep_selections(cx: &mut Context) {
}
pub fn keep_primary_selection(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let range = doc.selection().primary();
let selection = Selection::single(range.anchor, range.head);
@@ -1350,7 +1358,7 @@ pub fn save(cx: &mut Context) {
}
pub fn completion(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let language_server = match doc.language_server() {
Some(language_server) => language_server,
@@ -1364,6 +1372,8 @@ pub fn completion(cx: &mut Context) {
let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap_or_default();
+ drop(doc);
+
// TODO: if no completion, show some message or something
if !res.is_empty() {
// let snapshot = doc.state.clone();
@@ -1379,11 +1389,11 @@ pub fn completion(cx: &mut Context) {
match event {
PromptEvent::Abort => {
// revert state
- let doc = &mut editor.view_mut().doc;
+ let doc = &mut editor.view().doc.borrow_mut();
// doc.state = snapshot.clone();
}
PromptEvent::Validate => {
- let doc = &mut editor.view_mut().doc;
+ let doc = &mut editor.view().doc.borrow_mut();
// revert state to what it was before the last update
// doc.state = snapshot.clone();
@@ -1449,7 +1459,7 @@ pub fn completion(cx: &mut Context) {
pub fn hover(cx: &mut Context) {
use helix_lsp::lsp;
- let doc = cx.doc();
+ let mut doc = cx.doc();
let language_server = match doc.language_server() {
Some(language_server) => language_server,
@@ -1466,6 +1476,8 @@ pub fn hover(cx: &mut Context) {
let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
.unwrap_or_default();
+ drop(doc);
+
if let Some(hover) = res {
// hover.contents / .range <- used for visualizing
let contents = match hover.contents {
@@ -1491,12 +1503,12 @@ pub fn hover(cx: &mut Context) {
// view movements
pub fn next_view(cx: &mut Context) {
- cx.editor.tree.focus_next()
+ cx.editor.focus_next()
}
// comments
pub fn toggle_comments(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
doc.apply(&transaction);
@@ -1506,7 +1518,7 @@ pub fn toggle_comments(cx: &mut Context) {
// tree sitter node selection
pub fn expand_selection(cx: &mut Context) {
- let doc = cx.doc();
+ let mut doc = cx.doc();
if let Some(syntax) = doc.syntax() {
let text = doc.text().slice(..);