Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Configurable opening brace rule for multi-line if conditions #355

Closed
watt opened this issue Jan 13, 2016 · 3 comments
Closed

Configurable opening brace rule for multi-line if conditions #355

watt opened this issue Jan 13, 2016 · 3 comments
Labels
enhancement Ideas for improvements of existing features and rules.

Comments

@watt
Copy link

watt commented Jan 13, 2016

When writing an if that contains multiple let bindings, a where clause, etc. I often split the expressions into multiple lines indented one level below the if for readability. When doing this, I move the opening brace to a new line (matching the indentation level of the if) to help distinguish the statements within the if from the conditions, since they are at the same indentation level.

For example, instead of something like this:

if
    let blah = blah,
    let foo = blah.foo
    where foo.something {
    // do something
}

I find the following more readable:

if
    let blah = blah,
    let foo = blah.foo
    where foo.something
{
    // do something
}

I would like the opening brace rule to be configurable to support opening braces in this style, under these conditions.

@jpsim jpsim added the enhancement Ideas for improvements of existing features and rules. label Jan 13, 2016
@jpsim
Copy link
Collaborator

jpsim commented Jan 13, 2016

I think that'd be acceptable. Is this something you intend to work on yourself?

@watt
Copy link
Author

watt commented Jan 13, 2016

Yeah, I'll probably give it a shot in my spare time, but if it happened to get done by someone else I certainly wouldn't be upset.

@scottrhoyt
Copy link
Contributor

I too prefer this style for multi-line if, guard, and while, so I have been thinking about a way to accomplish it. Since the current implementation uses regex to find violations, I came up with what I think is a regex pattern to detect these cases: ((?:if|guard|while)\n(?:.+\n)+{). I'm no regex expert, so additional verification would be appreciated.

I was originally hoping to modify the existing regex to exclude these matches, but I have found that to be difficult--again, no regex expert. I thought a negative lookbehind would be the best option, but because the number of conditions is unbounded, this didn't pan out. My second approach is to find the ranges matching the existing regex, then find the ranges matching my regex, and then filter out the overlap. I have some performance concerns about this approach though.

Let me know what you guys think.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement Ideas for improvements of existing features and rules.
Projects
None yet
Development

No branches or pull requests

3 participants