-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Introduce "introduce match" #531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use ra_syntax::{ast::{self, AstNode}, TextUnit}; | ||
|
||
use crate::assists::{AssistCtx, Assist}; | ||
|
||
pub fn introduce_match<'a>(ctx: AssistCtx) -> Option<Assist> { | ||
let node = ctx.covering_node(); | ||
let _expr = node.ancestors().filter_map(ast::Expr::cast).next()?; | ||
|
||
ctx.build("introduce match", move |edit| { | ||
let first_part = format!("match {} {{\n ", node.text()); | ||
let match_expr = format!("{}_ => (),\n}}", first_part); | ||
edit.replace_node_and_indent(node, match_expr.into_boxed_str()); | ||
|
||
// FIXME: Set cursor at beginning of first match arm. | ||
// The following doesn't work because of re-indentation: | ||
// | ||
// edit.set_cursor(expr.range().start() + TextUnit::of_str(&first_part)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah... I think replace_and_indent could return an offset of the start of the substituted node? |
||
// | ||
// Workaround: | ||
edit.set_cursor(node.range().start() + TextUnit::of_str("match ")); | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::assists::{check_assist, check_assist_range}; | ||
|
||
#[test] | ||
fn test_introduce_match_simple() { | ||
check_assist( | ||
introduce_match, | ||
" | ||
fn foo() { | ||
let x = Some(42); | ||
x<|> | ||
}", | ||
" | ||
fn foo() { | ||
let x = Some(42); | ||
match <|>x { | ||
_ => (), | ||
} | ||
}", | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_introduce_match_expr_stmt() { | ||
check_assist_range( | ||
introduce_match, | ||
" | ||
fn foo() { | ||
<|>Some(42).map(|x| x + 1)<|> | ||
}", | ||
" | ||
fn foo() { | ||
match <|>Some(42).map(|x| x + 1) { | ||
_ => (), | ||
} | ||
}", | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_introduce_match_part_of_expr_stmt() { | ||
check_assist_range( | ||
introduce_match, | ||
" | ||
fn foo() { | ||
<|>Some(21)<|>.map(|x| x + 21); | ||
}", | ||
" | ||
fn foo() { | ||
match <|>Some(21) { | ||
_ => (), | ||
}.map(|x| x + 21); | ||
}", | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_introduce_match_last_expr() { | ||
check_assist_range( | ||
introduce_match, | ||
" | ||
fn foo() { | ||
bar(<|>Ok(42)<|>) | ||
}", | ||
// FIXME: This is the wrong indentation | ||
" | ||
fn foo() { | ||
bar(match <|>Ok(42) { | ||
_ => (), | ||
}) | ||
}", | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_introduce_match_last_full_expr() { | ||
check_assist_range( | ||
introduce_match, | ||
" | ||
fn foo() { | ||
<|>bar(1 + 1)<|> | ||
}", | ||
" | ||
fn foo() { | ||
match <|>bar(1 + 1) { | ||
_ => (), | ||
} | ||
}", | ||
); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a bit off-topic, but perhaps it helps anyone reading this
I too sometimes struggle with grammar :) A small tip from one non-native English speaker who likes to be correct to any other: there's two plugins that I use to help me avoid spelling issues in my code:
They complement each other, as one is used in "textual" contexts, such as README's, or code comments, while the other is used to avoid common spelling mistakes in my actual Rust code, and works with CamelCase situations.
Here's my config for both of them, in case it helps anyone:
It's easy to add words to the
cSpell.userWords
array as you work, using the contextual popup in the editor. I trimmed the list down to a few useful ones for Rust development. The same goes for the spellright spelling updates, but those are stored in$VSCODE/User/spellright.dict
:cc @matklad
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I do have a spelling problem in any language :-)
I've tried using a cspell check extension, but unfortunately it gives a fair amount of false positives, and in general is pretty distractive:
F8
brings you not to the next compiler error, but to the one of the dozen spelling mistakes.I'd love to have a spellchecker which only checks comments and have close to zeto false positiv rate, but I haven't invstigated this yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could start by using spellright, and configure it to only show you spelling mistakes in your code comments.
I agree that it's annoying that cspell gives plenty of false positives, but when I'm working in a project, I fairly quickly add all of the project-specific variable names to the list of ignored spellings, after which things quiet down.
Anyway, I agree, it's not perfect, but it helped me, and is "good enough" for now (especially if you skip cspell and only focus on comments/documentation in Rust).