Escape snippet bits in-between placing snippets

Done so that we don't shift the range that we insert the snippet at.
This commit is contained in:
DropDemBits 2024-02-16 00:14:39 -05:00
parent 1aeec93412
commit 1d8ed3408e
No known key found for this signature in database
GPG Key ID: 7FE02A6C1EDFA075

View File

@ -1000,23 +1000,35 @@ fn merge_text_and_snippet_edits(
let mut new_text = current_indel.insert; let mut new_text = current_indel.insert;
// escape out snippet text // find which snippet bits need to be escaped
stdx::replace(&mut new_text, '\\', r"\\"); let escape_places =
stdx::replace(&mut new_text, '$', r"\$"); new_text.rmatch_indices(['\\', '$']).map(|(insert, _)| insert).collect_vec();
let mut escape_places = escape_places.into_iter().peekable();
let mut escape_prior_bits = |new_text: &mut String, up_to: usize| {
for before in escape_places.peeking_take_while(|insert| *insert >= up_to) {
new_text.insert(before, '\\');
}
};
// ...and apply! // insert snippets, and escaping any needed bits along the way
for (index, range) in all_snippets.iter().rev() { for (index, range) in all_snippets.iter().rev() {
let start = (range.start() - new_range.start()).into(); let text_range = range - new_range.start();
let end = (range.end() - new_range.start()).into(); let (start, end) = (text_range.start().into(), text_range.end().into());
if range.is_empty() { if range.is_empty() {
escape_prior_bits(&mut new_text, start);
new_text.insert_str(start, &format!("${index}")); new_text.insert_str(start, &format!("${index}"));
} else { } else {
escape_prior_bits(&mut new_text, end);
new_text.insert(end, '}'); new_text.insert(end, '}');
escape_prior_bits(&mut new_text, start);
new_text.insert_str(start, &format!("${{{index}:")); new_text.insert_str(start, &format!("${{{index}:"));
} }
} }
// escape any remaining bits
escape_prior_bits(&mut new_text, 0);
edits.push(snippet_text_edit( edits.push(snippet_text_edit(
line_index, line_index,
true, true,