Skip to content

Commit

Permalink
feat: add function snippet syntax
Browse files Browse the repository at this point in the history
This patch adds real snippet support to function parameter completion.
This means that one could potentially tab through each parameter in the
parameter list and add their associated values.

This patch is hilariously small and direct now that all the refactoring is
done.

Fixes #435
Fixes #431
  • Loading branch information
rockstar committed Jul 20, 2022
1 parent bafcdec commit 7d1055b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,15 +695,16 @@ pub fn complete_call_expr(

completion_params
.into_iter()
.map(|(name, typ)| {
.enumerate()
.map(|(index, (name, typ))| {
let insert_text = if let Some(trigger) = trigger {
if trigger == "(" {
format!("{}: ", name)
format!("{}: ${}", name, index+1)
} else {
format!(" {}: ", name)
format!(" {}: ${}", name, index+1)
}
} else {
format!("{}: ", name)
format!("{}: ${}", name, index+1)
};

lsp::CompletionItem {
Expand Down
93 changes: 90 additions & 3 deletions src/server/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2020,6 +2020,93 @@ ab = 10
assert_eq!(expected, labels);
}

/// Function parameter snippets are returned with the snippet syntax
/// for completion on a a function already written, e.g. from an
/// automated process.
#[test]
async fn test_function_completion_snippet() {
let fluxscript = r#"
from()
// ^"#;
let server = create_server();
open_file(&server, fluxscript.to_string(), None).await;

let params = lsp::CompletionParams {
text_document_position: lsp::TextDocumentPositionParams {
text_document: lsp::TextDocumentIdentifier {
uri: lsp::Url::parse("file:///home/user/file.flux")
.unwrap(),
},
position: position_of(&fluxscript),
},
work_done_progress_params: lsp::WorkDoneProgressParams {
work_done_token: None,
},
partial_result_params: lsp::PartialResultParams {
partial_result_token: None,
},
context: Some(lsp::CompletionContext {
trigger_kind: lsp::CompletionTriggerKind::INVOKED,
trigger_character: None,
}),
};

let result =
server.completion(params.clone()).await.unwrap().unwrap();

let items = match result {
lsp::CompletionResponse::List(l) => l.items,
_ => unreachable!(),
};

expect![[r#"
[
{
"label": "bucket",
"kind": 5,
"detail": "string",
"insertText": "bucket: $1",
"insertTextFormat": 2
},
{
"label": "bucketID",
"kind": 5,
"detail": "string",
"insertText": "bucketID: $2",
"insertTextFormat": 2
},
{
"label": "host",
"kind": 5,
"detail": "string",
"insertText": "host: $3",
"insertTextFormat": 2
},
{
"label": "org",
"kind": 5,
"detail": "string",
"insertText": "org: $4",
"insertTextFormat": 2
},
{
"label": "orgID",
"kind": 5,
"detail": "string",
"insertText": "orgID: $5",
"insertTextFormat": 2
},
{
"label": "token",
"kind": 5,
"detail": "string",
"insertText": "token: $6",
"insertTextFormat": 2
}
]"#]]
.assert_eq(&serde_json::to_string_pretty(&items).unwrap());
}

// XXX: rockstar (6 Jul 2022) - Is this test correct? I think it's not. It's
// saying that when there's a `now`, and you type `n` it should complete to
// literally everything? That makes no sense.
Expand Down Expand Up @@ -2257,21 +2344,21 @@ csv.from(
"label": "csv",
"kind": 5,
"detail": "string",
"insertText": "csv: ",
"insertText": "csv: $1",
"insertTextFormat": 2
},
{
"label": "file",
"kind": 5,
"detail": "string",
"insertText": "file: ",
"insertText": "file: $2",
"insertTextFormat": 2
},
{
"label": "mode",
"kind": 5,
"detail": "string",
"insertText": "mode: ",
"insertText": "mode: $3",
"insertTextFormat": 2
}
]
Expand Down

0 comments on commit 7d1055b

Please sign in to comment.