Skip to content

Commit

Permalink
Two new rules: "Add space inside parens" and "Add space inside squar…
Browse files Browse the repository at this point in the history
…e brackets".
  • Loading branch information
sergey-ilyin committed Jun 8, 2022
1 parent f8e35f6 commit 9b9a4f3
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,35 @@ public struct _FormatRules {
}
}

/// Add space inside parens
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 )
}
}
}
}

/// Remove space immediately inside parens
public let spaceInsideParens = FormatRule(
help: "Remove space inside parentheses."
Expand Down Expand Up @@ -332,6 +361,35 @@ public struct _FormatRules {
}
}

/// Add space inside square brackets
public let addSpaceInsideBrackets = FormatRule(
help: "Add space inside square brackets."
)
{ 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 )
}
}
}
}

/// Remove space immediately inside square brackets
public let spaceInsideBrackets = FormatRule(
help: "Remove space inside square brackets."
Expand Down

0 comments on commit 9b9a4f3

Please sign in to comment.