move function rendering to presentation
This commit is contained in:
parent
d0a261468e
commit
b04cadc02c
@ -1,6 +1,6 @@
|
||||
use hir::{Ty, AdtDef};
|
||||
|
||||
use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind, CompletionKind};
|
||||
use crate::completion::{CompletionContext, Completions, CompletionKind};
|
||||
|
||||
/// Complete dot accesses, i.e. fields or methods (currently only fields).
|
||||
pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
||||
@ -50,14 +50,7 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty
|
||||
receiver.iterate_methods(ctx.db, |_ty, func| {
|
||||
let sig = func.signature(ctx.db);
|
||||
if sig.has_self_param() {
|
||||
CompletionItem::new(
|
||||
CompletionKind::Reference,
|
||||
ctx.source_range(),
|
||||
sig.name().to_string(),
|
||||
)
|
||||
.from_function(ctx, func)
|
||||
.kind(CompletionItemKind::Method)
|
||||
.add_to(acc);
|
||||
acc.add_function(CompletionKind::Reference, ctx, func);
|
||||
}
|
||||
None::<()>
|
||||
});
|
||||
|
@ -64,14 +64,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
|
||||
hir::ImplItem::Method(func) => {
|
||||
let sig = func.signature(ctx.db);
|
||||
if !sig.has_self_param() {
|
||||
CompletionItem::new(
|
||||
CompletionKind::Reference,
|
||||
ctx.source_range(),
|
||||
sig.name().to_string(),
|
||||
)
|
||||
.from_function(ctx, func)
|
||||
.kind(CompletionItemKind::Method)
|
||||
.add_to(acc);
|
||||
acc.add_function(CompletionKind::Reference, ctx, func);
|
||||
}
|
||||
None::<()>
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
created: "2019-02-18T09:22:24.268227065Z"
|
||||
creator: insta@0.6.2
|
||||
created: "2019-02-24T16:33:48.008220694Z"
|
||||
creator: insta@0.6.3
|
||||
source: crates/ra_ide_api/src/completion/completion_item.rs
|
||||
expression: kind_completions
|
||||
---
|
||||
@ -10,7 +10,7 @@ expression: kind_completions
|
||||
source_range: [67; 69),
|
||||
delete: [67; 69),
|
||||
insert: "new",
|
||||
kind: Method,
|
||||
kind: Function,
|
||||
detail: "fn new() -> Foo"
|
||||
}
|
||||
]
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
created: "2019-02-18T09:22:24.093082839Z"
|
||||
creator: insta@0.6.2
|
||||
created: "2019-02-24T16:33:47.990111169Z"
|
||||
creator: insta@0.6.3
|
||||
source: crates/ra_ide_api/src/completion/completion_item.rs
|
||||
expression: kind_completions
|
||||
---
|
||||
@ -10,7 +10,7 @@ expression: kind_completions
|
||||
source_range: [100; 100),
|
||||
delete: [100; 100),
|
||||
insert: "m()$0",
|
||||
kind: Method,
|
||||
kind: Function,
|
||||
detail: "fn m()",
|
||||
documentation: Documentation(
|
||||
"An associated method"
|
||||
|
Loading…
x
Reference in New Issue
Block a user