From 10726fdb65fda9144a5f9201272d065a268fc1b7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 10 Apr 2019 10:46:43 +0300 Subject: type-safer source-map for bindings --- crates/ra_ide_api/src/completion/complete_path.rs | 8 +++----- crates/ra_ide_api/src/goto_definition.rs | 1 + crates/ra_ide_api/src/references.rs | 11 +++++++---- 3 files changed, 11 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide_api/src') diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index e54fe7b7e..7e47fa6bd 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -1,4 +1,4 @@ -use hir::Resolution; +use hir::{Resolution, Either}; use ra_syntax::AstNode; use test_utils::tested_by; @@ -19,10 +19,8 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { for (name, res) in module_scope.entries() { if Some(module) == ctx.module { if let Some(import) = res.import { - if let hir::ImportSource::UseTree(tree) = - module.import_source(ctx.db, import) - { - if tree.syntax().range().contains_inclusive(ctx.offset) { + if let Either::A(use_tree) = module.import_source(ctx.db, import) { + if use_tree.syntax().range().contains_inclusive(ctx.offset) { // for `use self::foo<|>`, don't suggest `foo` as a completion tested_by!(dont_complete_current_use); continue; diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 660b43cfa..60c1f5085 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -113,6 +113,7 @@ pub(crate) fn reference_definition( let ptr = source_map.pat_syntax(pat).expect("pattern not found in syntax mapping"); let name = path.as_ident().cloned().expect("local binding from a multi-segment path"); + let ptr = ptr.either(|it| it.into(), |it| it.into()); let nav = NavigationTarget::from_scope_entry(file_id, name, ptr); return Exact(nav); } diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 20bbf11a3..3e30e047c 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -1,5 +1,5 @@ use relative_path::{RelativePath, RelativePathBuf}; -use hir::{ModuleSource, source_binder}; +use hir::{ModuleSource, source_binder, Either}; use ra_db::{SourceDatabase}; use ra_syntax::{ AstNode, SyntaxNode, SourceFile, @@ -89,9 +89,12 @@ pub(crate) fn find_all_refs( source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?; let scope = descr.scopes(db); let resolved = scope.resolve_local_name(name_ref)?; - let resolved = resolved.ptr().to_node(source_file); - let binding = find_node_at_offset::(syntax, resolved.range().end())?; - Some((binding, descr)) + if let Either::A(ptr) = resolved.ptr() { + if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file).kind() { + return Some((binding, descr)); + } + } + None } } -- cgit v1.2.3