Skip to content

Commit

Permalink
Disabled indenting of commented-out code
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Nov 24, 2017
1 parent bc1b008 commit 447be93
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
21 changes: 18 additions & 3 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,15 @@ extension FormatRules {
return true
}

func isCommentedCode(at index: Int) -> Bool {
if !scopeStack.isEmpty, formatter.token(at: index - 1)?.isSpace != true,
let nextToken = formatter.token(at: index + 1),
case let .space(space) = nextToken, space.hasPrefix(formatter.options.indent) {
return true
}
return false
}

if formatter.options.fragment,
let firstIndex = formatter.index(of: .nonSpaceOrLinebreak, after: -1),
let indentToken = formatter.token(at: firstIndex - 1), case let .space(string) = indentToken {
Expand Down Expand Up @@ -916,7 +925,7 @@ extension FormatRules {
}
// Check if line on which scope ends should be unindented
let start = formatter.startOfLine(at: i)
if formatter.options.indentComments ||
if !isCommentedCode(at: start), formatter.options.indentComments ||
formatter.next(.nonSpace, after: start - 1) != .startOfScope("/*"),
let nextToken = formatter.next(.nonSpaceOrCommentOrLinebreak, after: start - 1),
nextToken.isEndOfScope && nextToken != .endOfScope("*/") {
Expand Down Expand Up @@ -1012,8 +1021,8 @@ extension FormatRules {
indentStack.append(indent)
}
// Apply indent
if let nextToken = formatter.next(.nonSpace, after: i) {
switch nextToken {
if let nextTokenIndex = formatter.index(of: .nonSpace, after: i) {
switch formatter.tokens[nextTokenIndex] {
case .linebreak where formatter.options.truncateBlankLines:
formatter.insertSpace("", at: i + 1)
case .error:
Expand Down Expand Up @@ -1041,6 +1050,12 @@ extension FormatRules {
}
index = startIndex
}
case .startOfScope("//"):
// Avoid indenting commented code
if isCommentedCode(at: nextTokenIndex) {
return
}
formatter.insertSpace(indent, at: i + 1)
case .startOfScope("/*"), .commentBody, .endOfScope("*/"):
if formatter.options.indentComments { fallthrough }
default:
Expand Down
8 changes: 8 additions & 0 deletions Tests/RulesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,14 @@ class RulesTests: XCTestCase {
XCTAssertEqual(try format(input + "\n", rules: FormatRules.default, options: options), output + "\n")
}

func testCommentedCodeBlocksNotIndented() {
let input = "func foo() {\n// var foo: Int\n}"
let output = "func foo() {\n// var foo: Int\n}"
let options = FormatOptions(indentComments: false)
XCTAssertEqual(try format(input, rules: [FormatRules.indent], options: options), output)
XCTAssertEqual(try format(input + "\n", rules: FormatRules.default, options: options), output + "\n")
}

// indent multiline strings

func testSimpleMultilineString() {
Expand Down

0 comments on commit 447be93

Please sign in to comment.