Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix lsp request get db with lock #1554

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions kclvm/tools/src/LSP/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,6 @@ impl LanguageServerSnapshot {
valid
}

pub(crate) fn get_db(&self, path: &VfsPath) -> anyhow::Result<Arc<AnalysisDatabase>> {
let file_id = self.vfs.read().file_id(path);
match file_id {
Some(id) => match self.db.read().get(&id) {
Some(db) => match db {
Some(db) => Ok(Arc::clone(db)),
None => Err(anyhow!(LSPError::Retry)),
},
None => Err(anyhow::anyhow!(LSPError::AnalysisDatabaseNotFound(
path.clone()
))),
},
None => Err(anyhow::anyhow!(LSPError::FileIdNotFound(path.clone()))),
}
}

/// Attempts to get db in cache, this function does not block.
/// db.contains(file_id) && db.get(file_id).is_some() -> Compile completed
/// db.contains(file_id) && db.get(file_id).is_none() -> In compiling, retry to wait compile completed
Expand Down Expand Up @@ -252,8 +236,11 @@ pub(crate) fn handle_goto_definition(
if !snapshot.verify_request_path(&path.clone().into(), &sender) {
return Ok(None);
}
let db = match snapshot.get_db(&path.clone().into()) {
Ok(db) => db,
let db = match snapshot.try_get_db(&path.clone().into()) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
},
Err(_) => return Ok(None),
};
let kcl_pos = kcl_pos(&file, params.text_document_position_params.position);
Expand All @@ -278,8 +265,11 @@ pub(crate) fn handle_reference(
if !snapshot.verify_request_path(&path.clone().into(), &sender) {
return Ok(None);
}
let db = match snapshot.get_db(&path.clone().into()) {
Ok(db) => db,
let db = match snapshot.try_get_db(&path.clone().into()) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
},
Err(_) => return Ok(None),
};
let pos = kcl_pos(&file, params.text_document_position.position);
Expand Down Expand Up @@ -315,8 +305,11 @@ pub(crate) fn handle_completion(
return Ok(None);
}

let db = match snapshot.get_db(&path.clone().into()) {
Ok(db) => db,
let db = match snapshot.try_get_db(&path.clone().into()) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
},
Err(_) => return Ok(None),
};

Expand Down Expand Up @@ -367,8 +360,11 @@ pub(crate) fn handle_hover(
if !snapshot.verify_request_path(&path.clone().into(), &sender) {
return Ok(None);
}
let db = match snapshot.get_db(&path.clone().into()) {
Ok(db) => db,
let db = match snapshot.try_get_db(&path.clone().into()) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
},
Err(_) => return Ok(None),
};
let kcl_pos = kcl_pos(&file, params.text_document_position_params.position);
Expand Down Expand Up @@ -421,8 +417,11 @@ pub(crate) fn handle_rename(
if !snapshot.verify_request_path(&path.clone().into(), &sender) {
return Ok(None);
}
let db = match snapshot.get_db(&path.clone().into()) {
Ok(db) => db,
let db = match snapshot.try_get_db(&path.clone().into()) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
},
Err(_) => return Ok(None),
};
let kcl_pos = kcl_pos(&file, params.text_document_position.position);
Expand Down
Loading