Skip to content

Commit

Permalink
Update logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Dec 15, 2022
1 parent 56e8db4 commit aa5efcf
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 195 deletions.
51 changes: 45 additions & 6 deletions src/swift/SwiftAPIViewCore/Sources/Models/APIViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,32 @@ class APIViewModel: Tokenizable, Encodable {
add(token: item)
}

func punctuation(_ value: String, prefixSpace: Bool = false, postfixSpace: Bool=false) {
func punctuation(_ value: String, spacing: SpacingKind) {
checkIndent()
if prefixSpace {
if spacing == .TrimLeft {
self.trim()
}
if [SpacingKind.Leading, SpacingKind.Both].contains(spacing) {
self.whitespace()
}
let item = Token(definitionId: nil, navigateToId: nil, value: value, kind: .punctuation)
add(token: item)
if postfixSpace {
if [SpacingKind.Trailing, SpacingKind.Both].contains(spacing) {
self.whitespace()
}
}

func keyword(_ keyword: String, prefixSpace: Bool = false, postfixSpace: Bool = false) {
func keyword(_ keyword: String, spacing: SpacingKind) {
checkIndent()
if prefixSpace {
if spacing == .TrimLeft {
self.trim()
}
if [SpacingKind.Leading, SpacingKind.Both].contains(spacing) {
self.whitespace()
}
let item = Token(definitionId: nil, navigateToId: nil, value: keyword, kind: .keyword)
add(token: item)
if postfixSpace {
if [SpacingKind.Trailing, SpacingKind.Both].contains(spacing) {
self.whitespace()
}
}
Expand Down Expand Up @@ -312,4 +318,37 @@ class APIViewModel: Tokenizable, Encodable {
definitionIds.insert(defId)
return defId
}

/// Trime whitespace tokens
func trim(removeNewlines: Bool = false) {
var lineIds = [Token]()
while (!tokens.isEmpty) {
var continueTrim = true
if let kind = tokens.last?.kind {
switch kind {
case .whitespace:
_ = tokens.popLast()
case .newline:
if removeNewlines {
_ = tokens.popLast()
}
case .lineIdMarker:
if let popped = tokens.popLast() {
lineIds.append(popped)
}
default:
continueTrim = false
}
}
if !continueTrim {
break
}
}
// reappend the line id tokens
while (!lineIds.isEmpty) {
if let popped = lineIds.popLast() {
tokens.append(popped)
}
}
}
}
36 changes: 11 additions & 25 deletions src/swift/SwiftAPIViewCore/Sources/Models/PackageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ class PackageModel: Tokenizable, Linkable {
a.text("package")
a.whitespace()
a.text(name, definitionId: definitionId)
a.punctuation("{", prefixSpace: true)
a.punctuation("{", spacing: SwiftSyntax.TokenKind.leftBrace.spacing)
a.newline()
a.indent {
self.tokenize(statements: statements, apiview: a)
}
a.punctuation("}")
a.punctuation("}", spacing: SwiftSyntax.TokenKind.rightBrace.spacing)
a.newline()
}

Expand All @@ -99,38 +99,24 @@ class PackageModel: Tokenizable, Linkable {
let tokenKind = item.tokenKind

if tokenKind.isKeyword {
a.keyword(item.withoutTrivia().description, prefixSpace: false, postfixSpace: true)
a.keyword(item.withoutTrivia().description, spacing: tokenKind.spacing)
return
} else if tokenKind.isPunctuation {
let punct = item.withoutTrivia().description
// don't render curly braces from the code
let punctIgnore = ["{", "}"]

guard !punctIgnore.contains(punct) else { return }
let spacing = tokenKind.spacing
switch spacing {
case .Leading:
a.punctuation(punct, prefixSpace: true, postfixSpace: false)
case .Trailing:
a.punctuation(punct, prefixSpace: false, postfixSpace: true)
case .Both:
a.punctuation(punct, prefixSpace: true, postfixSpace: true)
case .Neither:
a.punctuation(punct, prefixSpace: false, postfixSpace: false)
}
a.punctuation(punct, spacing: tokenKind.spacing)
return
}
if case let SwiftSyntax.TokenKind.identifier(val) = tokenKind {
// FIXME: should not be text... should be a member declaration or reference
a.text(val)
} else if case let SwiftSyntax.TokenKind.spacedBinaryOperator(val) = tokenKind {
a.punctuation(val, prefixSpace: true, postfixSpace: true)
a.punctuation(val, spacing: .Both)
} else if case let SwiftSyntax.TokenKind.unspacedBinaryOperator(val) = tokenKind {
a.punctuation(val, prefixSpace: false, postfixSpace: false)
a.punctuation(val, spacing: .Neither)
} else if case let SwiftSyntax.TokenKind.prefixOperator(val) = tokenKind {
a.punctuation(val, prefixSpace: true, postfixSpace: false)
a.punctuation(val, spacing: .Leading)
} else if case let SwiftSyntax.TokenKind.postfixOperator(val) = tokenKind {
a.punctuation(val, prefixSpace: false, postfixSpace: true)
a.punctuation(val, spacing: .Trailing)
} else if case let SwiftSyntax.TokenKind.floatingLiteral(val) = tokenKind {
a.literal(val)
} else if case let SwiftSyntax.TokenKind.regexLiteral(val) = tokenKind {
Expand All @@ -140,7 +126,7 @@ class PackageModel: Tokenizable, Linkable {
} else if case let SwiftSyntax.TokenKind.integerLiteral(val) = tokenKind {
a.literal(val)
} else if case let SwiftSyntax.TokenKind.contextualKeyword(val) = tokenKind {
a.keyword(val, prefixSpace: true, postfixSpace: true)
a.keyword(val, spacing: tokenKind.spacing)
} else if case let SwiftSyntax.TokenKind.stringSegment(val) = tokenKind {
a.text(val)
} else {
Expand All @@ -150,15 +136,15 @@ class PackageModel: Tokenizable, Linkable {
}
case .memberDeclBlock:
let item = MemberDeclBlockSyntax(element)!
a.punctuation("{", prefixSpace: true)
a.punctuation("{", spacing: SwiftSyntax.TokenKind.leftBrace.spacing)
a.newline()
a.indent {
for child in item.members {
tokenize(element: child, apiview: a)
a.blankLines(set: 0)
}
}
a.punctuation("}")
a.punctuation("}", spacing: SwiftSyntax.TokenKind.rightBrace.spacing)
case .codeBlockItem:
// Don't render code blocks
break
Expand Down
33 changes: 0 additions & 33 deletions src/swift/SwiftAPIViewCore/Sources/Protocols/Commentable.swift

This file was deleted.

31 changes: 0 additions & 31 deletions src/swift/SwiftAPIViewCore/Sources/Protocols/Extensible.swift

This file was deleted.

76 changes: 0 additions & 76 deletions src/swift/SwiftAPIViewCore/Sources/Protocols/TypeModel.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ enum SpacingKind {
case Both
/// No spacing
case Neither
/// chomps any leading whitespace to the left
case TrimLeft
}

extension SwiftSyntax.TokenKind {
Expand All @@ -47,7 +49,16 @@ extension SwiftSyntax.TokenKind {
case .semicolon: return .Trailing
case .equal: return .Both
case .arrow: return .Both
default: return .Neither
case .postfixQuestionMark: return .TrimLeft
case .leftBrace: return .Leading
case .leftAngle: return .TrimLeft
case .leftParen: return .TrimLeft
case .leftSquareBracket: return .TrimLeft
case .initKeyword: return .Leading
case let .contextualKeyword(val):
return val == "objc" ? .TrimLeft : .Both
default:
return self.isKeyword ? .Both : .Neither
}
}
}
Loading

0 comments on commit aa5efcf

Please sign in to comment.