Skip to content

Various visibility (and other) cleanup. #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/SwiftFormatConfiguration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Foundation

/// A version number that can be specified in the configuration file, which allows us to change the
/// format in the future if desired and still support older files.
private let highestSupportedConfigurationVersion = 1
fileprivate let highestSupportedConfigurationVersion = 1

/// Holds the complete set of configured values and defaults.
public struct Configuration: Codable, Equatable {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatCore/Rule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public protocol Rule {
init(context: Context)
}

private var nameCache = [ObjectIdentifier: String]()
fileprivate var nameCache = [ObjectIdentifier: String]()

extension Rule {
/// By default, the `ruleName` is just the name of the implementing rule class.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatCore/RuleMask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ extension SourceRange {
/// The rule status comment directives implementation intentionally supports exactly the same nodes
/// as `TokenStreamCreator` to disable pretty printing. This ensures ignore comments for pretty
/// printing and for rules are as consistent as possible.
private class RuleStatusCollectionVisitor: SyntaxVisitor {
fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
/// Describes the possible matches for ignore directives, in comments.
enum RuleStatusDirectiveMatch {
/// There is a directive that applies to all rules.
Expand Down
60 changes: 0 additions & 60 deletions Sources/SwiftFormatCore/SyntaxProtocol+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,66 +13,6 @@
import SwiftSyntax

extension SyntaxProtocol {
/// Walks up from the current node to find the nearest node that is an
/// Expr, Stmt, or Decl.
public var containingExprStmtOrDecl: Syntax? {
var node: Syntax? = Syntax(self)
while let parent = node?.parent {
if parent.is(ExprSyntax.self) || parent.is(StmtSyntax.self) || parent.is(DeclSyntax.self) {
return parent
}
node = parent
}
return nil
}

/// Returns true if the node occupies a single line.
///
/// - Parameters:
/// - includingLeadingComment: If true, factor any possible leading comments into the
/// determination of whether the node occupies a single line.
/// - sourceLocationConverter: Used to convert source positions to line/column locations.
/// - Returns: True if the node occupies a single line.
public func isSingleLine(
includingLeadingComment: Bool,
sourceLocationConverter: SourceLocationConverter
) -> Bool {
guard let firstToken = firstToken, let lastToken = lastToken else { return true }

let startPosition: AbsolutePosition
if includingLeadingComment {
// Iterate over the trivia, stopping at the first comment, and using that as the start
// position.
var currentPosition = firstToken.position
var sawNewline = false
loop: for piece in firstToken.leadingTrivia {
switch piece {
case .docLineComment,
.docBlockComment,
.lineComment where sawNewline,
.blockComment where sawNewline:
// Non-doc line or block comments before we've seen the first newline should actually be
// considered trailing comments of the previous line.
break loop
case .newlines, .carriageReturns, .carriageReturnLineFeeds:
sawNewline = true
fallthrough
default:
currentPosition += piece.sourceLength
}
}
startPosition = currentPosition
} else {
startPosition = firstToken.positionAfterSkippingLeadingTrivia
}

let startLocation = sourceLocationConverter.location(for: startPosition)
let endLocation = sourceLocationConverter.location(
for: lastToken.endPositionBeforeTrailingTrivia)

return startLocation.line == endLocation.line
}

/// Returns the absolute position of the trivia piece at the given index in the receiver's leading
/// trivia collection.
///
Expand Down
89 changes: 0 additions & 89 deletions Sources/SwiftFormatCore/Trivia+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,6 @@
import SwiftSyntax

extension Trivia {
/// Returns the number of non-newline whitespace characters in this trivia.
public var numberOfSpaces: Int {
var count = 0
for piece in self {
switch piece {
case .tabs(let n), .spaces(let n): count += n
default: continue
}
}
return count
}

/// Returns the number of newlines in this trivia.
public var numberOfNewlines: Int {
var count = 0
for piece in self {
if case .newlines(let n) = piece {
count += n
}
}
return count
}

/// Returns the number of leading newlines in this trivia.
public var numberOfLeadingNewlines: Int {
var count = 0
loop: for piece in self {
switch piece {
case .newlines(let n): count += n
case .carriageReturns(let n): count += n
case .carriageReturnLineFeeds(let n): count += n
default: break loop
}
}
return count
}

public var numberOfComments: Int {
var count = 0
for piece in self {
Expand All @@ -71,14 +34,6 @@ extension Trivia {
}
}

public var hasSpaces: Bool {
for piece in self {
if case .tabs = piece { return true }
if case .spaces = piece { return true }
}
return false
}

/// Returns this set of trivia, without any whitespace characters.
public func withoutSpaces() -> Trivia {
return Trivia(
Expand All @@ -89,50 +44,6 @@ extension Trivia {
})
}

/// Returns this set of trivia without any trailing whitespace characters.
public func withoutTrailingSpaces() -> Trivia {
var pieces = [TriviaPiece]()
guard var prev = first else { return self }
for piece in dropFirst() {
switch (prev, piece) {
case (.spaces(_), .newlines(_)),
(.tabs(_), .newlines(_)):
prev = piece
default:
pieces.append(prev)
prev = piece
}
}
pieces.append(prev)
return Trivia(pieces: pieces).condensed()
}

/// Returns this set of trivia, without any leading newline characters.
public func withoutLeadingNewlines() -> Trivia {
let triviaCondensed = self.condensed()
guard let firstPieceOfTrivia = triviaCondensed.first else { return self }
if case .newlines(_) = firstPieceOfTrivia {
var pieces = [TriviaPiece]()
for piece in triviaCondensed.dropFirst() {
pieces.append(piece)
}
return Trivia(pieces: pieces)
} else {
return self
}
}

/// Returns this set of trivia, without any trailing newline characters.
public func withoutTrailingNewlines() -> Trivia {
let triviaCondensed = self.condensed()
guard let lastPieceOfTrivia = triviaCondensed.suffix(1).first else { return self }
if case .newlines(_) = lastPieceOfTrivia {
return Trivia(pieces: triviaCondensed.dropLast())
} else {
return self
}
}

/// Returns this set of trivia, without any newlines.
public func withoutNewlines() -> Trivia {
return Trivia(
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatPrettyPrint/Comment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct Comment {

let kind: Kind
var text: [String]
public var length: Int
var length: Int

init(kind: Kind, text: String) {
self.kind = kind
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftFormatPrettyPrint/PrettyPrint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -779,12 +779,12 @@ public class PrettyPrinter {

extension Diagnostic.Message {

static let moveEndOfLineComment = Diagnostic.Message(
public static let moveEndOfLineComment = Diagnostic.Message(
.warning, "move end-of-line comment that exceeds the line length")

static let addTrailingComma = Diagnostic.Message(
public static let addTrailingComma = Diagnostic.Message(
.warning, "add trailing comma to the last element in multiline collection literal")

static let removeTrailingComma = Diagnostic.Message(
public static let removeTrailingComma = Diagnostic.Message(
.warning, "remove trailing comma from the last element in single line collection literal")
}
4 changes: 0 additions & 4 deletions Sources/SwiftFormatPrettyPrint/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
//
//===----------------------------------------------------------------------===//

import SwiftFormatConfiguration
import SwiftFormatCore
import SwiftSyntax

enum GroupBreakStyle {
/// A consistent break indicates that the break will always be finalized as a newline
/// if wrapping occurs.
Expand Down
14 changes: 4 additions & 10 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import SwiftSyntax

/// Visits the nodes of a syntax tree and constructs a linear stream of formatting tokens that
/// tell the pretty printer how the source text should be laid out.
private final class TokenStreamCreator: SyntaxVisitor {
fileprivate final class TokenStreamCreator: SyntaxVisitor {
private var tokens = [Token]()
private var beforeMap = [TokenSyntax: [Token]]()
private var afterMap = [TokenSyntax: [[Token]]]()
Expand Down Expand Up @@ -2435,7 +2435,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
{
let nextToken = token.nextToken
guard let trivia = nextToken?.leadingTrivia,
let firstPiece = trivia[safe: 0]
let firstPiece = trivia.first
else {
return (false, [])
}
Expand Down Expand Up @@ -3186,12 +3186,6 @@ class CommentMovingRewriter: SyntaxRewriter {
}
}

extension Collection {
subscript(safe index: Index) -> Element? {
return index < endIndex ? self[index] : nil
}
}

extension TriviaPiece {
/// True if the trivia piece is garbage text.
fileprivate var isGarbageText: Bool {
Expand All @@ -3205,7 +3199,7 @@ extension TriviaPiece {
/// Returns whether the given trivia includes a directive to ignore formatting for the next node.
///
/// - Parameter trivia: Leading trivia for a node that the formatter supports ignoring.
private func isFormatterIgnorePresent(inTrivia trivia: Trivia) -> Bool {
fileprivate func isFormatterIgnorePresent(inTrivia trivia: Trivia) -> Bool {
func isFormatterIgnore(in commentText: String, prefix: String, suffix: String) -> Bool {
let trimmed =
commentText.dropFirst(prefix.count)
Expand Down Expand Up @@ -3237,7 +3231,7 @@ private func isFormatterIgnorePresent(inTrivia trivia: Trivia) -> Bool {
/// be safely ignored.
///
/// - Parameter node: A node that can be safely ignored.
private func shouldFormatterIgnore(node: Syntax) -> Bool {
fileprivate func shouldFormatterIgnore(node: Syntax) -> Bool {
// Regardless of the level of nesting, if the ignore directive is present on the first token
// contained within the node then the entire node is eligible for ignoring.
if let firstTrivia = node.firstToken?.leadingTrivia {
Expand Down
7 changes: 3 additions & 4 deletions Sources/SwiftFormatRules/AddModifierRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
//
//===----------------------------------------------------------------------===//

import Foundation
import SwiftFormatCore
import SwiftSyntax

private final class AddModifierRewriter: SyntaxRewriter {
let modifierKeyword: DeclModifierSyntax
fileprivate final class AddModifierRewriter: SyntaxRewriter {
private let modifierKeyword: DeclModifierSyntax

init(modifierKeyword: DeclModifierSyntax) {
self.modifierKeyword = modifierKeyword
Expand Down Expand Up @@ -162,7 +161,7 @@ private final class AddModifierRewriter: SyntaxRewriter {
/// this method does nothing and returns the given node as-is.
/// - Parameter node: A node that was updated to include a new modifier.
/// - Parameter modifiersProvider: A closure that returns all modifiers for the given node.
func nodeByRelocatingTrivia<NodeType: DeclSyntaxProtocol>(
private func nodeByRelocatingTrivia<NodeType: DeclSyntaxProtocol>(
in node: NodeType,
for modifiersProvider: (NodeType) -> ModifierListSyntax?
) -> NodeType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//
//===----------------------------------------------------------------------===//

import Foundation
import SwiftFormatCore
import SwiftSyntax

Expand Down Expand Up @@ -65,7 +64,7 @@ public final class AllPublicDeclarationsHaveDocumentation: SyntaxLintRule {
return .skipChildren
}

func diagnoseMissingDocComment(
private func diagnoseMissingDocComment(
_ decl: DeclSyntax,
name: String,
modifiers: ModifierListSyntax?
Expand All @@ -83,7 +82,7 @@ public final class AllPublicDeclarationsHaveDocumentation: SyntaxLintRule {
}

extension Diagnostic.Message {
static func declRequiresComment(_ name: String) -> Diagnostic.Message {
public static func declRequiresComment(_ name: String) -> Diagnostic.Message {
return .init(.warning, "add a documentation comment for '\(name)'")
}
}
5 changes: 2 additions & 3 deletions Sources/SwiftFormatRules/AlwaysUseLowerCamelCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//
//===----------------------------------------------------------------------===//

import Foundation
import SwiftFormatCore
import SwiftSyntax

Expand Down Expand Up @@ -41,7 +40,7 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
return .skipChildren
}

func diagnoseLowerCamelCaseViolations(_ identifier: TokenSyntax) {
private func diagnoseLowerCamelCaseViolations(_ identifier: TokenSyntax) {
guard case .identifier(let text) = identifier.tokenKind else { return }
if text.isEmpty { return }
if text.dropFirst().contains("_") || ("A"..."Z").contains(text.first!) {
Expand All @@ -53,7 +52,7 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
}

extension Diagnostic.Message {
static func variableNameMustBeLowerCamelCase(_ name: String) -> Diagnostic.Message {
public static func variableNameMustBeLowerCamelCase(_ name: String) -> Diagnostic.Message {
return .init(.warning, "rename variable '\(name)' using lower-camel-case")
}
}
Loading