diff --git a/src/input.rs b/src/input.rs index 60dcbe6..e55ad2e 100644 --- a/src/input.rs +++ b/src/input.rs @@ -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( @@ -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 { diff --git a/src/lsp.rs b/src/lsp.rs index 6bf657b..932b402 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -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 @@ -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, diff --git a/src/rime.rs b/src/rime.rs index 30f6229..82914dd 100644 --- a/src/rime.rs +++ b/src/rime.rs @@ -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) {