Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: LSP completion function detail #5993

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions tooling/lsp/src/requests/completion/completion_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,14 @@ impl<'a> NodeFinder<'a> {
let name = if self_prefix { format!("self.{}", name) } else { name.clone() };
let name = &name;
let description = func_meta_type_to_string(func_meta, func_self_type.is_some());
let mut has_arguments = false;

let completion_item = match function_completion_kind {
FunctionCompletionKind::Name => {
simple_completion_item(name, CompletionItemKind::FUNCTION, Some(description))
}
FunctionCompletionKind::Name => simple_completion_item(
name,
CompletionItemKind::FUNCTION,
Some(description.clone()),
),
FunctionCompletionKind::NameAndParameters => {
let kind = CompletionItemKind::FUNCTION;
let skip_first_argument = attribute_first_type.is_some();
Expand All @@ -227,20 +230,25 @@ impl<'a> NodeFinder<'a> {
function_kind,
skip_first_argument,
);
let (label, insert_text) = if insert_text.ends_with("()") {
if skip_first_argument {
(name.to_string(), insert_text.strip_suffix("()").unwrap().to_string())
} else {
(format!("{}()", name), insert_text)
}
} else {
(format!("{}(…)", name), insert_text)
};

snippet_completion_item(label, kind, insert_text, Some(description))
if insert_text.ends_with("()") {
let label =
if skip_first_argument { name.to_string() } else { format!("{}()", name) };
simple_completion_item(label, kind, Some(description.clone()))
} else {
has_arguments = true;
snippet_completion_item(
format!("{}(…)", name),
kind,
insert_text,
Some(description.clone()),
)
}
}
};

let completion_item = completion_item_with_detail(completion_item, description);

let completion_item = if is_operator {
completion_item_with_sort_text(completion_item, operator_sort_text())
} else if function_kind == FunctionKind::Any && name == "new" {
Expand All @@ -254,11 +262,16 @@ impl<'a> NodeFinder<'a> {
let completion_item = match function_completion_kind {
FunctionCompletionKind::Name => completion_item,
FunctionCompletionKind::NameAndParameters => {
completion_item_with_trigger_parameter_hints_command(completion_item)
if has_arguments {
completion_item_with_trigger_parameter_hints_command(completion_item)
} else {
completion_item
}
}
};
let completion_item =
self.completion_item_with_doc_comments(ReferenceId::Function(func_id), completion_item);

Some(completion_item)
}

Expand Down Expand Up @@ -494,3 +507,10 @@ pub(super) fn completion_item_with_trigger_parameter_hints_command(
..completion_item
}
}

pub(super) fn completion_item_with_detail(
completion_item: CompletionItem,
detail: String,
) -> CompletionItem {
CompletionItem { detail: Some(detail), ..completion_item }
}
71 changes: 25 additions & 46 deletions tooling/lsp/src/requests/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
requests::{
completion::{
completion_items::{
completion_item_with_sort_text,
completion_item_with_detail, completion_item_with_sort_text,
completion_item_with_trigger_parameter_hints_command, module_completion_item,
simple_completion_item, snippet_completion_item,
},
Expand Down Expand Up @@ -107,12 +107,22 @@
insert_text: impl Into<String>,
description: impl Into<String>,
) -> CompletionItem {
completion_item_with_trigger_parameter_hints_command(snippet_completion_item(
label,
CompletionItemKind::FUNCTION,
insert_text,
Some(description.into()),
))
let insert_text: String = insert_text.into();
let description: String = description.into();

let has_arguments = insert_text.ends_with(')') && !insert_text.ends_with("()");
let completion_item = if has_arguments {
completion_item_with_trigger_parameter_hints_command(snippet_completion_item(
label,
CompletionItemKind::FUNCTION,
insert_text,
Some(description.clone()),
))
} else {
simple_completion_item(label, CompletionItemKind::FUNCTION, Some(description.clone()))
};

completion_item_with_detail(completion_item, description)
}

fn field_completion_item(field: &str, typ: impl Into<String>) -> CompletionItem {
Expand All @@ -122,14 +132,14 @@
#[test]
async fn test_use_first_segment() {
let src = r#"
mod foobaz {}

Check warning on line 135 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foobaz)
mod foobar {}
use foob>|<

Check warning on line 137 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
"#;

assert_completion(
src,
vec![module_completion_item("foobaz"), module_completion_item("foobar")],

Check warning on line 142 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foobaz)
)
.await;
}
Expand Down Expand Up @@ -191,15 +201,8 @@
use foo::>|<
"#;

assert_completion(
src,
vec![simple_completion_item(
"bar",
CompletionItemKind::FUNCTION,
Some("fn(i32) -> u64".to_string()),
)],
)
.await;
assert_completion(src, vec![function_completion_item("bar", "bar()", "fn(i32) -> u64")])
.await;
}

