From c620860bd63df019287fc07819ea614bca6331e9 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Mon, 11 Nov 2024 15:08:26 +0000 Subject: [PATCH] feat: complete implementation for adding surrounding tag --- helix-term/src/commands.rs | 47 ++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 5153e3404..1aa1b14c3 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, SmartString, Syntax, Tendril, Transaction, + RopeReader, RopeSlice, Selection, SmallVec, Syntax, Tendril, Transaction, }; use helix_view::{ document::{FormatterError, Mode, SCRATCH_BUFFER_NAME}, @@ -5684,10 +5684,25 @@ fn surround_add(cx: &mut Context) { "tag name:".into(), Some('z'), ui::completers::none, - move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {}, - ) - .with_line("temp".into(), cx.editor); - cx.push_layer(prompt); + move |context: &mut compositor::Context, + input: &str, + event: PromptEvent| { + let (view, doc) = current!(context.editor); + + if event != PromptEvent::Validate { + return; + } + + let open = Tendril::from(format!("<{}>", input.to_string())); + let close = Tendril::from(format!("", input.to_string())); + let length = open.len() + close.len(); + + surround_add_impl(doc, view, length, open, close); + context.editor.mode = Mode::Normal; + }, + ); + // .with_line(String::from("temp"), cx.editor); + cx.push_layer(Box::new(prompt)); } else { let (open, close) = match_brackets::get_pair(ch); let open = Tendril::from(open.to_string()); @@ -5706,28 +5721,6 @@ 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; - - // 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); }) }