Skip to content

Commit

Permalink
fix: session management issue
Browse files Browse the repository at this point in the history
  • Loading branch information
wlh320 committed Feb 27, 2023
1 parent 889288b commit ed165d4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
27 changes: 14 additions & 13 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,37 +80,38 @@ impl InputState {

pub fn handle_new_input(&self, new_offset: usize, new_input: &Input) -> InputResult {
let rime = Rime::global();
let session_id = rime.find_session(self.session_id);
// new typing
if self.offset != new_offset {
rime.clear_composition(self.session_id);
return Self::handle_new_typing(self.session_id, new_input);
if self.offset != new_offset || self.session_id != session_id {
rime.clear_composition(session_id);
return Self::handle_new_typing(session_id, new_input);
}
// continue last typing
// handle pinyin
let diff_pinyin = diff(self.input.borrow_pinyin(), new_input.borrow_pinyin());
match diff_pinyin {
DiffResult::Add(suffix) => rime.process_str(self.session_id, suffix),
DiffResult::Delete(suffix) => rime.delete_keys(self.session_id, suffix.len()),
DiffResult::Add(suffix) => rime.process_str(session_id, suffix),
DiffResult::Delete(suffix) => rime.delete_keys(session_id, suffix.len()),
DiffResult::New => {
rime.clear_composition(self.session_id);
rime.process_str(self.session_id, new_input.borrow_pinyin());
rime.clear_composition(session_id);
rime.process_str(session_id, new_input.borrow_pinyin());
}
_ => (),
}
let raw_input = rime.get_raw_input(self.session_id);
let raw_input = rime.get_raw_input(session_id);
// handle select
let diff_select = diff(self.input.borrow_select(), new_input.borrow_select());
match diff_select {
DiffResult::Add(suffix) => rime.process_str(self.session_id, suffix),
DiffResult::Delete(suffix) => rime.delete_keys(self.session_id, suffix.len()),
DiffResult::Add(suffix) => rime.process_str(session_id, suffix),
DiffResult::Delete(suffix) => rime.delete_keys(session_id, suffix.len()),
DiffResult::New => {
rime.delete_keys(self.session_id, self.input.borrow_select().len());
rime.process_str(self.session_id, new_input.borrow_select());
rime.delete_keys(session_id, self.input.borrow_select().len());
rime.process_str(session_id, new_input.borrow_select());
}
_ => (),
}
InputResult {
session_id: self.session_id,
session_id,
raw_input,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl Backend {
let RimeResponse {
submitted,
candidates,
} = match rime.get_response_from_session(session_id, max_candidates) {
} = match rime.get_response_from_session(session_id) {
Ok(r) => r,
Err(e) => {
self.client.log_message(MessageType::ERROR, e).await;
Expand Down
31 changes: 17 additions & 14 deletions src/rime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::consts::{K_BACKSPACE, RAW_RE};
use librime_sys as librime;
use once_cell::sync::OnceCell;
use std::ffi::{CStr, CString, NulError};
use std::sync::RwLock;
use std::sync::Mutex;
use thiserror::Error;

macro_rules! rime_struct_init {
Expand Down Expand Up @@ -44,7 +44,7 @@ pub enum RimeError {

#[derive(Debug)]
pub struct RimeResponse {
/// submitted input
/// partially submitted input
pub submitted: String,
/// list of candidate provided by rime
pub candidates: Vec<Candidate>,
Expand Down Expand Up @@ -128,7 +128,7 @@ impl Rime {
&self,
context: &librime::RimeContext,
) -> Result<Vec<Candidate>, RimeError> {
let res = RwLock::new(Vec::new());
let res = Mutex::new(Vec::new());
for i in 0..context.menu.num_candidates {
let candidate = unsafe { *context.menu.candidates.offset(i as isize) };
let text = unsafe {
Expand All @@ -145,8 +145,8 @@ impl Rime {
})
.unwrap_or_default()
};
let order = res.read().unwrap().len() + 1;
res.write().unwrap().push(Candidate {
let order = (i + 1) as usize;
res.lock().unwrap().push(Candidate {
text,
comment,
order,
Expand Down Expand Up @@ -193,11 +193,7 @@ impl Rime {
}
}

pub fn get_response_from_session(
&self,
session_id: usize,
_max_candidates: usize,
) -> Result<RimeResponse, RimeError> {
pub fn get_response_from_session(&self, session_id: usize) -> Result<RimeResponse, RimeError> {
unsafe {
if librime::RimeFindSession(session_id) == 0 {
return Err(RimeError::SessionNotFound(session_id));
Expand Down Expand Up @@ -236,6 +232,16 @@ impl Rime {
unsafe { librime::RimeCreateSession() }
}

/// if session_id does not exist, create a new one
pub fn find_session(&self, session_id: usize) -> usize {
unsafe {
match librime::RimeFindSession(session_id) {
0 => librime::RimeCreateSession(),
_ => session_id,
}
}
}

pub fn process_key(&self, session_id: usize, key: i32) {
unsafe {
if librime::RimeFindSession(session_id) != 0 {
Expand Down Expand Up @@ -290,15 +296,12 @@ fn test_get_candidates() {
Rime::init(shared_data_dir, user_data_dir, log_dir).unwrap();
let rime = Rime::global();
// simulate typing
let max_candidates = 10;
let keys = vec![b'w', b'l', b'h'];
let session_id = rime.create_session();
for key in keys {
rime.process_key(session_id, key as i32);
}
let res = rime
.get_response_from_session(session_id, max_candidates)
.unwrap();
let res = rime.get_response_from_session(session_id).unwrap();
assert!(res.candidates.len() != 0);
rime.destroy_session(session_id);

Expand Down

0 comments on commit ed165d4

Please sign in to comment.