Skip to content

Commit

Permalink
Merge #2877
Browse files Browse the repository at this point in the history
2877: "Insert explicit type " assist fix #2869, fix typo r=matklad a=TomasKralCZ

So this was quite straightforward. I basically looked at how the other assists work and tried doing something simillar. I also fixed a typo in the other assist.

Co-authored-by: TomasKralCZ <tomas@kral.hk>
  • Loading branch information
bors[bot] and TomasKralCZ authored Jan 20, 2020
2 parents 648241e + 72792f6 commit de24097
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 27 deletions.
26 changes: 25 additions & 1 deletion crates/ra_assists/src/assists/add_explicit_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use hir::{db::HirDatabase, HirDisplay};
use ra_syntax::{
ast::{self, AstNode, LetStmt, NameOwner},
T,
TextRange, T,
};

use crate::{Assist, AssistCtx, AssistId};
Expand Down Expand Up @@ -34,6 +34,14 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi
// The binding must have a name
let name = pat.name()?;
let name_range = name.syntax().text_range();
// Assist should only be applicable if cursor is between 'let' and '='
let stmt_range = stmt.syntax().text_range();
let eq_range = stmt.eq_token()?.text_range();
let let_range = TextRange::from_to(stmt_range.start(), eq_range.start());
let cursor_in_range = ctx.frange.range.is_subrange(&let_range);
if !cursor_in_range {
return None;
}
// Assist not applicable if the type has already been specified
if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) {
return None;
Expand Down Expand Up @@ -109,4 +117,20 @@ mod tests {
fn add_explicit_type_not_applicable_if_specified_ty_is_tuple() {
check_assist_not_applicable(add_explicit_type, "fn f() { let a<|>: (i32, i32) = (3, 4); }");
}

#[test]
fn add_explicit_type_not_applicable_if_cursor_after_equals() {
check_assist_not_applicable(
add_explicit_type,
"fn f() {let a =<|> match 1 {2 => 3, 3 => 5};}",
)
}

#[test]
fn add_explicit_type_not_applicable_if_cursor_before_let() {
check_assist_not_applicable(
add_explicit_type,
"fn f() <|>{let a = match 1 {2 => 3, 3 => 5};}",
)
}
}
50 changes: 25 additions & 25 deletions crates/ra_assists/src/assists/inline_local_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{Assist, AssistCtx, AssistId};
// (1 + 2) * 4;
// }
// ```
pub(crate) fn inline_local_varialbe(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
pub(crate) fn inline_local_variable(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let let_stmt = ctx.find_node_at_offset::<ast::LetStmt>()?;
let bind_pat = match let_stmt.pat()? {
ast::Pat::BindPat(pat) => pat,
Expand Down Expand Up @@ -117,7 +117,7 @@ mod tests {
#[test]
fn test_inline_let_bind_literal_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn bar(a: usize) {}
fn foo() {
Expand Down Expand Up @@ -151,7 +151,7 @@ fn foo() {
#[test]
fn test_inline_let_bind_bin_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn bar(a: usize) {}
fn foo() {
Expand Down Expand Up @@ -185,7 +185,7 @@ fn foo() {
#[test]
fn test_inline_let_bind_function_call_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn bar(a: usize) {}
fn foo() {
Expand Down Expand Up @@ -219,7 +219,7 @@ fn foo() {
#[test]
fn test_inline_let_bind_cast_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn bar(a: usize): usize { a }
fn foo() {
Expand Down Expand Up @@ -253,7 +253,7 @@ fn foo() {
#[test]
fn test_inline_let_bind_block_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = { 10 + 1 };
Expand Down Expand Up @@ -285,7 +285,7 @@ fn foo() {
#[test]
fn test_inline_let_bind_paren_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = ( 10 + 1 );
Expand Down Expand Up @@ -317,7 +317,7 @@ fn foo() {
#[test]
fn test_not_inline_mut_variable() {
check_assist_not_applicable(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let mut a<|> = 1 + 1;
Expand All @@ -329,7 +329,7 @@ fn foo() {
#[test]
fn test_call_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = bar(10 + 1);
Expand All @@ -347,7 +347,7 @@ fn foo() {
#[test]
fn test_index_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let x = vec![1, 2, 3];
Expand All @@ -367,7 +367,7 @@ fn foo() {
#[test]
fn test_method_call_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let bar = vec![1];
Expand All @@ -387,7 +387,7 @@ fn foo() {
#[test]
fn test_field_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
struct Bar {
foo: usize
Expand Down Expand Up @@ -415,7 +415,7 @@ fn foo() {
#[test]
fn test_try_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() -> Option<usize> {
let bar = Some(1);
Expand All @@ -437,7 +437,7 @@ fn foo() -> Option<usize> {
#[test]
fn test_ref_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let bar = 10;
Expand All @@ -455,7 +455,7 @@ fn foo() {
#[test]
fn test_tuple_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = (10, 20);
Expand All @@ -471,7 +471,7 @@ fn foo() {
#[test]
fn test_array_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = [1, 2, 3];
Expand All @@ -487,7 +487,7 @@ fn foo() {
#[test]
fn test_paren() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = (10 + 20);
Expand All @@ -505,7 +505,7 @@ fn foo() {
#[test]
fn test_path_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let d = 10;
Expand All @@ -525,7 +525,7 @@ fn foo() {
#[test]
fn test_block_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = { 10 };
Expand All @@ -543,7 +543,7 @@ fn foo() {
#[test]
fn test_used_in_different_expr1() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = 10 + 20;
Expand All @@ -565,7 +565,7 @@ fn foo() {
#[test]
fn test_used_in_for_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = vec![10, 20];
Expand All @@ -581,7 +581,7 @@ fn foo() {
#[test]
fn test_used_in_while_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = 1 > 0;
Expand All @@ -597,7 +597,7 @@ fn foo() {
#[test]
fn test_used_in_break_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = 1 + 1;
Expand All @@ -617,7 +617,7 @@ fn foo() {
#[test]
fn test_used_in_return_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = 1 > 0;
Expand All @@ -633,7 +633,7 @@ fn foo() {
#[test]
fn test_used_in_match_expr() {
check_assist(
inline_local_varialbe,
inline_local_variable,
"
fn foo() {
let a<|> = 1 > 0;
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_assists/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ mod assists {
add_import::add_import,
add_missing_impl_members::add_missing_impl_members,
add_missing_impl_members::add_missing_default_members,
inline_local_variable::inline_local_varialbe,
inline_local_variable::inline_local_variable,
move_guard::move_guard_to_arm_body,
move_guard::move_arm_cond_to_match_guard,
move_bounds::move_bounds_to_where_clause,
Expand Down
4 changes: 4 additions & 0 deletions crates/ra_syntax/src/ast/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ impl ast::LetStmt {
Some(node) => node.kind() == T![;],
}
}

pub fn eq_token(&self) -> Option<SyntaxToken> {
self.syntax().children_with_tokens().find(|t| t.kind() == EQ).and_then(|it| it.into_token())
}
}

impl ast::ExprStmt {
Expand Down

0 comments on commit de24097

Please sign in to comment.