From 2579dc6d82dff5d2b86f490121fe8b087216b74e Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 5 Jul 2021 14:24:25 +0200 Subject: [PATCH] Update `inline_call` assist doc example --- .../ide_assists/src/handlers/inline_call.rs | 21 +++++++------------ crates/ide_assists/src/tests/generated.rs | 21 +++++++------------ crates/syntax/src/ted.rs | 14 +++++++------ crates/test_utils/src/minicore.rs | 9 ++++++++ 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/crates/ide_assists/src/handlers/inline_call.rs b/crates/ide_assists/src/handlers/inline_call.rs index 5b6992b6409..2059a7472d7 100644 --- a/crates/ide_assists/src/handlers/inline_call.rs +++ b/crates/ide_assists/src/handlers/inline_call.rs @@ -17,23 +17,18 @@ use crate::{ // Inlines a function or method body. // // ``` -// fn align(a: u32, b: u32) -> u32 { -// (a + b - 1) & !(b - 1) -// } -// fn main() { -// let x = align$0(1, 2); +// # //- minicore: option +// fn foo(name: Option<&str>) { +// let name = name.unwrap$0(); // } // ``` // -> // ``` -// fn align(a: u32, b: u32) -> u32 { -// (a + b - 1) & !(b - 1) -// } -// fn main() { -// let x = { -// let b = 2; -// (1 + b - 1) & !(b - 1) -// }; +// fn foo(name: Option<&str>) { +// let name = match name { +// Some(val) => val, +// None => panic!("called `Option::unwrap()` on a `None` value"), +// }; // } // ``` pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index c24c3803450..e35e68b40dc 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs @@ -923,22 +923,17 @@ fn doctest_inline_call() { check_doc_test( "inline_call", r#####" -fn align(a: u32, b: u32) -> u32 { - (a + b - 1) & !(b - 1) -} -fn main() { - let x = align$0(1, 2); +//- minicore: option +fn foo(name: Option<&str>) { + let name = name.unwrap$0(); } "#####, r#####" -fn align(a: u32, b: u32) -> u32 { - (a + b - 1) & !(b - 1) -} -fn main() { - let x = { - let b = 2; - (1 + b - 1) & !(b - 1) - }; +fn foo(name: Option<&str>) { + let name = match name { + Some(val) => val, + None => panic!("called `Option::unwrap()` on a `None` value"), + }; } "#####, ) diff --git a/crates/syntax/src/ted.rs b/crates/syntax/src/ted.rs index 3c05b488157..2396408270e 100644 --- a/crates/syntax/src/ted.rs +++ b/crates/syntax/src/ted.rs @@ -161,6 +161,14 @@ fn ws_before(position: &Position, new: &SyntaxElement) -> Option { } } + if prev.kind() == T!['{'] && ast::Stmt::can_cast(new.kind()) { + if let Some(block_expr) = prev.parent().and_then(ast::BlockExpr::cast) { + let mut indent = IndentLevel::from_element(&block_expr.syntax().clone().into()); + indent.0 += 1; + return Some(make::tokens::whitespace(&format!("\n{}", indent))); + } + } + ws_between(prev, new) } fn ws_after(position: &Position, new: &SyntaxElement) -> Option { @@ -187,12 +195,6 @@ fn ws_between(left: &SyntaxElement, right: &SyntaxElement) -> Option Option { + pub const fn unwrap(self) -> T { + match self { + Some(val) => val, + None => panic!("called `Option::unwrap()` on a `None` value"), + } + } + } } // endregion:option