-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathFormatter.swift
54 lines (49 loc) · 1.83 KB
/
Formatter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Formatter
import AST
import SwiftyKit
public class DefaultFormatter: Formatter {
let useTabs: Bool
let indentationWidth: Int
public init(useTabs: Bool, indentationWidth: Int) {
self.useTabs = useTabs
self.indentationWidth = indentationWidth
}
public func format(_ element: Element) {
let makeIndent: (Int) -> String = { [useTabs, indentationWidth] in
if useTabs {
return String(repeating: "\t", count: $0)
} else {
return spaceIndent(width: indentationWidth)($0)
}
}
let indentOptions = IndentStrategyVisitor.Options(
default: RegularIndentStrategy(makeIndent: makeIndent)
)
let newLineBetweenOptions = NewLineSpacingVisitor.Options(
functionDeclaration: (1, 1),
simpleVariableDeclaration: (0, 0),
codeBlockVariableDeclaration: (1, 1),
codeBlockSingleLineDeclaration: (0, 0),
getSetKeywordBlockVariableDeclaration: (0, 0),
typeDeclaration: (1, 1),
operatorDeclaration: (1, 1),
default: (0, 0)
)
let indentVisitor = FormatVisitor(indentStrategy: {
IndentStrategyVisitor.visit($0, context: indentOptions)
})
let newLineInsideOptions = NewLineSpacingVisitor.Options(
typeDeclaration: (1, 0)
)
let visitors: [ElementVisitor] = [
NewLineBetweenDeclaration(spacing: { NewLineSpacingVisitor.visit($0, context: newLineBetweenOptions) }),
NewLineInsideDeclaration(spacing: { NewLineSpacingVisitor.visit($0, context: newLineInsideOptions) }),
NewLineAroundImports(spacing: (1, 1)),
EmptyBraces(),
indentVisitor,
]
visitors.forEach {
element.accept(CompoundRecursiveVisitor($0))
}
}
}