From 27cadcd531c017aa7c78c6f7a36f2b7f2ce8a196 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 13:05:28 +1100 Subject: HasSource::source -> HasSource::source_old To start migrating HasSource::source to return an Option. --- crates/completion/src/completions/trait_impl.rs | 4 ++-- crates/completion/src/render/const_.rs | 2 +- crates/completion/src/render/function.rs | 2 +- crates/completion/src/render/macro_.rs | 2 +- crates/completion/src/render/type_alias.rs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/completion') diff --git a/crates/completion/src/completions/trait_impl.rs b/crates/completion/src/completions/trait_impl.rs index c4e0d0669..759253c53 100644 --- a/crates/completion/src/completions/trait_impl.rs +++ b/crates/completion/src/completions/trait_impl.rs @@ -156,7 +156,7 @@ fn add_function_impl( }; let range = TextRange::new(fn_def_node.text_range().start(), ctx.source_range().end()); - let function_decl = function_declaration(&func.source(ctx.db).value); + let function_decl = function_declaration(&func.source_old(ctx.db).value); match ctx.config.snippet_cap { Some(cap) => { let snippet = format!("{} {{\n $0\n}}", function_decl); @@ -200,7 +200,7 @@ fn add_const_impl( let const_name = const_.name(ctx.db).map(|n| n.to_string()); if let Some(const_name) = const_name { - let snippet = make_const_compl_syntax(&const_.source(ctx.db).value); + let snippet = make_const_compl_syntax(&const_.source_old(ctx.db).value); let range = TextRange::new(const_def_node.text_range().start(), ctx.source_range().end()); diff --git a/crates/completion/src/render/const_.rs b/crates/completion/src/render/const_.rs index 039bdabc0..a8820a4fe 100644 --- a/crates/completion/src/render/const_.rs +++ b/crates/completion/src/render/const_.rs @@ -27,7 +27,7 @@ struct ConstRender<'a> { impl<'a> 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_old(ctx.db()).value; ConstRender { ctx, const_, ast_node } } diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 316e05b52..d9ea425a0 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -34,7 +34,7 @@ impl<'a> FunctionRender<'a> { fn_: hir::Function, ) -> FunctionRender<'a> { let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string()); - let ast_node = fn_.source(ctx.db()).value; + let ast_node = fn_.source_old(ctx.db()).value; FunctionRender { ctx, name, func: fn_, ast_node } } diff --git a/crates/completion/src/render/macro_.rs b/crates/completion/src/render/macro_.rs index dac79592f..3d13fd9e2 100644 --- a/crates/completion/src/render/macro_.rs +++ b/crates/completion/src/render/macro_.rs @@ -96,7 +96,7 @@ impl<'a> MacroRender<'a> { } fn detail(&self) -> String { - let ast_node = self.macro_.source(self.ctx.db()).value; + let ast_node = self.macro_.source_old(self.ctx.db()).value; macro_label(&ast_node) } } diff --git a/crates/completion/src/render/type_alias.rs b/crates/completion/src/render/type_alias.rs index 9605c7fa9..4099a5d0e 100644 --- a/crates/completion/src/render/type_alias.rs +++ b/crates/completion/src/render/type_alias.rs @@ -27,7 +27,7 @@ struct TypeAliasRender<'a> { impl<'a> 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_old(ctx.db()).value; TypeAliasRender { ctx, type_alias, ast_node } } -- cgit v1.2.3 From ea4708c444509449b86c50b7b1b23f9ff5af4e97 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 13:50:50 +1100 Subject: Mark HasSource::source_old as deprecated but allow at all call sites --- crates/completion/src/completions/trait_impl.rs | 2 ++ crates/completion/src/render/const_.rs | 1 + crates/completion/src/render/function.rs | 1 + crates/completion/src/render/macro_.rs | 1 + crates/completion/src/render/type_alias.rs | 1 + 5 files changed, 6 insertions(+) (limited to 'crates/completion') diff --git a/crates/completion/src/completions/trait_impl.rs b/crates/completion/src/completions/trait_impl.rs index 759253c53..43b3d939f 100644 --- a/crates/completion/src/completions/trait_impl.rs +++ b/crates/completion/src/completions/trait_impl.rs @@ -156,6 +156,7 @@ fn add_function_impl( }; let range = TextRange::new(fn_def_node.text_range().start(), ctx.source_range().end()); + #[allow(deprecated)] let function_decl = function_declaration(&func.source_old(ctx.db).value); match ctx.config.snippet_cap { Some(cap) => { @@ -200,6 +201,7 @@ fn add_const_impl( let const_name = const_.name(ctx.db).map(|n| n.to_string()); if let Some(const_name) = const_name { + #[allow(deprecated)] let snippet = make_const_compl_syntax(&const_.source_old(ctx.db).value); let range = TextRange::new(const_def_node.text_range().start(), ctx.source_range().end()); diff --git a/crates/completion/src/render/const_.rs b/crates/completion/src/render/const_.rs index a8820a4fe..648a1afc5 100644 --- a/crates/completion/src/render/const_.rs +++ b/crates/completion/src/render/const_.rs @@ -27,6 +27,7 @@ struct ConstRender<'a> { impl<'a> ConstRender<'a> { fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> { + #[allow(deprecated)] let ast_node = const_.source_old(ctx.db()).value; ConstRender { ctx, const_, ast_node } } diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index d9ea425a0..4c8996204 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -34,6 +34,7 @@ impl<'a> FunctionRender<'a> { fn_: hir::Function, ) -> FunctionRender<'a> { let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string()); + #[allow(deprecated)] let ast_node = fn_.source_old(ctx.db()).value; FunctionRender { ctx, name, func: fn_, ast_node } diff --git a/crates/completion/src/render/macro_.rs b/crates/completion/src/render/macro_.rs index 3d13fd9e2..95408ff9a 100644 --- a/crates/completion/src/render/macro_.rs +++ b/crates/completion/src/render/macro_.rs @@ -96,6 +96,7 @@ impl<'a> MacroRender<'a> { } fn detail(&self) -> String { + #[allow(deprecated)] let ast_node = self.macro_.source_old(self.ctx.db()).value; macro_label(&ast_node) } diff --git a/crates/completion/src/render/type_alias.rs b/crates/completion/src/render/type_alias.rs index 4099a5d0e..276090015 100644 --- a/crates/completion/src/render/type_alias.rs +++ b/crates/completion/src/render/type_alias.rs @@ -27,6 +27,7 @@ struct TypeAliasRender<'a> { impl<'a> TypeAliasRender<'a> { fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> { + #[allow(deprecated)] let ast_node = type_alias.source_old(ctx.db()).value; TypeAliasRender { ctx, type_alias, ast_node } } -- cgit v1.2.3 From 14d0db0759c5b8e1d085ebab03a8b944a8921f2e Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 14:14:09 +1100 Subject: HasSource::source_old -> HasSource::source for places where proc-macros were special cased In #6901 some special case handling for proc-macros was introduced to prevent panicing as they have no AST. Now the new HasSource::source method is used that returns an option. Generally this was a pretty trivial change, the only thing of much interest is that `hir::MacroDef` now implements `TryToNav` not `ToNav` as this allows us to handle `HasSource::source` now returning an option. --- crates/completion/src/render/macro_.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'crates/completion') diff --git a/crates/completion/src/render/macro_.rs b/crates/completion/src/render/macro_.rs index 95408ff9a..0612591fd 100644 --- a/crates/completion/src/render/macro_.rs +++ b/crates/completion/src/render/macro_.rs @@ -39,20 +39,13 @@ impl<'a> MacroRender<'a> { } fn render(&self, import_to_add: Option) -> Option { - // FIXME: Currently proc-macro do not have ast-node, - // such that it does not have source - // more discussion: https://github.com/rust-analyzer/rust-analyzer/issues/6913 - if self.macro_.is_proc_macro() { - return None; - } - let mut builder = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label()) .kind(CompletionItemKind::Macro) .set_documentation(self.docs.clone()) .set_deprecated(self.ctx.is_deprecated(self.macro_)) .add_import(import_to_add) - .detail(self.detail()); + .detail(self.detail()?); let needs_bang = self.needs_bang(); builder = match self.ctx.snippet_cap() { @@ -95,10 +88,9 @@ impl<'a> MacroRender<'a> { format!("{}!", self.name) } - fn detail(&self) -> String { - #[allow(deprecated)] - let ast_node = self.macro_.source_old(self.ctx.db()).value; - macro_label(&ast_node) + fn detail(&self) -> Option { + let ast_node = self.macro_.source(self.ctx.db())?.value; + Some(macro_label(&ast_node)) } } -- cgit v1.2.3 From 7bfec89cf969aa630f184f7d4a66e8e12a423d2f Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 17:26:39 +1100 Subject: Make the result of Const, FunctionRender and TypeAliasRender constructors optional They use source() which now returns an Option so they need to too. --- crates/completion/src/completions.rs | 5 +++-- crates/completion/src/render.rs | 3 +-- crates/completion/src/render/const_.rs | 9 ++++----- crates/completion/src/render/function.rs | 11 +++++------ crates/completion/src/render/type_alias.rs | 9 ++++----- 5 files changed, 17 insertions(+), 20 deletions(-) (limited to 'crates/completion') diff --git a/crates/completion/src/completions.rs b/crates/completion/src/completions.rs index d9fe13485..00c9e76f0 100644 --- a/crates/completion/src/completions.rs +++ b/crates/completion/src/completions.rs @@ -106,8 +106,9 @@ impl Completions { func: hir::Function, local_name: Option, ) { - let item = render_fn(RenderContext::new(ctx), None, local_name, func); - self.add(item) + if let Some(item) = render_fn(RenderContext::new(ctx), None, local_name, func) { + self.add(item) + } } pub(crate) fn add_variant_pat( diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs index 1ba7201a1..ac0b2a513 100644 --- a/crates/completion/src/render.rs +++ b/crates/completion/src/render.rs @@ -157,8 +157,7 @@ impl<'a> Render<'a> { let kind = match resolution { ScopeDef::ModuleDef(Function(func)) => { - let item = render_fn(self.ctx, import_to_add, Some(local_name), *func); - return Some(item); + return render_fn(self.ctx, import_to_add, Some(local_name), *func); } ScopeDef::ModuleDef(Variant(_)) if self.ctx.completion.is_pat_binding_or_const diff --git a/crates/completion/src/render/const_.rs b/crates/completion/src/render/const_.rs index 648a1afc5..ce924f309 100644 --- a/crates/completion/src/render/const_.rs +++ b/crates/completion/src/render/const_.rs @@ -15,7 +15,7 @@ pub(crate) fn render_const<'a>( ctx: RenderContext<'a>, const_: hir::Const, ) -> Option { - ConstRender::new(ctx, const_).render() + ConstRender::new(ctx, const_)?.render() } #[derive(Debug)] @@ -26,10 +26,9 @@ struct ConstRender<'a> { } impl<'a> ConstRender<'a> { - fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> { - #[allow(deprecated)] - let ast_node = const_.source_old(ctx.db()).value; - ConstRender { ctx, const_, ast_node } + fn new(ctx: RenderContext<'a>, const_: hir::Const) -> Option> { + let ast_node = const_.source(ctx.db())?.value; + Some(ConstRender { ctx, const_, ast_node }) } fn render(self) -> Option { diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 4c8996204..081be14f4 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -14,9 +14,9 @@ pub(crate) fn render_fn<'a>( import_to_add: Option, local_name: Option, fn_: hir::Function, -) -> CompletionItem { +) -> Option { let _p = profile::span("render_fn"); - FunctionRender::new(ctx, local_name, fn_).render(import_to_add) + Some(FunctionRender::new(ctx, local_name, fn_)?.render(import_to_add)) } #[derive(Debug)] @@ -32,12 +32,11 @@ impl<'a> FunctionRender<'a> { ctx: RenderContext<'a>, local_name: Option, fn_: hir::Function, - ) -> FunctionRender<'a> { + ) -> Option> { let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string()); - #[allow(deprecated)] - let ast_node = fn_.source_old(ctx.db()).value; + let ast_node = fn_.source(ctx.db())?.value; - FunctionRender { ctx, name, func: fn_, ast_node } + Some(FunctionRender { ctx, name, func: fn_, ast_node }) } fn render(self, import_to_add: Option) -> CompletionItem { diff --git a/crates/completion/src/render/type_alias.rs b/crates/completion/src/render/type_alias.rs index 276090015..69b445b9c 100644 --- a/crates/completion/src/render/type_alias.rs +++ b/crates/completion/src/render/type_alias.rs @@ -15,7 +15,7 @@ pub(crate) fn render_type_alias<'a>( ctx: RenderContext<'a>, type_alias: hir::TypeAlias, ) -> Option { - TypeAliasRender::new(ctx, type_alias).render() + TypeAliasRender::new(ctx, type_alias)?.render() } #[derive(Debug)] @@ -26,10 +26,9 @@ struct TypeAliasRender<'a> { } impl<'a> TypeAliasRender<'a> { - fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> { - #[allow(deprecated)] - let ast_node = type_alias.source_old(ctx.db()).value; - TypeAliasRender { ctx, type_alias, ast_node } + fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> Option> { + let ast_node = type_alias.source(ctx.db())?.value; + Some(TypeAliasRender { ctx, type_alias, ast_node }) } fn render(self) -> Option { -- cgit v1.2.3 From 3a1f8e897bc2af56cd7a6c08bad1bf88cc97b257 Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Fri, 1 Jan 2021 17:30:13 +1100 Subject: Remove source_old from adding const and function impls --- crates/completion/src/completions/trait_impl.rs | 49 +++++++++++++------------ 1 file changed, 26 insertions(+), 23 deletions(-) (limited to 'crates/completion') diff --git a/crates/completion/src/completions/trait_impl.rs b/crates/completion/src/completions/trait_impl.rs index 43b3d939f..54bb897e9 100644 --- a/crates/completion/src/completions/trait_impl.rs +++ b/crates/completion/src/completions/trait_impl.rs @@ -156,20 +156,21 @@ fn add_function_impl( }; let range = TextRange::new(fn_def_node.text_range().start(), ctx.source_range().end()); - #[allow(deprecated)] - let function_decl = function_declaration(&func.source_old(ctx.db).value); - match ctx.config.snippet_cap { - Some(cap) => { - let snippet = format!("{} {{\n $0\n}}", function_decl); - builder.snippet_edit(cap, TextEdit::replace(range, snippet)) - } - None => { - let header = format!("{} {{", function_decl); - builder.text_edit(TextEdit::replace(range, header)) + if let Some(src) = func.source(ctx.db) { + let function_decl = function_declaration(&src.value); + match ctx.config.snippet_cap { + Some(cap) => { + let snippet = format!("{} {{\n $0\n}}", function_decl); + builder.snippet_edit(cap, TextEdit::replace(range, snippet)) + } + None => { + let header = format!("{} {{", function_decl); + builder.text_edit(TextEdit::replace(range, header)) + } } + .kind(completion_kind) + .add_to(acc); } - .kind(completion_kind) - .add_to(acc); } fn add_type_alias_impl( @@ -201,17 +202,19 @@ fn add_const_impl( let const_name = const_.name(ctx.db).map(|n| n.to_string()); if let Some(const_name) = const_name { - #[allow(deprecated)] - let snippet = make_const_compl_syntax(&const_.source_old(ctx.db).value); - - let range = TextRange::new(const_def_node.text_range().start(), ctx.source_range().end()); - - CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) - .text_edit(TextEdit::replace(range, snippet)) - .lookup_by(const_name) - .kind(CompletionItemKind::Const) - .set_documentation(const_.docs(ctx.db)) - .add_to(acc); + if let Some(source) = const_.source(ctx.db) { + let snippet = make_const_compl_syntax(&source.value); + + let range = + TextRange::new(const_def_node.text_range().start(), ctx.source_range().end()); + + CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) + .text_edit(TextEdit::replace(range, snippet)) + .lookup_by(const_name) + .kind(CompletionItemKind::Const) + .set_documentation(const_.docs(ctx.db)) + .add_to(acc); + } } } -- cgit v1.2.3 From 5c659c21dda26311be584d0bb89b80f8ae3d7bae Mon Sep 17 00:00:00 2001 From: Nick Spain Date: Sat, 2 Jan 2021 09:48:51 +1100 Subject: Handle case where detail doesn't exist without giving up on completion Co-authored-by: Aleksey Kladov --- crates/completion/src/render/macro_.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/completion') diff --git a/crates/completion/src/render/macro_.rs b/crates/completion/src/render/macro_.rs index 0612591fd..6f4f9945c 100644 --- a/crates/completion/src/render/macro_.rs +++ b/crates/completion/src/render/macro_.rs @@ -45,7 +45,7 @@ impl<'a> MacroRender<'a> { .set_documentation(self.docs.clone()) .set_deprecated(self.ctx.is_deprecated(self.macro_)) .add_import(import_to_add) - .detail(self.detail()?); + .set_detail(self.detail()); let needs_bang = self.needs_bang(); builder = match self.ctx.snippet_cap() { -- cgit v1.2.3