From 1bf75e560115f38c0961990b09c6c458bc190f6c Mon Sep 17 00:00:00 2001 From: he1pa <18012015693@163.com> Date: Tue, 28 Nov 2023 14:35:34 +0800 Subject: [PATCH] fix: fix unwrap() in request.rs and completion.rs Signed-off-by: he1pa <18012015693@163.com> --- kclvm/tools/src/LSP/src/completion.rs | 75 ++++++++++++++------------- kclvm/tools/src/LSP/src/request.rs | 29 ++++++----- kclvm/tools/src/LSP/src/state.rs | 75 +++++++++++++++------------ 3 files changed, 97 insertions(+), 82 deletions(-) diff --git a/kclvm/tools/src/LSP/src/completion.rs b/kclvm/tools/src/LSP/src/completion.rs index 583c145a8..f61c1493e 100644 --- a/kclvm/tools/src/LSP/src/completion.rs +++ b/kclvm/tools/src/LSP/src/completion.rs @@ -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 => {} @@ -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 = 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 = 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 { diff --git a/kclvm/tools/src/LSP/src/request.rs b/kclvm/tools/src/LSP/src/request.rs index 8038b6bdf..dccc0186c 100644 --- a/kclvm/tools/src/LSP/src/request.rs +++ b/kclvm/tools/src/LSP/src/request.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Ok}; +use anyhow::anyhow; use crossbeam_channel::Sender; use kclvm_config::modfile::KCL_FILE_SUFFIX; @@ -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())?; @@ -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. diff --git a/kclvm/tools/src/LSP/src/state.rs b/kclvm/tools/src/LSP/src/state.rs index ce6563015..8c00b5708 100644 --- a/kclvm/tools/src/LSP/src/state.rs +++ b/kclvm/tools/src/LSP/src/state.rs @@ -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::>(); + 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::>(); - sender.send(Task::Notify(lsp_server::Notification { - method: PublishDiagnostics::METHOD.to_owned(), - params: to_json(PublishDiagnosticsParams { - uri, - diagnostics, - version: None, - }) - .unwrap(), - })); } - Err(_) => {} } }); }