3367: Fix highlighting of const patterns r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-02-28 15:56:07 +00:00 committed by GitHub
commit c692e07b4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 143 additions and 70 deletions

View file

@ -17,8 +17,8 @@ use crate::{
db::HirDatabase,
source_analyzer::{resolve_hir_path, ReferenceDescriptor, SourceAnalyzer},
source_binder::{ChildContainer, SourceBinder},
Function, HirFileId, InFile, Local, MacroDef, Module, Name, Origin, Path, PathResolution,
ScopeDef, StructField, Trait, Type, TypeParam, VariantDef,
Function, HirFileId, InFile, Local, MacroDef, Module, ModuleDef, Name, Origin, Path,
PathResolution, ScopeDef, StructField, Trait, Type, TypeParam, VariantDef,
};
use hir_expand::ExpansionInfo;
use ra_prof::profile;
@ -129,6 +129,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
self.analyze(path.syntax()).resolve_path(self.db, path)
}
pub fn resolve_bind_pat_to_const(&self, pat: &ast::BindPat) -> Option<ModuleDef> {
self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat)
}
// FIXME: use this instead?
// pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option<???>;

View file

@ -11,9 +11,9 @@ use either::Either;
use hir_def::{
body::{
scope::{ExprScopes, ScopeId},
BodySourceMap,
Body, BodySourceMap,
},
expr::{ExprId, PatId},
expr::{ExprId, Pat, PatId},
resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs},
AsMacroCall, DefWithBodyId,
};
@ -25,8 +25,8 @@ use ra_syntax::{
};
use crate::{
db::HirDatabase, Adt, Const, EnumVariant, Function, Local, MacroDef, Path, Static, Struct,
Trait, Type, TypeAlias, TypeParam,
db::HirDatabase, Adt, Const, EnumVariant, Function, Local, MacroDef, ModuleDef, Path, Static,
Struct, Trait, Type, TypeAlias, TypeParam,
};
/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
@ -35,6 +35,7 @@ use crate::{
pub(crate) struct SourceAnalyzer {
file_id: HirFileId,
pub(crate) resolver: Resolver,
body: Option<Arc<Body>>,
body_source_map: Option<Arc<BodySourceMap>>,
infer: Option<Arc<InferenceResult>>,
scopes: Option<Arc<ExprScopes>>,
@ -66,7 +67,7 @@ impl SourceAnalyzer {
node: InFile<&SyntaxNode>,
offset: Option<TextUnit>,
) -> SourceAnalyzer {
let (_body, source_map) = db.body_with_source_map(def);
let (body, source_map) = db.body_with_source_map(def);
let scopes = db.expr_scopes(def);
let scope = match offset {
None => scope_for(&scopes, &source_map, node),
@ -75,6 +76,7 @@ impl SourceAnalyzer {
let resolver = resolver_for_scope(db, def, scope);
SourceAnalyzer {
resolver,
body: Some(body),
body_source_map: Some(source_map),
infer: Some(db.infer(def)),
scopes: Some(scopes),
@ -88,6 +90,7 @@ impl SourceAnalyzer {
) -> SourceAnalyzer {
SourceAnalyzer {
resolver,
body: None,
body_source_map: None,
infer: None,
scopes: None,
@ -197,6 +200,24 @@ impl SourceAnalyzer {
self.resolver.resolve_path_as_macro(db, path.mod_path()).map(|it| it.into())
}
pub(crate) fn resolve_bind_pat_to_const(
&self,
db: &impl HirDatabase,
pat: &ast::BindPat,
) -> Option<ModuleDef> {
let pat_id = self.pat_id(&pat.clone().into())?;
let body = self.body.as_ref()?;
let path = match &body[pat_id] {
Pat::Path(path) => path,
_ => return None,
};
let res = resolve_hir_path(db, &self.resolver, &path)?;
match res {
PathResolution::Def(def) => Some(def),
_ => None,
}
}
pub(crate) fn resolve_path(
&self,
db: &impl HirDatabase,