From 0635458a6bf883b5abcae024afb17a11688fef92 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 18:49:43 +0200 Subject: **Merge Imports** assist handles self --- crates/ra_assists/src/handlers/merge_imports.rs | 27 +++++++++++++++++++++++++ crates/syntax/src/ast/edit.rs | 11 +++++++--- crates/syntax/src/ast/make.rs | 3 +++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index 9c5c6eda7..47d465404 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs @@ -164,6 +164,33 @@ use std::fmt::{Display, Debug}; ); } + #[test] + fn merge_self1() { + check_assist( + merge_imports, + r" +use std::fmt<|>; +use std::fmt::Display; +", + r" +use std::fmt::{self, Display}; +", + ); + } + + #[test] + fn merge_self2() { + check_assist( + merge_imports, + r" +use std::{fmt, <|>fmt::Display}; +", + r" +use std::{fmt::{Display, self}}; +", + ); + } + #[test] fn test_merge_nested() { check_assist( diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs index 5ed123f91..2667d9af4 100644 --- a/crates/syntax/src/ast/edit.rs +++ b/crates/syntax/src/ast/edit.rs @@ -313,10 +313,15 @@ impl ast::UseTree { #[must_use] pub fn split_prefix(&self, prefix: &ast::Path) -> ast::UseTree { - let suffix = match split_path_prefix(&prefix) { - Some(it) => it, - None => return self.clone(), + let suffix = if self.path().as_ref() == Some(prefix) && self.use_tree_list().is_none() { + make::path_unqualified(make::path_segment_self()) + } else { + match split_path_prefix(&prefix) { + Some(it) => it, + None => return self.clone(), + } }; + let use_tree = make::use_tree( suffix, self.use_tree_list(), diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 254a37fe3..3009faed7 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -24,6 +24,9 @@ pub fn ty(text: &str) -> ast::Type { pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment { ast_from_text(&format!("use {};", name_ref)) } +pub fn path_segment_self() -> ast::PathSegment { + ast_from_text("use self;") +} pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path { path_from_text(&format!("use {}", segment)) } -- cgit v1.2.3