mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-27 12:54:42 +00:00
Handle special "final tabstop" in snippets
This commit is contained in:
parent
1371a20e58
commit
680fde9608
1 changed files with 38 additions and 38 deletions
|
@ -16,9 +16,13 @@ impl Snippet {
|
||||||
let mut tabstops = BTreeMap::new();
|
let mut tabstops = BTreeMap::new();
|
||||||
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
|
||||||
|
.remove(&0)
|
||||||
|
.unwrap_or_else(|| SmallVec::from_iter([text.len()..text.len()]));
|
||||||
Ok(Snippet {
|
Ok(Snippet {
|
||||||
text,
|
text,
|
||||||
tabstops: tabstops.into_values().collect(),
|
tabstops: tabstops.into_values().chain(Some(last_tabstop)).collect(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,58 +107,50 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_snippet_with_tabstops() {
|
fn test_snippet_without_tabstops() {
|
||||||
let snippet = Snippet::parse("one$1two").unwrap();
|
let snippet = Snippet::parse("one-two-three").unwrap();
|
||||||
assert_eq!(snippet.text, "onetwo");
|
assert_eq!(snippet.text, "one-two-three");
|
||||||
assert_eq!(
|
assert_eq!(tabstops(&snippet), &[vec![13..13]]);
|
||||||
snippet
|
|
||||||
.tabstops
|
|
||||||
.iter()
|
|
||||||
.map(SmallVec::as_slice)
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
&[vec![3..3]]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Multi-digit numbers
|
|
||||||
let snippet = Snippet::parse("one$123 $99").unwrap();
|
|
||||||
assert_eq!(snippet.text, "one ");
|
|
||||||
assert_eq!(
|
|
||||||
snippet
|
|
||||||
.tabstops
|
|
||||||
.iter()
|
|
||||||
.map(SmallVec::as_slice)
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
&[vec![4..4], vec![3..3]]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_snippet_with_placeholders() {
|
fn test_snippet_with_tabstops() {
|
||||||
|
let snippet = Snippet::parse("one$1two").unwrap();
|
||||||
|
assert_eq!(snippet.text, "onetwo");
|
||||||
|
assert_eq!(tabstops(&snippet), &[vec![3..3], vec![6..6]]);
|
||||||
|
|
||||||
|
// Multi-digit numbers
|
||||||
|
let snippet = Snippet::parse("one$123-$99-two").unwrap();
|
||||||
|
assert_eq!(snippet.text, "one--two");
|
||||||
|
assert_eq!(tabstops(&snippet), &[vec![4..4], vec![3..3], vec![8..8]]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_snippet_with_explicit_final_tabstop() {
|
||||||
|
let snippet = Snippet::parse(r#"<div class="$1">$0</div>"#).unwrap();
|
||||||
|
assert_eq!(snippet.text, r#"<div class=""></div>"#);
|
||||||
|
assert_eq!(tabstops(&snippet), &[vec![12..12], vec![14..14]]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_snippet_with_placeholders() {
|
||||||
let snippet = Snippet::parse("one${1:two}three${2:four}").unwrap();
|
let snippet = Snippet::parse("one${1:two}three${2:four}").unwrap();
|
||||||
assert_eq!(snippet.text, "onetwothreefour");
|
assert_eq!(snippet.text, "onetwothreefour");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
snippet
|
tabstops(&snippet),
|
||||||
.tabstops
|
&[vec![3..6], vec![11..15], vec![15..15]]
|
||||||
.iter()
|
|
||||||
.map(SmallVec::as_slice)
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
&[vec![3..6], vec![11..15]]
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_snippet_with_nested_placeholders() {
|
fn test_snippet_with_nested_placeholders() {
|
||||||
let snippet = Snippet::parse(
|
let snippet = Snippet::parse(
|
||||||
"for (${1:var ${2:i} = 0; ${2:i} < ${3:${4:array}.length}; ${2:i}++}) {$5}",
|
"for (${1:var ${2:i} = 0; ${2:i} < ${3:${4:array}.length}; ${2:i}++}) {$0}",
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(snippet.text, "for (var i = 0; i < array.length; i++) {}");
|
assert_eq!(snippet.text, "for (var i = 0; i < array.length; i++) {}");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
snippet
|
tabstops(&snippet),
|
||||||
.tabstops
|
|
||||||
.iter()
|
|
||||||
.map(SmallVec::as_slice)
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
&[
|
&[
|
||||||
vec![5..37],
|
vec![5..37],
|
||||||
vec![9..10, 16..17, 34..35],
|
vec![9..10, 16..17, 34..35],
|
||||||
|
@ -164,4 +160,8 @@ mod tests {
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn tabstops(snippet: &Snippet) -> Vec<Vec<Range<usize>>> {
|
||||||
|
snippet.tabstops.iter().map(|t| t.to_vec()).collect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue