on master help sign in
always parse the entire snippet

Previously any remaining text of the snippet that could not be parsed was ignored. This commit adds the `parse_all` function which reports an error if any text was not consumed by the parser

commitbbf480007d87631ef7b7f93cef170ec1af961406 authorPascal Kuthe <pascal.kuthe@semimod.de> dateMon 13 Mar 2023, 19:22 (4 years ago) parent7bf168d
1 file changed+29−2
helix-lsp/src/snippet.rs+29−2
helix-lsp/src/snippet.rs+29−2
@@ -384,9 +384,14 @@ mod parser {
384384 }
385385
386386 pub fn parse(s: &str) -> Result<Snippet, &str> {
387− snippet().parse(s).map(|(_input, elements)| elements)
387+ snippet().parse(s).and_then(|(remainder, snippet)| {
388+ if remainder.is_empty() {
389+ Ok(snippet)
390+ } else {
391+ Err(remainder)
392+ }
393+ })
388394 }
389−
390395 #[cfg(test)]
391396 mod test {
392397 use super::SnippetElement::*;
@@ -414,6 +419,28 @@ mod parser {
414419 )
415420 }
416421
422+ #[test]
423+ fn parse_unterminated_placeholder_error() {
424+ assert_eq!(Err("${1:)"), parse("match(${1:)"))
425+ }
426+
427+ #[test]
428+ fn parse_empty_placeholder() {
429+ assert_eq!(
430+ Ok(Snippet {
431+ elements: vec![
432+ Text("match(".into()),
433+ Placeholder {
434+ tabstop: 1,
435+ value: vec![],
436+ },
437+ Text(")".into())
438+ ]
439+ }),
440+ parse("match(${1:})")
441+ )
442+ }
443+
417444 #[test]
418445 fn parse_placeholders_in_statement() {
419446 assert_eq!(