diff --git a/.travis.yml b/.travis.yml index 18f88cb..05a2632 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: objective-c -osx_image: xcode11.2 +osx_image: xcode11.3 install: - swift package update - swift package generate-xcodeproj --enable-code-coverage diff --git a/Assets/Tests/KotlinTokenizer/enums.swift b/Assets/Tests/KotlinTokenizer/enums.swift index 0752ba9..32f26b5 100644 --- a/Assets/Tests/KotlinTokenizer/enums.swift +++ b/Assets/Tests/KotlinTokenizer/enums.swift @@ -6,7 +6,7 @@ enum CompassPoint { case west } -private enum Planet { +private enum Planet: Equatable { case mercury, venus, earth case mars, jupiter, saturn, uranus, neptune } diff --git a/Assets/Tests/KotlinTokenizer/foundation_types.kt b/Assets/Tests/KotlinTokenizer/foundation_types.kt index 433442a..c249deb 100644 --- a/Assets/Tests/KotlinTokenizer/foundation_types.kt +++ b/Assets/Tests/KotlinTokenizer/foundation_types.kt @@ -12,3 +12,4 @@ var map: Promise>? = null var map: Map>> var map = mapOf(1 to "a", 2 to "b") var map = mapOf() +method(value = listOf("value1", "value")) diff --git a/Assets/Tests/KotlinTokenizer/foundation_types.swift b/Assets/Tests/KotlinTokenizer/foundation_types.swift index 6ed5a3e..236fac2 100644 --- a/Assets/Tests/KotlinTokenizer/foundation_types.swift +++ b/Assets/Tests/KotlinTokenizer/foundation_types.swift @@ -12,3 +12,4 @@ var map: Promise<[Int: String]>? var map: [Int: Promise<[String: String]>] var map = [1: "a", 2: "b"] var map = [String: String]() +method(value: ["value1", "value"]) diff --git a/Assets/Tests/KotlinTokenizer/structs.kt b/Assets/Tests/KotlinTokenizer/structs.kt index 0d3fdcb..130045b 100644 --- a/Assets/Tests/KotlinTokenizer/structs.kt +++ b/Assets/Tests/KotlinTokenizer/structs.kt @@ -8,3 +8,11 @@ data class Person( fun eat() {} } + +data class User( + var id: Int? = 0, + var name: String? = null, + var content: Content): Codable { + + data class Content(val text: String) {} +} diff --git a/Assets/Tests/KotlinTokenizer/structs.swift b/Assets/Tests/KotlinTokenizer/structs.swift index 959319f..9c48c54 100644 --- a/Assets/Tests/KotlinTokenizer/structs.swift +++ b/Assets/Tests/KotlinTokenizer/structs.swift @@ -2,10 +2,19 @@ struct Data { var text: String } -struct Person { +struct Person: Equatable { let name: String let surname: String var age: Int func eat() {} } + +struct User: Codable { + struct Content { + let text: String + } + var id: Int? = 0 + var name: String? + var content: Content +} diff --git a/Sources/SwiftKotlinApp/Info.plist b/Sources/SwiftKotlinApp/Info.plist index fab7ff1..fc45ba4 100644 --- a/Sources/SwiftKotlinApp/Info.plist +++ b/Sources/SwiftKotlinApp/Info.plist @@ -17,7 +17,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 0.2.2 + $(MARKETING_VERSION) CFBundleVersion 1 LSMinimumSystemVersion diff --git a/Sources/SwiftKotlinCommandLine/main.swift b/Sources/SwiftKotlinCommandLine/main.swift index 4304141..c042c61 100644 --- a/Sources/SwiftKotlinCommandLine/main.swift +++ b/Sources/SwiftKotlinCommandLine/main.swift @@ -16,7 +16,7 @@ let kotlinTokenizer = KotlinTokenizer( CommentsAdditionTransformPlugin() ] ) -let version = "0.2.1" +let version = "0.2.3" let arguments = [ "output", "help", @@ -26,7 +26,7 @@ let arguments = [ func showHelp() { print("swiftkotlin, version \(version)") - print("copyright (c) 2019 Angel G. Olloqui") + print("copyright (c) 2020 Angel G. Olloqui") print("") print("usage: swiftkotlin [] [--output path]") print("") diff --git a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift index 3ab998b..84cbe1a 100644 --- a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift +++ b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift @@ -127,7 +127,7 @@ public class KotlinTokenizer: SwiftTokenizer { accessLevelModifier: declaration.accessLevelModifier, name: declaration.name, genericParameterClause: declaration.genericParameterClause, - typeInheritanceClause: declaration.typeInheritanceClause, + typeInheritanceClause: nil, genericWhereClause: declaration.genericWhereClause, members: otherMembers) newStruct.setSourceRange(declaration.sourceRange) @@ -136,13 +136,6 @@ public class KotlinTokenizer: SwiftTokenizer { .replacing({ $0.value == "struct"}, with: [declaration.newToken(.keyword, "data class")]) - if !staticMembers.isEmpty, let bodyStart = tokens.firstIndex(where: { $0.value == "{"}) { - let companionTokens = indent(tokenizeCompanion(staticMembers, node: declaration)) - .prefix(with: declaration.newToken(.linebreak, "\n")) - .suffix(with: declaration.newToken(.linebreak, "\n")) - tokens.insert(contentsOf: companionTokens, at: bodyStart + 1) - } - if !declarationMembers.isEmpty, let bodyStart = tokens.firstIndex(where: { $0.value == "{"}) { let linebreak = declaration.newToken(.linebreak, "\n") let declarationTokens: [Token] @@ -166,6 +159,21 @@ public class KotlinTokenizer: SwiftTokenizer { at: bodyStart - 1) } + if let typeInheritanceList = declaration.typeInheritanceClause?.typeInheritanceList.nonEquatable, + typeInheritanceList.isEmpty == false, + let bodyStart = tokens.firstIndex(where: { $0.value == "{"}) { + let clause = TypeInheritanceClause(classRequirement: false, typeInheritanceList: typeInheritanceList) + let inheritanceTokens = tokenize(clause, node: declaration) + tokens.insert(contentsOf: inheritanceTokens, at: bodyStart - 1) + } + + if !staticMembers.isEmpty, let bodyStart = tokens.firstIndex(where: { $0.value == "{"}) { + let companionTokens = indent(tokenizeCompanion(staticMembers, node: declaration)) + .prefix(with: declaration.newToken(.linebreak, "\n")) + .suffix(with: declaration.newToken(.linebreak, "\n")) + tokens.insert(contentsOf: companionTokens, at: bodyStart + 1) + } + return tokens } @@ -431,8 +439,9 @@ public class KotlinTokenizer: SwiftTokenizer { // Simple enums (no tuple values) if !simpleCases.contains(where: { $0.tuple != nil }) { - if declaration.typeInheritanceClause != nil { - return tokenizeSimpleValueEnum(declaration:declaration, simpleCases: simpleCases) + let typeInheritanceList = declaration.typeInheritanceClause?.typeInheritanceList.nonEquatable + if typeInheritanceList?.isEmpty == false { + return tokenizeSimpleValueEnum(declaration: declaration, simpleCases: simpleCases) } else { return tokenizeNoValueEnum(declaration: declaration, simpleCases: simpleCases) } @@ -588,12 +597,11 @@ public class KotlinTokenizer: SwiftTokenizer { case let .staticString(_, rawText): return [expression.newToken(.string, conversionUnicodeString(rawText, node: expression))] case .array(let exprs): - let isGenericTypeInfo = expression.lexicalParent is FunctionCallExpression - return - expression.newToken(.identifier, "listOf") + - expression.newToken(.startOfScope, isGenericTypeInfo ? "<" : "(") + + let isGenericTypeInfo = (expression.lexicalParent as? FunctionCallExpression)?.postfixExpression.textDescription.starts(with: "[") == true + return expression.newToken(.identifier, "listOf") + + expression.newToken(.startOfScope, isGenericTypeInfo ? "<" : "(") + exprs.map { tokenize($0) }.joined(token: expression.newToken(.delimiter, ", ")) + - expression.newToken(.endOfScope, isGenericTypeInfo ? ">" : ")") + expression.newToken(.endOfScope, isGenericTypeInfo ? ">" : ")") case .dictionary(let entries): let isGenericTypeInfo = expression.lexicalParent is FunctionCallExpression var entryTokens = entries.map { tokenize($0, node: expression) }.joined(token: expression.newToken(.delimiter, ", ")) diff --git a/Sources/SwiftKotlinFramework/utils/AST+Operations.swift b/Sources/SwiftKotlinFramework/utils/AST+Operations.swift index 0435185..8998b19 100644 --- a/Sources/SwiftKotlinFramework/utils/AST+Operations.swift +++ b/Sources/SwiftKotlinFramework/utils/AST+Operations.swift @@ -273,3 +273,10 @@ extension GuardStatement { (bodyStatement is ReturnStatement || bodyStatement is ThrowStatement) } } + +extension Collection where Iterator.Element == TypeIdentifier { + var nonEquatable: [TypeIdentifier] { + return filter { $0.names.contains { $0.name.textDescription != "Equatable" } } + } +} + diff --git a/SwiftKotlinApp.xcodeproj/project.pbxproj b/SwiftKotlinApp.xcodeproj/project.pbxproj index b75fe16..68938c0 100644 --- a/SwiftKotlinApp.xcodeproj/project.pbxproj +++ b/SwiftKotlinApp.xcodeproj/project.pbxproj @@ -297,6 +297,7 @@ INFOPLIST_FILE = "$(SRCROOT)/Sources/SwiftKotlinApp/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.12; + MARKETING_VERSION = 0.2.3; MTL_ENABLE_DEBUG_INFO = YES; PRODUCT_BUNDLE_IDENTIFIER = com.angelolloqui.SwiftKotlinApp; PRODUCT_NAME = SwiftKotlin; @@ -351,6 +352,7 @@ INFOPLIST_FILE = "$(SRCROOT)/Sources/SwiftKotlinApp/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; MACOSX_DEPLOYMENT_TARGET = 10.12; + MARKETING_VERSION = 0.2.3; MTL_ENABLE_DEBUG_INFO = NO; PRODUCT_BUNDLE_IDENTIFIER = com.angelolloqui.SwiftKotlinApp; PRODUCT_NAME = SwiftKotlin;