mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-28 05:53:45 +00:00
Merge #2270
2270: Reduce some duplication r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
ad10c4b8e2
6 changed files with 14 additions and 10 deletions
|
@ -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)
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -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)?
|
||||||
};
|
};
|
||||||
|
|
|
@ -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()?;
|
||||||
|
|
|
@ -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()];
|
||||||
|
|
|
@ -125,12 +125,12 @@ pub struct ModuleData {
|
||||||
pub impls: Vec<ImplId>,
|
pub impls: Vec<ImplId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, PartialEq, Eq, Clone)]
|
#[derive(Default, Debug, PartialEq, Eq)]
|
||||||
pub(crate) struct Declarations {
|
pub(crate) struct Declarations {
|
||||||
fns: FxHashMap<FileAstId<ast::FnDef>, FunctionId>,
|
fns: FxHashMap<FileAstId<ast::FnDef>, FunctionId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, PartialEq, Eq, Clone)]
|
#[derive(Debug, Default, PartialEq, Eq)]
|
||||||
pub struct ModuleScope {
|
pub struct ModuleScope {
|
||||||
items: FxHashMap<Name, Resolution>,
|
items: FxHashMap<Name, Resolution>,
|
||||||
/// Macros visable in current module in legacy textual scope
|
/// Macros visable in current module in legacy textual scope
|
||||||
|
|
Loading…
Reference in a new issue