/
RulesViewController.swift
226 lines (196 loc) · 8.84 KB
/
RulesViewController.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// RulesViewController.swift
// SwiftFormat for Xcode
//
// Created by Vincent Bernier on 27/01/18.
// Copyright 2018 Nick Lockwood
//
// Distributed under the permissive MIT license
// Get the latest version from here:
//
// https://github.com/nicklockwood/SwiftFormat
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Cocoa
extension FormatRule {
var toolTip: String {
return stripMarkdown(help)
}
}
extension FormatOptions.Descriptor {
// If this extension won't compile have a look at
// https://stackoverflow.com/questions/35673290/extension-of-a-nested-type-in-swift
// https://bugs.swift.org/browse/SR-631
var toolTip: String {
return stripMarkdown(help) + "."
}
}
final class RulesViewController: NSViewController {
private let ruleStore = RulesStore()
private let optionStore = OptionsStore()
private var viewModels = [UserSelectionType]()
@IBOutlet var tableView: NSTableView!
@IBOutlet var inferOptionsButton: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
inferOptionsButton.state = optionStore.inferOptions ? .on : .off
viewModels = buildRules()
NotificationCenter.default.addObserver(self, selector: #selector(didLoadNewConfiguration), name: .applicationDidLoadNewConfiguration, object: nil)
}
@objc private func didLoadNewConfiguration(_: Notification) {
viewModels = buildRules()
tableView?.reloadData()
inferOptionsButton?.state = (optionStore.inferOptions ? .on : .off)
}
@IBAction private func toggleInferOptions(_ sender: NSButton) {
optionStore.inferOptions = (sender.state == .on)
viewModels = buildRules()
tableView?.reloadData()
}
private func buildRules() -> [UserSelectionType] {
let optionsByName = Dictionary(uniqueKeysWithValues: optionStore
.options
.map { ($0.descriptor.argumentName, $0) })
var results = [UserSelectionType]()
ruleStore
.rules
.sorted()
.forEach { rule in
let formatRule = FormatRules.byName[rule.name]!
let associatedOptions = formatRule
.options
.compactMap { optionName in optionsByName[optionName] }
.sorted { $0.descriptor.displayName < $1.descriptor.displayName }
.compactMap { option -> UserSelectionType? in
guard !option.isDeprecated,
option.descriptor.argumentName != FormatOptions.Descriptor.indentation.argumentName else {
return nil
}
let descriptor = option.descriptor
let selection = option.argumentValue
let saveOption: (String) -> Void = { [weak self] in
var option = option
option.argumentValue = $0
self?.optionStore.save(option)
}
let enabled = !optionStore.inferOptions
switch descriptor.type {
case let .binary(t, f):
let list = UserSelectionList(
identifier: descriptor.argumentName,
title: descriptor.displayName,
description: descriptor.toolTip,
isEnabled: enabled,
selection: selection,
options: [t[0], f[0]],
observer: saveOption
)
return UserSelectionType.list(list)
case let .enum(values):
let list = UserSelectionList(
identifier: descriptor.argumentName,
title: descriptor.displayName,
description: descriptor.toolTip,
isEnabled: enabled,
selection: selection,
options: values,
observer: saveOption
)
return UserSelectionType.list(list)
case .text, .set:
let freeText = UserSelectionFreeText(
identifier: descriptor.argumentName,
title: descriptor.displayName,
description: descriptor.toolTip,
isEnabled: enabled,
selection: selection,
observer: { input in
if descriptor.validateArgument(input) {
saveOption(input)
}
},
validationStrategy: descriptor.validateArgument
)
return UserSelectionType.freeText(freeText)
}
}
let d = UserSelectionBinary(identifier: rule.name,
title: rule.name,
description: formatRule.toolTip,
isEnabled: true,
selection: rule.isEnabled,
observer: { [weak self] in
var updatedRule = rule
updatedRule.isEnabled = $0
self?.ruleStore.save(updatedRule)
})
results.append(UserSelectionType.binary(d))
results.append(contentsOf: associatedOptions)
}
return results
}
func model(forRow row: Int) -> UserSelectionType {
return viewModels[row]
}
}
// MARK: - Table View Data Source
extension RulesViewController: NSTableViewDataSource {
func numberOfRows(in _: NSTableView) -> Int {
return viewModels.count
}
func tableView(_: NSTableView, objectValueFor _: NSTableColumn?, row: Int) -> Any? {
return model(forRow: row).associatedValue()
}
}
// MARK: - Table View Delegate
extension RulesViewController: NSTableViewDelegate {
func tableView(_ tableView: NSTableView, viewFor _: NSTableColumn?, row: Int) -> NSView? {
let model = self.model(forRow: row)
let id: NSUserInterfaceItemIdentifier
switch model {
case .binary:
id = .binarySelectionTableCellView
case .list:
id = .listSelectionTableCellView
case .freeText:
id = .freeTextTableCellView
}
return tableView.makeView(withIdentifier: id, owner: self)
}
func tableView(_ tableView: NSTableView, rowViewForRow _: Int) -> NSTableRowView? {
if let rowView = tableView.makeView(withIdentifier: .ruleRowView, owner: self) as? NSTableRowView {
return rowView
}
let rowView = NSTableRowView(frame: .zero)
rowView.identifier = .ruleRowView
return rowView
}
func tableView(_: NSTableView, didAdd rowView: NSTableRowView, forRow row: Int) {
switch model(forRow: row) {
case .binary:
rowView.backgroundColor = NSColor.controlAlternatingRowBackgroundColors[1]
case .list, .freeText:
rowView.backgroundColor = NSColor.controlAlternatingRowBackgroundColors[0]
}
}
}
private extension NSUserInterfaceItemIdentifier {
static let ruleRowView = NSUserInterfaceItemIdentifier(rawValue: "RuleRowView")
}