mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
polish join
This commit is contained in:
parent
6effddb18c
commit
69eeae0c99
4 changed files with 32 additions and 2 deletions
|
@ -7,6 +7,7 @@ publish = false
|
|||
[dependencies]
|
||||
itertools = "0.7.8"
|
||||
superslice = "0.1.0"
|
||||
join_to_string = "0.1.1"
|
||||
|
||||
libsyntax2 = { path = "../libsyntax2" }
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@ use std::{
|
|||
fmt::{self, Write},
|
||||
};
|
||||
|
||||
use join_to_string::join;
|
||||
|
||||
use libsyntax2::{
|
||||
File, TextUnit,
|
||||
ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner},
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
extern crate libsyntax2;
|
||||
extern crate superslice;
|
||||
extern crate itertools;
|
||||
extern crate join_to_string;
|
||||
#[cfg(test)]
|
||||
#[macro_use]
|
||||
extern crate test_utils as _test_utils;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::mem;
|
||||
|
||||
use libsyntax2::{
|
||||
TextUnit, TextRange, SyntaxNodeRef, File, AstNode,
|
||||
TextUnit, TextRange, SyntaxNodeRef, File, AstNode, SyntaxKind,
|
||||
ast,
|
||||
algo::{
|
||||
walk::preorder,
|
||||
|
@ -48,6 +48,7 @@ pub fn join_lines(file: &File, range: TextRange) -> ActionResult {
|
|||
remove_newline(&mut edit, node, text.as_str(), off);
|
||||
}
|
||||
}
|
||||
eprintln!("{:?}", edit);
|
||||
|
||||
ActionResult {
|
||||
edit: edit.finish(),
|
||||
|
@ -93,8 +94,10 @@ fn remove_newline(
|
|||
match (node.prev_sibling(), node.next_sibling()) {
|
||||
(Some(prev), Some(next)) => {
|
||||
let range = TextRange::from_to(prev.range().start(), node.range().end());
|
||||
if prev.kind() == COMMA && (next.kind() == R_PAREN || next.kind() == R_BRACK) {
|
||||
if is_trailing_comma(prev.kind(), next.kind()) {
|
||||
edit.delete(range);
|
||||
} else if no_space_required(prev.kind(), next.kind()) {
|
||||
edit.delete(node.range());
|
||||
} else if prev.kind() == COMMA && next.kind() == R_CURLY {
|
||||
edit.replace(range, " ".to_string());
|
||||
} else {
|
||||
|
@ -121,6 +124,20 @@ fn remove_newline(
|
|||
);
|
||||
}
|
||||
|
||||
fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
|
||||
match (left, right) {
|
||||
(COMMA, R_PAREN) | (COMMA, R_BRACK) => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
fn no_space_required(left: SyntaxKind, right: SyntaxKind) -> bool {
|
||||
match (left, right) {
|
||||
(_, DOT) => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
fn join_single_expr_block(
|
||||
edit: &mut EditBuilder,
|
||||
node: SyntaxNodeRef,
|
||||
|
@ -252,6 +269,15 @@ struct Foo <|>{
|
|||
", r"
|
||||
struct Foo { f: u32 }
|
||||
");
|
||||
do_check(r"
|
||||
fn foo() {
|
||||
join(<|>type_params.type_params()
|
||||
.filter_map(|it| it.name())
|
||||
.map(|it| it.text())<|>)
|
||||
}", r"
|
||||
fn foo() {
|
||||
join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text()))
|
||||
}")
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue