Commit Graph

63 Commits

Author SHA1 Message Date
Rintaro Ishizaki
694e815f07 [Parser] Include all AST nodes from every #if region in ParserUnit
'ParserUnit' is used for analyzing syntax structures _mainly_ in
SourceKit.

Since we removed IfConfigDecl from AST, ParserUnit didn't
inclue any AST in #if ... #endif regions even for active region because
it used to consider all inactive. Instead, consider every region
"active" and include all the AST nodes.

rdar://117387631
2025-02-03 16:22:11 -08:00
Vincent Isambart
93c06738d3 Check that formatting is as expected 2024-09-18 13:16:31 +09:00
Vincent Isambart
a48ff77a75 Format macro decl without crashing 2024-09-13 14:38:19 +09:00
Doug Gregor
8ec800d35e Remove bespoke handling of IfConfigDecl within code formatting
The SourceKit code formatting tools have effectively been replaced
by swift-syntax's basic formatting functionality and more full-featured
formatters like swift-format and SwiftFormat. Remove the special
handling of IfConfigDecl as staging for the removal of IfConfigDecl.
2024-09-07 21:48:18 -07:00
Ben Barham
20f45ec284 Replace uses of presumed locations where they do not make sense
Various uses of `getPresumedLineAndColumnForLoc` were likely added when
that function was the very misleading name `getLineAndColumn`. Change
these to use `getLineAndColumnForBuffer` instead where appropriate, ie.
we want the underlying file rather than the location to display to the
user.

There were also some cases where the buffer identifier had been swapped
to use the display name instead, under the assumption that the presumed
location was needed. Updated those as well.

SingleRawComment: Lines are only used when merging comments, where the
original location is fine to use.

Index: Doesn't store the file set in #sourceLocation, so using the
presumed line would end up pointing to a location that makes no sense.

Editor functionality: Formatting and refactoring are on the current
file. Using the presumed location would result in incorrect
replacements.
2021-04-10 09:49:31 +10:00
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
58859f5699 [SourceKit/CodeFormat] Update indentation for braceless multiple trailing closures. 2020-05-06 01:56:41 -04:00
Nathan Hawes
e01c1a1579 [SourceKit/CodeFormat] Get 'trailing' indentation to work in more places and fix a bug where multi-line string indentation was modified.
E.g. if/guard condition patterns are column-aligned, but hitting enter after
the ',' below wasn't column-aligning the next empty line:

guard let x = Optional(42),
// No indentation added here after enter

Also the isTargetContext() check takes a start and end token location, but
wasn't accounting for the end location pointing a multiline string, so we
weren't walking into such nodes. This meant we didn't realise the target
location was within a multiline string in some cases, and we ended up
interfering with whitespace in its content.
2020-04-19 15:11:16 -07:00
Nathan Hawes
19d6effc5e [SourceKit/CodeFormat] Column-align multiple patterns in catch clauses.
Catch clauses now support mutliple patterns. Like 'case' patterns, these
should be column-aligned if split across multiple lines.

do {
  ...
} catch MyErr.a(let x),
        MyErr.b(let x) {
  print("hello")
}
2020-04-08 09:46:09 -07:00
Nathan Hawes
2a93296fb6 [SourceKit/CodeFormat] Simplify ContextOverride a little, and only propagate ContextLocs when necessary.
We don't actually need to set a ContextOverride unless the ContextLoc and L
paren/brace/bracket are on different lines. Combined with the fact that we
only set them if the L and R parens/braces/brackets are on different lines
to, it guarantees there will be at most one override that's applicable on
any given line, which lets us simplify the logic somewhat.
2020-03-11 23:06:44 -07:00
Nathan Hawes
aedafe980f [SourceKit/CodeFormat] Improve documentation and fix propagation of ContextLocs.
- Rename several symbols to make it clearer whether the ranges they deal with
  are open or closed.
- Add comments documenting the implementation of OutdentChecker::hasOutdent
- Fix a bug where code after a doc coment block of the '/**' style was being
  indented 1 space.
- Fix IsInStringLiteral not being set if the indent target was in a string
  segment of an interpolated multiline string.
- Update OutdentChecker::hasOutdent to propagate indent contexts from
  parent parens/brackets/braces to child parens/brackets/braces that start
  later in the same line (like FormatWalker already does). This changes the
  braces in the example below to 'inherit' a ContextLoc from their parent
  square brackets, which have a ContextLoc at 'foo'. This makes the whole
  expression be correctly considered 'outdenting':

  foo(a: "hello"
      b: "hello")[x: {
    print("hello")
  }]
