Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#101 Struct type inheritance #107

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/KotlinTokenizer/foundation_types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ var map: Promise<Map<Int, String>>? = null
var map: Map<Int, Promise<Map<String, String>>>
var map = mapOf(1 to "a", 2 to "b")
var map = mapOf<String , String>()
method(value = listOf("value1", "value"))
1 change: 1 addition & 0 deletions Assets/Tests/KotlinTokenizer/foundation_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
8 changes: 8 additions & 0 deletions Assets/Tests/KotlinTokenizer/structs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}
9 changes: 9 additions & 0 deletions Assets/Tests/KotlinTokenizer/structs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ struct Person {

func eat() {}
}

struct User: Codable {
struct Content {
let text: String
}
var id: Int? = 0
var name: String?
var content: Content
}
33 changes: 20 additions & 13 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand All @@ -166,6 +159,21 @@ public class KotlinTokenizer: SwiftTokenizer {
at: bodyStart - 1)
}

if let typeInheritanceList = declaration.typeInheritanceClause?.typeInheritanceList,
!typeInheritanceList.isEmpty,
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
}

Expand Down Expand Up @@ -588,12 +596,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, ", "))
Expand Down