mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
move function rendering to presentation
This commit is contained in:
parent
d0a261468e
commit
b04cadc02c
5 changed files with 49 additions and 24 deletions
|
@ -1,7 +1,11 @@
|
|||
//! This modules takes care of rendering various defenitions as completion items.
|
||||
use test_utils::tested_by;
|
||||
use hir::Docs;
|
||||
|
||||
use crate::completion::{Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem};
|
||||
use crate::completion::{
|
||||
Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem,
|
||||
function_label,
|
||||
};
|
||||
|
||||
impl Completions {
|
||||
pub(crate) fn add_field(
|
||||
|
@ -30,4 +34,39 @@ impl Completions {
|
|||
.detail(ty.to_string())
|
||||
.add_to(self);
|
||||
}
|
||||
|
||||
pub(crate) fn add_function(
|
||||
&mut self,
|
||||
kind: CompletionKind,
|
||||
ctx: &CompletionContext,
|
||||
func: hir::Function,
|
||||
) {
|
||||
let sig = func.signature(ctx.db);
|
||||
|
||||
let mut builder = CompletionItem::new(kind, ctx.source_range(), sig.name().to_string())
|
||||
.kind(if sig.has_self_param() {
|
||||
CompletionItemKind::Method
|
||||
} else {
|
||||
CompletionItemKind::Function
|
||||
})
|
||||
.set_documentation(func.docs(ctx.db))
|
||||
.set_detail(function_item_label(ctx, func));
|
||||
// If not an import, add parenthesis automatically.
|
||||
if ctx.use_item_syntax.is_none() && !ctx.is_call {
|
||||
tested_by!(inserts_parens_for_function_calls);
|
||||
let snippet =
|
||||
if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 {
|
||||
format!("{}()$0", sig.name())
|
||||
} else {
|
||||
format!("{}($0)", sig.name())
|
||||
};
|
||||
builder = builder.insert_snippet(snippet);
|
||||
}
|
||||
self.add(builder)
|
||||
}
|
||||
}
|
||||
|
||||
fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> {
|
||||
let node = function.source(ctx.db).1;
|
||||
function_label(&node)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue