Skip to content

Rule-based formatting is not applied when using offsets starting at 1 or greater #1114

@3dsnm

Description

@3dsnm

When swift-format is run with an offset range starting at 1 or greater, the formatter applies general formatting but does not apply any rule-based formatting.

Example

.swift-format:

{
  "rules": {
    "NoParensAroundConditions": true,
    "DoNotUseSemicolons": true
  }
}

input:

/// Some String....
 if (a = 0) {             NSLog("print: \(a)");}

Environment variable for convenience:

export CODE="/// Some String....\n if (a = 0) {             NSLog(\"print: \\(a)\");}"

And expected result:

/// Some String....
if a = 0 { NSLog("print: \(a)") }

These commands produce the expected result:

echo $CODE | swift-format format - --configuration .swift-format
echo $CODE | swift-format format - --configuration .swift-format --offsets 0:999

But the following commands do not:

echo $CODE | swift-format format - --configuration .swift-format --offsets 1:999
echo $CODE | swift-format format - --configuration .swift-format --offsets 0:1 --offsets 1:999

Actual output:

/// Some String....
if (a = 0) { NSLog("print: \(a)");  }

Although the sliced input beginning at offset 1 still forms valid Swift syntax (// Some String....), rule-based formatting are not applied, while other non-rule formatting is applied correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions