feat: add basic core functionality to replace Tag with another

pull/12055/head
Nikita Revenco 2 weeks ago
parent dfd1252f5f
commit 74b8ca9c72

@ -5725,8 +5725,6 @@ fn surround_replace(cx: &mut Context) {
}; };
// TODO: add back the logic for other surround changes // TODO: add back the logic for other surround changes
} else { } else {
// TODO: obtain these values from a function
// let change_pos: Vec<(usize, usize)> = vec![(4, 10), (13, 20), (24, 30), (33, 40)];
let change_pos = match surround::get_surround_pos_tag(text, selection, layer) { let change_pos = match surround::get_surround_pos_tag(text, selection, layer) {
Ok(c) => c, Ok(c) => c,
Err(err) => { Err(err) => {
@ -5736,43 +5734,37 @@ fn surround_replace(cx: &mut Context) {
}; };
let selection = selection.clone(); let selection = selection.clone();
let ranges: SmallVec<[Range; 1]> = let ranges: SmallVec<[Range; 1]> = change_pos
change_pos.iter().map(|p| Range::new(14, 14)).collect(); .iter()
.flat_map(|&((opening_tag_range, closing_tag_range), _)| {
vec![opening_tag_range, closing_tag_range]
})
.collect();
doc.set_selection( doc.set_selection(
view.id, view.id,
Selection::new(ranges, selection.primary_index() * 2), Selection::new(ranges, selection.primary_index() * 2),
); );
cx.on_next_key(move |cx, event| { cx.on_next_key(move |cx, _event| {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
// TODO: obtain this value from the input // TODO: obtain this value from the input
let open = "<help>"; let user_input = "help";
let close = "</help>";
// the changeset has to be sorted to allow nested surrounds
let mut sorted_pos: Vec<((usize, usize), &str)> = Vec::new();
// taking chunks of two at once to replace with <help> </help>
for p in change_pos.chunks(2) {
// let line_opening = p[0];
// let line_closing = p[1];
let line_opening = (1, 1);
let line_closing = (1, 1);
sorted_pos.push((line_opening, open));
sorted_pos.push((line_closing, close));
}
sorted_pos.sort_unstable();
let transaction = Transaction::change( let transaction = Transaction::change(
doc.text(), doc.text(),
sorted_pos.iter().map(|&(pos, change_tag)| { change_pos.iter().flat_map(|(pos, _)| {
let mut tag = Tendril::new(); let mut tag = Tendril::new();
tag.push_str(change_tag); tag.push_str(user_input);
// replace from pos.0 to pos.1 with t let opening_range = pos.0;
(pos.0, pos.1, Some(tag)) let closing_range = pos.1;
vec![
(opening_range.from(), opening_range.to(), Some(tag.clone())),
(closing_range.from(), closing_range.to(), Some(tag.clone())),
]
}), }),
); );

Loading…
Cancel
Save