Skip to content

Commit

Permalink
feat: add more lsp log. (#1616)
Browse files Browse the repository at this point in the history
feat: add more lsp log. fix temporary_workspace cache update

Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Aug 30, 2024
1 parent e337e12 commit b03dd40
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 17 deletions.
7 changes: 7 additions & 0 deletions kclvm/tools/src/LSP/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(crate) const RETRY_REQUEST: &str = "Retry Request";
pub(crate) enum LSPError {
Retry,
FileIdNotFound(VfsPath),
WorkSpaceIsEmpty(VfsPath),
AnalysisDatabaseNotFound(VfsPath),
}

Expand All @@ -24,6 +25,12 @@ impl fmt::Display for LSPError {
"Internal bug: Path {path} analysisDatabase not found, maybe compile failed"
)
}
LSPError::WorkSpaceIsEmpty(path) => {
write!(
f,
"Internal bug: Path {path} does not belong to any workspace"
)
}
}
}
}
Expand Down
43 changes: 28 additions & 15 deletions kclvm/tools/src/LSP/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,26 @@ impl LanguageServerSnapshot {
pub(crate) fn try_get_db(
&self,
path: &VfsPath,
sender: &Sender<Task>,
) -> anyhow::Result<Option<Arc<AnalysisDatabase>>> {
match self.try_get_db_state(path) {
Ok(db) => match db {
Some(db) => match db {
DBState::Ready(db) => Ok(Some(db.clone())),
DBState::Compiling(_) | DBState::Init => Ok(None),
DBState::Failed(e) => Err(anyhow::anyhow!(e)),
DBState::Compiling(_) | DBState::Init => {
log_message(
format!("Try get {:?} db state: In compiling, retry", path),
sender,
)?;
Ok(None)
}
DBState::Failed(e) => {
log_message(
format!("Try get {:?} db state: Failed: {:?}", path, e),
sender,
)?;
Err(anyhow::anyhow!(e))
}
},
None => Ok(None),
},
Expand Down Expand Up @@ -133,7 +146,7 @@ impl LanguageServerSnapshot {

None => {
if file_info.workspaces.is_empty() {
return Err(anyhow::anyhow!(LSPError::FileIdNotFound(
return Err(anyhow::anyhow!(LSPError::WorkSpaceIsEmpty(
path.clone()
)));
}
Expand Down Expand Up @@ -162,11 +175,11 @@ impl LanguageServerSnapshot {
pub(crate) fn handle_semantic_tokens_full(
snapshot: LanguageServerSnapshot,
params: lsp_types::SemanticTokensParams,
_sender: Sender<Task>,
sender: Sender<Task>,
) -> anyhow::Result<Option<SemanticTokensResult>> {
let file = file_path_from_url(&params.text_document.uri)?;
let path: VfsPath = from_lsp::abs_path(&params.text_document.uri)?.into();
let db = match snapshot.try_get_db(&path) {
let db = match snapshot.try_get_db(&path, &sender) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
Expand Down Expand Up @@ -244,7 +257,7 @@ pub(crate) fn handle_goto_definition(
if !snapshot.verify_request_path(&path.clone().into(), &sender) {
return Ok(None);
};
let db = match snapshot.try_get_db(&path.clone().into()) {
let db = match snapshot.try_get_db(&path.clone().into(), &sender) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
Expand Down Expand Up @@ -272,7 +285,7 @@ pub(crate) fn handle_reference(
if !snapshot.verify_request_path(&path.clone().into(), &sender) {
return Ok(None);
}
let db = match snapshot.try_get_db(&path.clone().into()) {
let db = match snapshot.try_get_db(&path.clone().into(), &sender) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
Expand Down Expand Up @@ -359,7 +372,7 @@ pub(crate) fn handle_hover(
if !snapshot.verify_request_path(&path.clone().into(), &sender) {
return Ok(None);
}
let db = match snapshot.try_get_db(&path.clone().into()) {
let db = match snapshot.try_get_db(&path.clone().into(), &sender) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
Expand All @@ -378,11 +391,11 @@ pub(crate) fn handle_hover(
pub(crate) fn handle_document_symbol(
snapshot: LanguageServerSnapshot,
params: lsp_types::DocumentSymbolParams,
_sender: Sender<Task>,
sender: Sender<Task>,
) -> anyhow::Result<Option<lsp_types::DocumentSymbolResponse>> {
let file = file_path_from_url(&params.text_document.uri)?;
let path = from_lsp::abs_path(&params.text_document.uri)?;
let db = match snapshot.try_get_db(&path.clone().into()) {
let db = match snapshot.try_get_db(&path.clone().into(), &sender) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
Expand Down Expand Up @@ -411,7 +424,7 @@ pub(crate) fn handle_rename(
if !snapshot.verify_request_path(&path.clone().into(), &sender) {
return Ok(None);
}
let db = match snapshot.try_get_db(&path.clone().into()) {
let db = match snapshot.try_get_db(&path.clone().into(), &sender) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
Expand Down Expand Up @@ -445,11 +458,11 @@ pub(crate) fn handle_rename(
pub(crate) fn handle_inlay_hint(
snapshot: LanguageServerSnapshot,
params: lsp_types::InlayHintParams,
_sender: Sender<Task>,
sender: Sender<Task>,
) -> anyhow::Result<Option<Vec<lsp_types::InlayHint>>> {
let file = file_path_from_url(&params.text_document.uri)?;
let path = from_lsp::abs_path(&params.text_document.uri)?;
let db = match snapshot.try_get_db(&path.clone().into()) {
let db = match snapshot.try_get_db(&path.clone().into(), &sender) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
Expand All @@ -463,13 +476,13 @@ pub(crate) fn handle_inlay_hint(
pub(crate) fn handle_signature_help(
snapshot: LanguageServerSnapshot,
params: lsp_types::SignatureHelpParams,
_sender: Sender<Task>,
sender: Sender<Task>,
) -> anyhow::Result<Option<lsp_types::SignatureHelp>> {
let file = file_path_from_url(&params.text_document_position_params.text_document.uri)?;
let pos = kcl_pos(&file, params.text_document_position_params.position);
let trigger_character = params.context.and_then(|ctx| ctx.trigger_character);
let path = from_lsp::abs_path(&params.text_document_position_params.text_document.uri)?;
let db = match snapshot.try_get_db(&path.clone().into()) {
let db = match snapshot.try_get_db(&path.clone().into(), &sender) {
Ok(option_db) => match option_db {
Some(db) => db,
None => return Err(anyhow!(LSPError::Retry)),
Expand Down
47 changes: 45 additions & 2 deletions kclvm/tools/src/LSP/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ impl LanguageServerState {
// open file
ChangeKind::Create => {
let filename = get_file_name(self.vfs.read(), file.file_id);
self.log_message(format!("Process changed file, open {:?}", filename));
match filename {
Ok(filename) => {
let uri = url_from_path(&filename).unwrap();
Expand Down Expand Up @@ -288,6 +289,7 @@ impl LanguageServerState {
// edit file
ChangeKind::Modify => {
let filename = get_file_name(self.vfs.read(), file.file_id);
self.log_message(format!("Process changed file, modify {:?}", filename));
match filename {
Ok(filename) => {
let opened_files = self.opened_files.read();
Expand Down Expand Up @@ -347,11 +349,25 @@ impl LanguageServerState {
}
// close file
ChangeKind::Delete => {
let filename = get_file_name(self.vfs.read(), file.file_id);
self.log_message(format!("Process changed file, close {:?}", filename));

let mut temporary_workspace = self.temporary_workspace.write();
if let Some(workspace) = temporary_workspace.remove(&file.file_id) {
let mut workspaces = self.analysis.workspaces.write();
if let Some(w) = workspace {
workspaces.remove(&w);
let mut contains = false;
let opened_file = self.opened_files.read();
for file_info in opened_file.values() {
if file_info.workspaces.contains(&w) {
contains = true;
break;
}
}
if !contains {
self.log_message(format!("Remove workspace {:?}", w));
workspaces.remove(&w);
}
}
}
}
Expand Down Expand Up @@ -531,8 +547,9 @@ impl LanguageServerState {

log_message(
format!(
"Compile workspace: {:?}, changed file: {:?}, use {:?} micros",
"Compile workspace: {:?}, main_pkg files: {:?}, changed file: {:?}, use {:?} micros",
workspace,
files,
filename,
start.elapsed().as_micros()
),
Expand Down Expand Up @@ -590,23 +607,49 @@ impl LanguageServerState {
match compile_res {
Ok((prog, gs)) => {
let mut workspaces = snapshot.workspaces.write();
log_message(
format!(
"Workspace {:?} compile success",workspace
),
&sender,
);
workspaces.insert(
workspace.clone(),
DBState::Ready(Arc::new(AnalysisDatabase { prog, gs, diags })),
);
drop(workspaces);
if temp && changed_file_id.is_some() {
let mut temporary_workspace = snapshot.temporary_workspace.write();

log_message(
format!(
"Insert file {:?} and workspace {:?} to temporary workspace", filename, workspace
),
&sender,
);
temporary_workspace
.insert(changed_file_id.unwrap(), Some(workspace.clone()));
drop(temporary_workspace);
}
}
Err(e) => {
let mut workspaces = snapshot.workspaces.write();
let mut temporary_workspace = snapshot.temporary_workspace.write();
log_message(
format!(
"Workspace {:?} compile failed: {:?}",workspace, e
),
&sender,
);
workspaces.insert(workspace, DBState::Failed(e.to_string()));
if temp && changed_file_id.is_some() {
let mut temporary_workspace = snapshot.temporary_workspace.write();
log_message(
format!(
"Reomve temporary workspace file id: {:?}",changed_file_id
),
&sender,
);
temporary_workspace.remove(&changed_file_id.unwrap());
drop(temporary_workspace);
}
Expand Down

0 comments on commit b03dd40

Please sign in to comment.