mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Merge #526
526: fix indent caclulation r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
8b985b427c
@ -2,15 +2,23 @@ use ra_syntax::{
|
|||||||
AstNode,
|
AstNode,
|
||||||
SyntaxNode, SyntaxKind::*,
|
SyntaxNode, SyntaxKind::*,
|
||||||
ast::{self, AstToken},
|
ast::{self, AstToken},
|
||||||
|
algo::generate,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// If the node is on the begining of the line, calculate indent.
|
/// If the node is on the begining of the line, calculate indent.
|
||||||
pub(crate) fn leading_indent(node: &SyntaxNode) -> Option<&str> {
|
pub(crate) fn leading_indent(node: &SyntaxNode) -> Option<&str> {
|
||||||
let prev = node.prev_sibling()?;
|
let prev = prev_leaf(node)?;
|
||||||
let ws_text = ast::Whitespace::cast(prev)?.text();
|
let ws_text = ast::Whitespace::cast(prev)?.text();
|
||||||
ws_text.rfind('\n').map(|pos| &ws_text[pos + 1..])
|
ws_text.rfind('\n').map(|pos| &ws_text[pos + 1..])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> {
|
||||||
|
generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| {
|
||||||
|
it.last_child()
|
||||||
|
})
|
||||||
|
.last()
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
|
pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
|
||||||
let expr = block.expr()?;
|
let expr = block.expr()?;
|
||||||
if expr.syntax().text().contains('\n') {
|
if expr.syntax().text().contains('\n') {
|
||||||
|
@ -227,6 +227,38 @@ fn foo() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn indents_new_chain_call_with_semi() {
|
||||||
|
type_dot(
|
||||||
|
r"
|
||||||
|
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
|
||||||
|
self.child_impl(db, name)
|
||||||
|
<|>;
|
||||||
|
}
|
||||||
|
",
|
||||||
|
r"
|
||||||
|
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
|
||||||
|
self.child_impl(db, name)
|
||||||
|
.;
|
||||||
|
}
|
||||||
|
",
|
||||||
|
);
|
||||||
|
type_dot(
|
||||||
|
r"
|
||||||
|
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
|
||||||
|
self.child_impl(db, name)
|
||||||
|
<|>;
|
||||||
|
}
|
||||||
|
",
|
||||||
|
r"
|
||||||
|
pub fn child(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> {
|
||||||
|
self.child_impl(db, name)
|
||||||
|
.;
|
||||||
|
}
|
||||||
|
",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn indents_continued_chain_call() {
|
fn indents_continued_chain_call() {
|
||||||
type_dot(
|
type_dot(
|
||||||
|
@ -12,18 +12,18 @@ pub const CURSOR_MARKER: &str = "<|>";
|
|||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! assert_eq_text {
|
macro_rules! assert_eq_text {
|
||||||
($expected:expr, $actual:expr) => {
|
($left:expr, $right:expr) => {
|
||||||
assert_eq_text!($expected, $actual,)
|
assert_eq_text!($left, $right,)
|
||||||
};
|
};
|
||||||
($expected:expr, $actual:expr, $($tt:tt)*) => {{
|
($left:expr, $right:expr, $($tt:tt)*) => {{
|
||||||
let expected = $expected;
|
let left = $left;
|
||||||
let actual = $actual;
|
let right = $right;
|
||||||
if expected != actual {
|
if left != right {
|
||||||
if expected.trim() == actual.trim() {
|
if left.trim() == right.trim() {
|
||||||
eprintln!("Expected:\n{:?}\n\nActual:\n{:?}\n\nWhitespace difference\n", expected, actual);
|
eprintln!("Left:\n{:?}\n\nRight:\n{:?}\n\nWhitespace difference\n", left, right);
|
||||||
} else {
|
} else {
|
||||||
let changeset = $crate::__Changeset::new(actual, expected, "\n");
|
let changeset = $crate::__Changeset::new(right, left, "\n");
|
||||||
eprintln!("Expected:\n{}\n\nActual:\n{}\n\nDiff:\n{}\n", expected, actual, changeset);
|
eprintln!("Left:\n{}\n\nRight:\n{}\n\nDiff:\n{}\n", left, right, changeset);
|
||||||
}
|
}
|
||||||
eprintln!($($tt)*);
|
eprintln!($($tt)*);
|
||||||
panic!("text differs");
|
panic!("text differs");
|
||||||
|
Loading…
Reference in New Issue
Block a user