Make AssistContext::frange private

This commit is contained in:
Lukas Wirth 2021-10-13 14:39:37 +02:00
parent 086563f751
commit ccad89a2db
15 changed files with 31 additions and 24 deletions

View file

@ -56,7 +56,7 @@ use crate::{
pub(crate) struct AssistContext<'a> { pub(crate) struct AssistContext<'a> {
pub(crate) config: &'a AssistConfig, pub(crate) config: &'a AssistConfig,
pub(crate) sema: Semantics<'a, RootDatabase>, pub(crate) sema: Semantics<'a, RootDatabase>,
pub(crate) frange: FileRange, frange: FileRange,
trimmed_range: TextRange, trimmed_range: TextRange,
source_file: SourceFile, source_file: SourceFile,
} }
@ -98,6 +98,14 @@ impl<'a> AssistContext<'a> {
self.frange.range.start() self.frange.range.start()
} }
pub(crate) fn file_id(&self) -> FileId {
self.frange.file_id
}
pub(crate) fn has_empty_selection(&self) -> bool {
self.trimmed_range.is_empty()
}
/// Returns the selected range trimmed for whitespace tokens, that is the range will be snapped /// Returns the selected range trimmed for whitespace tokens, that is the range will be snapped
/// to the nearest enclosed token. /// to the nearest enclosed token.
pub(crate) fn selection_trimmed(&self) -> TextRange { pub(crate) fn selection_trimmed(&self) -> TextRange {
@ -125,7 +133,6 @@ impl<'a> AssistContext<'a> {
/// Returns the element covered by the selection range, this excludes trailing whitespace in the selection. /// Returns the element covered by the selection range, this excludes trailing whitespace in the selection.
pub(crate) fn covering_element(&self) -> SyntaxElement { pub(crate) fn covering_element(&self) -> SyntaxElement {
self.source_file.syntax().covering_element(self.selection_trimmed()) self.source_file.syntax().covering_element(self.selection_trimmed())
// self.source_file.syntax().covering_element(self.frange.range)
} }
} }

View file

@ -92,7 +92,7 @@ fn edit_struct_def(
let record_fields = ast::make::record_field_list(record_fields); let record_fields = ast::make::record_field_list(record_fields);
let tuple_fields_text_range = tuple_fields.syntax().text_range(); let tuple_fields_text_range = tuple_fields.syntax().text_range();
edit.edit_file(ctx.frange.file_id); edit.edit_file(ctx.file_id());
if let Either::Left(strukt) = strukt { if let Either::Left(strukt) = strukt {
if let Some(w) = strukt.where_clause() { if let Some(w) = strukt.where_clause() {

View file

@ -115,7 +115,7 @@ fn collect_data(ident_pat: IdentPat, ctx: &AssistContext) -> Option<TupleData> {
let usages = ctx.sema.to_def(&ident_pat).map(|def| { let usages = ctx.sema.to_def(&ident_pat).map(|def| {
Definition::Local(def) Definition::Local(def)
.usages(&ctx.sema) .usages(&ctx.sema)
.in_scope(SearchScope::single_file(ctx.frange.file_id)) .in_scope(SearchScope::single_file(ctx.file_id()))
.all() .all()
}); });

View file

@ -125,7 +125,7 @@ impl Def {
Def::MacroDef(def) => Definition::Macro(*def), Def::MacroDef(def) => Definition::Macro(*def),
}; };
let search_scope = SearchScope::single_file(ctx.frange.file_id); let search_scope = SearchScope::single_file(ctx.file_id());
def.usages(&ctx.sema).in_scope(search_scope).at_least_one() def.usages(&ctx.sema).in_scope(search_scope).at_least_one()
} }
} }

View file

@ -309,7 +309,7 @@ impl LocalUsages {
Self( Self(
Definition::Local(var) Definition::Local(var)
.usages(&ctx.sema) .usages(&ctx.sema)
.in_scope(SearchScope::single_file(ctx.frange.file_id)) .in_scope(SearchScope::single_file(ctx.file_id()))
.all(), .all(),
) )
} }
@ -1039,7 +1039,7 @@ fn is_defined_outside_of_body(
body: &FunctionBody, body: &FunctionBody,
src: &hir::InFile<Either<ast::IdentPat, ast::SelfParam>>, src: &hir::InFile<Either<ast::IdentPat, ast::SelfParam>>,
) -> bool { ) -> bool {
src.file_id.original_file(ctx.db()) == ctx.frange.file_id src.file_id.original_file(ctx.db()) == ctx.file_id()
&& !body.contains_node(either_syntax(&src.value)) && !body.contains_node(either_syntax(&src.value))
} }

