From c586e410d4dd884ae270b9864e07f4f2b05c3777 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Mon, 11 Nov 2024 13:30:12 +0000 Subject: [PATCH] refactor: add_surrounding_impl extract main logic into an fn --- helix-core/src/match_brackets.rs | 44 ++++++++++++ helix-term/src/commands.rs | 119 ++++++++++++++++++++++--------- 2 files changed, 131 insertions(+), 32 deletions(-) diff --git a/helix-core/src/match_brackets.rs b/helix-core/src/match_brackets.rs index 7520d3e46..03525087e 100644 --- a/helix-core/src/match_brackets.rs +++ b/helix-core/src/match_brackets.rs @@ -205,6 +205,50 @@ pub fn find_matching_bracket_plaintext(doc: RopeSlice, cursor_pos: usize) -> Opt None } +// fn create_rename_prompt( +// editor: &Editor, +// prefill: String, +// history_register: Option, +// language_server_id: Option, +// ) -> Box { +// 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 /// [`BRACKETS`] returns (ch, ch). /// diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 50385e687..9b84cae33 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -36,7 +36,7 @@ use helix_core::{ textobject, unicode::width::UnicodeWidthChar, 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::{ document::{FormatterError, Mode, SCRATCH_BUFFER_NAME}, @@ -5616,6 +5616,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { ("a", "Argument/parameter (tree-sitter)"), ("c", "Comment (tree-sitter)"), ("T", "Test (tree-sitter)"), + ("x", "Tag (XML/HTML/JSX)"), ("e", "Data structure entry (tree-sitter)"), ("m", "Closest surrounding pair (tree-sitter)"), ("g", "Change"), @@ -5625,6 +5626,54 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { cx.editor.autoinfo = Some(Info::new(title, &help_text)); } +fn create_surround_prompt( + editor: &Editor, + prefill: String, + history_register: Option, +) -> Box { + let prompt = Prompt::new( + "tag name:".into(), + // TODO: change this to actually be history_register + Some('z'), + ui::completers::none, + move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {}, + ) + .with_line("temp".into(), editor); + + 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 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); +} + fn surround_add(cx: &mut Context) { cx.on_next_key(move |cx, event| { let (view, doc) = current!(cx.editor); @@ -5633,20 +5682,23 @@ fn surround_add(cx: &mut Context) { Some(ch) => { let mut open = Tendril::new(); let mut close = Tendril::new(); - let length = if ch == 'x' { - 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, length) + // 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(), @@ -5656,27 +5708,30 @@ fn surround_add(cx: &mut Context) { None => return, }; - 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; + surround_add_impl(doc, view, surround_len, open, close); + exit_select_mode(cx); - for range in selection.iter() { - changes.push((range.from(), range.from(), Some(open.clone()))); - changes.push((range.to(), range.to(), Some(close.clone()))); + // 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; - ranges.push( - Range::new(offs + range.from(), offs + range.to() + surround_len) - .with_direction(range.direction()), - ); + // for range in selection.iter() { + // changes.push((range.from(), range.from(), Some(open.clone()))); + // changes.push((range.to(), range.to(), Some(close.clone()))); - offs += surround_len; - } + // ranges.push( + // Range::new(offs + range.from(), offs + range.to() + surround_len) + // .with_direction(range.direction()), + // ); - 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); + // 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); }) }