mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
Get rid of do-er antipattern
This commit is contained in:
parent
caf0fa20a7
commit
4d333ebb63
7 changed files with 100 additions and 45 deletions
|
@ -52,12 +52,12 @@ impl Completions {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) {
|
pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) {
|
||||||
let item = Render::new(RenderContext::new(ctx)).add_field(field, ty);
|
let item = render_field(RenderContext::new(ctx), field, ty);
|
||||||
self.add(item);
|
self.add(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) {
|
pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) {
|
||||||
let item = Render::new(RenderContext::new(ctx)).add_tuple_field(field, ty);
|
let item = render_tuple_field(RenderContext::new(ctx), field, ty);
|
||||||
self.add(item);
|
self.add(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,9 +67,7 @@ impl Completions {
|
||||||
local_name: String,
|
local_name: String,
|
||||||
resolution: &ScopeDef,
|
resolution: &ScopeDef,
|
||||||
) {
|
) {
|
||||||
if let Some(item) =
|
if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) {
|
||||||
Render::new(RenderContext::new(ctx)).render_resolution(local_name, resolution)
|
|
||||||
{
|
|
||||||
self.add(item);
|
self.add(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,7 +82,7 @@ impl Completions {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
if let Some(item) = MacroRender::new(RenderContext::new(ctx), name, macro_).render() {
|
if let Some(item) = render_macro(RenderContext::new(ctx), name, macro_) {
|
||||||
self.add(item);
|
self.add(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -95,18 +93,18 @@ impl Completions {
|
||||||
func: hir::Function,
|
func: hir::Function,
|
||||||
local_name: Option<String>,
|
local_name: Option<String>,
|
||||||
) {
|
) {
|
||||||
let item = FunctionRender::new(RenderContext::new(ctx), local_name, func).render();
|
let item = render_fn(RenderContext::new(ctx), local_name, func);
|
||||||
self.add(item)
|
self.add(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
|
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
|
||||||
if let Some(item) = ConstRender::new(RenderContext::new(ctx), constant).render() {
|
if let Some(item) = render_const(RenderContext::new(ctx), constant) {
|
||||||
self.add(item);
|
self.add(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
|
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
|
||||||
if let Some(item) = TypeAliasRender::new(RenderContext::new(ctx), type_alias).render() {
|
if let Some(item) = render_type_alias(RenderContext::new(ctx), type_alias) {
|
||||||
self.add(item)
|
self.add(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,8 +115,7 @@ impl Completions {
|
||||||
variant: hir::EnumVariant,
|
variant: hir::EnumVariant,
|
||||||
path: ModPath,
|
path: ModPath,
|
||||||
) {
|
) {
|
||||||
let item =
|
let item = render_enum_variant(RenderContext::new(ctx), None, variant, Some(path));
|
||||||
EnumVariantRender::new(RenderContext::new(ctx), None, variant, Some(path)).render();
|
|
||||||
self.add(item);
|
self.add(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,8 +125,7 @@ impl Completions {
|
||||||
variant: hir::EnumVariant,
|
variant: hir::EnumVariant,
|
||||||
local_name: Option<String>,
|
local_name: Option<String>,
|
||||||
) {
|
) {
|
||||||
let item =
|
let item = render_enum_variant(RenderContext::new(ctx), local_name, variant, None);
|
||||||
EnumVariantRender::new(RenderContext::new(ctx), local_name, variant, None).render();
|
|
||||||
self.add(item);
|
self.add(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,14 +19,32 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) use crate::render::{
|
pub(crate) use crate::render::{
|
||||||
const_::ConstRender, enum_variant::EnumVariantRender, function::FunctionRender,
|
const_::render_const, enum_variant::render_enum_variant, function::render_fn,
|
||||||
macro_::MacroRender, type_alias::TypeAliasRender,
|
macro_::render_macro, type_alias::render_type_alias,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Generic renderer for completion items.
|
pub(crate) fn render_field<'a>(
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct Render<'a> {
|
|
||||||
ctx: RenderContext<'a>,
|
ctx: RenderContext<'a>,
|
||||||
|
field: hir::Field,
|
||||||
|
ty: &Type,
|
||||||
|
) -> CompletionItem {
|
||||||
|
Render::new(ctx).add_field(field, ty)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn render_tuple_field<'a>(
|
||||||
|
ctx: RenderContext<'a>,
|
||||||
|
field: usize,
|
||||||
|
ty: &Type,
|
||||||
|
) -> CompletionItem {
|
||||||
|
Render::new(ctx).add_tuple_field(field, ty)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn render_resolution<'a>(
|
||||||
|
ctx: RenderContext<'a>,
|
||||||
|
local_name: String,
|
||||||
|
resolution: &ScopeDef,
|
||||||
|
) -> Option<CompletionItem> {
|
||||||
|
Render::new(ctx).render_resolution(local_name, resolution)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Interface for data and methods required for items rendering.
|
/// Interface for data and methods required for items rendering.
|
||||||
|
@ -74,12 +92,18 @@ impl<'a> RenderContext<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generic renderer for completion items.
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Render<'a> {
|
||||||
|
ctx: RenderContext<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Render<'a> {
|
impl<'a> Render<'a> {
|
||||||
pub(crate) fn new(ctx: RenderContext<'a>) -> Render<'a> {
|
fn new(ctx: RenderContext<'a>) -> Render<'a> {
|
||||||
Render { ctx }
|
Render { ctx }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem {
|
fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem {
|
||||||
let is_deprecated = self.ctx.is_deprecated(field);
|
let is_deprecated = self.ctx.is_deprecated(field);
|
||||||
let name = field.name(self.ctx.db());
|
let name = field.name(self.ctx.db());
|
||||||
let mut item = CompletionItem::new(
|
let mut item = CompletionItem::new(
|
||||||
|
@ -96,17 +120,17 @@ impl<'a> Render<'a> {
|
||||||
item = item.set_score(score);
|
item = item.set_score(score);
|
||||||
}
|
}
|
||||||
|
|
||||||
return item.build();
|
item.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem {
|
fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem {
|
||||||
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), field.to_string())
|
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), field.to_string())
|
||||||
.kind(CompletionItemKind::Field)
|
.kind(CompletionItemKind::Field)
|
||||||
.detail(ty.display(self.ctx.db()).to_string())
|
.detail(ty.display(self.ctx.db()).to_string())
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn render_resolution(
|
fn render_resolution(
|
||||||
self,
|
self,
|
||||||
local_name: String,
|
local_name: String,
|
||||||
resolution: &ScopeDef,
|
resolution: &ScopeDef,
|
||||||
|
@ -120,15 +144,15 @@ impl<'a> Render<'a> {
|
||||||
|
|
||||||
let kind = match resolution {
|
let kind = match resolution {
|
||||||
ScopeDef::ModuleDef(Function(func)) => {
|
ScopeDef::ModuleDef(Function(func)) => {
|
||||||
let item = FunctionRender::new(self.ctx, Some(local_name), *func).render();
|
let item = render_fn(self.ctx, Some(local_name), *func);
|
||||||
return Some(item);
|
return Some(item);
|
||||||
}
|
}
|
||||||
ScopeDef::ModuleDef(EnumVariant(var)) => {
|
ScopeDef::ModuleDef(EnumVariant(var)) => {
|
||||||
let item = EnumVariantRender::new(self.ctx, Some(local_name), *var, None).render();
|
let item = render_enum_variant(self.ctx, Some(local_name), *var, None);
|
||||||
return Some(item);
|
return Some(item);
|
||||||
}
|
}
|
||||||
ScopeDef::MacroDef(mac) => {
|
ScopeDef::MacroDef(mac) => {
|
||||||
let item = MacroRender::new(self.ctx, local_name, *mac).render();
|
let item = render_macro(self.ctx, local_name, *mac);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,20 +11,27 @@ use crate::{
|
||||||
render::RenderContext,
|
render::RenderContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub(crate) fn render_const<'a>(
|
||||||
|
ctx: RenderContext<'a>,
|
||||||
|
const_: hir::Const,
|
||||||
|
) -> Option<CompletionItem> {
|
||||||
|
ConstRender::new(ctx, const_).render()
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct ConstRender<'a> {
|
struct ConstRender<'a> {
|
||||||
ctx: RenderContext<'a>,
|
ctx: RenderContext<'a>,
|
||||||
const_: hir::Const,
|
const_: hir::Const,
|
||||||
ast_node: Const,
|
ast_node: Const,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ConstRender<'a> {
|
impl<'a> ConstRender<'a> {
|
||||||
pub(crate) fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> {
|
fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> {
|
||||||
let ast_node = const_.source(ctx.db()).value;
|
let ast_node = const_.source(ctx.db()).value;
|
||||||
ConstRender { ctx, const_, ast_node }
|
ConstRender { ctx, const_, ast_node }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn render(self) -> Option<CompletionItem> {
|
fn render(self) -> Option<CompletionItem> {
|
||||||
let name = self.name()?;
|
let name = self.name()?;
|
||||||
let detail = self.detail();
|
let detail = self.detail();
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,17 @@ use crate::{
|
||||||
render::{builder_ext::Params, RenderContext},
|
render::{builder_ext::Params, RenderContext},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub(crate) fn render_enum_variant<'a>(
|
||||||
|
ctx: RenderContext<'a>,
|
||||||
|
local_name: Option<String>,
|
||||||
|
variant: hir::EnumVariant,
|
||||||
|
path: Option<ModPath>,
|
||||||
|
) -> CompletionItem {
|
||||||
|
EnumVariantRender::new(ctx, local_name, variant, path).render()
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct EnumVariantRender<'a> {
|
struct EnumVariantRender<'a> {
|
||||||
ctx: RenderContext<'a>,
|
ctx: RenderContext<'a>,
|
||||||
name: String,
|
name: String,
|
||||||
variant: hir::EnumVariant,
|
variant: hir::EnumVariant,
|
||||||
|
@ -21,7 +30,7 @@ pub(crate) struct EnumVariantRender<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> EnumVariantRender<'a> {
|
impl<'a> EnumVariantRender<'a> {
|
||||||
pub(crate) fn new(
|
fn new(
|
||||||
ctx: RenderContext<'a>,
|
ctx: RenderContext<'a>,
|
||||||
local_name: Option<String>,
|
local_name: Option<String>,
|
||||||
variant: hir::EnumVariant,
|
variant: hir::EnumVariant,
|
||||||
|
@ -51,7 +60,7 @@ impl<'a> EnumVariantRender<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn render(self) -> CompletionItem {
|
fn render(self) -> CompletionItem {
|
||||||
let mut builder = CompletionItem::new(
|
let mut builder = CompletionItem::new(
|
||||||
CompletionKind::Reference,
|
CompletionKind::Reference,
|
||||||
self.ctx.source_range(),
|
self.ctx.source_range(),
|
||||||
|
|
|
@ -8,8 +8,16 @@ use crate::{
|
||||||
render::{builder_ext::Params, RenderContext},
|
render::{builder_ext::Params, RenderContext},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub(crate) fn render_fn<'a>(
|
||||||
|
ctx: RenderContext<'a>,
|
||||||
|
local_name: Option<String>,
|
||||||
|
fn_: hir::Function,
|
||||||
|
) -> CompletionItem {
|
||||||
|
FunctionRender::new(ctx, local_name, fn_).render()
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct FunctionRender<'a> {
|
struct FunctionRender<'a> {
|
||||||
ctx: RenderContext<'a>,
|
ctx: RenderContext<'a>,
|
||||||
name: String,
|
name: String,
|
||||||
fn_: hir::Function,
|
fn_: hir::Function,
|
||||||
|
@ -17,7 +25,7 @@ pub(crate) struct FunctionRender<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> FunctionRender<'a> {
|
impl<'a> FunctionRender<'a> {
|
||||||
pub(crate) fn new(
|
fn new(
|
||||||
ctx: RenderContext<'a>,
|
ctx: RenderContext<'a>,
|
||||||
local_name: Option<String>,
|
local_name: Option<String>,
|
||||||
fn_: hir::Function,
|
fn_: hir::Function,
|
||||||
|
@ -28,7 +36,7 @@ impl<'a> FunctionRender<'a> {
|
||||||
FunctionRender { ctx, name, fn_, ast_node }
|
FunctionRender { ctx, name, fn_, ast_node }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn render(self) -> CompletionItem {
|
fn render(self) -> CompletionItem {
|
||||||
let params = self.params();
|
let params = self.params();
|
||||||
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
|
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
|
||||||
.kind(self.kind())
|
.kind(self.kind())
|
||||||
|
|
|
@ -9,8 +9,16 @@ use crate::{
|
||||||
render::RenderContext,
|
render::RenderContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub(crate) fn render_macro<'a>(
|
||||||
|
ctx: RenderContext<'a>,
|
||||||
|
name: String,
|
||||||
|
macro_: hir::MacroDef,
|
||||||
|
) -> Option<CompletionItem> {
|
||||||
|
MacroRender::new(ctx, name, macro_).render()
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct MacroRender<'a> {
|
struct MacroRender<'a> {
|
||||||
ctx: RenderContext<'a>,
|
ctx: RenderContext<'a>,
|
||||||
name: String,
|
name: String,
|
||||||
macro_: hir::MacroDef,
|
macro_: hir::MacroDef,
|
||||||
|
@ -20,11 +28,7 @@ pub(crate) struct MacroRender<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> MacroRender<'a> {
|
impl<'a> MacroRender<'a> {
|
||||||
pub(crate) fn new(
|
fn new(ctx: RenderContext<'a>, name: String, macro_: hir::MacroDef) -> MacroRender<'a> {
|
||||||
ctx: RenderContext<'a>,
|
|
||||||
name: String,
|
|
||||||
macro_: hir::MacroDef,
|
|
||||||
) -> MacroRender<'a> {
|
|
||||||
let docs = ctx.docs(macro_);
|
let docs = ctx.docs(macro_);
|
||||||
let docs_str = docs.as_ref().map_or("", |s| s.as_str());
|
let docs_str = docs.as_ref().map_or("", |s| s.as_str());
|
||||||
let (bra, ket) = guess_macro_braces(&name, docs_str);
|
let (bra, ket) = guess_macro_braces(&name, docs_str);
|
||||||
|
@ -32,7 +36,7 @@ impl<'a> MacroRender<'a> {
|
||||||
MacroRender { ctx, name, macro_, docs, bra, ket }
|
MacroRender { ctx, name, macro_, docs, bra, ket }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn render(&self) -> Option<CompletionItem> {
|
fn render(&self) -> Option<CompletionItem> {
|
||||||
// FIXME: Currently proc-macro do not have ast-node,
|
// FIXME: Currently proc-macro do not have ast-node,
|
||||||
// such that it does not have source
|
// such that it does not have source
|
||||||
if self.macro_.is_proc_macro() {
|
if self.macro_.is_proc_macro() {
|
||||||
|
|
|
@ -11,20 +11,27 @@ use crate::{
|
||||||
render::RenderContext,
|
render::RenderContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub(crate) fn render_type_alias<'a>(
|
||||||
|
ctx: RenderContext<'a>,
|
||||||
|
type_alias: hir::TypeAlias,
|
||||||
|
) -> Option<CompletionItem> {
|
||||||
|
TypeAliasRender::new(ctx, type_alias).render()
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct TypeAliasRender<'a> {
|
struct TypeAliasRender<'a> {
|
||||||
ctx: RenderContext<'a>,
|
ctx: RenderContext<'a>,
|
||||||
type_alias: hir::TypeAlias,
|
type_alias: hir::TypeAlias,
|
||||||
ast_node: TypeAlias,
|
ast_node: TypeAlias,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TypeAliasRender<'a> {
|
impl<'a> TypeAliasRender<'a> {
|
||||||
pub(crate) fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> {
|
fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> {
|
||||||
let ast_node = type_alias.source(ctx.db()).value;
|
let ast_node = type_alias.source(ctx.db()).value;
|
||||||
TypeAliasRender { ctx, type_alias, ast_node }
|
TypeAliasRender { ctx, type_alias, ast_node }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn render(self) -> Option<CompletionItem> {
|
fn render(self) -> Option<CompletionItem> {
|
||||||
let name = self.name()?;
|
let name = self.name()?;
|
||||||
let detail = self.detail();
|
let detail = self.detail();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue