Skip to content

Commit 968cab2

Browse files
authored
Merge pull request #50 from angelolloqui/feature/issue_45
Feature/issue 45
2 parents 6cde187 + fb8798b commit 968cab2

File tree

11 files changed

+235
-60
lines changed

11 files changed

+235
-60
lines changed

Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"repositoryURL": "https://github.com/yanagiba/swift-ast",
1616
"state": {
1717
"branch": null,
18-
"revision": "3e572e59de1f520201285a9b7c00f5b166c916ba",
18+
"revision": "e9657500e90aebc5220401d222a4664f6241f3d7",
1919
"version": null
2020
}
2121
},
@@ -24,7 +24,7 @@
2424
"repositoryURL": "https://github.com/angelolloqui/swift-transform",
2525
"state": {
2626
"branch": null,
27-
"revision": "ae7b9aa53f5c413a2d673927abdb1432f51a1e60",
27+
"revision": "3fc221cc73d30034bf1d32a21a42ba1474f21abf",
2828
"version": null
2929
}
3030
}

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ let package = Package(
3131
dependencies: [
3232
.package(
3333
url: "https://github.com/angelolloqui/swift-transform",
34-
.revision("ae7b9aa53f5c413a2d673927abdb1432f51a1e60")
34+
.revision("3fc221cc73d30034bf1d32a21a42ba1474f21abf")
3535
),
3636
],
3737
targets: [

Sources/SwiftKotlinApp/Base.lproj/Main.storyboard

+22-12
Original file line numberDiff line numberDiff line change
@@ -691,18 +691,18 @@
691691
<rect key="frame" x="0.0" y="0.0" width="680" height="480"/>
692692
<autoresizingMask key="autoresizingMask"/>
693693
<subviews>
694-
<scrollView horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" id="Lxu-KE-Cuh">
695-
<rect key="frame" x="10" y="10" width="325" height="435"/>
694+
<scrollView misplaced="YES" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" id="Lxu-KE-Cuh">
695+
<rect key="frame" x="10" y="27" width="325" height="418"/>
696696
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxX="YES" heightSizable="YES"/>
697697
<clipView key="contentView" id="rBo-yE-51C">
698-
<rect key="frame" x="1" y="1" width="323" height="433"/>
698+
<rect key="frame" x="1" y="1" width="323" height="416"/>
699699
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
700700
<subviews>
701701
<textView autoresizesSubviews="NO" drawsBackground="NO" importsGraphics="NO" richText="NO" verticallyResizable="YES" findStyle="panel" allowsUndo="YES" id="Edu-qR-eKg">
702-
<rect key="frame" x="0.0" y="0.0" width="308" height="433"/>
702+
<rect key="frame" x="0.0" y="0.0" width="308" height="416"/>
703703
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
704704
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
705-
<size key="minSize" width="308" height="433"/>
705+
<size key="minSize" width="308" height="416"/>
706706
<size key="maxSize" width="6000" height="10000000"/>
707707
<color key="insertionPointColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
708708
<connections>
@@ -717,22 +717,22 @@
717717
<autoresizingMask key="autoresizingMask"/>
718718
</scroller>
719719
<scroller key="verticalScroller" verticalHuggingPriority="750" doubleValue="1" horizontal="NO" id="4zw-mX-SGb">
720-
<rect key="frame" x="308" y="1" width="16" height="433"/>
720+
<rect key="frame" x="308" y="1" width="16" height="416"/>
721721
<autoresizingMask key="autoresizingMask"/>
722722
</scroller>
723723
</scrollView>
724-
<scrollView horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" id="MqM-2d-jmk">
725-
<rect key="frame" x="345" y="10" width="325" height="435"/>
724+
<scrollView misplaced="YES" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" id="MqM-2d-jmk">
725+
<rect key="frame" x="345" y="27" width="325" height="418"/>
726726
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" heightSizable="YES"/>
727727
<clipView key="contentView" id="KeA-4w-mWI">
728-
<rect key="frame" x="1" y="1" width="323" height="433"/>
728+
<rect key="frame" x="1" y="1" width="323" height="416"/>
729729
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
730730
<subviews>
731731
<textView autoresizesSubviews="NO" editable="NO" drawsBackground="NO" importsGraphics="NO" verticallyResizable="YES" findStyle="panel" id="pog-tu-0qG">
732-
<rect key="frame" x="0.0" y="-1" width="308" height="433"/>
732+
<rect key="frame" x="0.0" y="-1" width="308" height="416"/>
733733
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
734734
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
735-
<size key="minSize" width="308" height="433"/>
735+
<size key="minSize" width="308" height="416"/>
736736
<size key="maxSize" width="6000" height="10000000"/>
737737
<color key="insertionPointColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
738738
</textView>
@@ -744,7 +744,7 @@
744744
<autoresizingMask key="autoresizingMask"/>
745745
</scroller>
746746
<scroller key="verticalScroller" verticalHuggingPriority="750" doubleValue="1" horizontal="NO" id="pI7-5G-k6M">
747-
<rect key="frame" x="308" y="1" width="16" height="433"/>
747+
<rect key="frame" x="308" y="1" width="16" height="416"/>
748748
<autoresizingMask key="autoresizingMask"/>
749749
</scroller>
750750
</scrollView>
@@ -788,9 +788,19 @@
788788
<action selector="formatSwift:" target="XfG-lQ-9wD" id="osa-T5-3yn"/>
789789
</connections>
790790
</button>
791+
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" misplaced="YES" id="RLP-7n-pEc">
792+
<rect key="frame" x="9" y="6" width="663" height="17"/>
793+
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
794+
<textFieldCell key="cell" lineBreakMode="truncatingTail" enabled="NO" refusesFirstResponder="YES" sendsActionOnEndEditing="YES" id="nsR-Gf-VKi">
795+
<font key="font" metaFont="system"/>
796+
<color key="textColor" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
797+
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
798+
</textFieldCell>
799+
</textField>
791800
</subviews>
792801
</view>
793802
<connections>
803+
<outlet property="feedbackTextField" destination="RLP-7n-pEc" id="qbA-4J-fS9"/>
794804
<outlet property="kotlinTextView" destination="pog-tu-0qG" id="oZF-pH-Sgu"/>
795805
<outlet property="swiftTextView" destination="Edu-qR-eKg" id="i1y-Q0-wwf"/>
796806
</connections>

Sources/SwiftKotlinApp/ViewController.swift

+41-19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ViewController: NSViewController {
2121

2222
@IBOutlet var swiftTextView: NSTextView!
2323
@IBOutlet var kotlinTextView: NSTextView!
24+
@IBOutlet var feedbackTextField: NSTextField!
2425

2526
override func viewDidLoad() {
2627
super.viewDidLoad()
@@ -51,28 +52,31 @@ class ViewController: NSViewController {
5152
}
5253

5354
@IBAction func formatSwift(_ sender: AnyObject) {
54-
do {
55-
let swift = swiftTextView.string
56-
let swiftTokens = try swiftTokenizer.translate(content: swift)
57-
let formatted = self.attributedStringFromTokens(tokens: swiftTokens)
58-
self.swiftTextView.textStorage?.beginEditing()
59-
self.swiftTextView.textStorage?.setAttributedString(formatted)
60-
self.swiftTextView.textStorage?.endEditing()
61-
} catch {}
55+
let swift = swiftTextView.string
56+
let result = swiftTokenizer.translate(content: swift)
57+
updateFeedback(result: result)
58+
guard let swiftTokens = result.tokens else {
59+
return
60+
}
61+
let formatted = self.attributedStringFromTokens(tokens: swiftTokens)
62+
self.swiftTextView.textStorage?.beginEditing()
63+
self.swiftTextView.textStorage?.setAttributedString(formatted)
64+
self.swiftTextView.textStorage?.endEditing()
6265
}
6366

6467
func translateSwift() {
65-
do {
66-
let swift = swiftTextView.string
67-
let kotlinTokens = try kotlinTokenizer.translate(content: swift)
68-
69-
DispatchQueue.main.async {
70-
self.kotlinTextView.textStorage?.beginEditing()
71-
let formatted = self.attributedStringFromTokens(tokens: kotlinTokens)
72-
self.kotlinTextView.textStorage?.setAttributedString(formatted)
73-
self.kotlinTextView.textStorage?.endEditing()
74-
}
75-
} catch {}
68+
let swift = swiftTextView.string
69+
let result = kotlinTokenizer.translate(content: swift)
70+
updateFeedback(result: result)
71+
guard let kotlinTokens = result.tokens else {
72+
return
73+
}
74+
DispatchQueue.main.async {
75+
self.kotlinTextView.textStorage?.beginEditing()
76+
let formatted = self.attributedStringFromTokens(tokens: kotlinTokens)
77+
self.kotlinTextView.textStorage?.setAttributedString(formatted)
78+
self.kotlinTextView.textStorage?.endEditing()
79+
}
7680
}
7781

7882
func attributedStringFromTokens(tokens: [Token]) -> NSAttributedString {
@@ -82,6 +86,24 @@ class ViewController: NSViewController {
8286
}
8387
return attributedString
8488
}
89+
90+
func updateFeedback(result: TokenizationResult) {
91+
if let exception = result.exception {
92+
feedbackTextField.textColor = NSColor.red
93+
feedbackTextField.stringValue = "❌ - \(exception.localizedDescription)"
94+
} else if !result.diagnostics.isEmpty {
95+
if let error = result.diagnostics.filter({ $0.level != .warning }).first {
96+
self.feedbackTextField.textColor = NSColor.red
97+
self.feedbackTextField.stringValue = "❌ Line \(error.location.line) Col \(error.location.column) - \(error.kind.diagnosticMessage)"
98+
} else {
99+
let warn = result.diagnostics.first!
100+
feedbackTextField.textColor = NSColor.yellow
101+
feedbackTextField.stringValue = "⚠️ Line \(warn.location.line) Col \(warn.location.column) - \(warn.kind.diagnosticMessage)"
102+
}
103+
} else {
104+
feedbackTextField.stringValue = ""
105+
}
106+
}
85107
}
86108

87109
extension ViewController: NSTextViewDelegate {

Sources/SwiftKotlinCommandLine/main.swift

+25-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
import SwiftKotlinFramework
1111

1212
let kotlinTokenizer = KotlinTokenizer()
13-
let version = "0.0.3"
13+
let version = "0.1.0"
1414
let arguments = [
1515
"output",
1616
"help",
@@ -75,11 +75,21 @@ func execute(_ args: [String]) {
7575
input = (input ?? "") + line
7676
}
7777
if let input = input {
78-
guard let output = try? kotlinTokenizer.translate(content: input).joinedValues() else {
78+
let result = kotlinTokenizer.translate(content: input)
79+
guard let tokens = result.tokens else {
7980
print("error: could not parse input")
81+
if let error = result.exception {
82+
print(error)
83+
} else {
84+
result.diagnostics.forEach { d in
85+
print("\(d.location): \(d.kind.diagnosticMessage)")
86+
}
87+
}
8088
finished = true
8189
return
8290
}
91+
92+
let output = tokens.joinedValues()
8393
if let outputURL = outputURL {
8494
if (try? output.write(to: outputURL, atomically: true, encoding: String.Encoding.utf8)) != nil {
8595
print("swiftkotlin completed successfully")
@@ -110,6 +120,8 @@ func execute(_ args: [String]) {
110120

111121
// Format the code
112122
let start = CFAbsoluteTimeGetCurrent()
123+
124+
// TODO: Change this to first get the sources, then translate and finally write output
113125
let filesWritten = processInput(inputURL!, andWriteToOutput: outputURL!)
114126
let time = CFAbsoluteTimeGetCurrent() - start
115127
print("swiftkotlin completed. \(filesWritten) file\(filesWritten == 1 ? "" : "s") updated in \(String(format: "%.2f", time))s.")
@@ -181,11 +193,19 @@ func processInput(_ inputURL: URL, andWriteToOutput outputURL: URL) -> Int {
181193
print("error: failed to read contents of directory at: \(inputURL.path)")
182194
}
183195
} else if inputURL.pathExtension == "swift" {
184-
guard let output = try? kotlinTokenizer.translate(path: inputURL).joinedValues() else {
185-
print("error: could not parse file: \(inputURL.path)")
196+
let result = kotlinTokenizer.translate(path: inputURL)
197+
guard let tokens = result.tokens else {
198+
print("error: could not parse input")
199+
if let error = result.exception {
200+
print(error)
201+
} else {
202+
result.diagnostics.forEach { d in
203+
print("\(d.location): \(d.kind.diagnosticMessage)")
204+
}
205+
}
186206
return 0
187207
}
188-
208+
let output = tokens.joinedValues()
189209
if (try? output.write(to: outputURL.deletingPathExtension().appendingPathExtension("kt"), atomically: true, encoding: String.Encoding.utf8)) != nil {
190210
return 1
191211
} else {

Sources/SwiftKotlinFramework/KotlinTokenizer.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,9 @@ public class KotlinTokenizer: SwiftTokenizer {
897897
}
898898
}
899899
let expression = String(remainingText[..<remainingText.index(remainingText.startIndex, offsetBy: i - 1)])
900-
let computedExpression = (try? translate(content: expression).joinedValues().replacingOccurrences(of: "\n", with: "")) ?? expression
901-
interpolatedString += "${\(computedExpression)}"
900+
let computedExpression = translate(content: expression).tokens?.joinedValues().replacingOccurrences(of: "\n", with: "")
901+
902+
interpolatedString += "${\(computedExpression ?? expression)}"
902903
remainingText = String(remainingText[remainingText.index(remainingText.startIndex, offsetBy: i)...])
903904
}
904905

0 commit comments

Comments
 (0)