Group import data in a struct

This commit is contained in:
Kirill Bulatov 2020-11-27 12:22:10 +02:00
parent f4ae3650d8
commit f75f07019b
5 changed files with 49 additions and 52 deletions

View file

@ -204,7 +204,7 @@ impl CompletionItem {
trigger_call_info: None,
score: None,
ref_match: None,
import_data: None,
import_to_add: None,
}
}
@ -258,13 +258,21 @@ impl CompletionItem {
}
}
/// An extra import to add after the completion is applied.
#[derive(Clone)]
pub(crate) struct ImportToAdd {
pub(crate) import_path: ModPath,
pub(crate) import_scope: ImportScope,
pub(crate) merge_behaviour: Option<MergeBehaviour>,
}
/// A helper to make `CompletionItem`s.
#[must_use]
#[derive(Clone)]
pub(crate) struct Builder {
source_range: TextRange,
completion_kind: CompletionKind,
import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
import_to_add: Option<ImportToAdd>,
label: String,
insert_text: Option<String>,
insert_text_format: InsertTextFormat,
@ -288,9 +296,9 @@ impl Builder {
let mut insert_text = self.insert_text;
let mut text_edits = TextEdit::builder();
if let Some((import_path, import_scope, merge_behaviour)) = self.import_data {
let import = mod_path_to_ast(&import_path);
let mut import_path_without_last_segment = import_path;
if let Some(import_data) = self.import_to_add {
let import = mod_path_to_ast(&import_data.import_path);
let mut import_path_without_last_segment = import_data.import_path;
let _ = import_path_without_last_segment.segments.pop();
if !import_path_without_last_segment.segments.is_empty() {
@ -303,7 +311,11 @@ impl Builder {
label = format!("{}::{}", import_path_without_last_segment, label);
}
let rewriter = insert_use::insert_use(&import_scope, import, merge_behaviour);
let rewriter = insert_use::insert_use(
&import_data.import_scope,
import,
import_data.merge_behaviour,
);
if let Some(old_ast) = rewriter.rewrite_root() {
algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut text_edits);
}
@ -395,11 +407,8 @@ impl Builder {
self.trigger_call_info = Some(true);
self
}
pub(crate) fn import_data(
mut self,
import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
) -> Builder {
self.import_data = import_data;
pub(crate) fn add_import(mut self, import_to_add: Option<ImportToAdd>) -> Builder {
self.import_to_add = import_to_add;
self
}
pub(crate) fn set_ref_match(

View file

@ -16,8 +16,8 @@ use syntax::TextRange;
use test_utils::mark;
use crate::{
config::SnippetCap, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
CompletionScore,
config::SnippetCap, item::ImportToAdd, CompletionContext, CompletionItem, CompletionItemKind,
CompletionKind, CompletionScore,
};
use crate::render::{enum_variant::render_enum_variant, function::render_fn, macro_::render_macro};
@ -48,15 +48,15 @@ pub(crate) fn render_resolution<'a>(
pub(crate) fn render_resolution_with_import<'a>(
ctx: RenderContext<'a>,
import: ModPath,
import_path: ModPath,
import_scope: ImportScope,
merge_behaviour: Option<MergeBehaviour>,
resolution: &ScopeDef,
) -> Option<CompletionItem> {
let local_name = import.segments.last()?.to_string();
let local_name = import_path.segments.last()?.to_string();
Render::new(ctx).render_resolution(
local_name,
Some((import, import_scope, merge_behaviour)),
Some(ImportToAdd { import_path, import_scope, merge_behaviour }),
resolution,
)
}
@ -147,7 +147,7 @@ impl<'a> Render<'a> {
fn render_resolution(
self,
local_name: String,
import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
import_to_add: Option<ImportToAdd>,
resolution: &ScopeDef,
) -> Option<CompletionItem> {
let _p = profile::span("render_resolution");
@ -160,15 +160,16 @@ impl<'a> Render<'a> {
let kind = match resolution {
ScopeDef::ModuleDef(Function(func)) => {
let item = render_fn(self.ctx, import_data, Some(local_name), *func);
let item = render_fn(self.ctx, import_to_add, Some(local_name), *func);
return Some(item);
}
ScopeDef::ModuleDef(EnumVariant(var)) => {
let item = render_enum_variant(self.ctx, import_data, Some(local_name), *var, None);
let item =
render_enum_variant(self.ctx, import_to_add, Some(local_name), *var, None);
return Some(item);
}
ScopeDef::MacroDef(mac) => {
let item = render_macro(self.ctx, import_data, local_name, *mac);
let item = render_macro(self.ctx, import_to_add, local_name, *mac);
return item;
}
@ -193,7 +194,7 @@ impl<'a> Render<'a> {
local_name,
)
.kind(CompletionItemKind::UnresolvedReference)
.import_data(import_data)
.add_import(import_to_add)
.build();
return Some(item);
}
@ -248,7 +249,7 @@ impl<'a> Render<'a> {
let item = item
.kind(kind)
.import_data(import_data)
.add_import(import_to_add)
.set_documentation(docs)
.set_ref_match(ref_match)
.build();

View file

@ -1,24 +1,23 @@
//! Renderer for `enum` variants.
use hir::{HasAttrs, HirDisplay, ModPath, StructKind};
use ide_helpers::insert_use::{ImportScope, MergeBehaviour};
use itertools::Itertools;
use test_utils::mark;
use crate::{
item::{CompletionItem, CompletionItemKind, CompletionKind},
item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd},
render::{builder_ext::Params, RenderContext},
};
pub(crate) fn render_enum_variant<'a>(
ctx: RenderContext<'a>,
import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
import_to_add: Option<ImportToAdd>,
local_name: Option<String>,
variant: hir::EnumVariant,
path: Option<ModPath>,
) -> CompletionItem {
let _p = profile::span("render_enum_variant");
EnumVariantRender::new(ctx, local_name, variant, path).render(import_data)
EnumVariantRender::new(ctx, local_name, variant, path).render(import_to_add)
}
#[derive(Debug)]
@ -63,10 +62,7 @@ impl<'a> EnumVariantRender<'a> {
}
}
fn render(
self,
import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
) -> CompletionItem {
fn render(self, import_to_add: Option<ImportToAdd>) -> CompletionItem {
let mut builder = CompletionItem::new(
CompletionKind::Reference,
self.ctx.source_range(),
@ -75,7 +71,7 @@ impl<'a> EnumVariantRender<'a> {
.kind(CompletionItemKind::EnumVariant)
.set_documentation(self.variant.docs(self.ctx.db()))
.set_deprecated(self.ctx.is_deprecated(self.variant))
.import_data(import_data)
.add_import(import_to_add)
.detail(self.detail());
if self.variant_kind == StructKind::Tuple {

View file

@ -1,22 +1,21 @@
//! Renderer for function calls.
use hir::{HasSource, ModPath, Type};
use ide_helpers::insert_use::{ImportScope, MergeBehaviour};
use hir::{HasSource, Type};
use syntax::{ast::Fn, display::function_declaration};
use crate::{
item::{CompletionItem, CompletionItemKind, CompletionKind},
item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd},
render::{builder_ext::Params, RenderContext},
};
pub(crate) fn render_fn<'a>(
ctx: RenderContext<'a>,
import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
import_to_add: Option<ImportToAdd>,
local_name: Option<String>,
fn_: hir::Function,
) -> CompletionItem {
let _p = profile::span("render_fn");
FunctionRender::new(ctx, local_name, fn_).render(import_data)
FunctionRender::new(ctx, local_name, fn_).render(import_to_add)
}
#[derive(Debug)]
@ -39,10 +38,7 @@ impl<'a> FunctionRender<'a> {
FunctionRender { ctx, name, fn_, ast_node }
}
fn render(
self,
import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
) -> CompletionItem {
fn render(self, import_to_add: Option<ImportToAdd>) -> CompletionItem {
let params = self.params();
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
.kind(self.kind())
@ -50,7 +46,7 @@ impl<'a> FunctionRender<'a> {
.set_deprecated(self.ctx.is_deprecated(self.fn_))
.detail(self.detail())
.add_call_parens(self.ctx.completion, self.name, params)
.import_data(import_data)
.add_import(import_to_add)
.build()
}

View file

@ -1,24 +1,22 @@
//! Renderer for macro invocations.
use hir::{Documentation, HasSource, ModPath};
use ide_helpers::insert_use::{ImportScope, MergeBehaviour};
use hir::{Documentation, HasSource};
use syntax::display::macro_label;
use test_utils::mark;
use crate::{
item::{CompletionItem, CompletionItemKind, CompletionKind},
item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd},
render::RenderContext,
};
pub(crate) fn render_macro<'a>(
ctx: RenderContext<'a>,
// TODO kb add some object instead of a tuple?
import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
import_to_add: Option<ImportToAdd>,
name: String,
macro_: hir::MacroDef,
) -> Option<CompletionItem> {
let _p = profile::span("render_macro");
MacroRender::new(ctx, name, macro_).render(import_data)
MacroRender::new(ctx, name, macro_).render(import_to_add)
}
#[derive(Debug)]
@ -40,10 +38,7 @@ impl<'a> MacroRender<'a> {
MacroRender { ctx, name, macro_, docs, bra, ket }
}
fn render(
&self,
import_data: Option<(ModPath, ImportScope, Option<MergeBehaviour>)>,
) -> Option<CompletionItem> {
fn render(&self, import_to_add: Option<ImportToAdd>) -> Option<CompletionItem> {
// FIXME: Currently proc-macro do not have ast-node,
// such that it does not have source
if self.macro_.is_proc_macro() {
@ -55,7 +50,7 @@ impl<'a> MacroRender<'a> {
.kind(CompletionItemKind::Macro)
.set_documentation(self.docs.clone())
.set_deprecated(self.ctx.is_deprecated(self.macro_))
.import_data(import_data)
.add_import(import_to_add)
.detail(self.detail());
let needs_bang = self.needs_bang();