Skip to content

Commit

Permalink
Added writeCommentLine and support to group with the next line.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Oct 30, 2023
1 parent a2f322b commit 551eea6
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 18 deletions.
23 changes: 13 additions & 10 deletions Generator/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@ let package = Package(
.package(url: "https://github.com/tristanlabelle/swift-dotnetmetadata", branch: "main")
],
targets: [
// Code generator
.target(
name: "CodeWriters",
path: "Sources/CodeWriters"),
.target(
name: "ProjectionGenerator",
dependencies: [
"CodeWriters",
.product(name: "Collections", package: "swift-collections"),
.product(name: "DotNetMetadata", package: "swift-dotnetmetadata")
],
path: "Sources/ProjectionGenerator"),
.executableTarget(
name: "SwiftWinRT",
dependencies: [
"ProjectionGenerator",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Collections", package: "swift-collections"),
.product(name: "DotNetMetadata", package: "swift-dotnetmetadata"),
"ProjectionGenerator"
.product(name: "DotNetMetadata", package: "swift-dotnetmetadata")
],
path: "Sources/SwiftWinRT",
// Workaround for SPM library support limitations causing "LNK4217: locally defined symbol imported" spew
linkerSettings: [ .unsafeFlags(["-Xlinker", "-ignore:4217"]) ]),
.target(
name: "CodeWriters",
path: "Sources/CodeWriters"),
.target(
name: "ProjectionGenerator",
dependencies: [ "CodeWriters" ],
path: "Sources/ProjectionGenerator"),
.testTarget(
name: "Tests",
dependencies: [ "CodeWriters", "ProjectionGenerator" ],
Expand Down
6 changes: 4 additions & 2 deletions Generator/Sources/CodeWriters/IndentedTextOutputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ public class IndentedTextOutputStream: TextOutputStream {
return .withGroup(AnonymousVerticalGroup(id: lastAllocatedGroupID))
}

public func writeLine(grouping: VerticalGrouping = .withDefault, _ str: String = "") {
public func writeLine(grouping: VerticalGrouping = .withDefault, _ str: String = "", groupWithNext: Bool = false) {
beginLine(grouping: grouping)
write(str, endLine: true)
if groupWithNext { lineGrouping = nil }
}

private func write(_ str: Substring) {
Expand Down Expand Up @@ -128,8 +129,9 @@ public class IndentedTextOutputStream: TextOutputStream {
}
}

public func endLine() {
public func endLine(groupWithNext: Bool = false) {
lineState = .end
if groupWithNext { lineGrouping = nil }
}

public func writeIndentedBlock(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ extension IndentedTextOutputStream {
}

extension SwiftSyntaxWriter {
public func writeCommentLine(_ comment: String, groupWithNext: Bool = true) {
output.beginLine(grouping: .never)
output.write("// ")
output.write(comment)
output.endLine(groupWithNext: groupWithNext)
}

internal func writeTypeParameters(_ typeParameters: [String]) {
guard !typeParameters.isEmpty else { return }
var output = output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ extension SwiftAssemblyModuleFileWriter {
}

fileprivate func writeDefaultInitializer(_ classDefinition: ClassDefinition, to writer: SwiftTypeDefinitionWriter) throws {
writer.output.writeLine("// IActivationFactory")
writer.writeCommentLine("IActivationFactory")

// 00000035-0000-0000-C000-000000000046
let iactivationFactoryID = UUID(uuid: (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ extension SwiftAssemblyModuleFileWriter {

var nonDefaultInterfaceStoredProperties = [String]()
for interface in recursiveInterfaces {
try writer.output.writeLine("// \(WinRTTypeName.from(type: interface.asBoundType).description)")
try writer.writeCommentLine(WinRTTypeName.from(type: interface.asBoundType).description)
if interface == defaultInterface {
try writeMemberImplementations(
interfaceOrDelegate: interface.asBoundType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public struct SwiftAssemblyModuleFileWriter {
self.sourceFileWriter = SwiftSourceFileWriter(output: FileTextOutputStream(path: path))
self.module = module

sourceFileWriter.output.writeLine("// Generated by swift-winrt")
sourceFileWriter.output.writeLine("// swiftlint:disable all")
sourceFileWriter.writeCommentLine("Generated by swift-winrt")
sourceFileWriter.writeCommentLine("swiftlint:disable all", groupWithNext: false)

if importAbiModule {
sourceFileWriter.writeImport(module: projection.abiModuleName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public struct SwiftNamespaceModuleFileWriter {
self.sourceFileWriter = SwiftSourceFileWriter(output: FileTextOutputStream(path: path))
self.module = module

sourceFileWriter.output.writeLine("// Generated by swift-winrt")
sourceFileWriter.output.writeLine("// swiftlint:disable all")
sourceFileWriter.writeCommentLine("Generated by swift-winrt")
sourceFileWriter.writeCommentLine("swiftlint:disable all", groupWithNext: false)

sourceFileWriter.writeImport(module: module.assemblyModuleName)
}
Expand Down

0 comments on commit 551eea6

Please sign in to comment.