aboutsummaryrefslogtreecommitdiff
path: root/helix-lsp
diff options
context:
space:
mode:
authorUrgau2023-02-07 19:15:39 +0000
committerBlaž Hrastnik2023-03-08 01:48:35 +0000
commite973b71c83dcefbdb3a748f28ce3bfd51e9cd842 (patch)
tree9071c629cb395524ec97265ca898f37615468907 /helix-lsp
parent9c12e0fb765f065b43788da670ffb98159d64a5f (diff)
Optimize LSP snippet parsing
Diffstat (limited to 'helix-lsp')
-rw-r--r--helix-lsp/Cargo.toml1
-rw-r--r--helix-lsp/src/snippet.rs45
2 files changed, 28 insertions, 18 deletions
diff --git a/helix-lsp/Cargo.toml b/helix-lsp/Cargo.toml
index 7c71fa9f..9d76822d 100644
--- a/helix-lsp/Cargo.toml
+++ b/helix-lsp/Cargo.toml
@@ -27,4 +27,3 @@ thiserror = "1.0"
tokio = { version = "1.26", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "sync"] }
tokio-stream = "0.1.12"
which = "4.4"
-once_cell = "1.15"
diff --git a/helix-lsp/src/snippet.rs b/helix-lsp/src/snippet.rs
index 529c3b97..27b103d5 100644
--- a/helix-lsp/src/snippet.rs
+++ b/helix-lsp/src/snippet.rs
@@ -50,14 +50,11 @@ pub struct Snippet<'a> {
elements: Vec<SnippetElement<'a>>,
}
-pub fn parse<'a>(s: &'a str) -> Result<Snippet<'a>> {
+pub fn parse(s: &str) -> Result<Snippet<'_>> {
parser::parse(s).map_err(|rest| anyhow!("Failed to parse snippet. Remaining input: {}", rest))
}
mod parser {
- use helix_core::regex;
- use once_cell::sync::Lazy;
-
use helix_parsec::*;
use super::{CaseChange, FormatItem, Regex, Snippet, SnippetElement};
@@ -86,17 +83,34 @@ mod parser {
else ::= text
*/
- static DIGIT: Lazy<regex::Regex> = Lazy::new(|| regex::Regex::new(r"^[0-9]+").unwrap());
- static VARIABLE: Lazy<regex::Regex> =
- Lazy::new(|| regex::Regex::new(r"^[_a-zA-Z][_a-zA-Z0-9]*").unwrap());
- static TEXT: Lazy<regex::Regex> = Lazy::new(|| regex::Regex::new(r"^[^\$]+").unwrap());
-
fn var<'a>() -> impl Parser<'a, Output = &'a str> {
- pattern(&VARIABLE)
+ // var = [_a-zA-Z][_a-zA-Z0-9]*
+ move |input: &'a str| match input
+ .char_indices()
+ .take_while(|(p, c)| {
+ *c == '_'
+ || if *p == 0 {
+ c.is_ascii_alphabetic()
+ } else {
+ c.is_ascii_alphanumeric()
+ }
+ })
+ .last()
+ {
+ Some((index, c)) if index >= 1 => {
+ let index = index + c.len_utf8();
+ Ok((&input[index..], &input[0..index]))
+ }
+ _ => Err(input),
+ }
+ }
+
+ fn text<'a>() -> impl Parser<'a, Output = &'a str> {
+ take_while(|c| c != '$')
}
fn digit<'a>() -> impl Parser<'a, Output = usize> {
- filter_map(pattern(&DIGIT), |s| s.parse().ok())
+ filter_map(take_while(|c| c.is_ascii_digit()), |s| s.parse().ok())
}
fn case_change<'a>() -> impl Parser<'a, Output = CaseChange> {
@@ -152,7 +166,7 @@ mod parser {
|seq| { Conditional(seq.1, None, Some(seq.4)) }
),
// Any text
- map(pattern(&TEXT), Text),
+ map(text(), Text),
)
}
@@ -245,12 +259,9 @@ mod parser {
)
}
- fn text<'a>() -> impl Parser<'a, Output = SnippetElement<'a>> {
- map(pattern(&TEXT), SnippetElement::Text)
- }
-
fn anything<'a>() -> impl Parser<'a, Output = SnippetElement<'a>> {
- choice!(tabstop(), placeholder(), choice(), variable(), text())
+ let text = map(text(), SnippetElement::Text);
+ choice!(tabstop(), placeholder(), choice(), variable(), text)
}
fn snippet<'a>() -> impl Parser<'a, Output = Snippet<'a>> {