Skip to content

Commit

Permalink
add enum rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Jintin committed Oct 9, 2018
1 parent a679b82 commit f0b1f4f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Parser/Indent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Indent {
var isLeading: Bool
var leading: Int // leading for smart align
var inSwitch: Bool // is in switch block
var inEnum: Bool // is in enum block
var inCase: Bool // is case statement
var block: IndentType

Expand All @@ -46,6 +47,7 @@ class Indent {
isLeading = false
leading = 0
inSwitch = false
inEnum = false
inCase = false
block = .curly
}
Expand All @@ -58,6 +60,7 @@ class Indent {
self.isLeading = indent.isLeading
self.leading = indent.leading
self.inSwitch = false
self.inEnum = false
self.inCase = false
self.indentAdd = false
self.extraAdd = false
Expand Down
2 changes: 2 additions & 0 deletions Parser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ extension SwiftParser {
}
if isNext(word: "switch") {
isNextSwitch = true
} else if isNext(word: "enum") {
isNextEnum = true
}
let count = indent.count + (addExtra ? indent.extra : 0)
if count > 0 {
Expand Down
11 changes: 11 additions & 0 deletions Parser/SwiftParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SwiftParser {
var indentStack = [Indent]()
var newlineIndex: Int = 0
var isNextSwitch: Bool = false
var isNextEnum: Bool = false
var autoRemoveChar: Bool = false

init(string: String, preferences: Preferences? = nil) {
Expand Down Expand Up @@ -246,6 +247,10 @@ class SwiftParser {
indent.inSwitch = true
isNextSwitch = false
}
if isNextEnum {
indent.inEnum = true
isNextEnum = false
}
indent.count -= 1
trimWithIndent()
indent.count += 1
Expand Down Expand Up @@ -275,6 +280,7 @@ class SwiftParser {
} else {
indent = Indent()
}

if char == "}" {
if isNext(char: ".", skipBlank: true) {
trimWithIndent()
Expand Down Expand Up @@ -331,6 +337,8 @@ class SwiftParser {
if !isNext(string: "//") {
if isBetween(words: ("if", "let"), ("guard", "let")) {
indent.extra = 1
} else if isPrevious(str: "case") {
indent.extra = 1
} else if isNext(word: "else") {
if retString.lastWord() != "}" {
indent.extra = 1
Expand Down Expand Up @@ -366,6 +374,9 @@ class SwiftParser {
return 1
}
case ",":
if self.indent.inEnum {
return 0
}
if self.indent.line == 1 && (self.indent.block == .parentheses || self.indent.block == .square) {
self.indent.isLeading = true
}
Expand Down
7 changes: 7 additions & 0 deletions SwimatTests/tests/n-enum-indentation/after.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// #194
enum Foo {
case
one,
two,
three
}
7 changes: 7 additions & 0 deletions SwimatTests/tests/n-enum-indentation/before.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// #194
enum Foo {
case
one,
two,
three
}
4 changes: 4 additions & 0 deletions SwimatTests/tests/n-enum-indentation/preferences.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"areParametersAligned": false,
"areSemicolonsRemoved": false
}

0 comments on commit f0b1f4f

Please sign in to comment.