Skip to content

Commit

Permalink
fix: fix unwrap() in request.rs and completion.rs
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Nov 28, 2023
1 parent ab92612 commit 1bf75e5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 82 deletions.
75 changes: 39 additions & 36 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,28 +112,31 @@ pub(crate) fn completion(
Some(def) => {
let sema_info = def.get_sema_info();
let name = def.get_name();
let ty = sema_info.ty.clone().unwrap();
match symbol_ref.get_kind() {
kclvm_sema::core::symbol::SymbolKind::Schema => {
let schema_ty = ty.into_schema_type();
completions.insert(schema_ty_completion_item(&schema_ty));
}
kclvm_sema::core::symbol::SymbolKind::Package => {
completions.insert(KCLCompletionItem {
label: name,
detail: Some(ty.ty_str()),
documentation: sema_info.doc.clone(),
kind: Some(KCLCompletionItemKind::Module),
});
}
_ => {
completions.insert(KCLCompletionItem {
label: name,
detail: Some(ty.ty_str()),
documentation: sema_info.doc.clone(),
kind: None,
});
}
match &sema_info.ty {
Some(ty) => match symbol_ref.get_kind() {
kclvm_sema::core::symbol::SymbolKind::Schema => {
let schema_ty = ty.into_schema_type();
completions
.insert(schema_ty_completion_item(&schema_ty));
}
kclvm_sema::core::symbol::SymbolKind::Package => {
completions.insert(KCLCompletionItem {
label: name,
detail: Some(ty.ty_str()),
documentation: sema_info.doc.clone(),
kind: Some(KCLCompletionItemKind::Module),
});
}
_ => {
completions.insert(KCLCompletionItem {
label: name,
detail: Some(ty.ty_str()),
documentation: sema_info.doc.clone(),
kind: None,
});
}
},
None => {}
}
}
None => {}
Expand Down Expand Up @@ -584,27 +587,27 @@ mod tests {
let (file, program, _, _, gs) =
compile_test_file("src/test_data/completion_test/dot/completion.k");

// // test completion for var
// let pos = KCLPos {
// filename: file.to_owned(),
// line: 26,
// column: Some(1),
// };
// test completion for var
let pos = KCLPos {
filename: file.to_owned(),
line: 26,
column: Some(1),
};

// let got = completion(None, &program, &pos, &gs).unwrap();
// let mut got_labels: Vec<String> = match got {
// CompletionResponse::Array(arr) => arr.iter().map(|item| item.label.clone()).collect(),
// CompletionResponse::List(_) => panic!("test failed"),
// };
let got = completion(None, &program, &pos, &gs).unwrap();
let mut got_labels: Vec<String> = match got {
CompletionResponse::Array(arr) => arr.iter().map(|item| item.label.clone()).collect(),
CompletionResponse::List(_) => panic!("test failed"),
};

let mut expected_labels: Vec<&str> = vec![
"", // generate from error recovery of "pkg."
"subpkg", "math", "Person{}", "P{}", "p", "p1", "p2", "p3", "p4", "aaaa",
];
// got_labels.sort();
// expected_labels.sort();
got_labels.sort();
expected_labels.sort();

// assert_eq!(got_labels, expected_labels);
assert_eq!(got_labels, expected_labels);

// test completion for schema attr
let pos = KCLPos {
Expand Down
29 changes: 16 additions & 13 deletions kclvm/tools/src/LSP/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Ok};
use anyhow::anyhow;
use crossbeam_channel::Sender;

use kclvm_config::modfile::KCL_FILE_SUFFIX;
Expand Down Expand Up @@ -212,15 +212,16 @@ pub(crate) fn handle_completion(
// Some trigger characters need to re-compile
Some(ch) => match ch {
'=' | ':' => {
let db = parse_param_and_compile(
match parse_param_and_compile(
Param {
file: file.clone(),
module_cache: snapshot.module_cache.clone(),
},
Some(snapshot.vfs.clone()),
)
.unwrap();
(db.0, db.3)
) {
Ok((prog, _, _, gs)) => (prog, gs),
Err(_) => return Ok(None),
}
}
_ => {
let db = snapshot.get_db(&path.clone().into())?;
Expand Down Expand Up @@ -276,20 +277,22 @@ pub(crate) fn handle_document_symbol(
return Ok(None);
}

let db = parse_param_and_compile(
match parse_param_and_compile(
Param {
file: file.clone(),
module_cache: snapshot.module_cache.clone(),
},
Some(snapshot.vfs.clone()),
)
.unwrap();

let res = document_symbol(&file, &db.3);
if res.is_none() {
log_message(format!("File {file} Document symbol not found"), &sender)?;
) {
Ok((_, _, _, gs)) => {
let res = document_symbol(&file, &gs);
if res.is_none() {
log_message(format!("File {file} Document symbol not found"), &sender)?;
}
Ok(res)
}
Err(_) => return Ok(None),
}
Ok(res)
}

/// Called when a `textDocument/rename` request was received.
Expand Down
75 changes: 42 additions & 33 deletions kclvm/tools/src/LSP/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,46 +199,55 @@ impl LanguageServerState {
let start = Instant::now();
match get_file_name(vfs, file.file_id) {
Ok(filename) => {
let mut snapshot = self.snapshot();

self.thread_pool.execute({
let mut snapshot = self.snapshot();
let sender = self.task_sender.clone();
let module_cache = self.module_cache.clone();
let uri = url(&snapshot, file.file_id).unwrap();
move || match parse_param_and_compile(
Param {
file: filename.clone(),
module_cache,
},
Some(snapshot.vfs),
) {
Ok((prog, _, diags, gs)) => {
let mut db = snapshot.db.write();
db.insert(
file.file_id,
AnalysisDatabase {
prog,
diags: diags.clone(),
gs,
move || match url(&snapshot, file.file_id) {
Ok(uri) => {
match parse_param_and_compile(
Param {
file: filename.clone(),
module_cache,
},
Some(snapshot.vfs),
) {
Ok((prog, _, diags, gs)) => {
let mut db = snapshot.db.write();
db.insert(
file.file_id,
AnalysisDatabase {
prog,
diags: diags.clone(),
gs,
},
);

let diagnostics = diags
.iter()
.flat_map(|diag| {
kcl_diag_to_lsp_diags(diag, filename.as_str())
})
.collect::<Vec<Diagnostic>>();
sender.send(Task::Notify(lsp_server::Notification {
method: PublishDiagnostics::METHOD.to_owned(),
params: to_json(PublishDiagnosticsParams {
uri,
diagnostics,
version: None,
})
.unwrap(),
}));
}
Err(_) => {}
}
}
Err(_) => {
log_message(
format!("Interal bug: not a valid file:{:?}", filename),
&sender,
);

let diagnostics = diags
.iter()
.flat_map(|diag| kcl_diag_to_lsp_diags(diag, filename.as_str()))
.collect::<Vec<Diagnostic>>();
sender.send(Task::Notify(lsp_server::Notification {
method: PublishDiagnostics::METHOD.to_owned(),
params: to_json(PublishDiagnosticsParams {
uri,
diagnostics,
version: None,
})
.unwrap(),
}));
}
Err(_) => {}
}
});
}
Expand Down

0 comments on commit 1bf75e5

Please sign in to comment.