Skip to content

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

Conversation

killercup
Copy link
Member

Similar to "Introduce variable". This creates a match for the expression
under the cursor.

This is the simplest possible implementation for now, with two FIXMEs and
a lot of ideas that are just waiting to be implemented.

cf. #528

// 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));
Copy link
Member

Choose a reason for hiding this comment

The 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?

@killercup
Copy link
Member Author

I added the expression filter, but immediately noticed another edge case: Triggering it like check<|>_assist(introduce_match, "something", "other") will produce a match check_assist { ... }(introduce_match, "something", "other"). Probably not what we want.

@matklad
Copy link
Member

matklad commented Jan 13, 2019

Yeah, although match f { f => f} (args) is valid rust...

Another drawback is that this (just as introduce variable) will basically be always available. Perhaps we need to activate it only if the rage of expresison exactly matches selection?

That is, one would use extend selection to select expression, and then introduce variable/match?

@killercup
Copy link
Member Author

Hmm, only enabling this when there is a valid selection doesn't feel very convenient to me; in the best case it's one more shortcut to press (and know to press).

How likely are we able to make a good guess at what the user meant? E.g., checking the node we are at and deciding that if it's a function call to always match on the full call seems like a good idea to me.

@flodiebold
Copy link
Member

How about using the whole expression statement / last expression of the block if nothing is selected?

@matklad
Copy link
Member

matklad commented Jan 13, 2019

How about using the whole expression statement / last expression of the block if nothing is selected?

SGTM!

@killercup
Copy link
Member Author

How about using the whole expression statement / last expression of the block if nothing is selected?

Could work! I'll try to find some time tomorrow to change the code.

@matklad
Copy link
Member

matklad commented Jan 19, 2019

Note that I've had to rewrite the history recently, so expect storms while rebasing: #566 (comment)

Sorry about that: it's definitively my mistake 🙁

Similar to "Introduce variable". This creates a match for the expression
under the cursor.

This is the simples possible implementation for now, with two FIXMEs and
a lot of ideas that are just waiting to be implemented.

cf. rust-lang#528
@killercup killercup force-pushed the introduce-introduce-match branch from e53aeff to 015346c Compare January 19, 2019 13:56
@killercup
Copy link
Member Author

No worries, just rebased this real quick.

@@ -397,7 +397,7 @@ impl Analysis {
self.with_db(|db| db.find_all_refs(position))
}

/// Returns a short text descrbing element at position.
Copy link
Contributor

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:

{
    "spellright.language": ["en-GB"],
    "spellright.groupDictionaries": false,
    "spellright.documentTypes": [
        "markdown",
        "plaintext",
        "rust",
    ],
    "spellright.parserByClass": {
        "rust": {
            "parser": "code"
        }
    },
    "spellright.notificationClass": "information",
    "spellright.configurationScope": "user",
    "spellright.statusBarIndicator": false,

    "cSpell.checkLimit": 2048,
    "cSpell.language": "en-GB",
    "cSpell.showStatus": false,
    "cSpell.enabledLanguageIds": [
        "markdown",
        "plaintext",
        "rust"
    ],
    "cSpell.userWords": [
        "Deserialize",
        "Tuple",
        "alloc",
        "clippy",
        "rustc",
        "rustfmt",
        "struct",
    ],
    "cSpell.languageSettings": [
        {
            "languageId": "rust",
            "ignoreRegExpList": [
                "/\/\/\/? .*/"
            ]
        }
    ]
}

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:

clippy
fmt
impl
iter
mut
println
rustc
rustfmt
usize
utf8
vec

cc @matklad

Copy link
Member

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.

Copy link
Contributor

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).

@matklad
Copy link
Member

matklad commented Jan 25, 2019

note that we now have a postfix .match: https://github.com/rust-analyzer/rust-analyzer/blob/bce0c6267aab2e8ca33a3e78a1081736abbc1373/crates/ra_ide_api/src/completion/complete_postfix.rs#L38-L42

We need to make sure that the two impls share the code :)

@killercup
Copy link
Member Author

Closing because I won't have time to work on this right now. Will make a new PR later hopefully

@killercup killercup closed this Feb 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants