From 8d6615cf23eeeb8f4d6f4af4ad29358b06c0ac93 Mon Sep 17 00:00:00 2001 From: Angel Garcia Date: Fri, 22 Jun 2018 22:12:36 +0200 Subject: [PATCH] #36 WIP preserve code comments --- Sources/SwiftKotlinApp/ViewController.swift | 9 +- Sources/SwiftKotlinCommandLine/main.swift | 3 +- .../CommentsAdditionTransformPlugin.swift | 96 +++++++++++++++++++ SwiftKotlin.xcodeproj/project.pbxproj | 24 +---- 4 files changed, 109 insertions(+), 23 deletions(-) create mode 100644 Sources/SwiftKotlinFramework/plugins/CommentsAdditionTransformPlugin.swift diff --git a/Sources/SwiftKotlinApp/ViewController.swift b/Sources/SwiftKotlinApp/ViewController.swift index ca12f0d..ec450a9 100644 --- a/Sources/SwiftKotlinApp/ViewController.swift +++ b/Sources/SwiftKotlinApp/ViewController.swift @@ -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() ] ) diff --git a/Sources/SwiftKotlinCommandLine/main.swift b/Sources/SwiftKotlinCommandLine/main.swift index 8dc05cf..a2921e2 100644 --- a/Sources/SwiftKotlinCommandLine/main.swift +++ b/Sources/SwiftKotlinCommandLine/main.swift @@ -12,7 +12,8 @@ import SwiftKotlinFramework let kotlinTokenizer = KotlinTokenizer( tokenTransformPlugins: [ XCTTestToJUnitTokenTransformPlugin(), - FoundationMethodsTransformPlugin() + FoundationMethodsTransformPlugin(), + CommentsAdditionTransformPlugin() ] ) let version = "0.1.4" diff --git a/Sources/SwiftKotlinFramework/plugins/CommentsAdditionTransformPlugin.swift b/Sources/SwiftKotlinFramework/plugins/CommentsAdditionTransformPlugin.swift new file mode 100644 index 0000000..27ada18 --- /dev/null +++ b/Sources/SwiftKotlinFramework/plugins/CommentsAdditionTransformPlugin.swift @@ -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) + } +} diff --git a/SwiftKotlin.xcodeproj/project.pbxproj b/SwiftKotlin.xcodeproj/project.pbxproj index 2994d33..7082dd9 100644 --- a/SwiftKotlin.xcodeproj/project.pbxproj +++ b/SwiftKotlin.xcodeproj/project.pbxproj @@ -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 */; }; @@ -1029,6 +1030,7 @@ 78CAB0D41F9283D6009E2608 /* Tests */ = {isa = PBXFileReference; lastKnownFileType = folder; path = Tests; sourceTree = ""; }; 78CAB0D51F92843C009E2608 /* TransformPluginTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransformPluginTests.swift; sourceTree = ""; }; 78FE797E1FDDAB5900B64A2C /* FoundationMethodsTransformPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoundationMethodsTransformPlugin.swift; sourceTree = ""; }; + D5D4A11220DD723A000AD25C /* CommentsAdditionTransformPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentsAdditionTransformPlugin.swift; sourceTree = ""; }; OBJ_100 /* AssignmentOperatorExpression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssignmentOperatorExpression.swift; sourceTree = ""; }; OBJ_101 /* BinaryOperatorExpression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryOperatorExpression.swift; sourceTree = ""; }; OBJ_102 /* ClosureExpression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClosureExpression.swift; sourceTree = ""; }; @@ -1447,6 +1449,7 @@ 789D78F91F92792E009ED628 /* TransformPlugin.swift */, 78CAB0CE1F92808E009E2608 /* XCTTestToJUnitTokenTransformPlugin.swift */, 78FE797E1FDDAB5900B64A2C /* FoundationMethodsTransformPlugin.swift */, + D5D4A11220DD723A000AD25C /* CommentsAdditionTransformPlugin.swift */, ); path = plugins; sourceTree = ""; @@ -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; }; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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;