diff options
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r-- | crates/ra_assists/src/handlers/change_visibility.rs | 13 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/unwrap_block.rs | 67 |
2 files changed, 44 insertions, 36 deletions
diff --git a/crates/ra_assists/src/handlers/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs index 44f6a1dae..1cd532e80 100644 --- a/crates/ra_assists/src/handlers/change_visibility.rs +++ b/crates/ra_assists/src/handlers/change_visibility.rs | |||
@@ -47,8 +47,7 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> { | |||
47 | return None; | 47 | return None; |
48 | } | 48 | } |
49 | (vis_offset(&parent), keyword.text_range()) | 49 | (vis_offset(&parent), keyword.text_range()) |
50 | } else { | 50 | } else if let Some(field_name) = ctx.find_node_at_offset::<ast::Name>() { |
51 | let field_name: ast::Name = ctx.find_node_at_offset()?; | ||
52 | let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?; | 51 | let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?; |
53 | if field.name()? != field_name { | 52 | if field.name()? != field_name { |
54 | tested_by!(change_visibility_field_false_positive); | 53 | tested_by!(change_visibility_field_false_positive); |
@@ -58,6 +57,13 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> { | |||
58 | return None; | 57 | return None; |
59 | } | 58 | } |
60 | (vis_offset(field.syntax()), field_name.syntax().text_range()) | 59 | (vis_offset(field.syntax()), field_name.syntax().text_range()) |
60 | } else if let Some(field) = ctx.find_node_at_offset::<ast::TupleFieldDef>() { | ||
61 | if field.visibility().is_some() { | ||
62 | return None; | ||
63 | } | ||
64 | (vis_offset(field.syntax()), field.syntax().text_range()) | ||
65 | } else { | ||
66 | return None; | ||
61 | }; | 67 | }; |
62 | 68 | ||
63 | ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub(crate)", |edit| { | 69 | ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub(crate)", |edit| { |
@@ -129,7 +135,8 @@ mod tests { | |||
129 | change_visibility, | 135 | change_visibility, |
130 | r"struct S { <|>field: u32 }", | 136 | r"struct S { <|>field: u32 }", |
131 | r"struct S { <|>pub(crate) field: u32 }", | 137 | r"struct S { <|>pub(crate) field: u32 }", |
132 | ) | 138 | ); |
139 | check_assist(change_visibility, r"struct S ( <|>u32 )", r"struct S ( <|>pub(crate) u32 )"); | ||
133 | } | 140 | } |
134 | 141 | ||
135 | #[test] | 142 | #[test] |
diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index 58649c47e..859c70ad8 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs | |||
@@ -1,8 +1,8 @@ | |||
1 | use crate::{Assist, AssistCtx, AssistId}; | 1 | use crate::{Assist, AssistCtx, AssistId}; |
2 | 2 | ||
3 | use ast::{BlockExpr, Expr, ForExpr, IfExpr, LoopBodyOwner, LoopExpr, WhileExpr}; | 3 | use ast::LoopBodyOwner; |
4 | use ra_fmt::unwrap_trivial_block; | 4 | use ra_fmt::unwrap_trivial_block; |
5 | use ra_syntax::{ast, AstNode, TextRange, T}; | 5 | use ra_syntax::{ast, match_ast, AstNode, TextRange, T}; |
6 | 6 | ||
7 | // Assist: unwrap_block | 7 | // Assist: unwrap_block |
8 | // | 8 | // |
@@ -23,39 +23,40 @@ use ra_syntax::{ast, AstNode, TextRange, T}; | |||
23 | // ``` | 23 | // ``` |
24 | pub(crate) fn unwrap_block(ctx: AssistCtx) -> Option<Assist> { | 24 | pub(crate) fn unwrap_block(ctx: AssistCtx) -> Option<Assist> { |
25 | let l_curly_token = ctx.find_token_at_offset(T!['{'])?; | 25 | let l_curly_token = ctx.find_token_at_offset(T!['{'])?; |
26 | 26 | let block = ast::BlockExpr::cast(l_curly_token.parent())?; | |
27 | let res = if let Some(if_expr) = l_curly_token.ancestors().find_map(IfExpr::cast) { | 27 | let parent = block.syntax().parent()?; |
28 | // if expression | 28 | let (expr, expr_to_unwrap) = match_ast! { |
29 | let expr_to_unwrap = if_expr.blocks().find_map(|expr| extract_expr(ctx.frange.range, expr)); | 29 | match parent { |
30 | let expr_to_unwrap = expr_to_unwrap?; | 30 | ast::IfExpr(if_expr) => { |
31 | // Find if we are in a else if block | 31 | let expr_to_unwrap = if_expr.blocks().find_map(|expr| extract_expr(ctx.frange.range, expr)); |
32 | let ancestor = if_expr.syntax().ancestors().skip(1).find_map(ast::IfExpr::cast); | 32 | let expr_to_unwrap = expr_to_unwrap?; |
33 | 33 | // Find if we are in a else if block | |
34 | if let Some(ancestor) = ancestor { | 34 | let ancestor = if_expr.syntax().parent().and_then(ast::IfExpr::cast); |
35 | Some((ast::Expr::IfExpr(ancestor), expr_to_unwrap)) | 35 | |
36 | } else { | 36 | match ancestor { |
37 | Some((ast::Expr::IfExpr(if_expr), expr_to_unwrap)) | 37 | None => (ast::Expr::IfExpr(if_expr), expr_to_unwrap), |
38 | Some(ancestor) => (ast::Expr::IfExpr(ancestor), expr_to_unwrap), | ||
39 | } | ||
40 | }, | ||
41 | ast::ForExpr(for_expr) => { | ||
42 | let block_expr = for_expr.loop_body()?; | ||
43 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; | ||
44 | (ast::Expr::ForExpr(for_expr), expr_to_unwrap) | ||
45 | }, | ||
46 | ast::WhileExpr(while_expr) => { | ||
47 | let block_expr = while_expr.loop_body()?; | ||
48 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; | ||
49 | (ast::Expr::WhileExpr(while_expr), expr_to_unwrap) | ||
50 | }, | ||
51 | ast::LoopExpr(loop_expr) => { | ||
52 | let block_expr = loop_expr.loop_body()?; | ||
53 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; | ||
54 | (ast::Expr::LoopExpr(loop_expr), expr_to_unwrap) | ||
55 | }, | ||
56 | _ => return None, | ||
38 | } | 57 | } |
39 | } else if let Some(for_expr) = l_curly_token.ancestors().find_map(ForExpr::cast) { | ||
40 | // for expression | ||
41 | let block_expr = for_expr.loop_body()?; | ||
42 | extract_expr(ctx.frange.range, block_expr) | ||
43 | .map(|expr_to_unwrap| (ast::Expr::ForExpr(for_expr), expr_to_unwrap)) | ||
44 | } else if let Some(while_expr) = l_curly_token.ancestors().find_map(WhileExpr::cast) { | ||
45 | // while expression | ||
46 | let block_expr = while_expr.loop_body()?; | ||
47 | extract_expr(ctx.frange.range, block_expr) | ||
48 | .map(|expr_to_unwrap| (ast::Expr::WhileExpr(while_expr), expr_to_unwrap)) | ||
49 | } else if let Some(loop_expr) = l_curly_token.ancestors().find_map(LoopExpr::cast) { | ||
50 | // loop expression | ||
51 | let block_expr = loop_expr.loop_body()?; | ||
52 | extract_expr(ctx.frange.range, block_expr) | ||
53 | .map(|expr_to_unwrap| (ast::Expr::LoopExpr(loop_expr), expr_to_unwrap)) | ||
54 | } else { | ||
55 | None | ||
56 | }; | 58 | }; |
57 | 59 | ||
58 | let (expr, expr_to_unwrap) = res?; | ||
59 | ctx.add_assist(AssistId("unwrap_block"), "Unwrap block", |edit| { | 60 | ctx.add_assist(AssistId("unwrap_block"), "Unwrap block", |edit| { |
60 | edit.set_cursor(expr.syntax().text_range().start()); | 61 | edit.set_cursor(expr.syntax().text_range().start()); |
61 | edit.target(expr_to_unwrap.syntax().text_range()); | 62 | edit.target(expr_to_unwrap.syntax().text_range()); |
@@ -76,7 +77,7 @@ pub(crate) fn unwrap_block(ctx: AssistCtx) -> Option<Assist> { | |||
76 | }) | 77 | }) |
77 | } | 78 | } |
78 | 79 | ||
79 | fn extract_expr(cursor_range: TextRange, block: BlockExpr) -> Option<Expr> { | 80 | fn extract_expr(cursor_range: TextRange, block: ast::BlockExpr) -> Option<ast::Expr> { |
80 | let cursor_in_range = block.l_curly_token()?.text_range().contains_range(cursor_range); | 81 | let cursor_in_range = block.l_curly_token()?.text_range().contains_range(cursor_range); |
81 | 82 | ||
82 | if cursor_in_range { | 83 | if cursor_in_range { |