rust-analyzer/crates/ra_assists/src/handlers/split_import.rs
Aleksey Kladov 8a04372fec Fix panic in split_imports assist
The fix is admittedly quit literally just papering over.

Long-term, I see two more principled approaches:

* we switch to a fully tree-based impl, without parse . to_string
  step; with this approach, there shouldn't be any panics. The results
  might be nonsensical, but so was the original input.

* we preserve the invariant that re-parsing constructed node is an
  identity, and make all the `make_xxx` method return an `Option`.

closes #4044
2020-04-20 16:34:01 +02:00

71 lines
1.8 KiB
Rust

use std::iter::successors;
use ra_syntax::{ast, AstNode, T};
use crate::{Assist, AssistCtx, AssistId};
// Assist: split_import
//
// Wraps the tail of import into braces.
//
// ```
// use std::<|>collections::HashMap;
// ```
// ->
// ```
// use std::{collections::HashMap};
// ```
pub(crate) fn split_import(ctx: AssistCtx) -> Option<Assist> {
let colon_colon = ctx.find_token_at_offset(T![::])?;
let path = ast::Path::cast(colon_colon.parent())?.qualifier()?;
let top_path = successors(Some(path.clone()), |it| it.parent_path()).last()?;
let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast)?;
let new_tree = use_tree.split_prefix(&path);
if new_tree == use_tree {
return None;
}
let cursor = ctx.frange.range.start();
ctx.add_assist(AssistId("split_import"), "Split import", |edit| {
edit.target(colon_colon.text_range());
edit.replace_ast(use_tree, new_tree);
edit.set_cursor(cursor);
})
}
#[cfg(test)]
mod tests {
use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target};
use super::*;
#[test]
fn test_split_import() {
check_assist(
split_import,
"use crate::<|>db::RootDatabase;",
"use crate::<|>{db::RootDatabase};",
)
}
#[test]
fn split_import_works_with_trees() {
check_assist(
split_import,
"use crate:<|>:db::{RootDatabase, FileSymbol}",
"use crate:<|>:{db::{RootDatabase, FileSymbol}}",
)
}
#[test]
fn split_import_target() {
check_assist_target(split_import, "use crate::<|>db::{RootDatabase, FileSymbol}", "::");
}
#[test]
fn issue4044() {
check_assist_not_applicable(split_import, "use crate::<|>:::self;")
}
}