-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathViewController.swift
153 lines (137 loc) · 5.92 KB
/
ViewController.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
//
// ViewController.swift
// SwiftKotlinApp
//
// Created by Angel Garcia on 09/11/16.
// Copyright © 2016 Angel G. Olloqui. All rights reserved.
//
import Cocoa
import SwiftKotlinFramework
import Transform
class ViewController: NSViewController {
let swiftTokenizer = SwiftTokenizer(
tokenTransformPlugins: [
CommentsAdditionTransformPlugin()
]
)
let kotlinTokenizer = KotlinTokenizer(
tokenTransformPlugins: [
XCTTestToJUnitTokenTransformPlugin(),
FoundationMethodsTransformPlugin(),
UIKitTransformPlugin(),
CommentsAdditionTransformPlugin()
]
)
@IBOutlet var swiftTextView: NSTextView!
@IBOutlet var kotlinTextView: NSTextView!
@IBOutlet var feedbackTextField: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
self.swiftTextView.isAutomaticQuoteSubstitutionEnabled = false
self.swiftTextView.isAutomaticDashSubstitutionEnabled = false
self.swiftTextView.isAutomaticTextReplacementEnabled = false
self.kotlinTextView.isAutomaticQuoteSubstitutionEnabled = false
self.kotlinTextView.isAutomaticDashSubstitutionEnabled = false
self.kotlinTextView.isAutomaticTextReplacementEnabled = false
}
@IBAction func openSwiftFile(_ sender: AnyObject) {
let oPanel: NSOpenPanel = NSOpenPanel()
oPanel.canChooseDirectories = false
oPanel.canChooseFiles = true
oPanel.allowsMultipleSelection = false
oPanel.allowedFileTypes = ["swift"]
oPanel.prompt = "Open"
oPanel.beginSheetModal(for: self.view.window!, completionHandler: { (button: NSApplication.ModalResponse) -> Void in
if button == NSApplication.ModalResponse.OK {
let filePath = oPanel.urls.first!.path
let fileHandle = FileHandle(forReadingAtPath: filePath)
if let data = fileHandle?.readDataToEndOfFile() {
self.swiftTextView.textStorage?.beginEditing()
self.swiftTextView.textColor = NSColor.black
self.swiftTextView.string = String(data: data, encoding: .utf8) ?? ""
self.swiftTextView.textStorage?.endEditing()
self.translateSwift()
}
}
})
}
@IBAction func formatSwift(_ sender: AnyObject) {
let swift = swiftTextView.string
let result = swiftTokenizer.translate(content: swift)
updateFeedback(result: result)
guard let swiftTokens = result.tokens else {
return
}
let formatted = self.attributedStringFromTokens(tokens: swiftTokens)
self.swiftTextView.textStorage?.beginEditing()
self.swiftTextView.textStorage?.setAttributedString(formatted)
self.swiftTextView.textStorage?.endEditing()
}
func translateSwift() {
let swift = swiftTextView.string
let result = kotlinTokenizer.translate(content: swift)
updateFeedback(result: result)
guard let kotlinTokens = result.tokens else {
return
}
DispatchQueue.main.async {
self.kotlinTextView.textStorage?.beginEditing()
let formatted = self.attributedStringFromTokens(tokens: kotlinTokens)
self.kotlinTextView.textStorage?.setAttributedString(formatted)
self.kotlinTextView.textStorage?.endEditing()
}
}
func attributedStringFromTokens(tokens: [Token]) -> NSAttributedString {
let attributedString = NSMutableAttributedString()
tokens.forEach {
attributedString.append($0.attributedString)
}
return attributedString
}
func updateFeedback(result: TokenizationResult) {
if let exception = result.exception {
feedbackTextField.textColor = NSColor.red
feedbackTextField.stringValue = "❌ - \(exception.localizedDescription)"
} else if !result.diagnostics.isEmpty {
if let error = result.diagnostics.filter({ $0.level != .warning }).first {
self.feedbackTextField.textColor = NSColor.red
self.feedbackTextField.stringValue = "❌ Line \(error.location.line) Col \(error.location.column) - \(error.kind.diagnosticMessage)"
} else {
let warn = result.diagnostics.first!
feedbackTextField.textColor = NSColor.yellow
feedbackTextField.stringValue = "⚠️ Line \(warn.location.line) Col \(warn.location.column) - \(warn.kind.diagnosticMessage)"
}
} else {
feedbackTextField.stringValue = "✅"
}
}
}
extension ViewController: NSTextViewDelegate {
func textDidChange(_ notification: Notification) {
translateSwift()
}
}
extension Token {
var attributes: [NSAttributedString.Key : Any] {
switch self.kind {
case .keyword:
return [NSAttributedString.Key.foregroundColor: NSColor(red: 170.0/255.0, green: 13.0/255.0, blue: 145.0/255.0, alpha: 1)]
case .number:
return [NSAttributedString.Key.foregroundColor: NSColor(red: 28.0/255.0, green: 0.0/255.0, blue: 207.0/255.0, alpha: 1)]
case .string:
return [NSAttributedString.Key.foregroundColor: NSColor(red: 196.0/255.0, green: 26.0/255.0, blue: 22.0/255.0, alpha: 1)]
case .comment:
return [NSAttributedString.Key.foregroundColor: NSColor(red: 0, green: 116.0/255.0, blue: 0, alpha: 1)]
default:
if #available(OSX 10.14, *)
{
return [NSAttributedString.Key.foregroundColor: NSApp.mainWindow?.effectiveAppearance.name == .darkAqua ? NSColor.white : NSColor.black]
} else {
return [:]
}
}
}
var attributedString: NSAttributedString {
return NSAttributedString(string: self.value, attributes: self.attributes)
}
}