mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
8591: Remove SyntaxRewriter usage in insert_use in favor of mutable syntax trees r=matklad a=Veykril Unfortunately changing `insert_use` to not use `SyntaxRewriter` creates a lot of changes since so much relies on that. But on the other hand this should be the biggest usage of `SyntaxRewriter` I believe. 8638: Remove SyntaxRewriter::from_fn r=Veykril a=Veykril Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
5cbde9f531
15 changed files with 334 additions and 358 deletions
|
@ -3,20 +3,27 @@ use hir::{HirDisplay, PathResolution, SemanticsScope};
|
||||||
use ide_db::helpers::mod_path_to_ast;
|
use ide_db::helpers::mod_path_to_ast;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo::SyntaxRewriter,
|
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
SyntaxNode,
|
ted, SyntaxNode,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N {
|
pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: &N) {
|
||||||
SyntaxRewriter::from_fn(|element| match element {
|
let mut skip_to = None;
|
||||||
syntax::SyntaxElement::Node(n) => {
|
for event in node.syntax().preorder() {
|
||||||
let replacement = transformer.get_substitution(&n, transformer)?;
|
match event {
|
||||||
Some(replacement.into())
|
syntax::WalkEvent::Enter(node) if skip_to.is_none() => {
|
||||||
|
skip_to = transformer.get_substitution(&node, transformer).zip(Some(node));
|
||||||
|
}
|
||||||
|
syntax::WalkEvent::Enter(_) => (),
|
||||||
|
syntax::WalkEvent::Leave(node) => match &skip_to {
|
||||||
|
Some((replacement, skip_target)) if *skip_target == node => {
|
||||||
|
ted::replace(node, replacement.clone_for_update());
|
||||||
|
skip_to.take();
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.rewrite_ast(&node)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `AstTransform` helps with applying bulk transformations to syntax nodes.
|
/// `AstTransform` helps with applying bulk transformations to syntax nodes.
|
||||||
|
@ -191,11 +198,9 @@ impl<'a> AstTransform<'a> for QualifyPaths<'a> {
|
||||||
let found_path = from.find_use_path(self.source_scope.db.upcast(), def)?;
|
let found_path = from.find_use_path(self.source_scope.db.upcast(), def)?;
|
||||||
let mut path = mod_path_to_ast(&found_path);
|
let mut path = mod_path_to_ast(&found_path);
|
||||||
|
|
||||||
let type_args = p
|
let type_args = p.segment().and_then(|s| s.generic_arg_list());
|
||||||
.segment()
|
|
||||||
.and_then(|s| s.generic_arg_list())
|
|
||||||
.map(|arg_list| apply(recur, arg_list));
|
|
||||||
if let Some(type_args) = type_args {
|
if let Some(type_args) = type_args {
|
||||||
|
apply(recur, &type_args);
|
||||||
let last_segment = path.segment().unwrap();
|
let last_segment = path.segment().unwrap();
|
||||||
path = path.with_segment(last_segment.with_generic_args(type_args))
|
path = path.with_segment(last_segment.with_generic_args(type_args))
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,7 +93,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
||||||
|
|
||||||
let range = ctx.sema.original_range(&syntax_under_caret).range;
|
let range = ctx.sema.original_range(&syntax_under_caret).range;
|
||||||
let group_label = group_label(import_assets.import_candidate());
|
let group_label = group_label(import_assets.import_candidate());
|
||||||
let scope = ImportScope::find_insert_use_container(&syntax_under_caret, &ctx.sema)?;
|
let scope = ImportScope::find_insert_use_container_with_macros(&syntax_under_caret, &ctx.sema)?;
|
||||||
for import in proposed_imports {
|
for import in proposed_imports {
|
||||||
acc.add_group(
|
acc.add_group(
|
||||||
&group_label,
|
&group_label,
|
||||||
|
@ -101,9 +101,11 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
||||||
format!("Import `{}`", import.import_path),
|
format!("Import `{}`", import.import_path),
|
||||||
range,
|
range,
|
||||||
|builder| {
|
|builder| {
|
||||||
let rewriter =
|
let scope = match scope.clone() {
|
||||||
|
ImportScope::File(it) => ImportScope::File(builder.make_ast_mut(it)),
|
||||||
|
ImportScope::Module(it) => ImportScope::Module(builder.make_ast_mut(it)),
|
||||||
|
};
|
||||||
insert_use(&scope, mod_path_to_ast(&import.import_path), ctx.config.insert_use);
|
insert_use(&scope, mod_path_to_ast(&import.import_path), ctx.config.insert_use);
|
||||||
builder.rewrite(rewriter);
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use hir::{Module, ModuleDef, Name, Variant};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
defs::Definition,
|
defs::Definition,
|
||||||
helpers::{
|
helpers::{
|
||||||
insert_use::{insert_use, ImportScope},
|
insert_use::{insert_use, ImportScope, InsertUseConfig},
|
||||||
mod_path_to_ast,
|
mod_path_to_ast,
|
||||||
},
|
},
|
||||||
search::FileReference,
|
search::FileReference,
|
||||||
|
@ -13,9 +13,9 @@ use ide_db::{
|
||||||
};
|
};
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo::{find_node_at_offset, SyntaxRewriter},
|
algo::find_node_at_offset,
|
||||||
ast::{self, edit::IndentLevel, make, AstNode, NameOwner, VisibilityOwner},
|
ast::{self, make, AstNode, NameOwner, VisibilityOwner},
|
||||||
SourceFile, SyntaxElement, SyntaxNode, T,
|
ted, SyntaxNode, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
@ -62,40 +62,50 @@ pub(crate) fn extract_struct_from_enum_variant(
|
||||||
let mut visited_modules_set = FxHashSet::default();
|
let mut visited_modules_set = FxHashSet::default();
|
||||||
let current_module = enum_hir.module(ctx.db());
|
let current_module = enum_hir.module(ctx.db());
|
||||||
visited_modules_set.insert(current_module);
|
visited_modules_set.insert(current_module);
|
||||||
let mut def_rewriter = None;
|
// 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 {
|
for (file_id, references) in usages {
|
||||||
let mut rewriter = SyntaxRewriter::default();
|
|
||||||
let source_file = ctx.sema.parse(file_id);
|
|
||||||
for reference in references {
|
|
||||||
update_reference(
|
|
||||||
ctx,
|
|
||||||
&mut rewriter,
|
|
||||||
reference,
|
|
||||||
&source_file,
|
|
||||||
&enum_module_def,
|
|
||||||
&variant_hir_name,
|
|
||||||
&mut visited_modules_set,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if file_id == ctx.frange.file_id {
|
if file_id == ctx.frange.file_id {
|
||||||
def_rewriter = Some(rewriter);
|
def_file_references = Some(references);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
builder.edit_file(file_id);
|
builder.edit_file(file_id);
|
||||||
builder.rewrite(rewriter);
|
let source_file = builder.make_ast_mut(ctx.sema.parse(file_id));
|
||||||
}
|
let processed = process_references(
|
||||||
let mut rewriter = def_rewriter.unwrap_or_default();
|
ctx,
|
||||||
update_variant(&mut rewriter, &variant);
|
&mut visited_modules_set,
|
||||||
extract_struct_def(
|
source_file.syntax(),
|
||||||
&mut rewriter,
|
&enum_module_def,
|
||||||
&enum_ast,
|
&variant_hir_name,
|
||||||
variant_name.clone(),
|
references,
|
||||||
&field_list,
|
|
||||||
&variant.parent_enum().syntax().clone().into(),
|
|
||||||
enum_ast.visibility(),
|
|
||||||
);
|
);
|
||||||
|
processed.into_iter().for_each(|(path, node, import)| {
|
||||||
|
apply_references(ctx.config.insert_use, path, node, import)
|
||||||
|
});
|
||||||
|
}
|
||||||
builder.edit_file(ctx.frange.file_id);
|
builder.edit_file(ctx.frange.file_id);
|
||||||
builder.rewrite(rewriter);
|
let source_file = builder.make_ast_mut(ctx.sema.parse(ctx.frange.file_id));
|
||||||
|
let variant = builder.make_ast_mut(variant.clone());
|
||||||
|
if let Some(references) = def_file_references {
|
||||||
|
let processed = process_references(
|
||||||
|
ctx,
|
||||||
|
&mut visited_modules_set,
|
||||||
|
source_file.syntax(),
|
||||||
|
&enum_module_def,
|
||||||
|
&variant_hir_name,
|
||||||
|
references,
|
||||||
|
);
|
||||||
|
processed.into_iter().for_each(|(path, node, import)| {
|
||||||
|
apply_references(ctx.config.insert_use, path, node, import)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let def = create_struct_def(variant_name.clone(), &field_list, enum_ast.visibility());
|
||||||
|
let start_offset = &variant.parent_enum().syntax().clone();
|
||||||
|
ted::insert_raw(ted::Position::before(start_offset), def.syntax());
|
||||||
|
ted::insert_raw(ted::Position::before(start_offset), &make::tokens::blank_line());
|
||||||
|
|
||||||
|
update_variant(&variant);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -136,34 +146,11 @@ fn existing_definition(db: &RootDatabase, variant_name: &ast::Name, variant: &Va
|
||||||
.any(|(name, _)| name.to_string() == variant_name.to_string())
|
.any(|(name, _)| name.to_string() == variant_name.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_import(
|
fn create_struct_def(
|
||||||
ctx: &AssistContext,
|
|
||||||
rewriter: &mut SyntaxRewriter,
|
|
||||||
scope_node: &SyntaxNode,
|
|
||||||
module: &Module,
|
|
||||||
enum_module_def: &ModuleDef,
|
|
||||||
variant_hir_name: &Name,
|
|
||||||
) -> Option<()> {
|
|
||||||
let db = ctx.db();
|
|
||||||
let mod_path =
|
|
||||||
module.find_use_path_prefixed(db, *enum_module_def, ctx.config.insert_use.prefix_kind);
|
|
||||||
if let Some(mut mod_path) = mod_path {
|
|
||||||
mod_path.pop_segment();
|
|
||||||
mod_path.push_segment(variant_hir_name.clone());
|
|
||||||
let scope = ImportScope::find_insert_use_container(scope_node, &ctx.sema)?;
|
|
||||||
*rewriter += insert_use(&scope, mod_path_to_ast(&mod_path), ctx.config.insert_use);
|
|
||||||
}
|
|
||||||
Some(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn extract_struct_def(
|
|
||||||
rewriter: &mut SyntaxRewriter,
|
|
||||||
enum_: &ast::Enum,
|
|
||||||
variant_name: ast::Name,
|
variant_name: ast::Name,
|
||||||
field_list: &Either<ast::RecordFieldList, ast::TupleFieldList>,
|
field_list: &Either<ast::RecordFieldList, ast::TupleFieldList>,
|
||||||
start_offset: &SyntaxElement,
|
|
||||||
visibility: Option<ast::Visibility>,
|
visibility: Option<ast::Visibility>,
|
||||||
) -> Option<()> {
|
) -> ast::Struct {
|
||||||
let pub_vis = Some(make::visibility_pub());
|
let pub_vis = Some(make::visibility_pub());
|
||||||
let field_list = match field_list {
|
let field_list = match field_list {
|
||||||
Either::Left(field_list) => {
|
Either::Left(field_list) => {
|
||||||
|
@ -180,65 +167,90 @@ fn extract_struct_def(
|
||||||
.into(),
|
.into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
rewriter.insert_before(
|
make::struct_(visibility, variant_name, None, field_list).clone_for_update()
|
||||||
start_offset,
|
|
||||||
make::struct_(visibility, variant_name, None, field_list).syntax(),
|
|
||||||
);
|
|
||||||
rewriter.insert_before(start_offset, &make::tokens::blank_line());
|
|
||||||
|
|
||||||
if let indent_level @ 1..=usize::MAX = IndentLevel::from_node(enum_.syntax()).0 as usize {
|
|
||||||
rewriter
|
|
||||||
.insert_before(start_offset, &make::tokens::whitespace(&" ".repeat(4 * indent_level)));
|
|
||||||
}
|
|
||||||
Some(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_variant(rewriter: &mut SyntaxRewriter, variant: &ast::Variant) -> Option<()> {
|
fn update_variant(variant: &ast::Variant) -> Option<()> {
|
||||||
let name = variant.name()?;
|
let name = variant.name()?;
|
||||||
let tuple_field = make::tuple_field(None, make::ty(&name.text()));
|
let tuple_field = make::tuple_field(None, make::ty(&name.text()));
|
||||||
let replacement = make::variant(
|
let replacement = make::variant(
|
||||||
name,
|
name,
|
||||||
Some(ast::FieldList::TupleFieldList(make::tuple_field_list(iter::once(tuple_field)))),
|
Some(ast::FieldList::TupleFieldList(make::tuple_field_list(iter::once(tuple_field)))),
|
||||||
);
|
)
|
||||||
rewriter.replace(variant.syntax(), replacement.syntax());
|
.clone_for_update();
|
||||||
|
ted::replace(variant.syntax(), replacement.syntax());
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_reference(
|
fn apply_references(
|
||||||
|
insert_use_cfg: InsertUseConfig,
|
||||||
|
segment: ast::PathSegment,
|
||||||
|
node: SyntaxNode,
|
||||||
|
import: Option<(ImportScope, hir::ModPath)>,
|
||||||
|
) {
|
||||||
|
if let Some((scope, path)) = import {
|
||||||
|
insert_use(&scope, mod_path_to_ast(&path), insert_use_cfg);
|
||||||
|
}
|
||||||
|
ted::insert_raw(
|
||||||
|
ted::Position::before(segment.syntax()),
|
||||||
|
make::path_from_text(&format!("{}", segment)).clone_for_update().syntax(),
|
||||||
|
);
|
||||||
|
ted::insert_raw(ted::Position::before(segment.syntax()), make::token(T!['(']));
|
||||||
|
ted::insert_raw(ted::Position::after(&node), make::token(T![')']));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_references(
|
||||||
ctx: &AssistContext,
|
ctx: &AssistContext,
|
||||||
rewriter: &mut SyntaxRewriter,
|
visited_modules: &mut FxHashSet<Module>,
|
||||||
reference: FileReference,
|
source_file: &SyntaxNode,
|
||||||
source_file: &SourceFile,
|
|
||||||
enum_module_def: &ModuleDef,
|
enum_module_def: &ModuleDef,
|
||||||
variant_hir_name: &Name,
|
variant_hir_name: &Name,
|
||||||
visited_modules_set: &mut FxHashSet<Module>,
|
refs: Vec<FileReference>,
|
||||||
) -> Option<()> {
|
) -> Vec<(ast::PathSegment, SyntaxNode, Option<(ImportScope, hir::ModPath)>)> {
|
||||||
let offset = reference.range.start();
|
// we have to recollect here eagerly as we are about to edit the tree we need to calculate the changes
|
||||||
let (segment, expr) = if let Some(path_expr) =
|
// and corresponding nodes up front
|
||||||
find_node_at_offset::<ast::PathExpr>(source_file.syntax(), offset)
|
refs.into_iter()
|
||||||
{
|
.flat_map(|reference| {
|
||||||
// tuple variant
|
let (segment, scope_node, module) =
|
||||||
(path_expr.path()?.segment()?, path_expr.syntax().parent()?)
|
reference_to_node(&ctx.sema, source_file, reference)?;
|
||||||
} else if let Some(record_expr) =
|
if !visited_modules.contains(&module) {
|
||||||
find_node_at_offset::<ast::RecordExpr>(source_file.syntax(), offset)
|
let mod_path = module.find_use_path_prefixed(
|
||||||
{
|
ctx.sema.db,
|
||||||
// record variant
|
*enum_module_def,
|
||||||
(record_expr.path()?.segment()?, record_expr.syntax().clone())
|
ctx.config.insert_use.prefix_kind,
|
||||||
} else {
|
);
|
||||||
return None;
|
if let Some(mut mod_path) = mod_path {
|
||||||
};
|
mod_path.pop_segment();
|
||||||
|
mod_path.push_segment(variant_hir_name.clone());
|
||||||
|
let scope = ImportScope::find_insert_use_container(&scope_node)?;
|
||||||
|
visited_modules.insert(module);
|
||||||
|
return Some((segment, scope_node, Some((scope, mod_path))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some((segment, scope_node, None))
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
let module = ctx.sema.scope(&expr).module()?;
|
fn reference_to_node(
|
||||||
if !visited_modules_set.contains(&module) {
|
sema: &hir::Semantics<RootDatabase>,
|
||||||
if insert_import(ctx, rewriter, &expr, &module, enum_module_def, variant_hir_name).is_some()
|
source_file: &SyntaxNode,
|
||||||
{
|
reference: FileReference,
|
||||||
visited_modules_set.insert(module);
|
) -> Option<(ast::PathSegment, SyntaxNode, hir::Module)> {
|
||||||
|
let offset = reference.range.start();
|
||||||
|
if let Some(path_expr) = find_node_at_offset::<ast::PathExpr>(source_file, offset) {
|
||||||
|
// tuple variant
|
||||||
|
Some((path_expr.path()?.segment()?, path_expr.syntax().parent()?))
|
||||||
|
} else if let Some(record_expr) = find_node_at_offset::<ast::RecordExpr>(source_file, offset) {
|
||||||
|
// record variant
|
||||||
|
Some((record_expr.path()?.segment()?, record_expr.syntax().clone()))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
.and_then(|(segment, expr)| {
|
||||||
rewriter.insert_after(segment.syntax(), &make::token(T!['(']));
|
let module = sema.scope(&expr).module()?;
|
||||||
rewriter.insert_after(segment.syntax(), segment.syntax());
|
Some((segment, expr, module))
|
||||||
rewriter.insert_after(&expr, &make::token(T![')']));
|
})
|
||||||
Some(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -83,11 +83,9 @@ fn replace<T: AstNode + PartialEq>(
|
||||||
fields: impl Iterator<Item = T>,
|
fields: impl Iterator<Item = T>,
|
||||||
sorted_fields: impl IntoIterator<Item = T>,
|
sorted_fields: impl IntoIterator<Item = T>,
|
||||||
) {
|
) {
|
||||||
fields.zip(sorted_fields).filter(|(field, sorted)| field != sorted).for_each(
|
fields.zip(sorted_fields).for_each(|(field, sorted_field)| {
|
||||||
|(field, sorted_field)| {
|
ted::replace(field.syntax(), sorted_field.syntax().clone_for_update())
|
||||||
ted::replace(field.syntax(), sorted_field.syntax().clone_for_update());
|
});
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> {
|
fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> {
|
||||||
|
|
|
@ -4,9 +4,8 @@ use rustc_hash::FxHashMap;
|
||||||
use hir::{PathResolution, Semantics};
|
use hir::{PathResolution, Semantics};
|
||||||
use ide_db::RootDatabase;
|
use ide_db::RootDatabase;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo,
|
|
||||||
ast::{self, NameOwner},
|
ast::{self, NameOwner},
|
||||||
AstNode,
|
ted, AstNode,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
@ -75,13 +74,16 @@ pub(crate) fn reorder_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
||||||
}
|
}
|
||||||
|
|
||||||
let target = items.syntax().text_range();
|
let target = items.syntax().text_range();
|
||||||
acc.add(AssistId("reorder_impl", AssistKind::RefactorRewrite), "Sort methods", target, |edit| {
|
acc.add(
|
||||||
let mut rewriter = algo::SyntaxRewriter::default();
|
AssistId("reorder_impl", AssistKind::RefactorRewrite),
|
||||||
for (old, new) in methods.iter().zip(&sorted) {
|
"Sort methods",
|
||||||
rewriter.replace(old.syntax(), new.syntax());
|
target,
|
||||||
}
|
|builder| {
|
||||||
edit.rewrite(rewriter);
|
methods.into_iter().zip(sorted).for_each(|(old, new)| {
|
||||||
})
|
ted::replace(builder.make_ast_mut(old).syntax(), new.clone_for_update().syntax())
|
||||||
|
});
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_method_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> {
|
fn compute_method_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use ide_db::helpers::insert_use::{insert_use, ImportScope};
|
use ide_db::helpers::insert_use::{insert_use, ImportScope};
|
||||||
use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode};
|
use syntax::{ast, match_ast, ted, AstNode, SyntaxNode};
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ pub(crate) fn replace_qualified_name_with_use(
|
||||||
}
|
}
|
||||||
|
|
||||||
let target = path.syntax().text_range();
|
let target = path.syntax().text_range();
|
||||||
let scope = ImportScope::find_insert_use_container(path.syntax(), &ctx.sema)?;
|
let scope = ImportScope::find_insert_use_container_with_macros(path.syntax(), &ctx.sema)?;
|
||||||
let syntax = scope.as_syntax_node();
|
let syntax = scope.as_syntax_node();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite),
|
AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite),
|
||||||
|
@ -40,18 +40,17 @@ pub(crate) fn replace_qualified_name_with_use(
|
||||||
|builder| {
|
|builder| {
|
||||||
// Now that we've brought the name into scope, re-qualify all paths that could be
|
// Now that we've brought the name into scope, re-qualify all paths that could be
|
||||||
// affected (that is, all paths inside the node we added the `use` to).
|
// affected (that is, all paths inside the node we added the `use` to).
|
||||||
let mut rewriter = SyntaxRewriter::default();
|
let syntax = builder.make_mut(syntax.clone());
|
||||||
shorten_paths(&mut rewriter, syntax.clone(), &path);
|
|
||||||
if let Some(ref import_scope) = ImportScope::from(syntax.clone()) {
|
if let Some(ref import_scope) = ImportScope::from(syntax.clone()) {
|
||||||
rewriter += insert_use(import_scope, path, ctx.config.insert_use);
|
shorten_paths(&syntax, &path.clone_for_update());
|
||||||
builder.rewrite(rewriter);
|
insert_use(import_scope, path, ctx.config.insert_use);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds replacements to `re` that shorten `path` in all descendants of `node`.
|
/// Adds replacements to `re` that shorten `path` in all descendants of `node`.
|
||||||
fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: &ast::Path) {
|
fn shorten_paths(node: &SyntaxNode, path: &ast::Path) {
|
||||||
for child in node.children() {
|
for child in node.children() {
|
||||||
match_ast! {
|
match_ast! {
|
||||||
match child {
|
match child {
|
||||||
|
@ -60,34 +59,26 @@ fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path:
|
||||||
ast::Use(_it) => continue,
|
ast::Use(_it) => continue,
|
||||||
// Don't descend into submodules, they don't have the same `use` items in scope.
|
// Don't descend into submodules, they don't have the same `use` items in scope.
|
||||||
ast::Module(_it) => continue,
|
ast::Module(_it) => continue,
|
||||||
|
ast::Path(p) => if maybe_replace_path(p.clone(), path.clone()).is_none() {
|
||||||
ast::Path(p) => {
|
shorten_paths(p.syntax(), path);
|
||||||
match maybe_replace_path(rewriter, p.clone(), path.clone()) {
|
|
||||||
Some(()) => {},
|
|
||||||
None => shorten_paths(rewriter, p.syntax().clone(), path),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => shorten_paths(rewriter, child, path),
|
_ => shorten_paths(&child, path),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn maybe_replace_path(
|
fn maybe_replace_path(path: ast::Path, target: ast::Path) -> Option<()> {
|
||||||
rewriter: &mut SyntaxRewriter<'static>,
|
|
||||||
path: ast::Path,
|
|
||||||
target: ast::Path,
|
|
||||||
) -> Option<()> {
|
|
||||||
if !path_eq(path.clone(), target) {
|
if !path_eq(path.clone(), target) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shorten `path`, leaving only its last segment.
|
// Shorten `path`, leaving only its last segment.
|
||||||
if let Some(parent) = path.qualifier() {
|
if let Some(parent) = path.qualifier() {
|
||||||
rewriter.delete(parent.syntax());
|
ted::remove(parent.syntax());
|
||||||
}
|
}
|
||||||
if let Some(double_colon) = path.coloncolon_token() {
|
if let Some(double_colon) = path.coloncolon_token() {
|
||||||
rewriter.delete(&double_colon);
|
ted::remove(&double_colon);
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(())
|
Some(())
|
||||||
|
@ -150,6 +141,7 @@ Debug
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_replace_add_use_no_anchor_with_item_below() {
|
fn test_replace_add_use_no_anchor_with_item_below() {
|
||||||
check_assist(
|
check_assist(
|
||||||
|
|
|
@ -140,7 +140,8 @@ pub fn add_trait_assoc_items_to_impl(
|
||||||
|
|
||||||
let items = items
|
let items = items
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|it| ast_transform::apply(&*ast_transform, it))
|
.map(|it| it.clone_for_update())
|
||||||
|
.inspect(|it| ast_transform::apply(&*ast_transform, it))
|
||||||
.map(|it| match it {
|
.map(|it| match it {
|
||||||
ast::AssocItem::Fn(def) => ast::AssocItem::Fn(add_body(def)),
|
ast::AssocItem::Fn(def) => ast::AssocItem::Fn(add_body(def)),
|
||||||
ast::AssocItem::TypeAlias(def) => ast::AssocItem::TypeAlias(def.remove_bounds()),
|
ast::AssocItem::TypeAlias(def) => ast::AssocItem::TypeAlias(def.remove_bounds()),
|
||||||
|
|
|
@ -132,7 +132,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
|
||||||
|
|
||||||
let user_input_lowercased = potential_import_name.to_lowercase();
|
let user_input_lowercased = potential_import_name.to_lowercase();
|
||||||
let import_assets = import_assets(ctx, potential_import_name)?;
|
let import_assets = import_assets(ctx, potential_import_name)?;
|
||||||
let import_scope = ImportScope::find_insert_use_container(
|
let import_scope = ImportScope::find_insert_use_container_with_macros(
|
||||||
position_for_import(ctx, Some(import_assets.import_candidate()))?,
|
position_for_import(ctx, Some(import_assets.import_candidate()))?,
|
||||||
&ctx.sema,
|
&ctx.sema,
|
||||||
)?;
|
)?;
|
||||||
|
|
|
@ -377,11 +377,11 @@ impl ImportEdit {
|
||||||
pub fn to_text_edit(&self, cfg: InsertUseConfig) -> Option<TextEdit> {
|
pub fn to_text_edit(&self, cfg: InsertUseConfig) -> Option<TextEdit> {
|
||||||
let _p = profile::span("ImportEdit::to_text_edit");
|
let _p = profile::span("ImportEdit::to_text_edit");
|
||||||
|
|
||||||
let rewriter =
|
let new_ast = self.scope.clone_for_update();
|
||||||
insert_use::insert_use(&self.scope, mod_path_to_ast(&self.import.import_path), cfg);
|
insert_use::insert_use(&new_ast, mod_path_to_ast(&self.import.import_path), cfg);
|
||||||
let old_ast = rewriter.rewrite_root()?;
|
|
||||||
let mut import_insert = TextEdit::builder();
|
let mut import_insert = TextEdit::builder();
|
||||||
algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut import_insert);
|
algo::diff(self.scope.as_syntax_node(), new_ast.as_syntax_node())
|
||||||
|
.into_text_edit(&mut import_insert);
|
||||||
|
|
||||||
Some(import_insert.finish())
|
Some(import_insert.finish())
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,7 +179,7 @@ pub fn resolve_completion_edits(
|
||||||
) -> Option<Vec<TextEdit>> {
|
) -> Option<Vec<TextEdit>> {
|
||||||
let ctx = CompletionContext::new(db, position, config)?;
|
let ctx = CompletionContext::new(db, position, config)?;
|
||||||
let position_for_import = position_for_import(&ctx, None)?;
|
let position_for_import = position_for_import(&ctx, None)?;
|
||||||
let scope = ImportScope::find_insert_use_container(position_for_import, &ctx.sema)?;
|
let scope = ImportScope::find_insert_use_container_with_macros(position_for_import, &ctx.sema)?;
|
||||||
|
|
||||||
let current_module = ctx.sema.scope(position_for_import).module()?;
|
let current_module = ctx.sema.scope(position_for_import).module()?;
|
||||||
let current_crate = current_module.krate();
|
let current_crate = current_module.krate();
|
||||||
|
|
|
@ -4,13 +4,9 @@ use std::{cmp::Ordering, iter::successors};
|
||||||
use hir::Semantics;
|
use hir::Semantics;
|
||||||
use itertools::{EitherOrBoth, Itertools};
|
use itertools::{EitherOrBoth, Itertools};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo::SyntaxRewriter,
|
algo,
|
||||||
ast::{
|
ast::{self, edit::AstNodeEdit, make, AstNode, AttrsOwner, PathSegmentKind, VisibilityOwner},
|
||||||
self,
|
ted, AstToken, Direction, NodeOrToken, SyntaxNode, SyntaxToken,
|
||||||
edit::{AstNodeEdit, IndentLevel},
|
|
||||||
make, AstNode, AttrsOwner, PathSegmentKind, VisibilityOwner,
|
|
||||||
},
|
|
||||||
AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::RootDatabase;
|
use crate::RootDatabase;
|
||||||
|
@ -42,13 +38,18 @@ impl ImportScope {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determines the containing syntax node in which to insert a `use` statement affecting `position`.
|
/// Determines the containing syntax node in which to insert a `use` statement affecting `position`.
|
||||||
pub fn find_insert_use_container(
|
pub fn find_insert_use_container_with_macros(
|
||||||
position: &SyntaxNode,
|
position: &SyntaxNode,
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
) -> Option<Self> {
|
) -> Option<Self> {
|
||||||
sema.ancestors_with_macros(position.clone()).find_map(Self::from)
|
sema.ancestors_with_macros(position.clone()).find_map(Self::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Determines the containing syntax node in which to insert a `use` statement affecting `position`.
|
||||||
|
pub fn find_insert_use_container(position: &SyntaxNode) -> Option<Self> {
|
||||||
|
std::iter::successors(Some(position.clone()), SyntaxNode::parent).find_map(Self::from)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn as_syntax_node(&self) -> &SyntaxNode {
|
pub fn as_syntax_node(&self) -> &SyntaxNode {
|
||||||
match self {
|
match self {
|
||||||
ImportScope::File(file) => file.syntax(),
|
ImportScope::File(file) => file.syntax(),
|
||||||
|
@ -56,127 +57,32 @@ impl ImportScope {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn indent_level(&self) -> IndentLevel {
|
pub fn clone_for_update(&self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
ImportScope::File(file) => file.indent_level(),
|
ImportScope::File(file) => ImportScope::File(file.clone_for_update()),
|
||||||
ImportScope::Module(item_list) => item_list.indent_level() + 1,
|
ImportScope::Module(item_list) => ImportScope::Module(item_list.clone_for_update()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn first_insert_pos(&self) -> (InsertPosition<SyntaxElement>, AddBlankLine) {
|
|
||||||
match self {
|
|
||||||
ImportScope::File(_) => (InsertPosition::First, AddBlankLine::AfterTwice),
|
|
||||||
// don't insert the imports before the item list's opening curly brace
|
|
||||||
ImportScope::Module(item_list) => item_list
|
|
||||||
.l_curly_token()
|
|
||||||
.map(|b| (InsertPosition::After(b.into()), AddBlankLine::Around))
|
|
||||||
.unwrap_or((InsertPosition::First, AddBlankLine::AfterTwice)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn insert_pos_after_last_inner_element(&self) -> (InsertPosition<SyntaxElement>, AddBlankLine) {
|
|
||||||
self.as_syntax_node()
|
|
||||||
.children_with_tokens()
|
|
||||||
.filter(|child| match child {
|
|
||||||
NodeOrToken::Node(node) => is_inner_attribute(node.clone()),
|
|
||||||
NodeOrToken::Token(token) => is_inner_comment(token.clone()),
|
|
||||||
})
|
|
||||||
.last()
|
|
||||||
.map(|last_inner_element| {
|
|
||||||
(InsertPosition::After(last_inner_element), AddBlankLine::BeforeTwice)
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|| self.first_insert_pos())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_inner_attribute(node: SyntaxNode) -> bool {
|
|
||||||
ast::Attr::cast(node).map(|attr| attr.kind()) == Some(ast::AttrKind::Inner)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_inner_comment(token: SyntaxToken) -> bool {
|
|
||||||
ast::Comment::cast(token).and_then(|comment| comment.kind().doc)
|
|
||||||
== Some(ast::CommentPlacement::Inner)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert an import path into the given file/node. A `merge` value of none indicates that no import merging is allowed to occur.
|
/// Insert an import path into the given file/node. A `merge` value of none indicates that no import merging is allowed to occur.
|
||||||
pub fn insert_use<'a>(
|
pub fn insert_use<'a>(scope: &ImportScope, path: ast::Path, cfg: InsertUseConfig) {
|
||||||
scope: &ImportScope,
|
|
||||||
path: ast::Path,
|
|
||||||
cfg: InsertUseConfig,
|
|
||||||
) -> SyntaxRewriter<'a> {
|
|
||||||
let _p = profile::span("insert_use");
|
let _p = profile::span("insert_use");
|
||||||
let mut rewriter = SyntaxRewriter::default();
|
let use_item =
|
||||||
let use_item = make::use_(None, make::use_tree(path.clone(), None, None, false));
|
make::use_(None, make::use_tree(path.clone(), None, None, false)).clone_for_update();
|
||||||
// merge into existing imports if possible
|
// merge into existing imports if possible
|
||||||
if let Some(mb) = cfg.merge {
|
if let Some(mb) = cfg.merge {
|
||||||
for existing_use in scope.as_syntax_node().children().filter_map(ast::Use::cast) {
|
for existing_use in scope.as_syntax_node().children().filter_map(ast::Use::cast) {
|
||||||
if let Some(merged) = try_merge_imports(&existing_use, &use_item, mb) {
|
if let Some(merged) = try_merge_imports(&existing_use, &use_item, mb) {
|
||||||
rewriter.replace(existing_use.syntax(), merged.syntax());
|
ted::replace(existing_use.syntax(), merged.syntax());
|
||||||
return rewriter;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// either we weren't allowed to merge or there is no import that fits the merge conditions
|
// either we weren't allowed to merge or there is no import that fits the merge conditions
|
||||||
// so look for the place we have to insert to
|
// so look for the place we have to insert to
|
||||||
let (insert_position, add_blank) = find_insert_position(scope, path, cfg.group);
|
insert_use_(scope, path, cfg.group, use_item);
|
||||||
|
|
||||||
let indent = if let ident_level @ 1..=usize::MAX = scope.indent_level().0 as usize {
|
|
||||||
Some(make::tokens::whitespace(&" ".repeat(4 * ident_level)).into())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
let to_insert: Vec<SyntaxElement> = {
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
|
|
||||||
match add_blank {
|
|
||||||
AddBlankLine::Before | AddBlankLine::Around => {
|
|
||||||
buf.push(make::tokens::single_newline().into())
|
|
||||||
}
|
|
||||||
AddBlankLine::BeforeTwice => buf.push(make::tokens::blank_line().into()),
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
if add_blank.has_before() {
|
|
||||||
if let Some(indent) = indent.clone() {
|
|
||||||
cov_mark::hit!(insert_use_indent_before);
|
|
||||||
buf.push(indent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.push(use_item.syntax().clone().into());
|
|
||||||
|
|
||||||
match add_blank {
|
|
||||||
AddBlankLine::After | AddBlankLine::Around => {
|
|
||||||
buf.push(make::tokens::single_newline().into())
|
|
||||||
}
|
|
||||||
AddBlankLine::AfterTwice => buf.push(make::tokens::blank_line().into()),
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
// only add indentation *after* our stuff if there's another node directly after it
|
|
||||||
if add_blank.has_after() && matches!(insert_position, InsertPosition::Before(_)) {
|
|
||||||
if let Some(indent) = indent {
|
|
||||||
cov_mark::hit!(insert_use_indent_after);
|
|
||||||
buf.push(indent);
|
|
||||||
}
|
|
||||||
} else if add_blank.has_after() && matches!(insert_position, InsertPosition::After(_)) {
|
|
||||||
cov_mark::hit!(insert_use_no_indent_after);
|
|
||||||
}
|
|
||||||
|
|
||||||
buf
|
|
||||||
};
|
|
||||||
|
|
||||||
match insert_position {
|
|
||||||
InsertPosition::First => {
|
|
||||||
rewriter.insert_many_as_first_children(scope.as_syntax_node(), to_insert)
|
|
||||||
}
|
|
||||||
InsertPosition::Last => return rewriter, // actually unreachable
|
|
||||||
InsertPosition::Before(anchor) => rewriter.insert_many_before(&anchor, to_insert),
|
|
||||||
InsertPosition::After(anchor) => rewriter.insert_many_after(&anchor, to_insert),
|
|
||||||
}
|
|
||||||
rewriter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -> bool {
|
fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -> bool {
|
||||||
|
@ -235,7 +141,7 @@ pub fn try_merge_trees(
|
||||||
} else {
|
} else {
|
||||||
(lhs.split_prefix(&lhs_prefix), rhs.split_prefix(&rhs_prefix))
|
(lhs.split_prefix(&lhs_prefix), rhs.split_prefix(&rhs_prefix))
|
||||||
};
|
};
|
||||||
recursive_merge(&lhs, &rhs, merge).map(|it| it.clone_for_update())
|
recursive_merge(&lhs, &rhs, merge)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Recursively "zips" together lhs and rhs.
|
/// Recursively "zips" together lhs and rhs.
|
||||||
|
@ -334,7 +240,12 @@ fn recursive_merge(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(lhs.with_use_tree_list(make::use_tree_list(use_trees)))
|
|
||||||
|
Some(if let Some(old) = lhs.use_tree_list() {
|
||||||
|
lhs.replace_descendant(old, make::use_tree_list(use_trees)).clone_for_update()
|
||||||
|
} else {
|
||||||
|
lhs.clone()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Traverses both paths until they differ, returning the common prefix of both.
|
/// Traverses both paths until they differ, returning the common prefix of both.
|
||||||
|
@ -520,32 +431,15 @@ impl ImportGroup {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq)]
|
fn insert_use_(
|
||||||
enum AddBlankLine {
|
|
||||||
Before,
|
|
||||||
BeforeTwice,
|
|
||||||
Around,
|
|
||||||
After,
|
|
||||||
AfterTwice,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AddBlankLine {
|
|
||||||
fn has_before(&self) -> bool {
|
|
||||||
matches!(self, AddBlankLine::Before | AddBlankLine::BeforeTwice | AddBlankLine::Around)
|
|
||||||
}
|
|
||||||
fn has_after(&self) -> bool {
|
|
||||||
matches!(self, AddBlankLine::After | AddBlankLine::AfterTwice | AddBlankLine::Around)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_insert_position(
|
|
||||||
scope: &ImportScope,
|
scope: &ImportScope,
|
||||||
insert_path: ast::Path,
|
insert_path: ast::Path,
|
||||||
group_imports: bool,
|
group_imports: bool,
|
||||||
) -> (InsertPosition<SyntaxElement>, AddBlankLine) {
|
use_item: ast::Use,
|
||||||
|
) {
|
||||||
|
let scope_syntax = scope.as_syntax_node();
|
||||||
let group = ImportGroup::new(&insert_path);
|
let group = ImportGroup::new(&insert_path);
|
||||||
let path_node_iter = scope
|
let path_node_iter = scope_syntax
|
||||||
.as_syntax_node()
|
|
||||||
.children()
|
.children()
|
||||||
.filter_map(|node| ast::Use::cast(node.clone()).zip(Some(node)))
|
.filter_map(|node| ast::Use::cast(node.clone()).zip(Some(node)))
|
||||||
.flat_map(|(use_, node)| {
|
.flat_map(|(use_, node)| {
|
||||||
|
@ -557,9 +451,14 @@ fn find_insert_position(
|
||||||
|
|
||||||
if !group_imports {
|
if !group_imports {
|
||||||
if let Some((_, _, node)) = path_node_iter.last() {
|
if let Some((_, _, node)) = path_node_iter.last() {
|
||||||
return (InsertPosition::After(node.into()), AddBlankLine::Before);
|
cov_mark::hit!(insert_no_grouping_last);
|
||||||
|
ted::insert(ted::Position::after(node), use_item.syntax());
|
||||||
|
} else {
|
||||||
|
cov_mark::hit!(insert_no_grouping_last2);
|
||||||
|
ted::insert(ted::Position::first_child_of(scope_syntax), make::tokens::blank_line());
|
||||||
|
ted::insert(ted::Position::first_child_of(scope_syntax), use_item.syntax());
|
||||||
}
|
}
|
||||||
return (InsertPosition::First, AddBlankLine::AfterTwice);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterator that discards anything thats not in the required grouping
|
// Iterator that discards anything thats not in the required grouping
|
||||||
|
@ -572,43 +471,91 @@ fn find_insert_position(
|
||||||
// track the last element we iterated over, if this is still None after the iteration then that means we never iterated in the first place
|
// track the last element we iterated over, if this is still None after the iteration then that means we never iterated in the first place
|
||||||
let mut last = None;
|
let mut last = None;
|
||||||
// find the element that would come directly after our new import
|
// find the element that would come directly after our new import
|
||||||
let post_insert = group_iter.inspect(|(.., node)| last = Some(node.clone())).find(
|
let post_insert: Option<(_, _, SyntaxNode)> = group_iter
|
||||||
|&(ref path, has_tl, _)| {
|
.inspect(|(.., node)| last = Some(node.clone()))
|
||||||
|
.find(|&(ref path, has_tl, _)| {
|
||||||
use_tree_path_cmp(&insert_path, false, path, has_tl) != Ordering::Greater
|
use_tree_path_cmp(&insert_path, false, path, has_tl) != Ordering::Greater
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
match post_insert {
|
if let Some((.., node)) = post_insert {
|
||||||
|
cov_mark::hit!(insert_group);
|
||||||
// insert our import before that element
|
// insert our import before that element
|
||||||
Some((.., node)) => (InsertPosition::Before(node.into()), AddBlankLine::After),
|
return ted::insert(ted::Position::before(node), use_item.syntax());
|
||||||
|
}
|
||||||
|
if let Some(node) = last {
|
||||||
|
cov_mark::hit!(insert_group_last);
|
||||||
// there is no element after our new import, so append it to the end of the group
|
// there is no element after our new import, so append it to the end of the group
|
||||||
None => match last {
|
return ted::insert(ted::Position::after(node), use_item.syntax());
|
||||||
Some(node) => (InsertPosition::After(node.into()), AddBlankLine::Before),
|
}
|
||||||
// the group we were looking for actually doesnt exist, so insert
|
|
||||||
None => {
|
// the group we were looking for actually doesn't exist, so insert
|
||||||
// similar concept here to the `last` from above
|
|
||||||
let mut last = None;
|
let mut last = None;
|
||||||
// find the group that comes after where we want to insert
|
// find the group that comes after where we want to insert
|
||||||
let post_group = path_node_iter
|
let post_group = path_node_iter
|
||||||
.inspect(|(.., node)| last = Some(node.clone()))
|
.inspect(|(.., node)| last = Some(node.clone()))
|
||||||
.find(|(p, ..)| ImportGroup::new(p) > group);
|
.find(|(p, ..)| ImportGroup::new(p) > group);
|
||||||
match post_group {
|
if let Some((.., node)) = post_group {
|
||||||
Some((.., node)) => {
|
cov_mark::hit!(insert_group_new_group);
|
||||||
(InsertPosition::Before(node.into()), AddBlankLine::AfterTwice)
|
ted::insert(ted::Position::before(&node), use_item.syntax());
|
||||||
|
if let Some(node) = algo::non_trivia_sibling(node.into(), Direction::Prev) {
|
||||||
|
ted::insert(ted::Position::after(node), make::tokens::single_newline());
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
// there is no such group, so append after the last one
|
// there is no such group, so append after the last one
|
||||||
None => match last {
|
if let Some(node) = last {
|
||||||
Some(node) => {
|
cov_mark::hit!(insert_group_no_group);
|
||||||
(InsertPosition::After(node.into()), AddBlankLine::BeforeTwice)
|
ted::insert(ted::Position::after(&node), use_item.syntax());
|
||||||
|
ted::insert(ted::Position::after(node), make::tokens::single_newline());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
// there are no imports in this file at all
|
// there are no imports in this file at all
|
||||||
None => scope.insert_pos_after_last_inner_element(),
|
if let Some(last_inner_element) = scope_syntax
|
||||||
},
|
.children_with_tokens()
|
||||||
|
.filter(|child| match child {
|
||||||
|
NodeOrToken::Node(node) => is_inner_attribute(node.clone()),
|
||||||
|
NodeOrToken::Token(token) => is_inner_comment(token.clone()),
|
||||||
|
})
|
||||||
|
.last()
|
||||||
|
{
|
||||||
|
cov_mark::hit!(insert_group_empty_inner_attr);
|
||||||
|
ted::insert(ted::Position::after(&last_inner_element), use_item.syntax());
|
||||||
|
ted::insert(ted::Position::after(last_inner_element), make::tokens::single_newline());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
match scope {
|
||||||
|
ImportScope::File(_) => {
|
||||||
|
cov_mark::hit!(insert_group_empty_file);
|
||||||
|
ted::insert(ted::Position::first_child_of(scope_syntax), make::tokens::blank_line());
|
||||||
|
ted::insert(ted::Position::first_child_of(scope_syntax), use_item.syntax())
|
||||||
|
}
|
||||||
|
// don't insert the imports before the item list's opening curly brace
|
||||||
|
ImportScope::Module(item_list) => match item_list.l_curly_token() {
|
||||||
|
Some(b) => {
|
||||||
|
cov_mark::hit!(insert_group_empty_module);
|
||||||
|
ted::insert(ted::Position::after(&b), make::tokens::single_newline());
|
||||||
|
ted::insert(ted::Position::after(&b), use_item.syntax());
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// This should never happens, broken module syntax node
|
||||||
|
ted::insert(
|
||||||
|
ted::Position::first_child_of(scope_syntax),
|
||||||
|
make::tokens::blank_line(),
|
||||||
|
);
|
||||||
|
ted::insert(ted::Position::first_child_of(scope_syntax), use_item.syntax());
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_inner_attribute(node: SyntaxNode) -> bool {
|
||||||
|
ast::Attr::cast(node).map(|attr| attr.kind()) == Some(ast::AttrKind::Inner)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_inner_comment(token: SyntaxToken) -> bool {
|
||||||
|
ast::Comment::cast(token).and_then(|comment| comment.kind().doc)
|
||||||
|
== Some(ast::CommentPlacement::Inner)
|
||||||
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
|
@ -5,6 +5,7 @@ use test_utils::assert_eq_text;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_not_group() {
|
fn insert_not_group() {
|
||||||
|
cov_mark::check!(insert_no_grouping_last);
|
||||||
check(
|
check(
|
||||||
"use external_crate2::bar::A",
|
"use external_crate2::bar::A",
|
||||||
r"
|
r"
|
||||||
|
@ -26,6 +27,21 @@ use external_crate2::bar::A;",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn insert_not_group_empty() {
|
||||||
|
cov_mark::check!(insert_no_grouping_last2);
|
||||||
|
check(
|
||||||
|
"use external_crate2::bar::A",
|
||||||
|
r"",
|
||||||
|
r"use external_crate2::bar::A;
|
||||||
|
|
||||||
|
",
|
||||||
|
None,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_existing() {
|
fn insert_existing() {
|
||||||
check_full("std::fs", "use std::fs;", "use std::fs;")
|
check_full("std::fs", "use std::fs;", "use std::fs;")
|
||||||
|
@ -51,21 +67,21 @@ use std::bar::G;",
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_start_indent() {
|
fn insert_start_indent() {
|
||||||
cov_mark::check!(insert_use_indent_after);
|
|
||||||
check_none(
|
check_none(
|
||||||
"std::bar::AA",
|
"std::bar::AA",
|
||||||
r"
|
r"
|
||||||
use std::bar::B;
|
use std::bar::B;
|
||||||
use std::bar::D;",
|
use std::bar::C;",
|
||||||
r"
|
r"
|
||||||
use std::bar::AA;
|
use std::bar::AA;
|
||||||
use std::bar::B;
|
use std::bar::B;
|
||||||
use std::bar::D;",
|
use std::bar::C;",
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_middle() {
|
fn insert_middle() {
|
||||||
|
cov_mark::check!(insert_group);
|
||||||
check_none(
|
check_none(
|
||||||
"std::bar::EE",
|
"std::bar::EE",
|
||||||
r"
|
r"
|
||||||
|
@ -102,6 +118,7 @@ fn insert_middle_indent() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_end() {
|
fn insert_end() {
|
||||||
|
cov_mark::check!(insert_group_last);
|
||||||
check_none(
|
check_none(
|
||||||
"std::bar::ZZ",
|
"std::bar::ZZ",
|
||||||
r"
|
r"
|
||||||
|
@ -120,7 +137,6 @@ use std::bar::ZZ;",
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_end_indent() {
|
fn insert_end_indent() {
|
||||||
cov_mark::check!(insert_use_indent_before);
|
|
||||||
check_none(
|
check_none(
|
||||||
"std::bar::ZZ",
|
"std::bar::ZZ",
|
||||||
r"
|
r"
|
||||||
|
@ -201,6 +217,7 @@ fn insert_first_matching_group() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_missing_group_std() {
|
fn insert_missing_group_std() {
|
||||||
|
cov_mark::check!(insert_group_new_group);
|
||||||
check_none(
|
check_none(
|
||||||
"std::fmt",
|
"std::fmt",
|
||||||
r"
|
r"
|
||||||
|
@ -216,6 +233,7 @@ fn insert_missing_group_std() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_missing_group_self() {
|
fn insert_missing_group_self() {
|
||||||
|
cov_mark::check!(insert_group_no_group);
|
||||||
check_none(
|
check_none(
|
||||||
"self::fmt",
|
"self::fmt",
|
||||||
r"
|
r"
|
||||||
|
@ -242,6 +260,7 @@ fn main() {}",
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_empty_file() {
|
fn insert_empty_file() {
|
||||||
|
cov_mark::check!(insert_group_empty_file);
|
||||||
// empty files will get two trailing newlines
|
// empty files will get two trailing newlines
|
||||||
// this is due to the test case insert_no_imports above
|
// this is due to the test case insert_no_imports above
|
||||||
check_full(
|
check_full(
|
||||||
|
@ -255,7 +274,7 @@ fn insert_empty_file() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_empty_module() {
|
fn insert_empty_module() {
|
||||||
cov_mark::check!(insert_use_no_indent_after);
|
cov_mark::check!(insert_group_empty_module);
|
||||||
check(
|
check(
|
||||||
"foo::bar",
|
"foo::bar",
|
||||||
"mod x {}",
|
"mod x {}",
|
||||||
|
@ -270,6 +289,7 @@ fn insert_empty_module() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_after_inner_attr() {
|
fn insert_after_inner_attr() {
|
||||||
|
cov_mark::check!(insert_group_empty_inner_attr);
|
||||||
check_full(
|
check_full(
|
||||||
"foo::bar",
|
"foo::bar",
|
||||||
r"#![allow(unused_imports)]",
|
r"#![allow(unused_imports)]",
|
||||||
|
@ -615,7 +635,7 @@ fn check(
|
||||||
if module {
|
if module {
|
||||||
syntax = syntax.descendants().find_map(ast::Module::cast).unwrap().syntax().clone();
|
syntax = syntax.descendants().find_map(ast::Module::cast).unwrap().syntax().clone();
|
||||||
}
|
}
|
||||||
let file = super::ImportScope::from(syntax).unwrap();
|
let file = super::ImportScope::from(syntax.clone_for_update()).unwrap();
|
||||||
let path = ast::SourceFile::parse(&format!("use {};", path))
|
let path = ast::SourceFile::parse(&format!("use {};", path))
|
||||||
.tree()
|
.tree()
|
||||||
.syntax()
|
.syntax()
|
||||||
|
@ -623,12 +643,8 @@ fn check(
|
||||||
.find_map(ast::Path::cast)
|
.find_map(ast::Path::cast)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let rewriter = insert_use(
|
insert_use(&file, path, InsertUseConfig { merge: mb, prefix_kind: PrefixKind::Plain, group });
|
||||||
&file,
|
let result = file.as_syntax_node().to_string();
|
||||||
path,
|
|
||||||
InsertUseConfig { merge: mb, prefix_kind: PrefixKind::Plain, group },
|
|
||||||
);
|
|
||||||
let result = rewriter.rewrite(file.as_syntax_node()).to_string();
|
|
||||||
assert_eq_text!(ra_fixture_after, &result);
|
assert_eq_text!(ra_fixture_after, &result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -342,10 +342,10 @@ enum InsertPos {
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct SyntaxRewriter<'a> {
|
pub struct SyntaxRewriter<'a> {
|
||||||
f: Option<Box<dyn Fn(&SyntaxElement) -> Option<SyntaxElement> + 'a>>,
|
|
||||||
//FIXME: add debug_assertions that all elements are in fact from the same file.
|
//FIXME: add debug_assertions that all elements are in fact from the same file.
|
||||||
replacements: FxHashMap<SyntaxElement, Replacement>,
|
replacements: FxHashMap<SyntaxElement, Replacement>,
|
||||||
insertions: IndexMap<InsertPos, Vec<SyntaxElement>>,
|
insertions: IndexMap<InsertPos, Vec<SyntaxElement>>,
|
||||||
|
_pd: std::marker::PhantomData<&'a ()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for SyntaxRewriter<'_> {
|
impl fmt::Debug for SyntaxRewriter<'_> {
|
||||||
|
@ -357,14 +357,7 @@ impl fmt::Debug for SyntaxRewriter<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SyntaxRewriter<'a> {
|
impl SyntaxRewriter<'_> {
|
||||||
pub fn from_fn(f: impl Fn(&SyntaxElement) -> Option<SyntaxElement> + 'a) -> SyntaxRewriter<'a> {
|
|
||||||
SyntaxRewriter {
|
|
||||||
f: Some(Box::new(f)),
|
|
||||||
replacements: FxHashMap::default(),
|
|
||||||
insertions: IndexMap::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn delete<T: Clone + Into<SyntaxElement>>(&mut self, what: &T) {
|
pub fn delete<T: Clone + Into<SyntaxElement>>(&mut self, what: &T) {
|
||||||
let what = what.clone().into();
|
let what = what.clone().into();
|
||||||
let replacement = Replacement::Delete;
|
let replacement = Replacement::Delete;
|
||||||
|
@ -470,7 +463,7 @@ impl<'a> SyntaxRewriter<'a> {
|
||||||
pub fn rewrite(&self, node: &SyntaxNode) -> SyntaxNode {
|
pub fn rewrite(&self, node: &SyntaxNode) -> SyntaxNode {
|
||||||
let _p = profile::span("rewrite");
|
let _p = profile::span("rewrite");
|
||||||
|
|
||||||
if self.f.is_none() && self.replacements.is_empty() && self.insertions.is_empty() {
|
if self.replacements.is_empty() && self.insertions.is_empty() {
|
||||||
return node.clone();
|
return node.clone();
|
||||||
}
|
}
|
||||||
let green = self.rewrite_children(node);
|
let green = self.rewrite_children(node);
|
||||||
|
@ -495,7 +488,6 @@ impl<'a> SyntaxRewriter<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert!(self.f.is_none());
|
|
||||||
self.replacements
|
self.replacements
|
||||||
.keys()
|
.keys()
|
||||||
.filter_map(element_to_node_or_parent)
|
.filter_map(element_to_node_or_parent)
|
||||||
|
@ -510,10 +502,6 @@ impl<'a> SyntaxRewriter<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replacement(&self, element: &SyntaxElement) -> Option<Replacement> {
|
fn replacement(&self, element: &SyntaxElement) -> Option<Replacement> {
|
||||||
if let Some(f) = &self.f {
|
|
||||||
assert!(self.replacements.is_empty());
|
|
||||||
return f(element).map(Replacement::Single);
|
|
||||||
}
|
|
||||||
self.replacements.get(element).cloned()
|
self.replacements.get(element).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -574,7 +562,6 @@ fn element_to_green(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, row
|
||||||
|
|
||||||
impl ops::AddAssign for SyntaxRewriter<'_> {
|
impl ops::AddAssign for SyntaxRewriter<'_> {
|
||||||
fn add_assign(&mut self, rhs: SyntaxRewriter) {
|
fn add_assign(&mut self, rhs: SyntaxRewriter) {
|
||||||
assert!(rhs.f.is_none());
|
|
||||||
self.replacements.extend(rhs.replacements);
|
self.replacements.extend(rhs.replacements);
|
||||||
for (pos, insertions) in rhs.insertions.into_iter() {
|
for (pos, insertions) in rhs.insertions.into_iter() {
|
||||||
match self.insertions.entry(pos) {
|
match self.insertions.entry(pos) {
|
||||||
|
|
|
@ -632,6 +632,7 @@ pub mod tokens {
|
||||||
SOURCE_FILE
|
SOURCE_FILE
|
||||||
.tree()
|
.tree()
|
||||||
.syntax()
|
.syntax()
|
||||||
|
.clone_for_update()
|
||||||
.descendants_with_tokens()
|
.descendants_with_tokens()
|
||||||
.filter_map(|it| it.into_token())
|
.filter_map(|it| it.into_token())
|
||||||
.find(|it| it.kind() == WHITESPACE && it.text() == "\n\n")
|
.find(|it| it.kind() == WHITESPACE && it.text() == "\n\n")
|
||||||
|
|
|
@ -7,7 +7,7 @@ use std::{mem, ops::RangeInclusive};
|
||||||
use parser::T;
|
use parser::T;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{edit::IndentLevel, make},
|
ast::{self, edit::IndentLevel, make, AstNode},
|
||||||
SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken,
|
SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -147,6 +147,16 @@ pub fn append_child_raw(node: &(impl Into<SyntaxNode> + Clone), child: impl Elem
|
||||||
fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> {
|
fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> {
|
||||||
let prev = match &position.repr {
|
let prev = match &position.repr {
|
||||||
PositionRepr::FirstChild(_) => return None,
|
PositionRepr::FirstChild(_) => return None,
|
||||||
|
PositionRepr::After(it) if it.kind() == SyntaxKind::L_CURLY => {
|
||||||
|
if new.kind() == SyntaxKind::USE {
|
||||||
|
if let Some(item_list) = it.parent().and_then(ast::ItemList::cast) {
|
||||||
|
let mut indent = IndentLevel::from_element(&item_list.syntax().clone().into());
|
||||||
|
indent.0 += 1;
|
||||||
|
return Some(make::tokens::whitespace(&format!("\n{}", indent)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
it
|
||||||
|
}
|
||||||
PositionRepr::After(it) => it,
|
PositionRepr::After(it) => it,
|
||||||
};
|
};
|
||||||
ws_between(prev, new)
|
ws_between(prev, new)
|
||||||
|
@ -173,7 +183,10 @@ fn ws_between(left: &SyntaxElement, right: &SyntaxElement) -> Option<SyntaxToken
|
||||||
}
|
}
|
||||||
|
|
||||||
if right.kind() == SyntaxKind::USE {
|
if right.kind() == SyntaxKind::USE {
|
||||||
let indent = IndentLevel::from_element(left);
|
let mut indent = IndentLevel::from_element(left);
|
||||||
|
if left.kind() == SyntaxKind::USE {
|
||||||
|
indent.0 = IndentLevel::from_element(right).0.max(indent.0);
|
||||||
|
}
|
||||||
return Some(make::tokens::whitespace(&format!("\n{}", indent)));
|
return Some(make::tokens::whitespace(&format!("\n{}", indent)));
|
||||||
}
|
}
|
||||||
Some(make::tokens::single_space())
|
Some(make::tokens::single_space())
|
||||||
|
|
Loading…
Reference in a new issue