mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
Move explicit range handling out of goto_definition, use OperatorClass instead
This commit is contained in:
parent
3bc6e27993
commit
c7a8be110d
5 changed files with 70 additions and 53 deletions
|
@ -5,14 +5,17 @@
|
|||
|
||||
// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
|
||||
|
||||
use crate::documentation::{Documentation, HasDocs};
|
||||
use crate::famous_defs::FamousDefs;
|
||||
use crate::RootDatabase;
|
||||
use arrayvec::ArrayVec;
|
||||
use either::Either;
|
||||
use hir::{
|
||||
Adt, AsAssocItem, AsExternAssocItem, AssocItem, AttributeTemplate, BuiltinAttr, BuiltinType,
|
||||
Const, Crate, DefWithBody, DeriveHelper, DocLinkDef, ExternAssocItem, ExternCrateDecl, Field,
|
||||
Function, GenericParam, HasVisibility, HirDisplay, Impl, InlineAsmOperand, Label, Local, Macro,
|
||||
Module, ModuleDef, Name, PathResolution, Semantics, Static, StaticLifetime, ToolModule, Trait,
|
||||
TraitAlias, TupleField, TypeAlias, Variant, VariantDef, Visibility,
|
||||
Module, ModuleDef, Name, PathResolution, Semantics, Static, StaticLifetime, Struct, ToolModule,
|
||||
Trait, TraitAlias, TupleField, TypeAlias, Variant, VariantDef, Visibility,
|
||||
};
|
||||
use span::Edition;
|
||||
use stdx::{format_to, impl_from};
|
||||
|
@ -21,10 +24,6 @@ use syntax::{
|
|||
match_ast, SyntaxKind, SyntaxNode, SyntaxToken,
|
||||
};
|
||||
|
||||
use crate::documentation::{Documentation, HasDocs};
|
||||
use crate::famous_defs::FamousDefs;
|
||||
use crate::RootDatabase;
|
||||
|
||||
// FIXME: a more precise name would probably be `Symbol`?
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
|
||||
pub enum Definition {
|
||||
|
@ -319,6 +318,7 @@ impl IdentClass {
|
|||
.map(IdentClass::NameClass)
|
||||
.or_else(|| NameRefClass::classify_lifetime(sema, &lifetime).map(IdentClass::NameRefClass))
|
||||
},
|
||||
ast::RangeExpr(range_expr) => OperatorClass::classify_range(sema, &range_expr).map(IdentClass::Operator),
|
||||
ast::AwaitExpr(await_expr) => OperatorClass::classify_await(sema, &await_expr).map(IdentClass::Operator),
|
||||
ast::BinExpr(bin_expr) => OperatorClass::classify_bin(sema, &bin_expr).map(IdentClass::Operator),
|
||||
ast::IndexExpr(index_expr) => OperatorClass::classify_index(sema, &index_expr).map(IdentClass::Operator),
|
||||
|
@ -372,6 +372,9 @@ impl IdentClass {
|
|||
| OperatorClass::Index(func)
|
||||
| OperatorClass::Try(func),
|
||||
) => res.push(Definition::Function(func)),
|
||||
IdentClass::Operator(OperatorClass::Range(struct0)) => {
|
||||
res.push(Definition::Adt(Adt::Struct(struct0)))
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
|
@ -546,6 +549,7 @@ impl NameClass {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum OperatorClass {
|
||||
Range(Struct),
|
||||
Await(Function),
|
||||
Prefix(Function),
|
||||
Index(Function),
|
||||
|
@ -554,6 +558,13 @@ pub enum OperatorClass {
|
|||
}
|
||||
|
||||
impl OperatorClass {
|
||||
pub fn classify_range(
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
range_expr: &ast::RangeExpr,
|
||||
) -> Option<OperatorClass> {
|
||||
sema.resolve_range_expr(range_expr).map(OperatorClass::Range)
|
||||
}
|
||||
|
||||
pub fn classify_await(
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
await_expr: &ast::AwaitExpr,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//! See [`FamousDefs`].
|
||||
|
||||
use base_db::{CrateOrigin, LangCrateOrigin, SourceDatabase};
|
||||
use hir::{Crate, Enum, Function, Macro, Module, ScopeDef, Semantics, Struct, Trait};
|
||||
use hir::{Crate, Enum, Function, Macro, Module, ScopeDef, Semantics, Trait};
|
||||
|
||||
use crate::RootDatabase;
|
||||
|
||||
|
@ -102,14 +102,6 @@ impl FamousDefs<'_, '_> {
|
|||
self.find_trait("core:ops:Drop")
|
||||
}
|
||||
|
||||
pub fn core_ops_Range(&self) -> Option<Struct> {
|
||||
self.find_struct("core:ops:Range")
|
||||
}
|
||||
|
||||
pub fn core_ops_RangeInclusive(&self) -> Option<Struct> {
|
||||
self.find_struct("core:ops:RangeInclusive")
|
||||
}
|
||||
|
||||
pub fn core_marker_Copy(&self) -> Option<Trait> {
|
||||
self.find_trait("core:marker:Copy")
|
||||
}
|
||||
|
@ -145,13 +137,6 @@ impl FamousDefs<'_, '_> {
|
|||
.flatten()
|
||||
}
|
||||
|
||||
fn find_struct(&self, path: &str) -> Option<Struct> {
|
||||
match self.find_def(path)? {
|
||||
hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Struct(it))) => Some(it),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn find_trait(&self, path: &str) -> Option<Trait> {
|
||||
match self.find_def(path)? {
|
||||
hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue