refactor: add_surrounding_impl extract main logic into an fn

This commit is contained in:
Nikita Revenco 2024-11-11 13:30:12 +00:00
parent 3882bf9e9d
commit c586e410d4
2 changed files with 134 additions and 35 deletions

View File

@ -205,6 +205,50 @@ pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Opt
None None
} }
// fn create_rename_prompt(
// editor: &Editor,
// prefill: String,
// history_register: Option<char>,
// language_server_id: Option<LanguageServerId>,
// ) -> Box<ui::Prompt> {
// let prompt = ui::Prompt::new(
// "rename-to:".into(),
// history_register,
// ui::completers::none,
// move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
// if event != PromptEvent::Validate {
// return;
// }
// let (view, doc) = current!(cx.editor);
// let Some(language_server) = doc
// .language_servers_with_feature(LanguageServerFeature::RenameSymbol)
// .find(|ls| language_server_id.map_or(true, |id| id == ls.id()))
// else {
// cx.editor
// .set_error("No configured language server supports symbol renaming");
// return;
// };
// let offset_encoding = language_server.offset_encoding();
// let pos = doc.position(view.id, offset_encoding);
// let future = language_server
// .rename_symbol(doc.identifier(), pos, input.to_string())
// .unwrap();
// match block_on(future) {
// Ok(edits) => {
// let _ = cx.editor.apply_workspace_edit(offset_encoding, &edits);
// }
// Err(err) => cx.editor.set_error(err.to_string()),
// }
// },
// )
// .with_line(prefill, editor);
// Box::new(prompt)
// }
/// Returns the open and closing chars pair. If not found in /// Returns the open and closing chars pair. If not found in
/// [`BRACKETS`] returns (ch, ch). /// [`BRACKETS`] returns (ch, ch).
/// ///

View File

@ -36,7 +36,7 @@
textobject, textobject,
unicode::width::UnicodeWidthChar, unicode::width::UnicodeWidthChar,
visual_offset_from_block, Deletion, LineEnding, Position, Range, Rope, RopeGraphemes, visual_offset_from_block, Deletion, LineEnding, Position, Range, Rope, RopeGraphemes,
RopeReader, RopeSlice, Selection, SmallVec, Syntax, Tendril, Transaction, RopeReader, RopeSlice, Selection, SmallVec, SmartString, Syntax, Tendril, Transaction,
}; };
use helix_view::{ use helix_view::{
document::{FormatterError, Mode, SCRATCH_BUFFER_NAME}, document::{FormatterError, Mode, SCRATCH_BUFFER_NAME},
@ -5616,6 +5616,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
("a", "Argument/parameter (tree-sitter)"), ("a", "Argument/parameter (tree-sitter)"),
("c", "Comment (tree-sitter)"), ("c", "Comment (tree-sitter)"),
("T", "Test (tree-sitter)"), ("T", "Test (tree-sitter)"),
("x", "Tag (XML/HTML/JSX)"),
("e", "Data structure entry (tree-sitter)"), ("e", "Data structure entry (tree-sitter)"),
("m", "Closest surrounding pair (tree-sitter)"), ("m", "Closest surrounding pair (tree-sitter)"),
("g", "Change"), ("g", "Change"),
@ -5625,37 +5626,31 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
cx.editor.autoinfo = Some(Info::new(title, &help_text)); cx.editor.autoinfo = Some(Info::new(title, &help_text));
} }
fn surround_add(cx: &mut Context) { fn create_surround_prompt(
cx.on_next_key(move |cx, event| { editor: &Editor,
let (view, doc) = current!(cx.editor); prefill: String,
// surround_len is the number of new characters being added. history_register: Option<char>,
let (open, close, surround_len) = match event.char() { ) -> Box<Prompt> {
Some(ch) => { let prompt = Prompt::new(
let mut open = Tendril::new(); "tag name:".into(),
let mut close = Tendril::new(); // TODO: change this to actually be history_register
let length = if ch == 'x' { Some('z'),
let (o, c) = match_brackets::get_pair(ch); ui::completers::none,
open.push(o); move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {},
close.push(c); )
// Any character other than "x" will cause 2 chars to get added .with_line("temp".into(), editor);
2
} else {
let (o, c) = match_brackets::get_pair(ch);
open.push(o);
close.push(c);
// Any character other than "x" will cause 2 chars to get added
2
};
(open, close, length)
}
None if event.code == KeyCode::Enter => (
doc.line_ending.as_str().into(),
doc.line_ending.as_str().into(),
2 * doc.line_ending.len_chars(),
),
None => return,
};
Box::new(prompt)
}
fn surround_add_impl(
doc: &mut Document,
// cx: &mut Context<'_>,
view: &mut View,
surround_len: usize,
open: Tendril,
close: Tendril,
) {
let selection = doc.selection(view.id); let selection = doc.selection(view.id);
let mut changes = Vec::with_capacity(selection.len() * 2); let mut changes = Vec::with_capacity(selection.len() * 2);
let mut ranges = SmallVec::with_capacity(selection.len()); let mut ranges = SmallVec::with_capacity(selection.len());
@ -5676,7 +5671,67 @@ fn surround_add(cx: &mut Context) {
let transaction = Transaction::change(doc.text(), changes.into_iter()) let transaction = Transaction::change(doc.text(), changes.into_iter())
.with_selection(Selection::new(ranges, selection.primary_index())); .with_selection(Selection::new(ranges, selection.primary_index()));
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
// exit_select_mode(cx);
}
fn surround_add(cx: &mut Context) {
cx.on_next_key(move |cx, event| {
let (view, doc) = current!(cx.editor);
// surround_len is the number of new characters being added.
let (open, close, surround_len) = match event.char() {
Some(ch) => {
let mut open = Tendril::new();
let mut close = Tendril::new();
// let length = if ch == 'x' {
// let prompt = create_surround_prompt(cx.editor, "none".into(), Some('z'));
// cx.push_layer(prompt);
// let (o, c) = match_brackets::get_pair(ch);
// open.push(o);
// close.push(c);
// // Any character other than "x" will cause 2 chars to get added
// 2
// } else {
let (o, c) = match_brackets::get_pair(ch);
open.push(o);
close.push(c);
// Any character other than "x" will cause 2 chars to get added
// 2
// };
(open, close, 2)
}
None if event.code == KeyCode::Enter => (
doc.line_ending.as_str().into(),
doc.line_ending.as_str().into(),
2 * doc.line_ending.len_chars(),
),
None => return,
};
surround_add_impl(doc, view, surround_len, open, close);
exit_select_mode(cx); exit_select_mode(cx);
// let selection = doc.selection(view.id);
// let mut changes = Vec::with_capacity(selection.len() * 2);
// let mut ranges = SmallVec::with_capacity(selection.len());
// let mut offs = 0;
// for range in selection.iter() {
// changes.push((range.from(), range.from(), Some(open.clone())));
// changes.push((range.to(), range.to(), Some(close.clone())));
// ranges.push(
// Range::new(offs + range.from(), offs + range.to() + surround_len)
// .with_direction(range.direction()),
// );
// offs += surround_len;
// }
// let transaction = Transaction::change(doc.text(), changes.into_iter())
// .with_selection(Selection::new(ranges, selection.primary_index()));
// doc.apply(&transaction, view.id);
// exit_select_mode(cx);
}) })
} }