fix const completion (#2192)

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2024-04-22 14:53:49 -07:00
committed by GitHub
parent 03fcb73aca
commit c461db5f54
2 changed files with 54 additions and 2 deletions

View File

@ -736,10 +736,12 @@ pub fn completion_item_from_enum_schema(
anyhow::bail!("expected at least one enum value: {:#?}", o); anyhow::bail!("expected at least one enum value: {:#?}", o);
} }
let label = enum_values[0].to_string(); let serde_json::Value::String(ref enum_value) = enum_values[0] else {
anyhow::bail!("expected string enum value: {:#?}", enum_values[0]);
};
Ok(CompletionItem { Ok(CompletionItem {
label, label: enum_value.to_string(),
label_details: None, label_details: None,
kind: Some(kind), kind: Some(kind),
detail: Some(description.to_string()), detail: Some(description.to_string()),

View File

@ -793,6 +793,56 @@ st"#
} }
} }
#[tokio::test(flavor = "multi_thread")]
async fn test_kcl_lsp_completions_const_raw() {
let server = kcl_lsp_server(false).await.unwrap();
// Send open file.
server
.did_open(tower_lsp::lsp_types::DidOpenTextDocumentParams {
text_document: tower_lsp::lsp_types::TextDocumentItem {
uri: "file:///test.kcl".try_into().unwrap(),
language_id: "kcl".to_string(),
version: 1,
text: r#"con"#.to_string(),
},
})
.await;
// Send completion request.
let completions = server
.completion(tower_lsp::lsp_types::CompletionParams {
text_document_position: tower_lsp::lsp_types::TextDocumentPositionParams {
text_document: tower_lsp::lsp_types::TextDocumentIdentifier {
uri: "file:///test.kcl".try_into().unwrap(),
},
position: tower_lsp::lsp_types::Position { line: 0, character: 2 },
},
context: None,
partial_result_params: Default::default(),
work_done_progress_params: Default::default(),
})
.await
.unwrap()
.unwrap();
// Check the completions.
if let tower_lsp::lsp_types::CompletionResponse::Array(completions) = completions {
assert!(completions.len() > 10);
// Find the one with label "const".
let const_completion = completions
.iter()
.find(|completion| completion.label == "const")
.unwrap();
assert_eq!(
const_completion.kind,
Some(tower_lsp::lsp_types::CompletionItemKind::KEYWORD)
);
} else {
panic!("Expected array of completions");
}
}
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn test_kcl_lsp_on_hover() { async fn test_kcl_lsp_on_hover() {
let server = kcl_lsp_server(false).await.unwrap(); let server = kcl_lsp_server(false).await.unwrap();