Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions crates/ide-assists/src/handlers/move_const_to_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use syntax::{
ast::{
self, AstNode,
edit::{AstNodeEdit, IndentLevel},
syntax_factory::SyntaxFactory,
},
};

Expand Down Expand Up @@ -90,6 +91,8 @@ pub(crate) fn move_const_to_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
"Move const to impl block",
const_.syntax().text_range(),
|builder| {
let make = SyntaxFactory::with_mappings();

let usages = Definition::Const(def)
.usages(&ctx.sema)
.in_scope(&SearchScope::file_range(FileRange {
Expand All @@ -100,7 +103,6 @@ pub(crate) fn move_const_to_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->

let range_to_delete = match const_.syntax().next_sibling_or_token() {
Some(s) if matches!(s.kind(), SyntaxKind::WHITESPACE) => {
// Remove following whitespaces too.
const_.syntax().text_range().cover(s.text_range())
}
_ => const_.syntax().text_range(),
Expand All @@ -116,36 +118,30 @@ pub(crate) fn move_const_to_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
builder.replace(range, const_ref);
}

// Heuristically inserting the extracted const after the consecutive existing consts
// from the beginning of assoc items. We assume there are no inherent assoc type as
// above.
let last_const =
items.assoc_items().take_while(|it| matches!(it, ast::AssocItem::Const(_))).last();
let insert_offset = match &last_const {
Some(it) => it.syntax().text_range().end(),
None => match items.l_curly_token() {
Some(l_curly) => l_curly.text_range().end(),
// Not sure if this branch is ever reachable, but it wouldn't hurt to have a
// fallback.
None => items.syntax().text_range().start(),
},
};

// If the moved const will be the first item of the impl, add a new line after that.
//
// We're assuming the code is formatted according to Rust's standard style guidelines
// (i.e. no empty lines between impl's `{` token and its first assoc item).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete comments

let fixup = if last_const.is_none() { "\n" } else { "" };
let indent = IndentLevel::from_node(parent_fn.syntax());

let mut editor = builder.make_editor(const_.syntax());
let const_ = const_.clone_for_update();
let const_ = const_.reset_indent();
let const_ = const_.indent(indent);
let const_ = const_.indent_with_mapping(indent, &make);
builder.insert(insert_offset, format!("\n{indent}{const_}{fixup}"));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a migration


editor.add_mappings(make.finish_with_mappings());
builder.add_file_edits(ctx.vfs_file_id(), editor);
},
)
}

#[cfg(test)]
mod tests {
use crate::tests::{check_assist, check_assist_not_applicable};
Expand Down