Reduce some duplication

This commit is contained in:
Aleksey Kladov 2019-11-16 01:14:56 +03:00
parent 892671926b
commit f36ac059f3
5 changed files with 12 additions and 8 deletions

View file

@ -1,6 +1,5 @@
//! This module defines `AssistCtx` -- the API surface that is exposed to assists. //! This module defines `AssistCtx` -- the API surface that is exposed to assists.
use hir::{db::HirDatabase, SourceAnalyzer};
use hir::db::HirDatabase;
use ra_db::FileRange; use ra_db::FileRange;
use ra_fmt::{leading_indent, reindent}; use ra_fmt::{leading_indent, reindent};
use ra_syntax::{ use ra_syntax::{
@ -113,6 +112,13 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
pub(crate) fn covering_element(&self) -> SyntaxElement { pub(crate) fn covering_element(&self) -> SyntaxElement {
find_covering_element(self.source_file.syntax(), self.frange.range) 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 { pub(crate) fn covering_node_for_range(&self, range: TextRange) -> SyntaxElement {
find_covering_element(self.source_file.syntax(), range) 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 // Infer type
let db = ctx.db; 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)?; let ty = analyzer.type_of(db, &expr)?;
// Assist not applicable if the type is unknown // Assist not applicable if the type is unknown
if is_unknown(&ty) { 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 impl_item_list = impl_node.item_list()?;
let trait_def = { let trait_def = {
let file_id = ctx.frange.file_id; let analyzer = ctx.source_analyzer(impl_node.syntax(), None);
let analyzer = hir::SourceAnalyzer::new(ctx.db, file_id, impl_node.syntax(), None);
resolve_target_trait_def(ctx.db, &analyzer, &impl_node)? 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 expr = match_expr.expr()?;
let enum_def = { let enum_def = {
let file_id = ctx.frange.file_id; let analyzer = ctx.source_analyzer(expr.syntax(), None);
let analyzer = hir::SourceAnalyzer::new(ctx.db, file_id, expr.syntax(), None);
resolve_enum_def(ctx.db, &analyzer, &expr)? resolve_enum_def(ctx.db, &analyzer, &expr)?
}; };
let variant_list = enum_def.variant_list()?; 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 { } else {
let_stmt.syntax().text_range() 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 refs = analyzer.find_all_refs(&bind_pat);
let mut wrap_in_parens = vec![true; refs.len()]; let mut wrap_in_parens = vec![true; refs.len()];