mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-14 17:07:26 +00:00
Reuse Semantics instances
This commit is contained in:
parent
ec8b4dca02
commit
9710ad8c48
7 changed files with 39 additions and 39 deletions
|
@ -53,7 +53,7 @@ pub(crate) fn extract_struct_from_enum_variant(
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
let definition = Definition::ModuleDef(ModuleDef::EnumVariant(variant_hir));
|
let definition = Definition::ModuleDef(ModuleDef::EnumVariant(variant_hir));
|
||||||
let res = definition.find_usages(&ctx.db(), None);
|
let res = definition.find_usages(&ctx.sema, None);
|
||||||
let start_offset = variant.parent_enum().syntax().text_range().start();
|
let start_offset = variant.parent_enum().syntax().text_range().start();
|
||||||
let mut visited_modules_set = FxHashSet::default();
|
let mut visited_modules_set = FxHashSet::default();
|
||||||
visited_modules_set.insert(current_module);
|
visited_modules_set.insert(current_module);
|
||||||
|
|
|
@ -44,7 +44,7 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O
|
||||||
|
|
||||||
let def = ctx.sema.to_def(&bind_pat)?;
|
let def = ctx.sema.to_def(&bind_pat)?;
|
||||||
let def = Definition::Local(def);
|
let def = Definition::Local(def);
|
||||||
let refs = def.find_usages(ctx.db(), None);
|
let refs = def.find_usages(&ctx.sema, None);
|
||||||
if refs.is_empty() {
|
if refs.is_empty() {
|
||||||
mark::hit!(test_not_applicable_if_variable_unused);
|
mark::hit!(test_not_applicable_if_variable_unused);
|
||||||
return None;
|
return None;
|
||||||
|
|
|
@ -39,10 +39,11 @@ pub(crate) fn call_hierarchy(
|
||||||
|
|
||||||
pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Option<Vec<CallItem>> {
|
pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Option<Vec<CallItem>> {
|
||||||
let sema = Semantics::new(db);
|
let sema = Semantics::new(db);
|
||||||
|
|
||||||
// 1. Find all refs
|
// 1. Find all refs
|
||||||
// 2. Loop through refs and determine unique fndef. This will become our `from: CallHierarchyItem,` in the reply.
|
// 2. Loop through refs and determine unique fndef. This will become our `from: CallHierarchyItem,` in the reply.
|
||||||
// 3. Add ranges relative to the start of the fndef.
|
// 3. Add ranges relative to the start of the fndef.
|
||||||
let refs = references::find_all_refs(db, position, None)?;
|
let refs = references::find_all_refs(&sema, position, None)?;
|
||||||
|
|
||||||
let mut calls = CallLocations::default();
|
let mut calls = CallLocations::default();
|
||||||
|
|
||||||
|
|
|
@ -75,7 +75,7 @@ pub use crate::{
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use hir::Documentation;
|
pub use hir::{Documentation, Semantics};
|
||||||
pub use ra_assists::{Assist, AssistConfig, AssistId, ResolvedAssist};
|
pub use ra_assists::{Assist, AssistConfig, AssistId, ResolvedAssist};
|
||||||
pub use ra_db::{
|
pub use ra_db::{
|
||||||
Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRoot,
|
Canceled, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange, SourceRoot,
|
||||||
|
@ -385,7 +385,9 @@ impl Analysis {
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
search_scope: Option<SearchScope>,
|
search_scope: Option<SearchScope>,
|
||||||
) -> Cancelable<Option<ReferenceSearchResult>> {
|
) -> Cancelable<Option<ReferenceSearchResult>> {
|
||||||
self.with_db(|db| references::find_all_refs(db, position, search_scope).map(|it| it.info))
|
self.with_db(|db| {
|
||||||
|
references::find_all_refs(&Semantics::new(db), position, search_scope).map(|it| it.info)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a short text describing element at position.
|
/// Returns a short text describing element at position.
|
||||||
|
|
|
@ -86,12 +86,11 @@ impl IntoIterator for ReferenceSearchResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn find_all_refs(
|
pub(crate) fn find_all_refs(
|
||||||
db: &RootDatabase,
|
sema: &Semantics<RootDatabase>,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
search_scope: Option<SearchScope>,
|
search_scope: Option<SearchScope>,
|
||||||
) -> Option<RangeInfo<ReferenceSearchResult>> {
|
) -> Option<RangeInfo<ReferenceSearchResult>> {
|
||||||
let _p = profile("find_all_refs");
|
let _p = profile("find_all_refs");
|
||||||
let sema = Semantics::new(db);
|
|
||||||
let syntax = sema.parse(position.file_id).syntax().clone();
|
let syntax = sema.parse(position.file_id).syntax().clone();
|
||||||
|
|
||||||
let (opt_name, search_kind) = if let Some(name) =
|
let (opt_name, search_kind) = if let Some(name) =
|
||||||
|
@ -108,15 +107,15 @@ pub(crate) fn find_all_refs(
|
||||||
let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?;
|
let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?;
|
||||||
|
|
||||||
let references = def
|
let references = def
|
||||||
.find_usages(db, search_scope)
|
.find_usages(sema, search_scope)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind)
|
.filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let decl_range = def.try_to_nav(db)?.range();
|
let decl_range = def.try_to_nav(sema.db)?.range();
|
||||||
|
|
||||||
let declaration = Declaration {
|
let declaration = Declaration {
|
||||||
nav: def.try_to_nav(db)?,
|
nav: def.try_to_nav(sema.db)?,
|
||||||
kind: ReferenceKind::Other,
|
kind: ReferenceKind::Other,
|
||||||
access: decl_access(&def, &syntax, decl_range),
|
access: decl_access(&def, &syntax, decl_range),
|
||||||
};
|
};
|
||||||
|
|
|
@ -24,23 +24,24 @@ pub(crate) fn rename(
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
new_name: &str,
|
new_name: &str,
|
||||||
) -> Option<RangeInfo<SourceChange>> {
|
) -> Option<RangeInfo<SourceChange>> {
|
||||||
|
let sema = Semantics::new(db);
|
||||||
|
|
||||||
match lex_single_valid_syntax_kind(new_name)? {
|
match lex_single_valid_syntax_kind(new_name)? {
|
||||||
SyntaxKind::IDENT | SyntaxKind::UNDERSCORE => (),
|
SyntaxKind::IDENT | SyntaxKind::UNDERSCORE => (),
|
||||||
SyntaxKind::SELF_KW => return rename_to_self(db, position),
|
SyntaxKind::SELF_KW => return rename_to_self(&sema, position),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
|
|
||||||
let sema = Semantics::new(db);
|
|
||||||
let source_file = sema.parse(position.file_id);
|
let source_file = sema.parse(position.file_id);
|
||||||
let syntax = source_file.syntax();
|
let syntax = source_file.syntax();
|
||||||
if let Some(module) = find_module_at_offset(&sema, position, syntax) {
|
if let Some(module) = find_module_at_offset(&sema, position, syntax) {
|
||||||
rename_mod(db, position, module, new_name)
|
rename_mod(&sema, position, module, new_name)
|
||||||
} else if let Some(self_token) =
|
} else if let Some(self_token) =
|
||||||
syntax.token_at_offset(position.offset).find(|t| t.kind() == SyntaxKind::SELF_KW)
|
syntax.token_at_offset(position.offset).find(|t| t.kind() == SyntaxKind::SELF_KW)
|
||||||
{
|
{
|
||||||
rename_self_to_param(db, position, self_token, new_name)
|
rename_self_to_param(&sema, position, self_token, new_name)
|
||||||
} else {
|
} else {
|
||||||
rename_reference(sema.db, position, new_name)
|
rename_reference(&sema, position, new_name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,7 +98,7 @@ fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFil
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename_mod(
|
fn rename_mod(
|
||||||
db: &RootDatabase,
|
sema: &Semantics<RootDatabase>,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
module: Module,
|
module: Module,
|
||||||
new_name: &str,
|
new_name: &str,
|
||||||
|
@ -105,12 +106,12 @@ fn rename_mod(
|
||||||
let mut source_file_edits = Vec::new();
|
let mut source_file_edits = Vec::new();
|
||||||
let mut file_system_edits = Vec::new();
|
let mut file_system_edits = Vec::new();
|
||||||
|
|
||||||
let src = module.definition_source(db);
|
let src = module.definition_source(sema.db);
|
||||||
let file_id = src.file_id.original_file(db);
|
let file_id = src.file_id.original_file(sema.db);
|
||||||
match src.value {
|
match src.value {
|
||||||
ModuleSource::SourceFile(..) => {
|
ModuleSource::SourceFile(..) => {
|
||||||
// mod is defined in path/to/dir/mod.rs
|
// mod is defined in path/to/dir/mod.rs
|
||||||
let dst = if module.is_mod_rs(db) {
|
let dst = if module.is_mod_rs(sema.db) {
|
||||||
format!("../{}/mod.rs", new_name)
|
format!("../{}/mod.rs", new_name)
|
||||||
} else {
|
} else {
|
||||||
format!("{}.rs", new_name)
|
format!("{}.rs", new_name)
|
||||||
|
@ -122,17 +123,17 @@ fn rename_mod(
|
||||||
ModuleSource::Module(..) => {}
|
ModuleSource::Module(..) => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(src) = module.declaration_source(db) {
|
if let Some(src) = module.declaration_source(sema.db) {
|
||||||
let file_id = src.file_id.original_file(db);
|
let file_id = src.file_id.original_file(sema.db);
|
||||||
let name = src.value.name()?;
|
let name = src.value.name()?;
|
||||||
let edit = SourceFileEdit {
|
let edit = SourceFileEdit {
|
||||||
file_id: file_id,
|
file_id,
|
||||||
edit: TextEdit::replace(name.syntax().text_range(), new_name.into()),
|
edit: TextEdit::replace(name.syntax().text_range(), new_name.into()),
|
||||||
};
|
};
|
||||||
source_file_edits.push(edit);
|
source_file_edits.push(edit);
|
||||||
}
|
}
|
||||||
|
|
||||||
let RangeInfo { range, info: refs } = find_all_refs(db, position, None)?;
|
let RangeInfo { range, info: refs } = find_all_refs(sema, position, None)?;
|
||||||
let ref_edits = refs
|
let ref_edits = refs
|
||||||
.references
|
.references
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -142,8 +143,10 @@ fn rename_mod(
|
||||||
Some(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits)))
|
Some(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename_to_self(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo<SourceChange>> {
|
fn rename_to_self(
|
||||||
let sema = Semantics::new(db);
|
sema: &Semantics<RootDatabase>,
|
||||||
|
position: FilePosition,
|
||||||
|
) -> Option<RangeInfo<SourceChange>> {
|
||||||
let source_file = sema.parse(position.file_id);
|
let source_file = sema.parse(position.file_id);
|
||||||
let syn = source_file.syntax();
|
let syn = source_file.syntax();
|
||||||
|
|
||||||
|
@ -158,7 +161,7 @@ fn rename_to_self(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo
|
||||||
_ => return None, // not renaming other types
|
_ => return None, // not renaming other types
|
||||||
};
|
};
|
||||||
|
|
||||||
let RangeInfo { range, info: refs } = find_all_refs(db, position, None)?;
|
let RangeInfo { range, info: refs } = find_all_refs(sema, position, None)?;
|
||||||
|
|
||||||
let param_range = first_param.syntax().text_range();
|
let param_range = first_param.syntax().text_range();
|
||||||
let (param_ref, usages): (Vec<Reference>, Vec<Reference>) = refs
|
let (param_ref, usages): (Vec<Reference>, Vec<Reference>) = refs
|
||||||
|
@ -210,16 +213,15 @@ fn text_edit_from_self_param(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename_self_to_param(
|
fn rename_self_to_param(
|
||||||
db: &RootDatabase,
|
sema: &Semantics<RootDatabase>,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
self_token: SyntaxToken,
|
self_token: SyntaxToken,
|
||||||
new_name: &str,
|
new_name: &str,
|
||||||
) -> Option<RangeInfo<SourceChange>> {
|
) -> Option<RangeInfo<SourceChange>> {
|
||||||
let sema = Semantics::new(db);
|
|
||||||
let source_file = sema.parse(position.file_id);
|
let source_file = sema.parse(position.file_id);
|
||||||
let syn = source_file.syntax();
|
let syn = source_file.syntax();
|
||||||
|
|
||||||
let text = db.file_text(position.file_id);
|
let text = sema.db.file_text(position.file_id);
|
||||||
let fn_def = find_node_at_offset::<ast::FnDef>(syn, position.offset)?;
|
let fn_def = find_node_at_offset::<ast::FnDef>(syn, position.offset)?;
|
||||||
let search_range = fn_def.syntax().text_range();
|
let search_range = fn_def.syntax().text_range();
|
||||||
|
|
||||||
|
@ -249,11 +251,11 @@ fn rename_self_to_param(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename_reference(
|
fn rename_reference(
|
||||||
db: &RootDatabase,
|
sema: &Semantics<RootDatabase>,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
new_name: &str,
|
new_name: &str,
|
||||||
) -> Option<RangeInfo<SourceChange>> {
|
) -> Option<RangeInfo<SourceChange>> {
|
||||||
let RangeInfo { range, info: refs } = find_all_refs(db, position, None)?;
|
let RangeInfo { range, info: refs } = find_all_refs(sema, position, None)?;
|
||||||
|
|
||||||
let edit = refs
|
let edit = refs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
|
@ -180,20 +180,20 @@ impl Definition {
|
||||||
|
|
||||||
pub fn find_usages(
|
pub fn find_usages(
|
||||||
&self,
|
&self,
|
||||||
db: &RootDatabase,
|
sema: &Semantics<RootDatabase>,
|
||||||
search_scope: Option<SearchScope>,
|
search_scope: Option<SearchScope>,
|
||||||
) -> Vec<Reference> {
|
) -> Vec<Reference> {
|
||||||
let _p = profile("Definition::find_usages");
|
let _p = profile("Definition::find_usages");
|
||||||
|
|
||||||
let search_scope = {
|
let search_scope = {
|
||||||
let base = self.search_scope(db);
|
let base = self.search_scope(sema.db);
|
||||||
match search_scope {
|
match search_scope {
|
||||||
None => base,
|
None => base,
|
||||||
Some(scope) => base.intersection(&scope),
|
Some(scope) => base.intersection(&scope),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let name = match self.name(db) {
|
let name = match self.name(sema.db) {
|
||||||
None => return Vec::new(),
|
None => return Vec::new(),
|
||||||
Some(it) => it.to_string(),
|
Some(it) => it.to_string(),
|
||||||
};
|
};
|
||||||
|
@ -202,11 +202,10 @@ impl Definition {
|
||||||
let mut refs = vec![];
|
let mut refs = vec![];
|
||||||
|
|
||||||
for (file_id, search_range) in search_scope {
|
for (file_id, search_range) in search_scope {
|
||||||
let text = db.file_text(file_id);
|
let text = sema.db.file_text(file_id);
|
||||||
let search_range =
|
let search_range =
|
||||||
search_range.unwrap_or(TextRange::up_to(TextSize::of(text.as_str())));
|
search_range.unwrap_or(TextRange::up_to(TextSize::of(text.as_str())));
|
||||||
|
|
||||||
let sema = Semantics::new(db);
|
|
||||||
let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
|
let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
|
||||||
|
|
||||||
for (idx, _) in text.match_indices(pat) {
|
for (idx, _) in text.match_indices(pat) {
|
||||||
|
@ -222,9 +221,6 @@ impl Definition {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME: reuse sb
|
|
||||||
// See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098
|
|
||||||
|
|
||||||
match classify_name_ref(&sema, &name_ref) {
|
match classify_name_ref(&sema, &name_ref) {
|
||||||
Some(NameRefClass::Definition(def)) if &def == self => {
|
Some(NameRefClass::Definition(def)) if &def == self => {
|
||||||
let kind = if is_record_lit_name_ref(&name_ref)
|
let kind = if is_record_lit_name_ref(&name_ref)
|
||||||
|
|
Loading…
Reference in a new issue