AbsolutePosition had value semantics anyway, the only reason it was a
reference type was so that we can use it in AtomicCache. But that can be
worked around by boxing it into a reference type.
This adds a couple of changes to support building SwiftSyntax without an ObjC
runtime as well as swift-corelibs-foundation instead of the ObjC Foundation
implemetation. This is needed to support building SwiftSyntax on Linux.
AbsolutePosition being a mutable reference type easily leads to bugs
where an AbsolutePosition is modified. Making it immutable eliminates
this issue. Furthermore, the introduction of SourceLength should allow
easier manipulation of AbsolutePositions on the client side.
We still cannot make AbsolutePosition a value type since it is used
inside AtomicCache, but the immutability gives the same safety.
SourcePresence and ID are already shared between the two node kinds and
the node's length will soon be cached in RawSyntax as well. By making it
a struct, we will be able to compute the node's length when it is being
constructed in the initialiser.
* [SwiftSyntax] Add accessors for source locations and test diagnostic emission
* Add tests for endLocation
* Pre-emptively copy AbsolutePosition to avoid mutating it twice
* [SwiftSyntax] Add SyntaxRewriter.visitAny(_:)
This function, when overridden, allows Syntax rewriters to perform custom dynamic visitation behavior. If a user wanted to, say, store a series of transformations accessible by metatype, they can override visitAny and do their own runtime dispatch.
If a non-nil result is returned from visitAny, the original specialized visitors are skipped.
We have two similar objects for source location. AbsoluteLocation
calculates the offset of a syntax node on the fly. SourceLocation is
designed to serialize a Swift syntax diagnostics to the driver. The only
difference is AbsoluteLocation doesn't contain source file name however
SourceLocation does. This patch bridges them by making AbsoluteLocation
a private member of SourceLocation. We also expect Swift syntax to
be file-name agnostic. The clients should keep track of the file name
when emitting diagnostics.
* [SwiftSyntax] Add replace method SyntaxCollections
* Change replaceSubrange method call to subscript setter call
* [SwiftSyntax] Add test for SyntaxCollections
* Fix mistakes in SyntaxCollections tests
* Use syntax's raw value instead of syntax itself in replacing method
* Update SyntaxCollections tests
Although libSyntax is designed in a way that trivia is attached to
tokens, we shouldn't restrict clients to access trivia only from a token.
An example can be doc-comment, which conceptually attaches to a declaration rather
than the start token of a declaration, like at-attributes.
Syntax nodes are designed as reusable blocks to construct Swift source
code. For this reason, we don't track absolute position in each node;
instead, the absolute position should be calculated on the fly when
needed. We recently found absolute positions are useful to bridge with
sourcekitd, which typically speaks in the language of line, column and
offset. Therefore, this patch tries to add a computed
property on SyntaxNode to get its absolute position.
To compute the absolute position of a SyntaxNode from scratch requires tree traversal.
However, getting the absolute position from these added APIs doesn't necessarily
mean we'll traverse since SyntaxData will actively cache the computed position.
Also, since we recursively compute the absolute position, all the position caches
for a SyntaxNode's previous siblings and ancestors will be populated as well during
the calculation of this SyntaxNode.