From fbd66af24421c39077f2f09f92c7f02fd8d61297 Mon Sep 17 00:00:00 2001 From: zilcH40 Date: Tue, 24 Dec 2024 17:01:38 +0800 Subject: [PATCH] chore: remove unnecessary format --- src/lsp.rs | 20 +++++++++++++------- src/utils.rs | 21 +++++++++++---------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/lsp.rs b/src/lsp.rs index 963501a..4e96495 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -186,9 +186,10 @@ impl Backend { .unwrap_or(0); let start_position = utils::offset_to_position(&rope, real_offset, encoding)?; let range = Range::new(start_position, position); - let filter_prefix = (self.config.read().await.long_filter_text).then_some( - utils::surrounding_word(&Cow::from(rope.slice(line_begin..real_offset))), - ); + let filter_prefix = (self.config.read().await.long_filter_text).then_some({ + let slice = &Cow::from(rope.slice(line_begin..real_offset)); + utils::surrounding_word(slice).to_string() + }); // TODO: Does compiler know the right time to drop the lock, // or it will wait until the end of this function? drop(rope); @@ -241,7 +242,9 @@ impl Backend { _ => text.clone(), }; if show_filter_text_in_label { - label += &format!(" ({})", filter_text); + label.push_str(" ("); + label.push_str(&filter_text); + label.push(')'); } let label_details = (!c.comment.is_empty()).then_some(CompletionItemLabelDetails { detail: Some(c.comment.clone()), @@ -301,7 +304,7 @@ impl LanguageServer for Backend { triggers.extend_from_slice(user_triggers); triggers }; - + // negotiate position encoding let encoding_options = params .capabilities .general @@ -420,8 +423,11 @@ impl LanguageServer for Backend { self.notify_work_begin(token.clone(), command).await; let mut config = self.config.write().await; config.enabled = !config.enabled; - let status = format!("Rime is {}", if config.enabled { "ON" } else { "OFF" }); - self.notify_work_done(token.clone(), &status).await; + let status = match config.enabled { + true => "Rime is ON", + false => "Rime is OFF", + }; + self.notify_work_done(token.clone(), status).await; // return a bool representing if rime-ls is enabled return Ok(Some(Value::from(config.enabled))); } diff --git a/src/utils.rs b/src/utils.rs index d9c8c4a..a41ecc4 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -44,7 +44,6 @@ pub fn position_to_offset(rope: &Rope, position: Position, encoding: Encoding) - Encoding::UTF16 => rope.line(line).try_utf16_cu_to_char(col).ok()?, Encoding::UTF32 => col, }; - //let col8 = rope.line(line).try_utf16_cu_to_char(col).ok()?; let offset = rope.try_line_to_char(line).ok()? + col_offset; Some(offset) }) @@ -135,9 +134,11 @@ fn char_is_word(ch: char) -> bool { ch.is_alphanumeric() || ch == '_' } -pub fn surrounding_word(s: &str) -> String { +#[inline] +pub fn surrounding_word(s: &str) -> &str { let end = s.len(); let mut start = end; + for ch in s.chars().rev() { if char_is_word(ch) { start -= ch.len_utf8(); @@ -145,16 +146,16 @@ pub fn surrounding_word(s: &str) -> String { break; } } - s[start..end].to_string() + &s[start..end] } #[test] fn test_surrounding_word() { - assert_eq!(surrounding_word(""), "".to_string()); - assert_eq!(surrounding_word(" "), "".to_string()); - assert_eq!(surrounding_word("hello_world"), "hello_world".to_string()); - assert_eq!(surrounding_word("hello world"), "world".to_string()); - assert_eq!(surrounding_word("汉字nihao"), "汉字nihao".to_string()); - assert_eq!(surrounding_word("汉,字nihao"), "字nihao".to_string()); - assert_eq!(surrounding_word("汉。字nihao"), "字nihao".to_string()); + assert_eq!(surrounding_word(""), ""); + assert_eq!(surrounding_word(" "), ""); + assert_eq!(surrounding_word("hello_world"), "hello_world"); + assert_eq!(surrounding_word("hello world"), "world"); + assert_eq!(surrounding_word("汉字nihao"), "汉字nihao"); + assert_eq!(surrounding_word("汉,字nihao"), "字nihao"); + assert_eq!(surrounding_word("汉。字nihao"), "字nihao"); }