Skip to content

Fix incorrect indentation free functions. #115

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 1 commit into from
Jan 9, 2020
Merged
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
84 changes: 42 additions & 42 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2865,45 +2865,45 @@ extension Collection {
}

/// 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 {
func isFormatterIgnore(in commentText: String, prefix: String, suffix: String) -> Bool {
let trimmed =
commentText.dropFirst(prefix.count)
.dropLast(suffix.count)
.trimmingCharacters(in: .whitespaces)
return trimmed == "swift-format-ignore"
}

for piece in trivia {
switch piece {
case .lineComment(let text):
if isFormatterIgnore(in: text, prefix: "//", suffix: "") { return true }
break
case .blockComment(let text):
if isFormatterIgnore(in: text, prefix: "/*", suffix: "*/") { return true }
break
default:
break
}
}
return false
}

/// Returns whether the formatter should ignore the given node by printing it without changing the
/// node's internal text representation (i.e. print all text inside of the node as it was in the
/// original source).
///
/// - Note: The caller is responsible for ensuring that the given node is a type of node that can
/// be safely ignored.
///
/// - Parameter node: A node that can be safely ignored.
private 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 {
return isFormatterIgnorePresent(inTrivia: firstTrivia)
}
return false
}
///
/// - Parameter trivia: Leading trivia for a node that the formatter supports ignoring.
private func isFormatterIgnorePresent(inTrivia trivia: Trivia) -> Bool {
func isFormatterIgnore(in commentText: String, prefix: String, suffix: String) -> Bool {
let trimmed =
commentText.dropFirst(prefix.count)
.dropLast(suffix.count)
.trimmingCharacters(in: .whitespaces)
return trimmed == "swift-format-ignore"
}

for piece in trivia {
switch piece {
case .lineComment(let text):
if isFormatterIgnore(in: text, prefix: "//", suffix: "") { return true }
break
case .blockComment(let text):
if isFormatterIgnore(in: text, prefix: "/*", suffix: "*/") { return true }
break
default:
break
}
}
return false
}

/// Returns whether the formatter should ignore the given node by printing it without changing the
/// node's internal text representation (i.e. print all text inside of the node as it was in the
/// original source).
///
/// - Note: The caller is responsible for ensuring that the given node is a type of node that can
/// be safely ignored.
///
/// - Parameter node: A node that can be safely ignored.
private 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 {
return isFormatterIgnorePresent(inTrivia: firstTrivia)
}
return false
}