Skip to content

Commit

Permalink
#36 WIP preserve code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel Garcia authored and Angel Garcia committed Jun 22, 2018
1 parent 0009e04 commit 8d6615c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 23 deletions.
9 changes: 7 additions & 2 deletions Sources/SwiftKotlinApp/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import Transform

class ViewController: NSViewController {

let swiftTokenizer = SwiftTokenizer()
let swiftTokenizer = SwiftTokenizer(
tokenTransformPlugins: [
CommentsAdditionTransformPlugin()
]
)
let kotlinTokenizer = KotlinTokenizer(
tokenTransformPlugins: [
XCTTestToJUnitTokenTransformPlugin(),
FoundationMethodsTransformPlugin()
FoundationMethodsTransformPlugin(),
CommentsAdditionTransformPlugin()
]
)

Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftKotlinCommandLine/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import SwiftKotlinFramework
let kotlinTokenizer = KotlinTokenizer(
tokenTransformPlugins: [
XCTTestToJUnitTokenTransformPlugin(),
FoundationMethodsTransformPlugin()
FoundationMethodsTransformPlugin(),
CommentsAdditionTransformPlugin()
]
)
let version = "0.1.4"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// CommentsAdditionTransformPlugin.swift
// SwiftKotlinPackageDescription
//
// Created by Angel Garcia on 22/06/2018.
//

import Foundation
import Transform
import AST
import Source

public class CommentsAdditionTransformPlugin: TokenTransformPlugin {
public var name: String {
return "Comments addition"
}

public var description: String {
return "Adds parse comments to closest generated element"
}

public init() {}

public func transform(tokens: [Token], topDeclaration: TopLevelDeclaration) throws -> [Token] {
var newTokens = [Token]()
var sortedComments = topDeclaration.comments.sorted { $0.location.line < $1.location.line }

var position = 0
while position < tokens.count && !sortedComments.isEmpty {
let token = tokens[position]
let comment = sortedComments[0]
var consumeComment = false

if let tokenRange = token.sourceRange,
tokenRange.isValid {

if tokenRange.start.isAfter(location: comment.location) {
consumeComment = true
}
}

if consumeComment, let node = token.node {
newTokens.append(node.newToken(.comment, comment.fomattedContent()))
sortedComments.removeFirst()
} else {
newTokens.append(token)
position += 1
}
}

newTokens += tokens[position...]

while !sortedComments.isEmpty {
let comment = sortedComments[0]
newTokens.append(topDeclaration.newToken(.comment, comment.fomattedContent()))
sortedComments.removeFirst()
}

return newTokens
}
}

extension Comment {
func fomattedContent() -> String {
if content.contains("\n") {
return "/*\(content)*/\n"
} else {
return "//\(content)\n"
}
}
}

extension Token {
var sourceRange: SourceRange? {
return (origin as? SourceLocatable)?.sourceRange
}
}

extension SourceRange {
func contains(location: SourceLocation) -> Bool {
return start.isBefore(location: location) && end.isAfter(location: location)
}
}

extension SourceLocation {
func isBefore(location: SourceLocation) -> Bool {
guard line != location.line else {
return column < location.column
}
return line < location.line
}

func isAfter(location: SourceLocation) -> Bool {
return !isBefore(location: location)
}
}
24 changes: 4 additions & 20 deletions SwiftKotlin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
78CAB0CF1F92808E009E2608 /* XCTTestToJUnitTokenTransformPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78CAB0CE1F92808E009E2608 /* XCTTestToJUnitTokenTransformPlugin.swift */; };
78CAB0D61F92843C009E2608 /* TransformPluginTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78CAB0D51F92843C009E2608 /* TransformPluginTests.swift */; };
78FE79801FDDABC000B64A2C /* FoundationMethodsTransformPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78FE797E1FDDAB5900B64A2C /* FoundationMethodsTransformPlugin.swift */; };
D5D4A11420DD7351000AD25C /* CommentsAdditionTransformPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5D4A11220DD723A000AD25C /* CommentsAdditionTransformPlugin.swift */; };
OBJ_265 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; };
OBJ_271 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_39 /* Package.swift */; };
OBJ_277 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_230 /* Package.swift */; };
Expand Down Expand Up @@ -1029,6 +1030,7 @@
78CAB0D41F9283D6009E2608 /* Tests */ = {isa = PBXFileReference; lastKnownFileType = folder; path = Tests; sourceTree = "<group>"; };
78CAB0D51F92843C009E2608 /* TransformPluginTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransformPluginTests.swift; sourceTree = "<group>"; };
78FE797E1FDDAB5900B64A2C /* FoundationMethodsTransformPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoundationMethodsTransformPlugin.swift; sourceTree = "<group>"; };
D5D4A11220DD723A000AD25C /* CommentsAdditionTransformPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentsAdditionTransformPlugin.swift; sourceTree = "<group>"; };
OBJ_100 /* AssignmentOperatorExpression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssignmentOperatorExpression.swift; sourceTree = "<group>"; };
OBJ_101 /* BinaryOperatorExpression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryOperatorExpression.swift; sourceTree = "<group>"; };
OBJ_102 /* ClosureExpression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClosureExpression.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1447,6 +1449,7 @@
789D78F91F92792E009ED628 /* TransformPlugin.swift */,
78CAB0CE1F92808E009E2608 /* XCTTestToJUnitTokenTransformPlugin.swift */,
78FE797E1FDDAB5900B64A2C /* FoundationMethodsTransformPlugin.swift */,
D5D4A11220DD723A000AD25C /* CommentsAdditionTransformPlugin.swift */,
);
path = plugins;
sourceTree = "<group>";
Expand Down Expand Up @@ -2512,6 +2515,7 @@
789D78FE1F9279BF009ED628 /* SwiftTokenizer.swift in Sources */,
789D79041F927B63009ED628 /* AST+Operations.swift in Sources */,
789D79031F927B5F009ED628 /* Token+Operations.swift in Sources */,
D5D4A11420DD7351000AD25C /* CommentsAdditionTransformPlugin.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -3544,7 +3548,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/SwiftKotlinFramework_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = SwiftKotlinFramework;
Expand All @@ -3568,7 +3571,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/SwiftKotlinFramework_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = SwiftKotlinFramework;
Expand Down Expand Up @@ -3650,7 +3652,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Transform_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Transform;
Expand All @@ -3674,7 +3675,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Transform_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Transform;
Expand All @@ -3698,7 +3698,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Tooling_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Tooling;
Expand All @@ -3722,7 +3721,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Tooling_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Tooling;
Expand All @@ -3746,7 +3744,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Sema_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Sema;
Expand All @@ -3770,7 +3767,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Sema_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Sema;
Expand All @@ -3794,7 +3790,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Parser_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Parser;
Expand All @@ -3818,7 +3813,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Parser_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Parser;
Expand All @@ -3842,7 +3836,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Lexer_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Lexer;
Expand All @@ -3866,7 +3859,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Lexer_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Lexer;
Expand All @@ -3890,7 +3882,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/AST_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = AST;
Expand All @@ -3914,7 +3905,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/AST_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = AST;
Expand All @@ -3938,7 +3928,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Diagnostic_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Diagnostic;
Expand All @@ -3962,7 +3951,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Diagnostic_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Diagnostic;
Expand All @@ -3986,7 +3974,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Source_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Source;
Expand All @@ -4010,7 +3997,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Source_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Source;
Expand All @@ -4034,7 +4020,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Bocho_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Bocho;
Expand All @@ -4058,7 +4043,6 @@
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = SwiftKotlin.xcodeproj/Bocho_Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACH_O_TYPE = staticlib;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Bocho;
Expand Down

0 comments on commit 8d6615c

Please sign in to comment.