Skip to content

Commit 952f385

Browse files
committed
Impl make::blank_line
1 parent 07ff9ee commit 952f385

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

crates/assists/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use syntax::{
1616

1717
use crate::assist_config::SnippetCap;
1818

19-
pub(crate) use insert_use::{find_insert_use_container, insert_use_statement};
19+
pub(crate) use insert_use::{find_insert_use_container, insert_use, MergeBehaviour};
2020

2121
pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
2222
extract_trivial_expression(&block)

crates/assists/src/utils/insert_use.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ use syntax::{
99
Direction, InsertPosition, SyntaxElement, SyntaxNode, T,
1010
};
1111

12-
use crate::assist_context::AssistContext;
1312
use test_utils::mark;
1413

1514
/// Determines the containing syntax node in which to insert a `use` statement affecting `position`.
1615
pub(crate) fn find_insert_use_container(
1716
position: &SyntaxNode,
18-
ctx: &AssistContext,
17+
ctx: &crate::assist_context::AssistContext,
1918
) -> Option<Either<ast::ItemList, ast::SourceFile>> {
2019
ctx.sema.ancestors_with_macros(position.clone()).find_map(|n| {
2120
if let Some(module) = ast::Module::cast(n.clone()) {
@@ -25,19 +24,9 @@ pub(crate) fn find_insert_use_container(
2524
})
2625
}
2726

28-
pub(crate) fn insert_use_statement(
29-
// Ideally the position of the cursor, used to
30-
position: &SyntaxNode,
31-
path_to_import: &str,
32-
ctx: &crate::assist_context::AssistContext,
33-
builder: &mut text_edit::TextEditBuilder,
34-
) {
35-
insert_use(position.clone(), make::path_from_text(path_to_import), Some(MergeBehaviour::Full));
36-
}
37-
3827
/// Insert an import path into the given file/node. A `merge` value of none indicates that no import merging is allowed to occur.
3928
pub fn insert_use(
40-
where_: SyntaxNode,
29+
where_: &SyntaxNode,
4130
path: ast::Path,
4231
merge: Option<MergeBehaviour>,
4332
) -> SyntaxNode {
@@ -49,42 +38,36 @@ pub fn insert_use(
4938
let to_delete: SyntaxElement = existing_use.syntax().clone().into();
5039
let to_delete = to_delete.clone()..=to_delete;
5140
let to_insert = iter::once(merged.syntax().clone().into());
52-
return algo::replace_children(&where_, to_delete, to_insert);
41+
return algo::replace_children(where_, to_delete, to_insert);
5342
}
5443
}
5544
}
5645

5746
// either we weren't allowed to merge or there is no import that fits the merge conditions
5847
// so look for the place we have to insert to
59-
let (insert_position, add_blank) = find_insert_position(&where_, path);
48+
let (insert_position, add_blank) = find_insert_position(where_, path);
6049

6150
let to_insert: Vec<SyntaxElement> = {
6251
let mut buf = Vec::new();
6352

6453
match add_blank {
6554
AddBlankLine::Before => buf.push(make::tokens::single_newline().into()),
66-
AddBlankLine::BeforeTwice => {
67-
buf.push(make::tokens::single_newline().into());
68-
buf.push(make::tokens::single_newline().into());
69-
}
55+
AddBlankLine::BeforeTwice => buf.push(make::tokens::blank_line().into()),
7056
_ => (),
7157
}
7258

7359
buf.push(use_item.syntax().clone().into());
7460

7561
match add_blank {
7662
AddBlankLine::After => buf.push(make::tokens::single_newline().into()),
77-
AddBlankLine::AfterTwice => {
78-
buf.push(make::tokens::single_newline().into());
79-
buf.push(make::tokens::single_newline().into());
80-
}
63+
AddBlankLine::AfterTwice => buf.push(make::tokens::blank_line().into()),
8164
_ => (),
8265
}
8366

8467
buf
8568
};
8669

87-
algo::insert_children(&where_, insert_position, to_insert)
70+
algo::insert_children(where_, insert_position, to_insert)
8871
}
8972

9073
fn try_merge_imports(
@@ -613,7 +596,7 @@ use foo::bar;",
613596
.find_map(ast::Path::cast)
614597
.unwrap();
615598

616-
let result = insert_use(file, path, mb).to_string();
599+
let result = insert_use(&file, path, mb).to_string();
617600
assert_eq_text!(&result, ra_fixture_after);
618601
}
619602

crates/syntax/src/ast/make.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ pub mod tokens {
339339
use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken};
340340

341341
pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> =
342-
Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;"));
342+
Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;\n\n"));
343343

344344
pub fn single_space() -> SyntaxToken {
345345
SOURCE_FILE
@@ -379,6 +379,16 @@ pub mod tokens {
379379
.unwrap()
380380
}
381381

382+
pub fn blank_line() -> SyntaxToken {
383+
SOURCE_FILE
384+
.tree()
385+
.syntax()
386+
.descendants_with_tokens()
387+
.filter_map(|it| it.into_token())
388+
.find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n\n")
389+
.unwrap()
390+
}
391+
382392
pub struct WsBuilder(SourceFile);
383393

384394
impl WsBuilder {

0 commit comments

Comments
 (0)