Skip to content
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

New rules: "Add space inside parens", "Add space inside square brackets" #1192

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sergey-ilyin
Copy link

Need option to insert spaces inside parenthesis and square brackets.

"These spaces create very useful visual distinction between the function and their parameters, which helps a lot with avoiding the density that results from having no spaces there, visually merging the function name and parameters, increasing cognitive load." (quote from issue #1023)

Example of formatted code:

public let addSpaceInsideParens = FormatRule(
        help: "Add space inside parentheses."
    )
    { formatter in
        formatter.forEach( .startOfScope( "(" ) )
        { i, _ in
            if let nextToken = formatter.token( at: i + 1 )
            {
                if !nextToken.isSpaceOrLinebreak,
                   ![ .endOfScope( ")" ) ].contains( nextToken )
                {
                    formatter.insert( .space( " " ), at: i + 1 )
                }
            }
        }
        formatter.forEach( .endOfScope( ")" ) )
        { i, _ in
            if let prevToken = formatter.token( at: i - 1 )
            {
                if !prevToken.isSpaceOrLinebreak,
                   ![ .startOfScope( "(" ) ].contains( prevToken )
                {
                    formatter.insert( .space( " " ), at: i )
                }
            }
        }
    }

@nicklockwood
Copy link
Owner

Hi @sergey-ilyin, sorry for the delayed response. The idea behind these rules seems fine, but they conflict with the existing rules that remove those spaces, which isn't ideal. If someone were to accidentally enable both rules then the result would be random.

A better solution would be to make these into configuration options for the existing spaceInsideBrackets/Parens rules.

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.

2 participants