Skip to content

Commit

Permalink
fix: schema trigger on initial input
Browse files Browse the repository at this point in the history
refactor input handling, fix that schema cannot be triggered on initial input
  • Loading branch information
wlh320 committed Sep 13, 2024
1 parent 3b57baa commit 4c5f42a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
24 changes: 12 additions & 12 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ impl InputState {
}
}

pub fn handle_first_state(new_input: &Input) -> InputResult {
pub fn handle_first_input(new_input: &Input, schema_trigger: &str) -> InputResult {
let rime = Rime::global();
let session_id = rime.create_session();
Self::handle_new_typing(session_id, new_input)
if !schema_trigger.is_empty() && new_input.borrow_pinyin() == &schema_trigger {
Self::handle_schema(session_id)
} else {
Self::handle_new_typing(session_id, new_input)
}
}

pub fn handle_new_input(
Expand All @@ -116,17 +120,13 @@ impl InputState {
max_tokens: usize,
) -> InputResult {
let rime = Rime::global();
let session_id = rime.find_session(self.session_id);
// new typing
if self.offset != new_offset || self.session_id != session_id || !self.is_incomplete {
rime.clear_composition(session_id);
if !schema_trigger.is_empty() && new_input.borrow_pinyin() == &schema_trigger {
return Self::handle_schema(session_id);
} else {
return Self::handle_new_typing(session_id, new_input);
}
let has_session = rime.find_session(self.session_id);
// new typing (create new session)
if self.offset != new_offset || !has_session || !self.is_incomplete {
return Self::handle_first_input(new_input, schema_trigger);
}
// continue last typing
// continue last typing (with last session)
let session_id = self.session_id;
// handle pinyin
let diff_pinyin = diff(self.input.borrow_pinyin(), new_input.borrow_pinyin());
match diff_pinyin {
Expand Down
17 changes: 9 additions & 8 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,15 @@ impl Backend {
session_id,
raw_input,
} = match (*last_state).as_ref() {
Some(state) => state.handle_new_input(
new_offset,
&new_input,
&self.config.read().await.schema_trigger_character,
self.config.read().await.max_tokens,
),
None => InputState::handle_first_state(&new_input),
Some(state) => {
let schema_trigger = &self.config.read().await.schema_trigger_character;
let max_tokens = self.config.read().await.max_tokens;
state.handle_new_input(new_offset, &new_input, schema_trigger, max_tokens)
}
None => {
let schema_trigger = &self.config.read().await.schema_trigger_character;
InputState::handle_first_input(&new_input, schema_trigger)
}
};

// prevent deleting puncts before real pinyin input
Expand Down Expand Up @@ -224,7 +226,6 @@ impl Backend {
)
};
let order_to_sort_text = utils::build_order_to_sort_text(max_candidates);

let candidate_to_completion_item = |(i, c): (usize, Candidate)| -> CompletionItem {
let text = match is_selecting {
true => submitted.clone() + &c.text,
Expand Down
9 changes: 3 additions & 6 deletions src/rime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,10 @@ impl Rime {
rime_call!(api->create_session)
}

/// if session_id does not exist, create a new one
pub fn find_session(&self, session_id: usize) -> usize {
/// return if session exists
pub fn find_session(&self, session_id: usize) -> bool {
let api = Self::get_api();
match rime_call!(api->find_session, session_id) {
0 => rime_call!(api->create_session),
_ => session_id,
}
rime_call!(api->find_session, session_id) != 0
}

pub fn process_key(&self, session_id: usize, key: i32) {
Expand Down

0 comments on commit 4c5f42a

Please sign in to comment.