Skip to content

Commit

Permalink
#101 Fixed type inheritance on structs
Browse files Browse the repository at this point in the history
  • Loading branch information
angelolloqui committed Mar 21, 2020
1 parent 7d1c0bf commit 158cd94
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
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
}
24 changes: 16 additions & 8 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

0 comments on commit 158cd94

Please sign in to comment.