Skip to content

Commit

Permalink
* refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jintin committed Nov 13, 2016
1 parent 25d85d0 commit acc3be3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
10 changes: 5 additions & 5 deletions Extension/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -102,7 +102,7 @@ extension String {
result.append(next)
}
target = index(after: target)
if next == symbol.end {
if next == type.stopSymbol() {
break
}
}
Expand Down
31 changes: 20 additions & 11 deletions Extension/Indent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}

Expand Down

0 comments on commit acc3be3

Please sign in to comment.