View file

@ -72,7 +72,7 @@ pub(crate) fn extract_struct_from_enum_variant(
// record file references of the file the def resides in, we only want to swap to the edited file in the builder once // record file references of the file the def resides in, we only want to swap to the edited file in the builder once
let mut def_file_references = None; let mut def_file_references = None;
for (file_id, references) in usages { for (file_id, references) in usages {
if file_id == ctx.frange.file_id { if file_id == ctx.file_id() {
def_file_references = Some(references); def_file_references = Some(references);
continue; continue;
} }
@ -89,7 +89,7 @@ pub(crate) fn extract_struct_from_enum_variant(
apply_references(ctx.config.insert_use, path, node, import) apply_references(ctx.config.insert_use, path, node, import)
}); });
} }
builder.edit_file(ctx.frange.file_id); builder.edit_file(ctx.file_id());
let variant = builder.make_mut(variant.clone()); let variant = builder.make_mut(variant.clone());
if let Some(references) = def_file_references { if let Some(references) = def_file_references {

View file

@ -26,7 +26,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
// } // }
// ``` // ```
pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
if ctx.frange.range.is_empty() { if ctx.has_empty_selection() {
return None; return None;
} }

View file

@ -28,7 +28,7 @@ use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
// } // }
// ``` // ```
pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
if ctx.frange.range.is_empty() { if ctx.has_empty_selection() {
return None; return None;
} }
@ -43,7 +43,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option
let node = node.ancestors().take_while(|anc| anc.text_range() == node.text_range()).last()?; let node = node.ancestors().take_while(|anc| anc.text_range() == node.text_range()).last()?;
let to_extract = node let to_extract = node
.descendants() .descendants()
.take_while(|it| ctx.frange.range.contains_range(it.text_range())) .take_while(|it| ctx.selection_trimmed().contains_range(it.text_range()))
.find_map(valid_target_expr)?; .find_map(valid_target_expr)?;
if let Some(ty_info) = ctx.sema.type_of_expr(&to_extract) { if let Some(ty_info) = ctx.sema.type_of_expr(&to_extract) {

View file

@ -364,7 +364,7 @@ fn get_fn_target(
target_module: &Option<Module>, target_module: &Option<Module>,
call: CallExpr, call: CallExpr,
) -> Option<(GeneratedFunctionTarget, FileId, TextSize)> { ) -> Option<(GeneratedFunctionTarget, FileId, TextSize)> {
let mut file = ctx.frange.file_id; let mut file = ctx.file_id();
let target = match target_module { let target = match target_module {
Some(target_module) => { Some(target_module) => {
let module_source = target_module.definition_source(ctx.db()); let module_source = target_module.definition_source(ctx.db());

View file

@ -59,7 +59,7 @@ use crate::{
// } // }
// ``` // ```
pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let def_file = ctx.frange.file_id; let def_file = ctx.file_id();
let name = ctx.find_node_at_offset::<ast::Name>()?; let name = ctx.find_node_at_offset::<ast::Name>()?;
let ast_func = name.syntax().parent().and_then(ast::Fn::cast)?; let ast_func = name.syntax().parent().and_then(ast::Fn::cast)?;
let func_body = ast_func.body()?; let func_body = ast_func.body()?;
@ -199,7 +199,7 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
let param_list = fn_source.value.param_list()?; let param_list = fn_source.value.param_list()?;
let FileRange { file_id, range } = fn_source.syntax().original_file_range(ctx.sema.db); let FileRange { file_id, range } = fn_source.syntax().original_file_range(ctx.sema.db);
if file_id == ctx.frange.file_id && range.contains(ctx.offset()) { if file_id == ctx.file_id() && range.contains(ctx.offset()) {
cov_mark::hit!(inline_call_recursive); cov_mark::hit!(inline_call_recursive);
return None; return None;
} }

View file

@ -33,7 +33,7 @@ use crate::{
// } // }
// ``` // ```
pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let file_id = ctx.frange.file_id; let file_id = ctx.file_id();
let range = ctx.selection_trimmed(); let range = ctx.selection_trimmed();
let InlineData { let_stmt, delete_let, references, target } = let InlineData { let_stmt, delete_let, references, target } =
if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() { if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() {

View file

@ -25,7 +25,7 @@ use crate::{
// ``` // ```
pub(crate) fn move_from_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn move_from_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?; let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?;
let module = ctx.sema.to_module_def(ctx.frange.file_id)?; let module = ctx.sema.to_module_def(ctx.file_id())?;
// Enable this assist if the user select all "meaningful" content in the source file // Enable this assist if the user select all "meaningful" content in the source file
let trimmed_selected_range = trimmed_text_range(&source_file, ctx.selection_trimmed()); let trimmed_selected_range = trimmed_text_range(&source_file, ctx.selection_trimmed());
let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range()); let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range());
@ -41,13 +41,13 @@ pub(crate) fn move_from_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option
let target = source_file.syntax().text_range(); let target = source_file.syntax().text_range();
let module_name = module.name(ctx.db())?.to_string(); let module_name = module.name(ctx.db())?.to_string();
let path = format!("../{}.rs", module_name); let path = format!("../{}.rs", module_name);
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; let dst = AnchoredPathBuf { anchor: ctx.file_id(), path };
acc.add( acc.add(
AssistId("move_from_mod_rs", AssistKind::Refactor), AssistId("move_from_mod_rs", AssistKind::Refactor),
format!("Convert {}/mod.rs to {}.rs", module_name, module_name), format!("Convert {}/mod.rs to {}.rs", module_name, module_name),
target, target,
|builder| { |builder| {
builder.move_file(ctx.frange.file_id, dst); builder.move_file(ctx.file_id(), dst);
}, },
) )
} }

View file

@ -86,7 +86,7 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
buf, buf,
); );
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; let dst = AnchoredPathBuf { anchor: ctx.file_id(), path };
builder.create_file(dst, contents); builder.create_file(dst, contents);
}, },
) )

View file

@ -25,7 +25,7 @@ use crate::{
// ``` // ```
pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?; let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?;
let module = ctx.sema.to_module_def(ctx.frange.file_id)?; let module = ctx.sema.to_module_def(ctx.file_id())?;
// Enable this assist if the user select all "meaningful" content in the source file // Enable this assist if the user select all "meaningful" content in the source file
let trimmed_selected_range = trimmed_text_range(&source_file, ctx.selection_trimmed()); let trimmed_selected_range = trimmed_text_range(&source_file, ctx.selection_trimmed());
let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range()); let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range());
@ -41,13 +41,13 @@ pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<(
let target = source_file.syntax().text_range(); let target = source_file.syntax().text_range();
let module_name = module.name(ctx.db())?.to_string(); let module_name = module.name(ctx.db())?.to_string();
let path = format!("./{}/mod.rs", module_name); let path = format!("./{}/mod.rs", module_name);
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; let dst = AnchoredPathBuf { anchor: ctx.file_id(), path };
acc.add( acc.add(
AssistId("move_to_mod_rs", AssistKind::Refactor), AssistId("move_to_mod_rs", AssistKind::Refactor),
format!("Convert {}.rs to {}/mod.rs", module_name, module_name), format!("Convert {}.rs to {}/mod.rs", module_name, module_name),
target, target,
|builder| { |builder| {
builder.move_file(ctx.frange.file_id, dst); builder.move_file(ctx.file_id(), dst);
}, },
) )
} }

View file

@ -82,7 +82,7 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists};
// } // }
// ``` // ```
pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
if ctx.frange.range.is_empty() { if ctx.has_empty_selection() {
cov_mark::hit!(not_applicable_if_no_selection); cov_mark::hit!(not_applicable_if_no_selection);
return None; return None;
} }