|
| 1 | +use std::iter::successors; |
| 2 | + |
| 3 | +use ast::{edit::AstNodeEdit, make}; |
| 4 | +use ra_syntax::{ast, AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T}; |
| 5 | + |
| 6 | +use crate::{Assist, AssistCtx, AssistId}; |
| 7 | + |
| 8 | +// Assist: merge_imports |
| 9 | +// |
| 10 | +// Merges two imports with a common prefix. |
| 11 | +// |
| 12 | +// ``` |
| 13 | +// use std::<|>fmt::Formatter; |
| 14 | +// use std::io; |
| 15 | +// ``` |
| 16 | +// -> |
| 17 | +// ``` |
| 18 | +// use std::{fmt::Formatter, io}; |
| 19 | +// ``` |
| 20 | +pub(crate) fn merge_imports(ctx: AssistCtx) -> Option<Assist> { |
| 21 | + let tree: ast::UseTree = ctx.find_node_at_offset()?; |
| 22 | + let use_item = tree.syntax().parent().and_then(ast::UseItem::cast)?; |
| 23 | + let (merged, to_delete) = [Direction::Prev, Direction::Next] |
| 24 | + .iter() |
| 25 | + .copied() |
| 26 | + .filter_map(|dir| next_use_item(&use_item, dir)) |
| 27 | + .filter_map(|it| Some((it.clone(), it.use_tree()?))) |
| 28 | + .find_map(|(use_item, use_tree)| { |
| 29 | + Some((try_merge_trees(&tree, &use_tree)?, use_item.clone())) |
| 30 | + })?; |
| 31 | + let mut offset = ctx.frange.range.start(); |
| 32 | + ctx.add_assist(AssistId("merge_imports"), "Merge imports", |edit| { |
| 33 | + edit.replace_ast(tree, merged); |
| 34 | + |
| 35 | + let mut range = to_delete.syntax().text_range(); |
| 36 | + let next_ws = to_delete |
| 37 | + .syntax() |
| 38 | + .next_sibling_or_token() |
| 39 | + .and_then(|it| it.into_token()) |
| 40 | + .and_then(ast::Whitespace::cast); |
| 41 | + if let Some(ws) = next_ws { |
| 42 | + range = range.extend_to(&ws.syntax().text_range()) |
| 43 | + } |
| 44 | + edit.delete(range); |
| 45 | + if range.end() <= offset { |
| 46 | + offset -= range.len(); |
| 47 | + } |
| 48 | + edit.set_cursor(offset); |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +fn next_use_item(this_use_item: &ast::UseItem, direction: Direction) -> Option<ast::UseItem> { |
| 53 | + this_use_item.syntax().siblings(direction).skip(1).find_map(ast::UseItem::cast) |
| 54 | +} |
| 55 | + |
| 56 | +fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTree> { |
| 57 | + let lhs_path = old.path()?; |
| 58 | + let rhs_path = new.path()?; |
| 59 | + |
| 60 | + let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?; |
| 61 | + |
| 62 | + let lhs = old.split_prefix(&lhs_prefix); |
| 63 | + let rhs = new.split_prefix(&rhs_prefix); |
| 64 | + |
| 65 | + let mut to_insert: Vec<SyntaxElement> = Vec::new(); |
| 66 | + to_insert.push(make::token(T![,]).into()); |
| 67 | + to_insert.push(make::tokens::single_space().into()); |
| 68 | + to_insert.extend( |
| 69 | + rhs.use_tree_list()? |
| 70 | + .syntax() |
| 71 | + .children_with_tokens() |
| 72 | + .filter(|it| it.kind() != T!['{'] && it.kind() != T!['}']), |
| 73 | + ); |
| 74 | + let use_tree_list = lhs.use_tree_list()?; |
| 75 | + let pos = InsertPosition::Before(use_tree_list.r_curly()?.into()); |
| 76 | + let use_tree_list = use_tree_list.insert_children(pos, to_insert); |
| 77 | + Some(lhs.with_use_tree_list(use_tree_list)) |
| 78 | +} |
| 79 | + |
| 80 | +fn common_prefix(lhs: &ast::Path, rhs: &ast::Path) -> Option<(ast::Path, ast::Path)> { |
| 81 | + let mut res = None; |
| 82 | + let mut lhs_curr = first_path(&lhs); |
| 83 | + let mut rhs_curr = first_path(&rhs); |
| 84 | + loop { |
| 85 | + match (lhs_curr.segment(), rhs_curr.segment()) { |
| 86 | + (Some(lhs), Some(rhs)) if lhs.syntax().text() == rhs.syntax().text() => (), |
| 87 | + _ => break, |
| 88 | + } |
| 89 | + res = Some((lhs_curr.clone(), rhs_curr.clone())); |
| 90 | + |
| 91 | + match (lhs_curr.parent_path(), rhs_curr.parent_path()) { |
| 92 | + (Some(lhs), Some(rhs)) => { |
| 93 | + lhs_curr = lhs; |
| 94 | + rhs_curr = rhs; |
| 95 | + } |
| 96 | + _ => break, |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + res |
| 101 | +} |
| 102 | + |
| 103 | +fn first_path(path: &ast::Path) -> ast::Path { |
| 104 | + successors(Some(path.clone()), |it| it.qualifier()).last().unwrap() |
| 105 | +} |
| 106 | + |
| 107 | +#[cfg(test)] |
| 108 | +mod tests { |
| 109 | + use crate::helpers::check_assist; |
| 110 | + |
| 111 | + use super::*; |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn test_merge_first() { |
| 115 | + check_assist( |
| 116 | + merge_imports, |
| 117 | + r" |
| 118 | +use std::fmt<|>::Debug; |
| 119 | +use std::fmt::Display; |
| 120 | +", |
| 121 | + r" |
| 122 | +use std::fmt<|>::{Debug, Display}; |
| 123 | +", |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn test_merge_second() { |
| 129 | + check_assist( |
| 130 | + merge_imports, |
| 131 | + r" |
| 132 | +use std::fmt::Debug; |
| 133 | +use std::fmt<|>::Display; |
| 134 | +", |
| 135 | + r" |
| 136 | +use std::fmt<|>::{Display, Debug}; |
| 137 | +", |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + #[ignore] |
| 143 | + fn test_merge_nested() { |
| 144 | + check_assist( |
| 145 | + merge_imports, |
| 146 | + r" |
| 147 | +use std::{fmt<|>::Debug, fmt::Display}; |
| 148 | +", |
| 149 | + r" |
| 150 | +use std::{fmt::{Debug, Display}}; |
| 151 | +", |
| 152 | + ) |
| 153 | + } |
| 154 | +} |
0 commit comments