2020-03-10 21:04:22 -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
f27ccabdb1 SourceKit/Formatting: avoid indenting for consecutive dot-member calls
rdar://52392291
2019-07-12 14:03:03 -07:00
Xi Ge
c2d8c460f3 SourceKit/Indentation: avoid indenting closing paren of function-like decls if it appears in a new line
rdar://51719094
2019-07-02 12:41:08 -07:00
Xi Ge
5280254325 Sourcekit/Indentation: avoid indenting dot member access appearing after trailing closure 2019-06-11 15:12:30 -07:00
Xi Ge
2b190648c0 SourceKit/Indentation: avoid indenting the end of subscript expressions in call chain.
rdar://50591281
2019-06-11 10:54:07 -07:00
Xi Ge
c7cf96660a SourceKit/Indentation: align function names in chained trailing closures
Chained trailing closures are sibling-like thus they should be aligned.

rdar://22205716
2019-06-10 17:07:20 -07:00
mishal_shah
1e38fc3030 Update master to build with Xcode 11 beta, macOS 10.15, iOS 13, tvOS 13, and watchOS 6 SDKs 2019-06-03 22:50:02 -07:00
Xi Ge
b520708f07 Sourcekit/Indentation: avoid indenting the end of array/dictionary literals if they appear at the argument end
rdar://40093469
2019-05-30 15:51:50 -07:00
Argyrios Kyrtzidis
1dcb1e083a [sourcekitd] Expose the indentation option for extra indentation of switch case
rdar://51079623
2019-05-23 16:21:30 -07:00
Xi Ge
8dbbb2ba1d sourcekitd/format: avoid performing sibling-based indentation for PrefixUnaryExpr.
rdar://47659949
2019-01-31 15:44:19 -08:00
Keith Smiley
2d91af2077 IDE: Fix formatting of closing square brackets
Previously closing square brackets would be caught in the logic for
elements in collections, indenting them to the same level. Now they are
indented with the normal non-sibling logic, which indents them
correctly.
2018-07-25 20:27:32 -07:00
Xi Ge
7fe3c5cfdd Formatting: Avoid crashing when indent width is 0. rdar://37835956 (#14873) 2018-02-28 11:00:20 -08:00
Xi Ge
61317432d7 Formatting: sanitize formatting option of 0 tab-width. rdar://37835956 (#14861)
If the format option specifies using tab of zero width, we reset the
tab width to a non-zero value.
2018-02-27 13:00:27 -08:00
Rintaro Ishizaki
3cecef02b9 [SourceKit] Add regression test for rdar 32789463 (already fixed) (#14151)
This crasher has already been fixed in 34e2aec662
2018-01-25 22:06:16 +09:00
Argyrios Kyrtzidis
fdd407f904 [test] Re-enable test/SourceKit/CodeFormat/indent-implicit-getter.swift (#12317)
This was disabled a long time ago due to a crash but I cannot reproduce such a crash currently.
rdar://26484736
2017-10-06 16:37:09 -07:00
Xi Ge
b17ccf1ae7 Formatting: when aligning siblings in indentation, we should respect use tab setting. rdar://32611247 (#11334)
We use white spaces for the remaining that cannot be filled with tabs.
2017-08-04 12:14:23 -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
David Farler
776a799a86 Revert "[test][SourceKit] Add a test to ensure sibling-based indentation work for arguments with trailing comments. rdar://27776466"
The code that fixed this test has to be reverted for the time being.

This reverts commit 6880afaeba.
2016-11-18 13:23:31 -08:00
Xi Ge
6880afaeba [test][SourceKit] Add a test to ensure sibling-based indentation work for arguments with trailing comments. rdar://27776466
The radar is no longer an issue after @bitjammer's change on preserving
trivia on tokens.
2016-11-16 16:19:43 -08:00
Xi Ge
e5d77911a2 [SourceKit] Indent property getters disregarding of empty bodies. rdar://28049927 (#5246)
[SourceKit] Indentation: when the indented line starts with open brace and the
line before starts with a leading declaration keywords, we never add
indentation level on the brace. rdar://28049927
2016-10-11 19:23:29 -07:00
Xi Ge
7cb5d9a8a2 [SourceKit] When function calls taking multiple closures, align the end of them. rdar://27473586 (#4787) 2016-09-14 19:13:50 -07:00
Xi Ge
365bb52a31 [SourceKit] Avoid adding indentation at the end of a CaptureListExpr. rdar://28193169 (#4749) 2016-09-13 13:33:34 -07:00
Xi Ge
c29286171f [Parser] Preserve empty getter functions for indentation (#4586)
* [Parser] Preserve empty getter functions to make sure indentation inside its body still works. rdar://28049927

* Using the consistent identifier even though they are identical.
2016-09-01 14:56:23 -07:00
Dmitri Gribenko
d175b3b66d Migrate FileCheck to %FileCheck in tests 2016-08-10 23:52:02 -07:00
John McCall
c8c41b385c Implement SE-0077: precedence group declarations.
What I've implemented here deviates from the current proposal text
in the following ways:

- I had to introduce a FunctionArrowPrecedence to capture the parsing
  of -> in expression contexts.

- I found it convenient to continue to model the assignment property
  explicitly.

- The comparison and casting operators have historically been
  non-associative; I have chosen to preserve that, since I don't
  think this proposal intended to change it.

- This uses the precedence group names and higherThan/lowerThan
  as agreed in discussion.
2016-07-26 14:04:57 -07:00
David Farler
7bfaeb57f1 [SE-0081] Warn on deprecated where clause inside angle brackets
and provide a fix-it to move it to the new location as referenced
in SE-0081.

Fix up a few stray places in the standard library that is still using
the old syntax.

Update any ./test files that aren't expecting the new warning/fix-it
in -verify mode.

While investigating what I thought was a new crash due to this new
diagnostic, I discovered two sources of quite a few compiler crashers
related to unterminated generic parameter lists, where the right
angle bracket source location was getting unconditionally set to
the current token, even though it wasn't actually a '>'.
2016-07-26 01:41:10 -07:00
Xi Ge
5d07b4b068 [SourceKit][Format] Avoid indenting when multiple function call ends at the same line.
This fixes SR-603.
2016-07-25 17:57:57 -07:00
Doug Gregor
823c24b355 [SE-0112] Rename ErrorProtocol to Error.
This is bullet (5) of the proposed solution in SE-0112, and the last
major piece to be implemented.
2016-07-12 10:53:52 -07:00
Xi Ge
2ed177046e [SourceKit] Check location validity before getting the line number to fix a crash. rdar://26756189 (#3364) 2016-07-06 21:26:09 -07:00
Argyrios Kyrtzidis
d10220920b [IDE/format] Disable the sibling check for dictionary/array literal elements.
This ends up creating too much indentation in some common cases.
This effectively get us back to indentation behavior of Swift 2.2.

Addresses rdar://26290643.
2016-05-23 17:23:40 -07:00
Xi Ge
6ad5e8bc13 [SourceKit][Test] Add a test to ensure function arguments' labels are aligned properly. rdar://26069993 2016-05-03 14:33:32 -07:00
Xi Ge
589d9a8a3e CodeFormat: Avoid indenting the close bracket of a closure if the closure is on the RHS of an assignment expression. rdar://24507930 2016-03-29 13:08:55 -07:00
Xi Ge
701277e751 Allow SourceEntityWalker to visit operator decls, so that they can get indented properly. rdar://25246628 2016-03-19 14:47:58 -07:00
Xi Ge
5dbabf5cb3 [Test] SourceKit: Add a test for indenting unnamed parameters. rdar://25178551 2016-03-18 17:24:31 -07:00
Ben Langmuir
14ce05977c Disable CodeFormat/indent-implicit-getter.swift while we investigate a crash 2016-03-18 12:08:29 -07:00
Xi Ge
a91cc97058 [SourceKit] Indentation: aligning parameters when argument names are present. rdar://24862107
Fix is in AST by removing unnecessary override.
Originally reported by: https://twitter.com/JohnRHeaton/status/702526804138074112
2016-02-26 12:09:24 -08:00
Xi Ge
c5408c8beb [SourceKit] Indentation: indent the first element in a collection expression according to the position of the left bracket, if they are not in the same line.
We treat this as a mutated version of sibling-based indentation, where an extra level is needed. rdar://24630624
2016-02-18 13:28:44 -08:00
Xi Ge
4e85583718 add test 2016-02-18 13:28:44 -08:00