Commit Graph

4 Commits

Author SHA1 Message Date
Ben Barham
77c70e9fe6 [SourceKit/CodeFormat] Indent lines in multi-line strings
When the multi-line string is unterminated, the indentation of a line
will be the same as the first previous line that had contents, unless
that line is the line containing the start quotes. In that case the
indentation will be:
  - the same as the start quote indentation, if the quotes are the only
    contents on the line, or
  - an extra indentation level to the start quote line otherwise

Lines within a terminated multi-line string or lines with content will
only ever be indented if their current indentation is invalid (ie. their
indentation level is less than that of their end quotes).

This rule is to prevent any signficant (and possibly unintended)
whitespace being added to existing strings during a whole file/range
format - Xcode does not remove whitespace from whitespace-only lines by
default. This could be improved if the reformat was sent the actual
range rather than a line at a time. Different indentation could then be
chosen if the range was in fact a single line.

Resolves rdar://32181422
2020-08-13 12:35:57 +10:00
Nathan Hawes
99edbf0e56 [SourceKit/CodeFormat] Don't column-align PatternBindingDecl entries in certain cases.
Don't column align PBD entries if any entry spans from the same line as
the var/let to another line. E.g.

```
// Previous behavior:
let foo = someItem
      .getValue(), // Column-alignment looks ok here, but...
    bar = otherItem
      .getValue()

getAThing()
  .andDoStuffWithIt()
let foo = someItem
      .getValue() // looks over-indented here, which is more common.
getOtherThing()
  .andDoStuffWithIt()

// New behavior
getAThing()
  .andDoStuffWithIt()
let foo = someItem
  .getValue() // No column alignment in this case...
doOtherThing()

let foo = someItem
   .getValue(), // Or in this case (unfortunate, but less common)...
   bar = otherItem
       .getValue()

let foo = someItem.getValue(),
    bar = otherItem.getValue() // but still column-aligned in this case.
```

Resolves rdar://problem/63309288
2020-05-21 11:49:35 -07:00
Nathan Hawes
a368434432 [SourceKit/CodeFormat] Re-work and improve the indentation implementation.
This restructures the indentation logic around producing a single IndentContext
for the line being indented. An IndentContext has:
- a ContextLoc, which points to a source location to indent relative to,
- a Kind, indicating whether we should align with that location exactly, or
  with the start of the content on its containing line, and
- an IndentLevel with the relative number of levels to indent by.

It also improves the handling of:
- chained and nested parens, braces, square brackets and angle brackets, and
  how those interact with the exact alignment of parameters, call arguments,
  and tuple, array and dictionary elements.
- Indenting to the correct level after an incomplete expression, statement or
  decl.

Resolves:
rdar://problem/59135010
rdar://problem/25519439
rdar://problem/50137394
rdar://problem/48410444
rdar://problem/48643521
rdar://problem/42171947
rdar://problem/40130724
rdar://problem/41405163
rdar://problem/39367027
rdar://problem/36332430
rdar://problem/34464828
rdar://problem/33113738
rdar://problem/32314354
rdar://problem/30106520
rdar://problem/29773848
rdar://problem/27301544
rdar://problem/27776466
rdar://problem/27230819
rdar://problem/25490868
rdar://problem/23482354
rdar://problem/20193017
rdar://problem/47117735
rdar://problem/55950781
rdar://problem/55939440
rdar://problem/53247352
rdar://problem/54326612
rdar://problem/53131527
rdar://problem/48399673
rdar://problem/51361639
rdar://problem/58285950
rdar://problem/58286076
rdar://problem/53828204
rdar://problem/58286182
rdar://problem/58504167
rdar://problem/58286327
rdar://problem/53828026
rdar://problem/57623821
rdar://problem/56965360
rdar://problem/54470937
rdar://problem/55580761
rdar://problem/46928002
rdar://problem/35807378
rdar://problem/39397252
rdar://problem/26692035
rdar://problem/33760223
rdar://problem/48934744
rdar://problem/43315903
rdar://problem/24630624
2020-03-10 21:04:21 -07:00
Xi Ge
df64cd7686 Formatting: Avoid formatting multi-line string literals since it may change behavior. rdar://32135036 (#9582) 2017-05-13 18:21:18 -07:00