mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-24 19:10:24 +00:00
Handle escaped characters in snippets
This commit is contained in:
parent
bdb39f6247
commit
022bb28a59
1 changed files with 42 additions and 8 deletions
|
@ -17,13 +17,20 @@ impl Snippet {
|
||||||
parse_snippet(source, false, &mut text, &mut tabstops)
|
parse_snippet(source, false, &mut text, &mut tabstops)
|
||||||
.context("failed to parse snippet")?;
|
.context("failed to parse snippet")?;
|
||||||
|
|
||||||
let last_tabstop = tabstops
|
let len = text.len() as isize;
|
||||||
.remove(&0)
|
let final_tabstop = tabstops.remove(&0);
|
||||||
.unwrap_or_else(|| SmallVec::from_iter([text.len() as isize..text.len() as isize]));
|
let mut tabstops = tabstops.into_values().collect::<Vec<_>>();
|
||||||
Ok(Snippet {
|
|
||||||
text,
|
if let Some(final_tabstop) = final_tabstop {
|
||||||
tabstops: tabstops.into_values().chain(Some(last_tabstop)).collect(),
|
tabstops.push(final_tabstop);
|
||||||
})
|
} else {
|
||||||
|
let end_tabstop = [len..len].into_iter().collect();
|
||||||
|
if !tabstops.last().map_or(false, |t| *t == end_tabstop) {
|
||||||
|
tabstops.push(end_tabstop);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Snippet { text, tabstops })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,6 +46,13 @@ fn parse_snippet<'a>(
|
||||||
Some('$') => {
|
Some('$') => {
|
||||||
source = parse_tabstop(&source[1..], text, tabstops)?;
|
source = parse_tabstop(&source[1..], text, tabstops)?;
|
||||||
}
|
}
|
||||||
|
Some('\\') => {
|
||||||
|
source = &source[1..];
|
||||||
|
if let Some(c) = source.chars().next() {
|
||||||
|
text.push(c);
|
||||||
|
source = &source[c.len_utf8()..];
|
||||||
|
}
|
||||||
|
}
|
||||||
Some('}') => {
|
Some('}') => {
|
||||||
if nested {
|
if nested {
|
||||||
return Ok(source);
|
return Ok(source);
|
||||||
|
@ -48,7 +62,7 @@ fn parse_snippet<'a>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(_) => {
|
Some(_) => {
|
||||||
let chunk_end = source.find(&['}', '$']).unwrap_or(source.len());
|
let chunk_end = source.find(&['}', '$', '\\']).unwrap_or(source.len());
|
||||||
let (chunk, rest) = source.split_at(chunk_end);
|
let (chunk, rest) = source.split_at(chunk_end);
|
||||||
text.push_str(chunk);
|
text.push_str(chunk);
|
||||||
source = rest;
|
source = rest;
|
||||||
|
@ -125,9 +139,22 @@ mod tests {
|
||||||
assert_eq!(tabstops(&snippet), &[vec![4..4], vec![3..3], vec![8..8]]);
|
assert_eq!(tabstops(&snippet), &[vec![4..4], vec![3..3], vec![8..8]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_snippet_with_last_tabstop_at_end() {
|
||||||
|
let snippet = Snippet::parse(r#"foo.$1"#).unwrap();
|
||||||
|
|
||||||
|
// If the final tabstop is already at the end of the text, don't insert
|
||||||
|
// an additional tabstop at the end.
|
||||||
|
assert_eq!(snippet.text, r#"foo."#);
|
||||||
|
assert_eq!(tabstops(&snippet), &[vec![4..4]]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_snippet_with_explicit_final_tabstop() {
|
fn test_snippet_with_explicit_final_tabstop() {
|
||||||
let snippet = Snippet::parse(r#"<div class="$1">$0</div>"#).unwrap();
|
let snippet = Snippet::parse(r#"<div class="$1">$0</div>"#).unwrap();
|
||||||
|
|
||||||
|
// If the final tabstop is explicitly specified via '$0', then
|
||||||
|
// don't insert an additional tabstop at the end.
|
||||||
assert_eq!(snippet.text, r#"<div class=""></div>"#);
|
assert_eq!(snippet.text, r#"<div class=""></div>"#);
|
||||||
assert_eq!(tabstops(&snippet), &[vec![12..12], vec![14..14]]);
|
assert_eq!(tabstops(&snippet), &[vec![12..12], vec![14..14]]);
|
||||||
}
|
}
|
||||||
|
@ -161,6 +188,13 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_snippet_parsing_with_escaped_dollar_sign() {
|
||||||
|
let snippet = Snippet::parse("\"\\$schema\": $1").unwrap();
|
||||||
|
assert_eq!(snippet.text, "\"$schema\": ");
|
||||||
|
assert_eq!(tabstops(&snippet), &[vec![11..11]]);
|
||||||
|
}
|
||||||
|
|
||||||
fn tabstops(snippet: &Snippet) -> Vec<Vec<Range<isize>>> {
|
fn tabstops(snippet: &Snippet) -> Vec<Vec<Range<isize>>> {
|
||||||
snippet.tabstops.iter().map(|t| t.to_vec()).collect()
|
snippet.tabstops.iter().map(|t| t.to_vec()).collect()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue