Skip to content

Commit

Permalink
chore: remove unnecessary format
Browse files Browse the repository at this point in the history
  • Loading branch information
wlh320 committed Dec 24, 2024
1 parent 66ad811 commit fbd66af
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
20 changes: 13 additions & 7 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -301,7 +304,7 @@ impl LanguageServer for Backend {
triggers.extend_from_slice(user_triggers);
triggers
};

// negotiate position encoding
let encoding_options = params
.capabilities
.general
Expand Down Expand Up @@ -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)));
}
Expand Down
21 changes: 11 additions & 10 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -135,26 +134,28 @@ 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();
} else {
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");
}

0 comments on commit fbd66af

Please sign in to comment.