mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 04:53:34 +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]
|
[dependencies]
|
||||||
itertools = "0.7.8"
|
itertools = "0.7.8"
|
||||||
superslice = "0.1.0"
|
superslice = "0.1.0"
|
||||||
|
join_to_string = "0.1.1"
|
||||||
|
|
||||||
libsyntax2 = { path = "../libsyntax2" }
|
libsyntax2 = { path = "../libsyntax2" }
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ use std::{
|
||||||
fmt::{self, Write},
|
fmt::{self, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use join_to_string::join;
|
||||||
|
|
||||||
use libsyntax2::{
|
use libsyntax2::{
|
||||||
File, TextUnit,
|
File, TextUnit,
|
||||||
ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner},
|
ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner},
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
extern crate libsyntax2;
|
extern crate libsyntax2;
|
||||||
extern crate superslice;
|
extern crate superslice;
|
||||||
extern crate itertools;
|
extern crate itertools;
|
||||||
|
extern crate join_to_string;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate test_utils as _test_utils;
|
extern crate test_utils as _test_utils;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
use libsyntax2::{
|
use libsyntax2::{
|
||||||
TextUnit, TextRange, SyntaxNodeRef, File, AstNode,
|
TextUnit, TextRange, SyntaxNodeRef, File, AstNode, SyntaxKind,
|
||||||
ast,
|
ast,
|
||||||
algo::{
|
algo::{
|
||||||
walk::preorder,
|
walk::preorder,
|
||||||
|
@ -48,6 +48,7 @@ pub fn join_lines(file: &File, range: TextRange) -> ActionResult {
|
||||||
remove_newline(&mut edit, node, text.as_str(), off);
|
remove_newline(&mut edit, node, text.as_str(), off);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
eprintln!("{:?}", edit);
|
||||||
|
|
||||||
ActionResult {
|
ActionResult {
|
||||||
edit: edit.finish(),
|
edit: edit.finish(),
|
||||||
|
@ -93,8 +94,10 @@ fn remove_newline(
|
||||||
match (node.prev_sibling(), node.next_sibling()) {
|
match (node.prev_sibling(), node.next_sibling()) {
|
||||||
(Some(prev), Some(next)) => {
|
(Some(prev), Some(next)) => {
|
||||||
let range = TextRange::from_to(prev.range().start(), node.range().end());
|
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);
|
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 {
|
} else if prev.kind() == COMMA && next.kind() == R_CURLY {
|
||||||
edit.replace(range, " ".to_string());
|
edit.replace(range, " ".to_string());
|
||||||
} else {
|
} 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(
|
fn join_single_expr_block(
|
||||||
edit: &mut EditBuilder,
|
edit: &mut EditBuilder,
|
||||||
node: SyntaxNodeRef,
|
node: SyntaxNodeRef,
|
||||||
|
@ -252,6 +269,15 @@ struct Foo <|>{
|
||||||
", r"
|
", r"
|
||||||
struct Foo { f: u32 }
|
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]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue