type-safer source-map for bindings

This commit is contained in:
Aleksey Kladov 2019-04-10 10:46:43 +03:00
parent 1cd184d653
commit 10726fdb65
7 changed files with 45 additions and 30 deletions

View file

@ -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;

View file

@ -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);
}

View file

@ -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::<ast::BindPat>(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
}
}