From 9feca80b08d7d69afd550edac9562ba08f27c0eb Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 17 May 2021 21:44:41 +0200 Subject: [PATCH 1/4] Make HighlightConfiguration::configure use into iterator instead of a Vec --- cli/loader/src/lib.rs | 2 +- cli/src/tests/helpers/fixtures.rs | 2 +- highlight/src/c_lib.rs | 2 +- highlight/src/lib.rs | 10 +++++++--- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cli/loader/src/lib.rs b/cli/loader/src/lib.rs index 4e9af25f..3efa74b0 100644 --- a/cli/loader/src/lib.rs +++ b/cli/loader/src/lib.rs @@ -661,7 +661,7 @@ impl<'a> LanguageConfiguration<'a> { } } } - result.configure(&all_highlight_names); + result.configure(&all_highlight_names.as_slice()); Ok(Some(result)) } }) diff --git a/cli/src/tests/helpers/fixtures.rs b/cli/src/tests/helpers/fixtures.rs index 854f7e67..91c4956a 100644 --- a/cli/src/tests/helpers/fixtures.rs +++ b/cli/src/tests/helpers/fixtures.rs @@ -50,7 +50,7 @@ pub fn get_highlight_config( &locals_query, ) .unwrap(); - result.configure(highlight_names); + result.configure(&highlight_names); result } diff --git a/highlight/src/c_lib.rs b/highlight/src/c_lib.rs index 334b8db0..d48a180c 100644 --- a/highlight/src/c_lib.rs +++ b/highlight/src/c_lib.rs @@ -112,7 +112,7 @@ pub extern "C" fn ts_highlighter_add_language( let mut config = HighlightConfiguration::new(language, highlight_query, injection_query, locals_query) .or(Err(ErrorCode::InvalidQuery))?; - config.configure(&this.highlight_names); + config.configure(&this.highlight_names.as_slice()); this.languages.insert(scope_name, (injection_regex, config)); Ok(()) diff --git a/highlight/src/lib.rs b/highlight/src/lib.rs index a0cfc34e..c8cdc1aa 100644 --- a/highlight/src/lib.rs +++ b/highlight/src/lib.rs @@ -293,7 +293,11 @@ impl HighlightConfiguration { /// /// When highlighting, results are returned as `Highlight` values, which contain the index /// of the matched highlight this list of highlight names. - pub fn configure(&mut self, recognized_names: &[String]) { + pub fn configure<'a, I, J>(&mut self, recognized_names: &I) + where + I: IntoIterator + Copy, + J: AsRef + 'a, + { let mut capture_parts = Vec::new(); self.highlight_indices.clear(); self.highlight_indices @@ -303,10 +307,10 @@ impl HighlightConfiguration { let mut best_index = None; let mut best_match_len = 0; - for (i, recognized_name) in recognized_names.iter().enumerate() { + for (i, recognized_name) in recognized_names.into_iter().enumerate() { let mut len = 0; let mut matches = true; - for part in recognized_name.split('.') { + for part in recognized_name.as_ref().split('.') { len += 1; if !capture_parts.contains(&part) { matches = false; From 005eb12cfebdb2cd8f11708f5e6c38934841566c Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 17 May 2021 21:54:16 +0200 Subject: [PATCH 2/4] update readme accordingly --- highlight/README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/highlight/README.md b/highlight/README.md index 89b19d7f..c04c7c08 100644 --- a/highlight/README.md +++ b/highlight/README.md @@ -18,7 +18,7 @@ tree-sitter-javascript = "0.19" Define the list of highlight names that you will recognize: ```rust -let highlight_names : Vec = [ +let highlight_names = &[ "attribute", "constant", "function.builtin", @@ -37,11 +37,7 @@ let highlight_names : Vec = [ "variable", "variable.builtin", "variable.parameter", -] -.iter() -.cloned() -.map(String::from) -.collect(); +]; ``` Create a highlighter. You need one of these for each thread that you're using for syntax highlighting: From 652dec950ceeb89603bca4c0315fa0c2bb6b650e Mon Sep 17 00:00:00 2001 From: Edgar Date: Thu, 20 May 2021 21:54:36 +0200 Subject: [PATCH 3/4] use impl instead of intoiterator --- highlight/src/lib.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/highlight/src/lib.rs b/highlight/src/lib.rs index c8cdc1aa..ae8f7b88 100644 --- a/highlight/src/lib.rs +++ b/highlight/src/lib.rs @@ -293,10 +293,7 @@ impl HighlightConfiguration { /// /// When highlighting, results are returned as `Highlight` values, which contain the index /// of the matched highlight this list of highlight names. - pub fn configure<'a, I, J>(&mut self, recognized_names: &I) - where - I: IntoIterator + Copy, - J: AsRef + 'a, + pub fn configure(&mut self, recognized_names: &[impl AsRef]) { let mut capture_parts = Vec::new(); self.highlight_indices.clear(); From cd9307b597831297daadf8bc159a9d61c5ee9f23 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Thu, 17 Jun 2021 09:17:13 +0200 Subject: [PATCH 4/4] format --- highlight/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/highlight/src/lib.rs b/highlight/src/lib.rs index ae8f7b88..b7bfeba8 100644 --- a/highlight/src/lib.rs +++ b/highlight/src/lib.rs @@ -293,8 +293,7 @@ impl HighlightConfiguration { /// /// When highlighting, results are returned as `Highlight` values, which contain the index /// of the matched highlight this list of highlight names. - pub fn configure(&mut self, recognized_names: &[impl AsRef]) - { + pub fn configure(&mut self, recognized_names: &[impl AsRef]) { let mut capture_parts = Vec::new(); self.highlight_indices.clear(); self.highlight_indices