#[test]
Expand Down Expand Up @@ -299,7 +302,7 @@
mod bar {
mod something {}

use super::foob>|<

Check warning on line 305 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
}
"#;

Expand Down Expand Up @@ -1541,7 +1544,7 @@
async fn test_auto_import_suggests_modules_too() {
let src = r#"
mod foo {
mod barbaz {

Check warning on line 1547 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)
fn hello_world() {}
}
}
Expand All @@ -1554,11 +1557,11 @@
assert_eq!(items.len(), 1);

let item = &items[0];
assert_eq!(item.label, "barbaz");

Check warning on line 1560 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)
assert_eq!(
item.label_details,
Some(CompletionItemLabelDetails {
detail: Some("(use foo::barbaz)".to_string()),

Check warning on line 1564 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)
description: None
})
);
Expand Down Expand Up @@ -1665,18 +1668,14 @@
async fn test_completes_after_first_letter_of_path() {
let src = r#"
fn main() {
h>|<ello();

Check warning on line 1671 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (ello)
}

fn hello_world() {}
"#;
assert_completion_excluding_auto_import(
src,
vec![simple_completion_item(
"hello_world",
CompletionItemKind::FUNCTION,
Some("fn()".to_string()),
)],
vec![function_completion_item("hello_world", "hello_world()", "fn()")],
)
.await;
}
Expand All @@ -1694,11 +1693,7 @@
"#;
assert_completion_excluding_auto_import(
src,
vec![simple_completion_item(
"bar",
CompletionItemKind::FUNCTION,
Some("fn()".to_string()),
)],
vec![function_completion_item("bar", "bar()", "fn()")],
)
.await;
}
Expand All @@ -1716,11 +1711,7 @@
"#;
assert_completion_excluding_auto_import(
src,
vec![simple_completion_item(
"bar",
CompletionItemKind::FUNCTION,
Some("fn()".to_string()),
)],
vec![function_completion_item("bar", "bar()", "fn()")],
)
.await;
}
Expand All @@ -1738,11 +1729,7 @@
"#;
assert_completion_excluding_auto_import(
src,
vec![simple_completion_item(
"bar",
CompletionItemKind::FUNCTION,
Some("fn()".to_string()),
)],
vec![function_completion_item("bar", "bar()", "fn()")],
)
.await;
}
Expand All @@ -1762,11 +1749,7 @@
"#;
assert_completion_excluding_auto_import(
src,
vec![simple_completion_item(
"bar",
CompletionItemKind::FUNCTION,
Some("fn(self)".to_string()),
)],
vec![function_completion_item("bar", "bar()", "fn(self)")],
)
.await;
}
Expand All @@ -1786,11 +1769,7 @@
"#;
assert_completion_excluding_auto_import(
src,
vec![simple_completion_item(
"bar",
CompletionItemKind::FUNCTION,
Some("fn(self)".to_string()),
)],
vec![function_completion_item("bar", "bar()", "fn(self)")],
)
.await;
}
Expand Down Expand Up @@ -1825,7 +1804,7 @@
}

fn main() {
foob>|<

Check warning on line 1807 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
}
"#;

Expand All @@ -1852,7 +1831,7 @@
}

fn main() {
foob>|<

Check warning on line 1834 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
}
"#;

Expand Down
Loading