Code completions for @settings (#5622)

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-03-05 16:23:46 +13:00
committed by GitHub
parent de85c31e71
commit e8af61e11f
5 changed files with 69 additions and 37 deletions

View File

@ -1247,13 +1247,13 @@ impl LanguageServer for Backend {
};
let position = position_to_char_index(params.text_document_position.position, current_code);
if ast.ast.get_non_code_meta_for_position(position).is_some() {
if ast.ast.in_comment(position) {
// If we are in a code comment we don't want to show completions.
return Ok(None);
}
// Get the completion items for the ast.
let Ok(variables) = ast.ast.completion_items() else {
let Ok(variables) = ast.ast.completion_items(position) else {
return Ok(Some(CompletionResponse::Array(completions)));
};

View File

@ -644,7 +644,10 @@ async fn test_kcl_lsp_completions() {
uri: "file:///test.kcl".try_into().unwrap(),
language_id: "kcl".to_string(),
version: 1,
text: r#"thing= 1
// Blank lines to check that we get completions even in an AST newline thing.
text: r#"
thing= 1
st"#
.to_string(),
},
@ -658,7 +661,7 @@ st"#
text_document: tower_lsp::lsp_types::TextDocumentIdentifier {
uri: "file:///test.kcl".try_into().unwrap(),
},
position: tower_lsp::lsp_types::Position { line: 0, character: 16 },
position: tower_lsp::lsp_types::Position { line: 0, character: 0 },
},
context: None,
partial_result_params: Default::default(),
@ -671,6 +674,7 @@ st"#
// Check the completions.
if let tower_lsp::lsp_types::CompletionResponse::Array(completions) = completions {
assert!(completions.len() > 10);
assert!(completions.iter().any(|c| c.label == "@settings"));
} else {
panic!("Expected array of completions");
}