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.
This commit is contained in:
Nick Spain 2021-01-01 14:14:09 +11:00
parent ea4708c444
commit 14d0db0759
4 changed files with 11 additions and 42 deletions

View file

@ -39,20 +39,13 @@ impl<'a> MacroRender<'a> {
} }
fn render(&self, import_to_add: Option<ImportEdit>) -> Option<CompletionItem> { fn render(&self, import_to_add: Option<ImportEdit>) -> Option<CompletionItem> {
// 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 = let mut builder =
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label()) CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label())
.kind(CompletionItemKind::Macro) .kind(CompletionItemKind::Macro)
.set_documentation(self.docs.clone()) .set_documentation(self.docs.clone())
.set_deprecated(self.ctx.is_deprecated(self.macro_)) .set_deprecated(self.ctx.is_deprecated(self.macro_))
.add_import(import_to_add) .add_import(import_to_add)
.detail(self.detail()); .detail(self.detail()?);
let needs_bang = self.needs_bang(); let needs_bang = self.needs_bang();
builder = match self.ctx.snippet_cap() { builder = match self.ctx.snippet_cap() {
@ -95,10 +88,9 @@ impl<'a> MacroRender<'a> {
format!("{}!", self.name) format!("{}!", self.name)
} }
fn detail(&self) -> String { fn detail(&self) -> Option<String> {
#[allow(deprecated)] let ast_node = self.macro_.source(self.ctx.db())?.value;
let ast_node = self.macro_.source_old(self.ctx.db()).value; Some(macro_label(&ast_node))
macro_label(&ast_node)
} }
} }

View file

@ -983,14 +983,7 @@ impl MacroDef {
/// XXX: this parses the file /// XXX: this parses the file
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
// FIXME: Currently proc-macro do not have ast-node, self.source(db)?.value.name().map(|it| it.as_name())
// such that it does not have source
// more discussion: https://github.com/rust-analyzer/rust-analyzer/issues/6913
if self.is_proc_macro() {
return None;
}
#[allow(deprecated)]
self.source_old(db).value.name().map(|it| it.as_name())
} }
/// Indicate it is a proc-macro /// Indicate it is a proc-macro

View file

@ -210,15 +210,7 @@ impl ToNav for FileSymbol {
impl TryToNav for Definition { impl TryToNav for Definition {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
match self { match self {
Definition::Macro(it) => { Definition::Macro(it) => it.try_to_nav(db),
// 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 it.is_proc_macro() {
return None;
}
Some(it.to_nav(db))
}
Definition::Field(it) => Some(it.to_nav(db)), Definition::Field(it) => Some(it.to_nav(db)),
Definition::ModuleDef(it) => it.try_to_nav(db), Definition::ModuleDef(it) => it.try_to_nav(db),
Definition::SelfType(it) => Some(it.to_nav(db)), Definition::SelfType(it) => Some(it.to_nav(db)),
@ -366,10 +358,9 @@ impl ToNav for hir::Field {
} }
} }
impl ToNav for hir::MacroDef { impl TryToNav for hir::MacroDef {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
#[allow(deprecated)] let src = self.source(db)?;
let src = self.source_old(db);
log::debug!("nav target {:#?}", src.value.syntax()); log::debug!("nav target {:#?}", src.value.syntax());
let mut res = NavigationTarget::from_named( let mut res = NavigationTarget::from_named(
db, db,
@ -377,7 +368,7 @@ impl ToNav for hir::MacroDef {
SymbolKind::Macro, SymbolKind::Macro,
); );
res.docs = self.docs(db); res.docs = self.docs(db);
res Some(res)
} }
} }

View file

@ -327,14 +327,7 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
let mod_path = definition_mod_path(db, &def); let mod_path = definition_mod_path(db, &def);
return match def { return match def {
Definition::Macro(it) => { Definition::Macro(it) => {
// FIXME: Currently proc-macro do not have ast-node, let label = macro_label(&it.source(db)?.value);
// such that it does not have source
// more discussion: https://github.com/rust-analyzer/rust-analyzer/issues/6913
if it.is_proc_macro() {
return None;
}
#[allow(deprecated)]
let label = macro_label(&it.source_old(db).value);
from_def_source_labeled(db, it, Some(label), mod_path) from_def_source_labeled(db, it, Some(label), mod_path)
} }
Definition::Field(def) => { Definition::Field(def) => {