2270: Reduce some duplication r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-11-15 22:15:30 +00:00 committed by GitHub
commit ad10c4b8e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 10 deletions

View File

@ -1,6 +1,5 @@
//! This module defines `AssistCtx` -- the API surface that is exposed to assists.
use hir::db::HirDatabase;
use hir::{db::HirDatabase, SourceAnalyzer};
use ra_db::FileRange;
use ra_fmt::{leading_indent, reindent};
use ra_syntax::{
@ -113,6 +112,13 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
pub(crate) fn covering_element(&self) -> SyntaxElement {
find_covering_element(self.source_file.syntax(), self.frange.range)
}
pub(crate) fn source_analyzer(
&self,
node: &SyntaxNode,
offset: Option<TextUnit>,
) -> SourceAnalyzer {
SourceAnalyzer::new(self.db, self.frange.file_id, node, offset)
}
pub(crate) fn covering_node_for_range(&self, range: TextRange) -> SyntaxElement {
find_covering_element(self.source_file.syntax(), range)

View File

@ -40,7 +40,7 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi
}
// Infer type
let db = ctx.db;
let analyzer = hir::SourceAnalyzer::new(db, ctx.frange.file_id, stmt.syntax(), None);
let analyzer = ctx.source_analyzer(stmt.syntax(), None);
let ty = analyzer.type_of(db, &expr)?;
// Assist not applicable if the type is unknown
if is_unknown(&ty) {

View File

@ -100,8 +100,7 @@ fn add_missing_impl_members_inner(
let impl_item_list = impl_node.item_list()?;
let trait_def = {
let file_id = ctx.frange.file_id;
let analyzer = hir::SourceAnalyzer::new(ctx.db, file_id, impl_node.syntax(), None);
let analyzer = ctx.source_analyzer(impl_node.syntax(), None);
resolve_target_trait_def(ctx.db, &analyzer, &impl_node)?
};

View File

@ -47,8 +47,7 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
let expr = match_expr.expr()?;
let enum_def = {
let file_id = ctx.frange.file_id;
let analyzer = hir::SourceAnalyzer::new(ctx.db, file_id, expr.syntax(), None);
let analyzer = ctx.source_analyzer(expr.syntax(), None);
resolve_enum_def(ctx.db, &analyzer, &expr)?
};
let variant_list = enum_def.variant_list()?;

View File

@ -45,7 +45,7 @@ pub(crate) fn inline_local_varialbe(ctx: AssistCtx<impl HirDatabase>) -> Option<
} else {
let_stmt.syntax().text_range()
};
let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, bind_pat.syntax(), None);
let analyzer = ctx.source_analyzer(bind_pat.syntax(), None);
let refs = analyzer.find_all_refs(&bind_pat);
let mut wrap_in_parens = vec![true; refs.len()];

View File

@ -125,12 +125,12 @@ pub struct ModuleData {
pub impls: Vec<ImplId>,
}
#[derive(Default, Debug, PartialEq, Eq, Clone)]
#[derive(Default, Debug, PartialEq, Eq)]
pub(crate) struct Declarations {
fns: FxHashMap<FileAstId<ast::FnDef>, FunctionId>,
}
#[derive(Debug, Default, PartialEq, Eq, Clone)]
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ModuleScope {
items: FxHashMap<Name, Resolution>,
/// Macros visable in current module in legacy textual scope