Repository navigation
Expand file tree
/
Copy pathMainWindowController.swift
More file actions
356 lines (298 loc) · 10.9 KB
/
Copy pathMainWindowController.swift
File metadata and controls
356 lines (298 loc) · 10.9 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import AppKit
@IBObject public class MainWindowController : NSWindowController, INSTextDelegate, ITextViewDelegate {
init() {
super.init(windowNibName: "MainWindow")
// Custom initialization
}
public override func windowDidLoad() {
super.windowDidLoad()
// Implement this method to handle any initialization after your window controller's
// window has been loaded from its nib file.
textField.delegate = self
updatetextView()
if let lastFile = NSUserDefaults.standardUserDefaults.stringForKey(KEY_LAST_OPEN_FILE) {
openFile(lastFile)
}
}
public func openFile(_ filename: String)
{
textField.tryLoadFile(filename)
}
private func updatetextView()
{
textField.font = NSFont.fontWithName("Menlo",size:13);
//textField.drawsBackground = false
textField.textStorage?.font = textField.font
/*let fieldEditor = window.fieldEditor(true, forObject: codeField)
fieldEditor.setSelectedRange(NSMakeRange(0, 0))
fieldEditor.setNeedsDisplay(true)
fieldEditor.backgroundColor = codeField.backgroundColor*/
}
@IBOutlet var textField: TextView
let KEY_LAST_OPEN_FILE = "LastOpenFile"
func textView(_ textView: TextView, loadedFile fileName: String) {
window?.title = "Change Log Tool — \(fileName.lastPathComponent)"
NSUserDefaults.standardUserDefaults.setObject(fileName, forKey: KEY_LAST_OPEN_FILE)
}
func textView(_ textView: TextView, gotNonFileText text: String) {
window?.title = "Change Log Tool"
NSUserDefaults.standardUserDefaults.setObject(nil, forKey: KEY_LAST_OPEN_FILE)
}
func processLines(_ processor: (String) -> String) {
let lines = textField.string?.componentsSeparatedByString("\n")
let newLines = NSMutableArray()
for l in lines {
if let l2 = processor(l) {
newLines.addObject(l2)
}
}
setTextField(to: newLines)
}
func processLinesInBlocks(_ processor: (NSArray) -> NSArray) {
if let lines = textField.string?.componentsSeparatedByString("\n") {
let newLines = NSMutableArray(capacity: lines.count)
let currentBlock = NSMutableArray(capacity: lines.count)
let processCurrentBlock = {
if currentBlock.count > 0 {
let processedBlock = processor(currentBlock)
newLines.addObjectsFromArray(processedBlock)
currentBlock.removeAllObjects()
}
}
for l in lines {
let l2 = l.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet)
if !l2.hasPrefix("*") {
processCurrentBlock()
newLines.addObject(l)
} else {
currentBlock.addObject(l)
}
}
processCurrentBlock()
setTextField(to: newLines)
}
}
@IBAction func save(_ sender: AnyObject?) {
if let fileName = textField.fileName {
textField.string?.writeToFile(fileName, atomically: true, encoding: .UTF8StringEncoding, error: nil)
}
}
@IBAction func gitToMarkdown(_ sender: AnyObject?) {
processLines(convertGitLineToMarkdown)
removeEmptySections(sender)
}
@IBAction func removeEmptySections(_ sender: AnyObject?) {
let lines = textField.string?.componentsSeparatedByString("\n")
let newLines = NSMutableArray()
var headline: String? = nil
var hasSpace: Boolean = false
for l in lines {
if l.hasPrefix("#") {
headline = l
} else if headline != nil && l == "" {
hasSpace = true
} else {
if let h = headline {
newLines.addObject(h)
headline = nil
if hasSpace {
newLines.addObject("")
hasSpace = false
}
}
newLines.addObject(l)
}
}
setTextField(to: newLines);
}
@IBAction func sortByID(_ sender: AnyObject?) {
processLinesInBlocks() { lines in
return self.sortLinesByID(lines)
}
}
@IBAction func sortByText(_ sender: AnyObject?) {
processLinesInBlocks() { lines in
return self.sortLinesByText(lines)
}
}
@IBAction func cleanup(_ sender: AnyObject?) {
processLines() { line in
if line.length == 0 || line.hasPrefix("#") {
return line
}
var (issueID, l) = self.getIssueIDAndRemainderFromLine(line)
l = l.stringByReplacingOccurrencesOfString("Hydrogene", withString: "C#", options: .CaseInsensitiveSearch, range: NSMakeRange(0, l.length))
l = l.stringByReplacingOccurrencesOfString("Silver", withString: "Swift", options: .CaseInsensitiveSearch, range: NSMakeRange(0, l.length))
l = l.stringByReplacingOccurrencesOfString("csC", withString: "Complete Class") // keep case sensitive
l = l.stringByReplacingOccurrencesOfString("GoToDefinition", withString: "Go to Definition", options: .CaseInsensitiveSearch, range: NSMakeRange(0, l.length))
l = l.stringByReplacingOccurrencesOfString("GTD", withString: "Go to Definition", options: .CaseInsensitiveSearch, range: NSMakeRange(0, l.length))
if issueID != "0" {
return "* \(issueID): \(l)"
} else {
return "* \(l)"
}
}
}
//
// Processing
//
func distinctArray(_ lines: NSArray) -> NSArray {
return lines.valueForKeyPath("@distinctUnionOfObjects.self")!
}
func sortLinesByID(_ lines: NSArray) -> NSArray {
return distinctArray(lines).sortedArrayUsingComparator() { a, b in
let idA = self.getIssueIDFromLine(a)
let idB = self.getIssueIDFromLine(b)
if idA > idB {
return .OrderedAscending
} else if idA < idB {
return .OrderedDescending
} else {
let textA = self.getTextFromLine(a)
let textB = self.getTextFromLine(b)
if !textA.hasPrefix("Merged") && textB.hasPrefix("Merged") {
return .OrderedAscending
} else if textA.hasPrefix("Merged") && !textB.hasPrefix("Merged") {
return .OrderedDescending
} else {
return textA.lowercaseString.compare(textB.lowercaseString)
}
}
}
}
func sortLinesByText(_ lines: NSArray) -> NSArray {
return distinctArray(lines).sortedArrayUsingComparator() { a, b in
let idA = self.getIssueIDFromLine(a)
let idB = self.getIssueIDFromLine(b)
let textA = self.getTextFromLine(a)
let textB = self.getTextFromLine(b)
if (idA == 0) == (idB == 0) {
if !textA.hasPrefix("Merged") && textB.hasPrefix("Merged") {
return .OrderedAscending
} else if textA.hasPrefix("Merged") && !textB.hasPrefix("Merged") {
return .OrderedDescending
} else {
return textA.lowercaseString.compare(textB.lowercaseString)
}
} else if idA == 0 {
return .OrderedDescending
} else {
return .OrderedAscending
}
}
}
private func stringStartsWithGitRev(_ string: NSString!) -> Bool {
let len = length(string)
if len < 7 {
return false
}
for i in 0 ..< len {
var c: unichar = string.characterAtIndex(i)
if i > 0 && c == " " {
return true
}
if !(c >= "a" && c <= "f" || c >= "A" && c <= "F" || c >= "0" && c <= "9") {
return false
}
}
return true
}
private func getIssueIDAndRemainderFromLine(_ line: String) -> (String, String) {
var issueID: String? = nil
var l = line
let extractIssueID = { (range: NSRange) in
var potentialIssueID: NSString! = l.substringToIndex(range.location)
var number = abs(potentialIssueID.intValue())
if number > 0 || potentialIssueID == "0" {
issueID = potentialIssueID
l = l.substringFromIndex(range.location + 1).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet)
} else {
var trimmedPotentialIssueID = potentialIssueID.substring(fromIndex: 1)
number = abs(trimmedPotentialIssueID.intValue())
if number > 0 {
issueID = potentialIssueID
l = l.substringFromIndex(range.location + 1).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet)
} else {
issueID = nil
}
}
}
if l.hasPrefix("* ") {
l = l.substringFromIndex(2)
}
var colon = l.rangeOfString(":")
if colon.location != NSNotFound && colon.location > 0 && colon.location < 20 {
extractIssueID(colon)
} else {
colon = l.rangeOfString(" ")
if colon.location != NSNotFound && colon.location > 0 && colon.location < 20 {
extractIssueID(colon)
}
}
l = l.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet)
return (coalesce(issueID, "0"), l) // E486 Parameter 1 is "String?", should be "T1", in call to Tuple!.New<T1,T2>(_ aItem1: T1, _ aItem2: T2) -> Tuple2<T1,T2>!
//return (issueID, l) // E486 Parameter 1 is "String?", should be "T1", in call to Tuple!.New<T1,T2>(_ aItem1: T1, _ aItem2: T2) -> Tuple2<T1,T2>!
// E486 Parameter 2 is "String", should be "T2", in call to Tuple!.New<T1,T2>(_ aItem1: T1, _ aItem2: T2) -> Tuple2<T1,T2>!
}
private func getIssueIDFromLine(_ line: String) -> String {
let (issueID, _) = getIssueIDAndRemainderFromLine(line)
return issueID
}
private func getTextFromLine(_ line: String) -> String {
let (_, text) = getIssueIDAndRemainderFromLine(line)
return text
}
private func convertGitLineToMarkdown(_ line: String) -> String? {
var l = line.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet)
if l.length() == 0 || l.hasPrefix("#") || l.hasPrefix("*") {
return line
}
if stringStartsWithGitRev(l) {
var dash = l.rangeOfString(" - ")
if dash.location > 0 && dash.location <= 16 {
l = l.substringFromIndex(dash.location + 3)
var parens = l.rangeOfString("(", options: NSStringCompareOptions.BackwardsSearch)
if parens.location > 0 && parens.location < l.length() {
l = l.substringToIndex(parens.location)
parens = l.rangeOfString("(", options: NSStringCompareOptions.BackwardsSearch)
if parens.location > 0 && parens.location < l.length() {
l = l.substringToIndex(parens.location)
}
}
}
}
var issueID: String
(issueID, l) = getIssueIDAndRemainderFromLine(l)
if issueID == 0 {
if l.length() == 0 || l.hasPrefix("Merge ") || l.hasPrefix("Squashed ") {
return nil
}
}
if issueID != "0" {
return "* \(issueID): \(l)"
} else {
return "* \(l)"
}
}
func setTextField(to newText: NSArray<String>) {
//let oldText = textField.string
//if let lUndoManager = textField.undoManager {
//// Prepare the undo invocation target
////let lUndoTarget = lUndoManager.prepare(invocationTarget: textField) as! NSTextView
////lUndoTarget.string = oldText
//lUndoManager.registerUndo(target: self, selector: #selector(restoreText(_:)), object: oldText)
//lUndoManager.setActionName("Change Text")
//}
// Replace the text in the editor, which updates the NSTextField and the undo stack
textField.string = newText.componentsJoinedByString("\n")
}
//@objc
//func restoreText(_ oldText: String) {
//let currentText = textField.string
//if let lUndoManager = textField.undoManager {
//lUndoManager.registerUndo(target: self, selector: #selector(restoreText(_:)), object: currentText)
//lUndoManager.setActionName("Change Text")
//}
//textField.string = oldText
//}
}