This commit is contained in:
Aleksey Kladov 2019-04-13 11:32:58 +03:00
parent b260641e0c
commit 2facb5e061
2 changed files with 12 additions and 13 deletions

View file

@ -1,7 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use ra_syntax::TextRange;
use ra_arena::{Arena, RawId, impl_arena_id}; use ra_arena::{Arena, RawId, impl_arena_id};
use crate::{ use crate::{
@ -171,22 +170,14 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
}; };
} }
#[derive(Debug)]
pub struct ReferenceDescriptor {
pub range: TextRange,
pub name: String,
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ra_db::SourceDatabase; use ra_db::SourceDatabase;
use ra_syntax::{algo::find_node_at_offset, AstNode, SyntaxNodePtr}; use ra_syntax::{algo::find_node_at_offset, AstNode, SyntaxNodePtr, ast};
use test_utils::{extract_offset, assert_eq_text}; use test_utils::{extract_offset, assert_eq_text};
use crate::{source_binder::SourceAnalyzer, mock::MockDatabase}; use crate::{source_binder::SourceAnalyzer, mock::MockDatabase};
use super::*;
fn do_check(code: &str, expected: &[&str]) { fn do_check(code: &str, expected: &[&str]) {
let (off, code) = extract_offset(code); let (off, code) = extract_offset(code);
let code = { let code = {

View file

@ -10,7 +10,7 @@ use std::sync::Arc;
use rustc_hash::{FxHashSet, FxHashMap}; use rustc_hash::{FxHashSet, FxHashMap};
use ra_db::{FileId, FilePosition}; use ra_db::{FileId, FilePosition};
use ra_syntax::{ use ra_syntax::{
SyntaxNode, AstPtr, TextUnit, SyntaxNodePtr, SyntaxNode, AstPtr, TextUnit, SyntaxNodePtr, TextRange,
ast::{self, AstNode, NameOwner}, ast::{self, AstNode, NameOwner},
algo::find_node_at_offset, algo::find_node_at_offset,
SyntaxKind::*, SyntaxKind::*,
@ -19,7 +19,7 @@ use ra_syntax::{
use crate::{ use crate::{
HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name, HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name,
AsName, Module, HirFileId, Crate, Trait, Resolver, AsName, Module, HirFileId, Crate, Trait, Resolver,
expr::{BodySourceMap, scope::{ReferenceDescriptor, ScopeId, ExprScopes}}, expr::{BodySourceMap, scope::{ScopeId, ExprScopes}},
ids::LocationCtx, ids::LocationCtx,
expr, AstId expr, AstId
}; };
@ -203,6 +203,12 @@ impl ScopeEntryWithSyntax {
} }
} }
#[derive(Debug)]
pub struct ReferenceDescriptor {
pub range: TextRange,
pub name: String,
}
impl SourceAnalyzer { impl SourceAnalyzer {
pub fn new( pub fn new(
db: &impl HirDatabase, db: &impl HirDatabase,
@ -318,6 +324,8 @@ impl SourceAnalyzer {
} }
pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> { pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> {
// FIXME: at least, this should work with any DefWithBody, but ideally
// this should be hir-based altogether
let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
let ptr = Either::A(AstPtr::new(pat.into())); let ptr = Either::A(AstPtr::new(pat.into()));
fn_def fn_def
@ -329,7 +337,7 @@ impl SourceAnalyzer {
Some(entry) => entry.ptr() == ptr, Some(entry) => entry.ptr() == ptr,
}) })
.map(|name_ref| ReferenceDescriptor { .map(|name_ref| ReferenceDescriptor {
name: name_ref.syntax().text().to_string(), name: name_ref.text().to_string(),
range: name_ref.syntax().range(), range: name_ref.syntax().range(),
}) })
.collect() .collect()