mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Make AssistContext::frange private
This commit is contained in:
parent
086563f751
commit
ccad89a2db
@ -56,7 +56,7 @@ use crate::{
|
||||
pub(crate) struct AssistContext<'a> {
|
||||
pub(crate) config: &'a AssistConfig,
|
||||
pub(crate) sema: Semantics<'a, RootDatabase>,
|
||||
pub(crate) frange: FileRange,
|
||||
frange: FileRange,
|
||||
trimmed_range: TextRange,
|
||||
source_file: SourceFile,
|
||||
}
|
||||
@ -98,6 +98,14 @@ impl<'a> AssistContext<'a> {
|
||||
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
|
||||
/// to the nearest enclosed token.
|
||||
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.
|
||||
pub(crate) fn covering_element(&self) -> SyntaxElement {
|
||||
self.source_file.syntax().covering_element(self.selection_trimmed())
|
||||
// self.source_file.syntax().covering_element(self.frange.range)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,7 +92,7 @@ fn edit_struct_def(
|
||||
let record_fields = ast::make::record_field_list(record_fields);
|
||||
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 Some(w) = strukt.where_clause() {
|
||||
|
@ -115,7 +115,7 @@ fn collect_data(ident_pat: IdentPat, ctx: &AssistContext) -> Option<TupleData> {
|
||||
let usages = ctx.sema.to_def(&ident_pat).map(|def| {
|
||||
Definition::Local(def)
|
||||
.usages(&ctx.sema)
|
||||
.in_scope(SearchScope::single_file(ctx.frange.file_id))
|
||||
.in_scope(SearchScope::single_file(ctx.file_id()))
|
||||
.all()
|
||||
});
|
||||
|
||||
|
@ -125,7 +125,7 @@ impl 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()
|
||||
}
|
||||
}
|
||||
|
@ -309,7 +309,7 @@ impl LocalUsages {
|
||||
Self(
|
||||
Definition::Local(var)
|
||||
.usages(&ctx.sema)
|
||||
.in_scope(SearchScope::single_file(ctx.frange.file_id))
|
||||
.in_scope(SearchScope::single_file(ctx.file_id()))
|
||||
.all(),
|
||||
)
|
||||
}
|
||||
@ -1039,7 +1039,7 @@ fn is_defined_outside_of_body(
|
||||
body: &FunctionBody,
|
||||
src: &hir::InFile<Either<ast::IdentPat, ast::SelfParam>>,
|
||||
) -> 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))
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
let mut def_file_references = None;
|
||||
for (file_id, references) in usages {
|
||||
if file_id == ctx.frange.file_id {
|
||||
if file_id == ctx.file_id() {
|
||||
def_file_references = Some(references);
|
||||
continue;
|
||||
}
|
||||
@ -89,7 +89,7 @@ pub(crate) fn extract_struct_from_enum_variant(
|
||||
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());
|
||||
if let Some(references) = def_file_references {
|
||||
|
@ -26,7 +26,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||
// }
|
||||
// ```
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||
if ctx.frange.range.is_empty() {
|
||||
if ctx.has_empty_selection() {
|
||||
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 to_extract = node
|
||||
.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)?;
|
||||
|
||||
if let Some(ty_info) = ctx.sema.type_of_expr(&to_extract) {
|
||||
|
@ -364,7 +364,7 @@ fn get_fn_target(
|
||||
target_module: &Option<Module>,
|
||||
call: CallExpr,
|
||||
) -> Option<(GeneratedFunctionTarget, FileId, TextSize)> {
|
||||
let mut file = ctx.frange.file_id;
|
||||
let mut file = ctx.file_id();
|
||||
let target = match target_module {
|
||||
Some(target_module) => {
|
||||
let module_source = target_module.definition_source(ctx.db());
|
||||
|
@ -59,7 +59,7 @@ use crate::{
|
||||
// }
|
||||
// ```
|
||||
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 ast_func = name.syntax().parent().and_then(ast::Fn::cast)?;
|
||||
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 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);
|
||||
return None;
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ use crate::{
|
||||
// }
|
||||
// ```
|
||||
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 InlineData { let_stmt, delete_let, references, target } =
|
||||
if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() {
|
||||
|
@ -25,7 +25,7 @@ use crate::{
|
||||
// ```
|
||||
pub(crate) fn move_from_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||
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
|
||||
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());
|
||||
@ -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 module_name = module.name(ctx.db())?.to_string();
|
||||
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(
|
||||
AssistId("move_from_mod_rs", AssistKind::Refactor),
|
||||
format!("Convert {}/mod.rs to {}.rs", module_name, module_name),
|
||||
target,
|
||||
|builder| {
|
||||
builder.move_file(ctx.frange.file_id, dst);
|
||||
builder.move_file(ctx.file_id(), dst);
|
||||
},
|
||||
)
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
|
||||
buf,
|
||||
);
|
||||
|
||||
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
|
||||
let dst = AnchoredPathBuf { anchor: ctx.file_id(), path };
|
||||
builder.create_file(dst, contents);
|
||||
},
|
||||
)
|
||||
|
@ -25,7 +25,7 @@ use crate::{
|
||||
// ```
|
||||
pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||
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
|
||||
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());
|
||||
@ -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 module_name = module.name(ctx.db())?.to_string();
|
||||
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(
|
||||
AssistId("move_to_mod_rs", AssistKind::Refactor),
|
||||
format!("Convert {}.rs to {}/mod.rs", module_name, module_name),
|
||||
target,
|
||||
|builder| {
|
||||
builder.move_file(ctx.frange.file_id, dst);
|
||||
builder.move_file(ctx.file_id(), dst);
|
||||
},
|
||||
)
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists};
|
||||
// }
|
||||
// ```
|
||||
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);
|
||||
return None;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user