Move impls of ToNav that use source() to TryToNav

This commit is contained in:
Nick Spain 2021-01-01 17:13:15 +11:00
parent 6800c606ec
commit 3f1b3df65b
5 changed files with 64 additions and 69 deletions

View file

@ -8,7 +8,7 @@ use ide_db::RootDatabase;
use syntax::{ast, match_ast, AstNode, TextRange};
use crate::{
display::ToNav, goto_definition, references, FilePosition, NavigationTarget, RangeInfo,
display::TryToNav, goto_definition, references, FilePosition, NavigationTarget, RangeInfo,
};
#[derive(Debug, Clone)]
@ -61,7 +61,7 @@ pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Optio
match node {
ast::Fn(it) => {
let def = sema.to_def(&it)?;
Some(def.to_nav(sema.db))
def.try_to_nav(sema.db)
},
_ => None,
}
@ -99,7 +99,7 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio
match callable.kind() {
hir::CallableKind::Function(it) => {
let fn_def: hir::Function = it.into();
let nav = fn_def.to_nav(db);
let nav = fn_def.try_to_nav(db)?;
Some(nav)
}
_ => None,
@ -107,7 +107,7 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio
}
FnCallNode::MethodCallExpr(expr) => {
let function = sema.resolve_method_call(&expr)?;
Some(function.to_nav(db))
function.try_to_nav(db)
}
} {
Some((func_target, name_ref.syntax().text_range()))

View file

@ -211,12 +211,12 @@ impl TryToNav for Definition {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
match self {
Definition::Macro(it) => it.try_to_nav(db),
Definition::Field(it) => Some(it.to_nav(db)),
Definition::Field(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) => it.try_to_nav(db),
Definition::Local(it) => Some(it.to_nav(db)),
Definition::TypeParam(it) => Some(it.to_nav(db)),
Definition::LifetimeParam(it) => Some(it.to_nav(db)),
Definition::TypeParam(it) => it.try_to_nav(db),
Definition::LifetimeParam(it) => it.try_to_nav(db),
Definition::Label(it) => Some(it.to_nav(db)),
Definition::ConstParam(it) => Some(it.to_nav(db)),
}
@ -225,18 +225,17 @@ impl TryToNav for Definition {
impl TryToNav for hir::ModuleDef {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let res = match self {
hir::ModuleDef::Module(it) => it.to_nav(db),
hir::ModuleDef::Function(it) => it.to_nav(db),
hir::ModuleDef::Adt(it) => it.to_nav(db),
hir::ModuleDef::Variant(it) => it.to_nav(db),
hir::ModuleDef::Const(it) => it.to_nav(db),
hir::ModuleDef::Static(it) => it.to_nav(db),
hir::ModuleDef::Trait(it) => it.to_nav(db),
hir::ModuleDef::TypeAlias(it) => it.to_nav(db),
hir::ModuleDef::BuiltinType(_) => return None,
};
Some(res)
match self {
hir::ModuleDef::Module(it) => Some(it.to_nav(db)),
hir::ModuleDef::Function(it) => it.try_to_nav(db),
hir::ModuleDef::Adt(it) => it.try_to_nav(db),
hir::ModuleDef::Variant(it) => it.try_to_nav(db),
hir::ModuleDef::Const(it) => it.try_to_nav(db),
hir::ModuleDef::Static(it) => it.try_to_nav(db),
hir::ModuleDef::Trait(it) => it.try_to_nav(db),
hir::ModuleDef::TypeAlias(it) => it.try_to_nav(db),
hir::ModuleDef::BuiltinType(_) => None,
}
}
}
@ -271,14 +270,13 @@ impl ToNavFromAst for hir::Trait {
const KIND: SymbolKind = SymbolKind::Trait;
}
impl<D> ToNav for D
impl<D> TryToNav for D
where
D: HasSource + ToNavFromAst + Copy + HasAttrs,
D::Ast: ast::NameOwner + ShortLabel,
{
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
#[allow(deprecated)]
let src = self.source_old(db);
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let src = self.source(db)?;
let mut res = NavigationTarget::from_named(
db,
src.as_ref().map(|it| it as &dyn ast::NameOwner),
@ -286,7 +284,7 @@ where
);
res.docs = self.docs(db);
res.description = src.value.short_label();
res
Some(res)
}
}
@ -305,10 +303,9 @@ impl ToNav for hir::Module {
}
}
impl ToNav for hir::Impl {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
#[allow(deprecated)]
let src = self.source_old(db);
impl TryToNav for hir::Impl {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let src = self.source(db)?;
let derive_attr = self.is_builtin_derive(db);
let frange = if let Some(item) = &derive_attr {
item.syntax().original_file_range(db)
@ -321,22 +318,21 @@ impl ToNav for hir::Impl {
src.value.self_ty().map(|ty| src.with_value(ty.syntax()).original_file_range(db).range)
};
NavigationTarget::from_syntax(
Some(NavigationTarget::from_syntax(
frange.file_id,
"impl".into(),
focus_range,
frange.range,
SymbolKind::Impl,
)
))
}
}
impl ToNav for hir::Field {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
#[allow(deprecated)]
let src = self.source_old(db);
impl TryToNav for hir::Field {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let src = self.source(db)?;
match &src.value {
let field_source = match &src.value {
FieldSource::Named(it) => {
let mut res =
NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field);
@ -354,7 +350,8 @@ impl ToNav for hir::Field {
SymbolKind::Field,
)
}
}
};
Some(field_source)
}
}
@ -372,22 +369,22 @@ impl TryToNav for hir::MacroDef {
}
}
impl ToNav for hir::Adt {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
impl TryToNav for hir::Adt {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
match self {
hir::Adt::Struct(it) => it.to_nav(db),
hir::Adt::Union(it) => it.to_nav(db),
hir::Adt::Enum(it) => it.to_nav(db),
hir::Adt::Struct(it) => it.try_to_nav(db),
hir::Adt::Union(it) => it.try_to_nav(db),
hir::Adt::Enum(it) => it.try_to_nav(db),
}
}
}
impl ToNav for hir::AssocItem {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
impl TryToNav for hir::AssocItem {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
match self {
AssocItem::Function(it) => it.to_nav(db),
AssocItem::Const(it) => it.to_nav(db),
AssocItem::TypeAlias(it) => it.to_nav(db),
AssocItem::Function(it) => it.try_to_nav(db),
AssocItem::Const(it) => it.try_to_nav(db),
AssocItem::TypeAlias(it) => it.try_to_nav(db),
}
}
}
@ -441,10 +438,9 @@ impl ToNav for hir::Label {
}
}
impl ToNav for hir::TypeParam {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
#[allow(deprecated)]
let src = self.source_old(db);
impl TryToNav for hir::TypeParam {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let src = self.source(db)?;
let full_range = match &src.value {
Either::Left(it) => it.syntax().text_range(),
Either::Right(it) => it.syntax().text_range(),
@ -453,7 +449,7 @@ impl ToNav for hir::TypeParam {
Either::Left(_) => None,
Either::Right(it) => it.name().map(|it| it.syntax().text_range()),
};
NavigationTarget {
Some(NavigationTarget {
file_id: src.file_id.original_file(db),
name: self.name(db).to_string().into(),
kind: Some(SymbolKind::TypeParam),
@ -462,16 +458,15 @@ impl ToNav for hir::TypeParam {
container_name: None,
description: None,
docs: None,
}
})
}
}
impl ToNav for hir::LifetimeParam {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
#[allow(deprecated)]
let src = self.source_old(db);
impl TryToNav for hir::LifetimeParam {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let src = self.source(db)?;
let full_range = src.value.syntax().text_range();
NavigationTarget {
Some(NavigationTarget {
file_id: src.file_id.original_file(db),
name: self.name(db).to_string().into(),
kind: Some(SymbolKind::LifetimeParam),
@ -480,7 +475,7 @@ impl ToNav for hir::LifetimeParam {
container_name: None,
description: None,
docs: None,
}
})
}
}

View file

@ -2,7 +2,7 @@ use hir::{Crate, Impl, Semantics};
use ide_db::RootDatabase;
use syntax::{algo::find_node_at_offset, ast, AstNode};
use crate::{display::ToNav, FilePosition, NavigationTarget, RangeInfo};
use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo};
// Feature: Go to Implementation
//
@ -55,7 +55,7 @@ fn impls_for_def(
impls
.into_iter()
.filter(|impl_def| ty.is_equal_for_find_impls(&impl_def.target_ty(sema.db)))
.map(|imp| imp.to_nav(sema.db))
.filter_map(|imp| imp.try_to_nav(sema.db))
.collect(),
)
}
@ -69,7 +69,7 @@ fn impls_for_trait(
let impls = Impl::for_trait(sema.db, krate, tr);
Some(impls.into_iter().map(|imp| imp.to_nav(sema.db)).collect())
Some(impls.into_iter().filter_map(|imp| imp.try_to_nav(sema.db)).collect())
}
#[cfg(test)]

View file

@ -1,7 +1,7 @@
use ide_db::RootDatabase;
use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T};
use crate::{display::ToNav, FilePosition, NavigationTarget, RangeInfo};
use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo};
// Feature: Go to Type Definition
//
@ -37,7 +37,7 @@ pub(crate) fn goto_type_definition(
let adt_def = ty.autoderef(db).filter_map(|ty| ty.as_adt()).last()?;
let nav = adt_def.to_nav(db);
let nav = adt_def.try_to_nav(db)?;
Some(RangeInfo::new(node.text_range(), vec![nav]))
}

View file

@ -13,7 +13,7 @@ use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset,
use test_utils::mark;
use crate::{
display::{macro_label, ShortLabel, ToNav, TryToNav},
display::{macro_label, ShortLabel, TryToNav},
doc_links::{remove_links, rewrite_links},
markdown_remove::remove_markdown,
markup::Markup,
@ -183,10 +183,10 @@ fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<Hov
match def {
Definition::ModuleDef(it) => match it {
ModuleDef::Adt(Adt::Struct(it)) => Some(to_action(it.to_nav(db))),
ModuleDef::Adt(Adt::Union(it)) => Some(to_action(it.to_nav(db))),
ModuleDef::Adt(Adt::Enum(it)) => Some(to_action(it.to_nav(db))),
ModuleDef::Trait(it) => Some(to_action(it.to_nav(db))),
ModuleDef::Adt(Adt::Struct(it)) => Some(to_action(it.try_to_nav(db)?)),
ModuleDef::Adt(Adt::Union(it)) => Some(to_action(it.try_to_nav(db)?)),
ModuleDef::Adt(Adt::Enum(it)) => Some(to_action(it.try_to_nav(db)?)),
ModuleDef::Trait(it) => Some(to_action(it.try_to_nav(db)?)),
_ => None,
},
_ => None,