diff --git a/Extension/Extension.swift b/Extension/Extension.swift index c239ec4..ae36b89 100644 --- a/Extension/Extension.swift +++ b/Extension/Extension.swift @@ -75,16 +75,16 @@ extension String { typealias StringObj = (string: String, index: String.Index) func findParentheses(from start: String.Index, reFormat: Bool = true) throws -> StringObj { - return try findBlock(from: start, symbol: ("(", ")"), reFormat: reFormat) + return try findBlock(type: .parentheses, from: start, reFormat: reFormat) } func findSquare(from start: String.Index, reFormat: Bool = true) throws -> StringObj { - return try findBlock(from: start, symbol: ("[", "]"), reFormat: reFormat) + return try findBlock(type: .square, from: start, reFormat: reFormat) } - func findBlock(from start: String.Index, symbol: (start: String, end: Character), reFormat: Bool) throws -> StringObj { + func findBlock(type: IndentType, from start: String.Index, reFormat: Bool) throws -> StringObj { var target = index(after: start) - var result = symbol.start + var result = String(type.rawValue) while target < endIndex { let next = self[target] @@ -102,7 +102,7 @@ extension String { result.append(next) } target = index(after: target) - if next == symbol.end { + if next == type.stopSymbol() { break } } diff --git a/Extension/Indent.swift b/Extension/Indent.swift index d7179db..56e1111 100644 --- a/Extension/Indent.swift +++ b/Extension/Indent.swift @@ -2,6 +2,17 @@ import Foundation enum IndentType: Character { case parentheses = "(", square = "[", curly = "{" + + func stopSymbol() -> Character { + switch self { + case .parentheses: + return ")" + case .square: + return "]" + case .curly: + return "}" + } + } } class Indent { @@ -30,13 +41,8 @@ class Indent { self.count = indent.count self.extra = indent.extra self.inSwitch = false - if block == .curly { - self.leading = 0 - } else { - self.leading = indent.leading - } - if self.block != .parentheses && !indent.indentAdd { + if block != .parentheses && !indent.indentAdd { self.count += 1 self.indentAdd = true } else { @@ -48,11 +54,14 @@ class Indent { } else { self.extraAdd = false } - if self.block == .parentheses { - self.leading = offset - self.count * Indent.size - 1 - if self.leading < 0 { - self.leading = 0 - } + + switch block { + case .curly: + self.leading = 0 + case .parentheses: + self.leading = max(offset - count * Indent.size - 1, 0) + case .square: + self.leading = indent.leading } }