mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 05:08:52 +00:00
Impl make::blank_line
This commit is contained in:
parent
07ff9eeca8
commit
952f385682
3 changed files with 20 additions and 27 deletions
|
@ -16,7 +16,7 @@ use syntax::{
|
||||||
|
|
||||||
use crate::assist_config::SnippetCap;
|
use crate::assist_config::SnippetCap;
|
||||||
|
|
||||||
pub(crate) use insert_use::{find_insert_use_container, insert_use_statement};
|
pub(crate) use insert_use::{find_insert_use_container, insert_use, MergeBehaviour};
|
||||||
|
|
||||||
pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
|
pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
|
||||||
extract_trivial_expression(&block)
|
extract_trivial_expression(&block)
|
||||||
|
|
|
@ -9,13 +9,12 @@ use syntax::{
|
||||||
Direction, InsertPosition, SyntaxElement, SyntaxNode, T,
|
Direction, InsertPosition, SyntaxElement, SyntaxNode, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::assist_context::AssistContext;
|
|
||||||
use test_utils::mark;
|
use test_utils::mark;
|
||||||
|
|
||||||
/// Determines the containing syntax node in which to insert a `use` statement affecting `position`.
|
/// Determines the containing syntax node in which to insert a `use` statement affecting `position`.
|
||||||
pub(crate) fn find_insert_use_container(
|
pub(crate) fn find_insert_use_container(
|
||||||
position: &SyntaxNode,
|
position: &SyntaxNode,
|
||||||
ctx: &AssistContext,
|
ctx: &crate::assist_context::AssistContext,
|
||||||
) -> Option<Either<ast::ItemList, ast::SourceFile>> {
|
) -> Option<Either<ast::ItemList, ast::SourceFile>> {
|
||||||
ctx.sema.ancestors_with_macros(position.clone()).find_map(|n| {
|
ctx.sema.ancestors_with_macros(position.clone()).find_map(|n| {
|
||||||
if let Some(module) = ast::Module::cast(n.clone()) {
|
if let Some(module) = ast::Module::cast(n.clone()) {
|
||||||
|
@ -25,19 +24,9 @@ pub(crate) fn find_insert_use_container(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn insert_use_statement(
|
|
||||||
// Ideally the position of the cursor, used to
|
|
||||||
position: &SyntaxNode,
|
|
||||||
path_to_import: &str,
|
|
||||||
ctx: &crate::assist_context::AssistContext,
|
|
||||||
builder: &mut text_edit::TextEditBuilder,
|
|
||||||
) {
|
|
||||||
insert_use(position.clone(), make::path_from_text(path_to_import), Some(MergeBehaviour::Full));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Insert an import path into the given file/node. A `merge` value of none indicates that no import merging is allowed to occur.
|
/// Insert an import path into the given file/node. A `merge` value of none indicates that no import merging is allowed to occur.
|
||||||
pub fn insert_use(
|
pub fn insert_use(
|
||||||
where_: SyntaxNode,
|
where_: &SyntaxNode,
|
||||||
path: ast::Path,
|
path: ast::Path,
|
||||||
merge: Option<MergeBehaviour>,
|
merge: Option<MergeBehaviour>,
|
||||||
) -> SyntaxNode {
|
) -> SyntaxNode {
|
||||||
|
@ -49,24 +38,21 @@ pub fn insert_use(
|
||||||
let to_delete: SyntaxElement = existing_use.syntax().clone().into();
|
let to_delete: SyntaxElement = existing_use.syntax().clone().into();
|
||||||
let to_delete = to_delete.clone()..=to_delete;
|
let to_delete = to_delete.clone()..=to_delete;
|
||||||
let to_insert = iter::once(merged.syntax().clone().into());
|
let to_insert = iter::once(merged.syntax().clone().into());
|
||||||
return algo::replace_children(&where_, to_delete, to_insert);
|
return algo::replace_children(where_, to_delete, to_insert);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// either we weren't allowed to merge or there is no import that fits the merge conditions
|
// either we weren't allowed to merge or there is no import that fits the merge conditions
|
||||||
// so look for the place we have to insert to
|
// so look for the place we have to insert to
|
||||||
let (insert_position, add_blank) = find_insert_position(&where_, path);
|
let (insert_position, add_blank) = find_insert_position(where_, path);
|
||||||
|
|
||||||
let to_insert: Vec<SyntaxElement> = {
|
let to_insert: Vec<SyntaxElement> = {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
|
|
||||||
match add_blank {
|
match add_blank {
|
||||||
AddBlankLine::Before => buf.push(make::tokens::single_newline().into()),
|
AddBlankLine::Before => buf.push(make::tokens::single_newline().into()),
|
||||||
AddBlankLine::BeforeTwice => {
|
AddBlankLine::BeforeTwice => buf.push(make::tokens::blank_line().into()),
|
||||||
buf.push(make::tokens::single_newline().into());
|
|
||||||
buf.push(make::tokens::single_newline().into());
|
|
||||||
}
|
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,17 +60,14 @@ pub fn insert_use(
|
||||||
|
|
||||||
match add_blank {
|
match add_blank {
|
||||||
AddBlankLine::After => buf.push(make::tokens::single_newline().into()),
|
AddBlankLine::After => buf.push(make::tokens::single_newline().into()),
|
||||||
AddBlankLine::AfterTwice => {
|
AddBlankLine::AfterTwice => buf.push(make::tokens::blank_line().into()),
|
||||||
buf.push(make::tokens::single_newline().into());
|
|
||||||
buf.push(make::tokens::single_newline().into());
|
|
||||||
}
|
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
buf
|
buf
|
||||||
};
|
};
|
||||||
|
|
||||||
algo::insert_children(&where_, insert_position, to_insert)
|
algo::insert_children(where_, insert_position, to_insert)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_merge_imports(
|
fn try_merge_imports(
|
||||||
|
@ -613,7 +596,7 @@ use foo::bar;",
|
||||||
.find_map(ast::Path::cast)
|
.find_map(ast::Path::cast)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let result = insert_use(file, path, mb).to_string();
|
let result = insert_use(&file, path, mb).to_string();
|
||||||
assert_eq_text!(&result, ra_fixture_after);
|
assert_eq_text!(&result, ra_fixture_after);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -339,7 +339,7 @@ pub mod tokens {
|
||||||
use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken};
|
use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken};
|
||||||
|
|
||||||
pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> =
|
pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> =
|
||||||
Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;"));
|
Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;\n\n"));
|
||||||
|
|
||||||
pub fn single_space() -> SyntaxToken {
|
pub fn single_space() -> SyntaxToken {
|
||||||
SOURCE_FILE
|
SOURCE_FILE
|
||||||
|
@ -379,6 +379,16 @@ pub mod tokens {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn blank_line() -> SyntaxToken {
|
||||||
|
SOURCE_FILE
|
||||||
|
.tree()
|
||||||
|
.syntax()
|
||||||
|
.descendants_with_tokens()
|
||||||
|
.filter_map(|it| it.into_token())
|
||||||
|
.find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n\n")
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
pub struct WsBuilder(SourceFile);
|
pub struct WsBuilder(SourceFile);
|
||||||
|
|
||||||
impl WsBuilder {
|
impl WsBuilder {
|
||||||
|
|
Loading…
Reference in a new issue