|
|
@ -5715,20 +5715,23 @@ fn surround_replace(cx: &mut Context) {
|
|
|
|
let text = doc.text().slice(..);
|
|
|
|
let text = doc.text().slice(..);
|
|
|
|
let selection = doc.selection(view.id);
|
|
|
|
let selection = doc.selection(view.id);
|
|
|
|
|
|
|
|
|
|
|
|
// the first character represents the index of the first change
|
|
|
|
if false {
|
|
|
|
// the second character represents the index of the second change
|
|
|
|
let change_pos = match surround::get_surround_pos_tag(text, selection, "lol", layer) {
|
|
|
|
let change_pos =
|
|
|
|
|
|
|
|
// also interested in changing this
|
|
|
|
|
|
|
|
match surround::get_surround_pos(doc.syntax(), text, selection, surround_ch, layer) {
|
|
|
|
|
|
|
|
Ok(c) => c,
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(err) => {
|
|
|
|
Err(err) => {
|
|
|
|
cx.editor.set_error(err.to_string());
|
|
|
|
cx.editor.set_error(err.to_string());
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: add back the logic for other surround changes
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// TODO: obtain these values from a function
|
|
|
|
|
|
|
|
let change_pos: Vec<(usize, usize)> = vec![(4, 10), (13, 20), (24, 30), (33, 40)];
|
|
|
|
|
|
|
|
|
|
|
|
let selection = selection.clone();
|
|
|
|
let selection = selection.clone();
|
|
|
|
let ranges: SmallVec<[Range; 1]> = change_pos.iter().map(|&p| Range::point(p)).collect();
|
|
|
|
let ranges: SmallVec<[Range; 1]> =
|
|
|
|
|
|
|
|
change_pos.iter().map(|&p| Range::new(p.0, p.1)).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),
|
|
|
@ -5736,36 +5739,40 @@ fn surround_replace(cx: &mut Context) {
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
let to = match event.char() {
|
|
|
|
|
|
|
|
Some(to) => to,
|
|
|
|
// TODO: obtain this value from the input
|
|
|
|
None => return doc.set_selection(view.id, selection),
|
|
|
|
let open = "<help>";
|
|
|
|
};
|
|
|
|
let close = "</help>";
|
|
|
|
// we are interested in changing this specifically
|
|
|
|
|
|
|
|
let (open, close) = match_brackets::get_pair(to);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the changeset has to be sorted to allow nested surrounds
|
|
|
|
// the changeset has to be sorted to allow nested surrounds
|
|
|
|
let mut sorted_pos: Vec<(usize, char)> = Vec::new();
|
|
|
|
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) {
|
|
|
|
for p in change_pos.chunks(2) {
|
|
|
|
sorted_pos.push((p[0], open));
|
|
|
|
let line_opening = p[0];
|
|
|
|
sorted_pos.push((p[1], close));
|
|
|
|
let line_closing = p[1];
|
|
|
|
|
|
|
|
sorted_pos.push((line_opening, open));
|
|
|
|
|
|
|
|
sorted_pos.push((line_closing, close));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sorted_pos.sort_unstable();
|
|
|
|
sorted_pos.sort_unstable();
|
|
|
|
|
|
|
|
|
|
|
|
let transaction = Transaction::change(
|
|
|
|
let transaction = Transaction::change(
|
|
|
|
doc.text(),
|
|
|
|
doc.text(),
|
|
|
|
sorted_pos.iter().map(|&pos| {
|
|
|
|
sorted_pos.iter().map(|&(pos, change_tag)| {
|
|
|
|
// "pos" is the idx of the character we are replacing
|
|
|
|
let mut tag = Tendril::new();
|
|
|
|
// "t" is the replacement character
|
|
|
|
tag.push_str(change_tag);
|
|
|
|
let mut t = Tendril::new();
|
|
|
|
|
|
|
|
t.push(pos.1);
|
|
|
|
// replace from pos.0 to pos.1 with t
|
|
|
|
log::error!("{:?}, {:?}", pos.0, Some(&t));
|
|
|
|
(pos.0, pos.1, Some(tag))
|
|
|
|
(pos.0, pos.0 + 1, Some(t))
|
|
|
|
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// we don't need to touch this
|
|
|
|
doc.set_selection(view.id, selection);
|
|
|
|
doc.set_selection(view.id, selection);
|
|
|
|
doc.apply(&transaction, view.id);
|
|
|
|
doc.apply(&transaction, view.id);
|
|
|
|
exit_select_mode(cx);
|
|
|
|
exit_select_mode(cx);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|