Skip to content

Commit

Permalink
fix bug and make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
wlh320 committed Dec 23, 2024
1 parent 47173de commit 072ea2a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Backend {
async fn get_completions(&self, uri: Url, position: Position) -> Option<CompletionList> {
// get new input
let rope = self.documents.get(uri.as_str())?;
let encoding = self.encoding.read().await.clone();
let encoding = *self.encoding.read().await;
let line_begin = {
let line_pos = Position::new(position.line, 0);
utils::position_to_offset(&rope, line_pos, encoding)?
Expand Down Expand Up @@ -184,7 +184,8 @@ impl Backend {
.and_then(utils::option_string)
.and_then(|rime_raw_input| new_input.borrow_pinyin().rfind(&rime_raw_input))
.unwrap_or(0);
let range = Range::new(utils::offset_to_position(&rope, real_offset)?, position);
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))),
);
Expand Down Expand Up @@ -356,7 +357,7 @@ impl LanguageServer for Backend {
}

async fn did_change(&self, params: DidChangeTextDocumentParams) {
let encoding = self.encoding.read().await.clone();
let encoding = *self.encoding.read().await;
let url = params.text_document.uri;
if let Some(mut rope) = self.documents.get_mut(url.as_str()) {
for change in params.content_changes {
Expand Down
15 changes: 5 additions & 10 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use tower_lsp::lsp_types::{Position, PositionEncodingKind};

use crate::consts::AUTO_TRIGGER_RE;

#[derive(Clone, Copy)]
#[derive(Default, Debug, Clone, Copy)]
pub enum Encoding {
UTF8,
#[default]
UTF16,
UTF32,
}
Expand All @@ -21,22 +22,16 @@ impl Encoding {
}
}

impl Default for Encoding {
fn default() -> Self {
Encoding::UTF16
}
}

pub fn select_encoding(options: Option<Vec<PositionEncodingKind>>) -> Encoding {
match options {
// prefer utf-32 because of no conversion cost
// we prefer utf-32 here because there are no conversion costs
Some(v) if v.contains(&PositionEncodingKind::new("utf-32")) => Encoding::UTF32,
Some(v) if v.contains(&PositionEncodingKind::new("utf-8")) => Encoding::UTF8,
_ => Encoding::default(),
}
}

/// UTF-16 Position -> char index
/// UTF-8/16/32 Position -> char index
pub fn position_to_offset(rope: &Rope, position: Position, encoding: Encoding) -> Option<usize> {
let (line, col) = (position.line as usize, position.character as usize);
// position is at the end of rope
Expand All @@ -55,7 +50,7 @@ pub fn position_to_offset(rope: &Rope, position: Position, encoding: Encoding) -
})
}

/// char index -> UTF-16 Position
/// char index -> UTF-8/16/32 Position
pub fn offset_to_position(rope: &Rope, offset: usize, encoding: Encoding) -> Option<Position> {
let line = rope.try_char_to_line(offset).ok()?;
let col_offset = offset - rope.try_line_to_char(line).ok()?;
Expand Down

0 comments on commit 072ea2a

Please sign in to comment.