Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Sep 19, 2023
2 parents 93d883e + 7350642 commit 93830c2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
29 changes: 25 additions & 4 deletions Plugins/Libraries/LighterCodeGenAST/Generation/GenLiterals.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Created by Helge Heß.
// Copyright © 2022 ZeeZide GmbH.
// Copyright © 2022-2023 ZeeZide GmbH.
//

import Foundation
Expand Down Expand Up @@ -32,9 +32,13 @@ public extension CodeGenerator {
assert(!pounds.isEmpty)
// ##"Hello # "##

if value.contains("\n") { // meh
let indent =
String(repeating: configuration.indent, count: indentationLevel + 1)
if value.isMultilineASCIIString { // Meh
// We don't want CRs in beautiful Swift code.
let value = value.replacingOccurrences(of: "\r\n", with: "\n")
let indent = String(
repeating: configuration.indent,
count: indentationLevel + 1
)
var s = "\n"
s += "\(indent)\(pounds)\"\"\"\n"
for line in value.split(separator: "\n",
Expand All @@ -52,6 +56,7 @@ public extension CodeGenerator {
else { // complete me :-)
let escaped = value
.replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "\r", with: "\\r")
.replacingOccurrences(of: "\n", with: "\\n")
.replacingOccurrences(of: "\t", with: "\\t")
.replacingOccurrences(of: "\"", with: "\\\"")
Expand Down Expand Up @@ -79,3 +84,19 @@ fileprivate let unsafeSwiftStringLiteralCharacters : CharacterSet = {
safeCharacters.formUnion(.init(charactersIn: "_,;&'@#!$(){}+-*/:."))
return safeCharacters.inverted
}()

extension String {

/// Returns true if the string is a multiline string, i.e. contains LF or CR.
/// Does *not* consider arbitrary newline Unicode characters.
var isMultilineASCIIString : Bool {
// Note: Don't do this: `self.contains("\n")` (Issue #23).
// If the String contains CR LF (`\r\n`, ASCII 13,10), this doesn't
// match! `\r\n` is turned into a Unicode composed character in the
// Swift String. Which interestingly has an `asciiValue`, 10 and returns
// `true` on `isASCII`.
// This does work: `self.contains(where: \.isNewline)`, but in here
// we really want just the ASCII codes for source.
self.utf8.contains(where: { $0 == 10 /* NL */ || $0 == 13 /* CR */ })
}
}
4 changes: 2 additions & 2 deletions Plugins/Libraries/LighterCodeGenAST/Nodes/TypeComment.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Created by Helge Heß.
// Copyright © 2022 ZeeZide GmbH.
// Copyright © 2022-2023 ZeeZide GmbH.
//

/// A comment on top of e.g. a class or struct.
Expand All @@ -19,7 +19,7 @@ public struct TypeComment: Equatable {
/// Initialize a new type example.
public init(headline: String, code: String, language: String? = "swift") {
self.headline = headline
self.code = code
self.code = code.replacingOccurrences(of: "\r\n", with: "\n")
self.language = language
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/EntityGenTests/ASTRecordMatcherGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ final class ASTRecordMatcherGenerationTests: XCTestCase {
builder.generateExtension(e)
return builder.source
}()
print("GOT:\n-----\n\(source)\n-----")
//print("GOT:\n-----\n\(source)\n-----")

XCTAssertTrue(source.contains("func registerSwiftMatcher("))
XCTAssertTrue(source.contains("func unregisterSwiftMatcher("))
Expand Down

0 comments on commit 93830c2

Please sign in to comment.