Skip to content

Commit

Permalink
Make test text work correctly for children.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Dec 13, 2024
1 parent 8c73275 commit 6fb95db
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 47 deletions.
8 changes: 5 additions & 3 deletions src/swift/SwiftAPIViewCore/Sources/Models/APIViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ class APIViewModel: Tokenizable, Encodable {

/// Returns the text-based representation of all lines
var text: String {
return reviewLines.map { line in
var value = ""
for line in reviewLines {
let lineText = line.text()
return lineText
}.joined()
value += lineText
}
return value
}

// MARK: Initializers
Expand Down
12 changes: 2 additions & 10 deletions src/swift/SwiftAPIViewCore/Sources/Models/DeclarationModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ class DeclarationModel: Tokenizable, Linkable, Equatable {
let attrText = attr.withoutTrivia().description.filter { !$0.isWhitespace }
a.lineMarker("\(definitionId!).\(attrText)")
attr.tokenize(apiview: a, parent: parent)
// FIXME: Newline
// a.newline()
// a.blankLines(set: 0)
a.blankLines(set: 0)
}
case .token:
if child.withoutTrivia().description == self.name {
Expand Down Expand Up @@ -195,13 +193,7 @@ class DeclarationModel: Tokenizable, Linkable, Equatable {
}
}
if !extensions.isEmpty {
// FIXME: Newline
//a.blankLines(set: 1)
for ext in extensions {
ext.tokenize(apiview: a, parent: self)
// FIXME: Newline
//a.blankLines(set: 1)
}
extensions.tokenize(apiview: a, parent: self)
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/swift/SwiftAPIViewCore/Sources/Models/ExtensionModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ class ExtensionModel: Tokenizable {
}

extension Array<ExtensionModel> {
func tokenize(apiview a: APIViewModel, parent: Linkable?) {
a.blankLines(set: 1)
let lastIdx = self.count - 1
for (idx, ext) in self.enumerated() {
ext.tokenize(apiview: a, parent: parent)
if idx != lastIdx {
a.blankLines(set: 1)
}
}
}

func resolveDuplicates() -> [ExtensionModel] {
var resolved = [String: ExtensionModel]()
for ext in self {
Expand Down
23 changes: 5 additions & 18 deletions src/swift/SwiftAPIViewCore/Sources/Models/PackageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,34 +110,21 @@ class PackageModel: Tokenizable, Linkable {
options.applySpacing(SwiftSyntax.TokenKind.leftBrace.spacing)
a.punctuation("{", options: options)
a.indent {
// FIXME: Newline
//a.blankLines(set: 0)
a.blankLines(set: 0)
for member in members {
member.tokenize(apiview: a, parent: self)
// FIXME: Newline
//a.blankLines(set: 1)
a.blankLines(set: 1)
}
// render any orphaned extensions
if !extensions.isEmpty {
a.comment("Non-package extensions")
// FIXME: Newline
//a.newline()
let endIdx = extensions.count - 1
for (idx, ext) in extensions.enumerated() {
ext.tokenize(apiview: a, parent: nil)
if idx != endIdx {
// FIXME: Newline
//a.blankLines(set: 1)
}
}
extensions.tokenize(apiview: a, parent: nil)
}
}
options.applySpacing(SwiftSyntax.TokenKind.rightBrace.spacing)
// FIXME: Newline
//a.blankLines(set: 0)
a.blankLines(set: 0)
a.punctuation("}", options: options)
// FIXME: Newline
// a.newline()
a.newline()
resolveTypeReferences(apiview: a)
}

Expand Down
6 changes: 4 additions & 2 deletions src/swift/SwiftAPIViewCore/Sources/Models/ReviewLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ class ReviewLine: Tokenizable, Encodable {
for token in tokens {
value += token.text(withPreview: value)
}
if tokens.count > 0 {
value += "\n"
}
let childrenLines = self.children.map { $0.text(indent: indent + 2) }
for line in childrenLines {
value += "\(line)\n"
value += line
}
value += "\n"
return value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,10 @@ extension SyntaxProtocol {
for (idx, child) in children.enumerated() {
let beforeCount = a.currentLine.tokens.count
child.tokenize(apiview: a, parent: parent)
// no blank lines for the last member, or if tokenizing didn't
// actually add anything
if (idx != lastIdx && a.currentLine.tokens.count > beforeCount) {
// FIXME: Newline
//a.blankLines(set: 1)
}
// skip if no tokens were actually added
guard (a.currentLine.tokens.count > beforeCount) else { continue }
// add 1 blank line, except for the last member
a.blankLines(set: idx != lastIdx ? 1 : 0)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
0A61B5242D0B764600FC6B19 /* ReviewLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A61B5202D0B764600FC6B19 /* ReviewLine.swift */; };
0A61B5272D0B76C400FC6B19 /* NamespaceStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A61B5252D0B76C400FC6B19 /* NamespaceStack.swift */; };
0A61B5282D0B76C400FC6B19 /* ReviewTokenOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A61B5262D0B76C400FC6B19 /* ReviewTokenOptions.swift */; };
0A61B52A2D0CADF300FC6B19 /* UtilTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A61B5292D0CADE300FC6B19 /* UtilTests.swift */; };
0A61B52A2D0CADF300FC6B19 /* TestUtilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A61B5292D0CADE300FC6B19 /* TestUtilityTests.swift */; };
0A6C658A292D9EA00075C56F /* APIViewManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A846A1927879D0400C967A8 /* APIViewManager.swift */; };
0A6C658B292D9ED60075C56F /* CommandLineArguments.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A846A0827879D0400C967A8 /* CommandLineArguments.swift */; };
0A6C658C292D9ED60075C56F /* Errors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A846A0C27879D0400C967A8 /* Errors.swift */; };
Expand Down Expand Up @@ -87,7 +87,7 @@
0A61B5212D0B764600FC6B19 /* ReviewToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReviewToken.swift; sourceTree = "<group>"; };
0A61B5252D0B76C400FC6B19 /* NamespaceStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NamespaceStack.swift; sourceTree = "<group>"; };
0A61B5262D0B76C400FC6B19 /* ReviewTokenOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReviewTokenOptions.swift; sourceTree = "<group>"; };
0A61B5292D0CADE300FC6B19 /* UtilTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UtilTests.swift; sourceTree = "<group>"; };
0A61B5292D0CADE300FC6B19 /* TestUtilityTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUtilityTests.swift; sourceTree = "<group>"; };
0A76BF8A294A9A11007C776E /* TokenKind+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TokenKind+Extensions.swift"; sourceTree = "<group>"; };
0A76BF8E294B8BCD007C776E /* SyntaxProtocol+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SyntaxProtocol+Extensions.swift"; sourceTree = "<group>"; };
0A76BF90294B940A007C776E /* DeclarationModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeclarationModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -212,7 +212,7 @@
B2DA7E212D0A0A6F0059E51F /* TestFiles */,
B2DA7DFF2D0A0A6A0059E51F /* ExpectFiles */,
0A846A0327879D0400C967A8 /* SwiftAPIViewCoreTests.swift */,
0A61B5292D0CADE300FC6B19 /* UtilTests.swift */,
0A61B5292D0CADE300FC6B19 /* TestUtilityTests.swift */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -477,7 +477,7 @@
buildActionMask = 2147483647;
files = (
0A846A2F27879E7800C967A8 /* SwiftAPIViewCoreTests.swift in Sources */,
0A61B52A2D0CADF300FC6B19 /* UtilTests.swift in Sources */,
0A61B52A2D0CADF300FC6B19 /* TestUtilityTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import XCTest
@testable import SwiftAPIViewCore

class UtilTests: XCTestCase {
class TestUtilityTests: XCTestCase {

override func setUpWithError() throws {
try super.setUpWithError()
Expand Down Expand Up @@ -73,11 +73,13 @@ class UtilTests: XCTestCase {
line2.tokens = [ReviewToken(kind: .text, value: "public class Foo()", options: options)]
let child1 = ReviewLine()
child1.tokens = [ReviewToken(kind: .text, value: "func getFoo() -> Foo", options: options)]
line2.children = [child1]

let child2 = ReviewLine()
let child3 = ReviewLine()
child3.tokens = [ReviewToken(kind: .text, value: "func setFoo(_: Foo)", options: options)]
line2.children = [child1, child2, child3]
model.reviewLines = [line1, ReviewLine(), ReviewLine(), line2]
let generated = model.text
let expected = "Some text\n\n\npublic class Foo()\n func getFoo() -> Foo\n\n"
let expected = "Some text\n\n\npublic class Foo()\n func getFoo() -> Foo\n\n func setFoo(_: Foo)\n"
compare(expected: expected, actual: generated)
}
}

0 comments on commit 6fb95db

Please sign in to comment.