Skip to content

Commit bceba67

Browse files
committed
Merge remote-tracking branch 'upstream/main' into wasm32-wasi
2 parents d85de09 + d8b446b commit bceba67

35 files changed

+1105
-323
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ endif()
6868
find_package(SwiftSyntax CONFIG)
6969
if(NOT SwiftSyntax_FOUND)
7070
FetchContent_Declare(Syntax
71-
GIT_REPOSITORY https://github.com/apple/swift-syntax
71+
GIT_REPOSITORY https://github.com/swiftlang/swift-syntax
7272
GIT_TAG main)
7373
list(APPEND _SF_VENDOR_DEPENDENCIES Syntax)
7474
endif()

CODE_OF_CONDUCT.md

-55
This file was deleted.

CONTRIBUTING.md

-11
This file was deleted.

Documentation/PrettyPrinter.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ if someCondition {
263263
### Token Generation
264264

265265
Token generation begins with the abstract syntax tree (AST) of the Swift source
266-
file, provided by the [SwiftSyntax](https://github.com/apple/swift-syntax)
266+
file, provided by the [SwiftSyntax](https://github.com/swiftlang/swift-syntax)
267267
library. We have overloaded a `visit` method for each of the different kinds of
268268
syntax nodes. Most of these nodes are higher-level, and are composed of other
269269
nodes. For example, `FunctionDeclSyntax` contains

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ var dependencies: [Package.Dependency] {
163163
return [
164164
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2"),
165165
.package(url: "https://github.com/kkebo/swift-markdown.git", branch: "swift-markdown-wasm32-wasi-0.4"),
166-
.package(url: "https://github.com/apple/swift-syntax.git", branch: "main"),
166+
.package(url: "https://github.com/swiftlang/swift-syntax.git", branch: "main"),
167167
]
168168
}
169169
}

README.md

+30-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The checked items are currently supported. The others are planned to be supporte
2727
# swift-format
2828

2929
`swift-format` provides the formatting technology for
30-
[SourceKit-LSP](https://github.com/apple/sourcekit-lsp) and the building
30+
[SourceKit-LSP](https://github.com/swiftlang/sourcekit-lsp) and the building
3131
blocks for doing code formatting transformations.
3232

3333
This package can be used as a [command line tool](#command-line-usage)
@@ -44,7 +44,7 @@ invoked via an [API](#api-usage).
4444
### Swift 5.8 and later
4545

4646
As of Swift 5.8, swift-format depends on the version of
47-
[SwiftSyntax](https://github.com/apple/swift-syntax) whose parser has been
47+
[SwiftSyntax](https://github.com/swiftlang/swift-syntax) whose parser has been
4848
rewritten in Swift and no longer has dependencies on libraries in the
4949
Swift toolchain.
5050

@@ -60,7 +60,7 @@ SwiftSyntax; the 5.8 release of swift-format is `508.0.0`, not `0.50800.0`.
6060
### Swift 5.7 and earlier
6161

6262
`swift-format` versions 0.50700.0 and earlier depend on versions of
63-
[SwiftSyntax](https://github.com/apple/swift-syntax) that used a standalone
63+
[SwiftSyntax](https://github.com/swiftlang/swift-syntax) that used a standalone
6464
parsing library distributed as part of the Swift toolchain. When using these
6565
versions, you should check out and build `swift-format` from the release
6666
tag or branch that is compatible with the version of Swift you are using.
@@ -87,8 +87,11 @@ For example, if you are using Xcode 13.3 (Swift 5.6), you will need
8787
## Getting swift-format
8888

8989
If you are mainly interested in using swift-format (rather than developing it),
90-
then you can get swift-format either via [Homebrew](https://brew.sh/) or by checking out the
91-
source and building it.
90+
then you can get it in three different ways:
91+
92+
### Included in Xcode
93+
94+
Xcode 16 and above include swift-format in the toolchain. You can run `swift-format` from anywhere on the system using `swift format` (notice the space instead of dash). To find the path at which `swift-format` is installed, run `xcrun --find swift-format`.
9295

9396
### Installing via Homebrew
9497

@@ -100,7 +103,7 @@ Install `swift-format` using the following commands:
100103

101104
```sh
102105
VERSION=510.1.0 # replace this with the version you need
103-
git clone https://github.com/apple/swift-format.git
106+
git clone https://github.com/swiftlang/swift-format.git
104107
cd swift-format
105108
git checkout "tags/$VERSION"
106109
swift build -c release
@@ -277,3 +280,24 @@ been merged into `main`.
277280

278281
If you are interested in developing `swift-format`, there is additional
279282
documentation about that [here](Documentation/Development.md).
283+
284+
## Contributing
285+
286+
Contributions to Swift are welcomed and encouraged! Please see the
287+
[Contributing to Swift guide](https://swift.org/contributing/).
288+
289+
Before submitting the pull request, please make sure you have [tested your
290+
changes](https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md)
291+
and that they follow the Swift project [guidelines for contributing
292+
code](https://swift.org/contributing/#contributing-code).
293+
294+
To be a truly great community, [Swift.org](https://swift.org/) needs to welcome
295+
developers from all walks of life, with different backgrounds, and with a wide
296+
range of experience. A diverse and friendly community will have more great
297+
ideas, more unique perspectives, and produce more great code. We will work
298+
diligently to make the Swift community welcoming to everyone.
299+
300+
To give clarity of what is expected of our members, Swift has adopted the
301+
code of conduct defined by the Contributor Covenant. This document is used
302+
across many open source communities, and we think it articulates our values
303+
well. For more, see the [Code of Conduct](https://swift.org/code-of-conduct/).
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
import SwiftSyntax
15+
16+
/// The selection as given on the command line - an array of offets and lengths
17+
public enum Selection {
18+
case infinite
19+
case ranges([Range<AbsolutePosition>])
20+
21+
/// Create a selection from an array of utf8 ranges. An empty array means an infinite selection.
22+
public init(offsetRanges: [Range<Int>]) {
23+
if offsetRanges.isEmpty {
24+
self = .infinite
25+
} else {
26+
let ranges = offsetRanges.map {
27+
AbsolutePosition(utf8Offset: $0.lowerBound) ..< AbsolutePosition(utf8Offset: $0.upperBound)
28+
}
29+
self = .ranges(ranges)
30+
}
31+
}
32+
33+
public func contains(_ position: AbsolutePosition) -> Bool {
34+
switch self {
35+
case .infinite:
36+
return true
37+
case .ranges(let ranges):
38+
return ranges.contains { $0.contains(position) }
39+
}
40+
}
41+
42+
public func overlapsOrTouches(_ range: Range<AbsolutePosition>) -> Bool {
43+
switch self {
44+
case .infinite:
45+
return true
46+
case .ranges(let ranges):
47+
return ranges.contains { $0.overlapsOrTouches(range) }
48+
}
49+
}
50+
}
51+
52+
53+
public extension Syntax {
54+
/// - Returns: `true` if the node is _completely_ inside any range in the selection
55+
func isInsideSelection(_ selection: Selection) -> Bool {
56+
switch selection {
57+
case .infinite:
58+
return true
59+
case .ranges(let ranges):
60+
return ranges.contains { return $0.lowerBound <= position && endPosition <= $0.upperBound }
61+
}
62+
}
63+
}

Sources/SwiftFormat/API/SwiftFormatter.swift

+13-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -70,6 +70,7 @@ public final class SwiftFormatter {
7070
try format(
7171
source: String(contentsOf: url, encoding: .utf8),
7272
assumingFileURL: url,
73+
selection: .infinite,
7374
to: &outputStream,
7475
parsingDiagnosticHandler: parsingDiagnosticHandler)
7576
}
@@ -86,6 +87,7 @@ public final class SwiftFormatter {
8687
/// - url: A file URL denoting the filename/path that should be assumed for this syntax tree,
8788
/// which is associated with any diagnostics emitted during formatting. If this is nil, a
8889
/// dummy value will be used.
90+
/// - selection: The ranges to format
8991
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
9092
/// be written.
9193
/// - parsingDiagnosticHandler: An optional callback that will be notified if there are any
@@ -94,6 +96,7 @@ public final class SwiftFormatter {
9496
public func format<Output: TextOutputStream>(
9597
source: String,
9698
assumingFileURL url: URL?,
99+
selection: Selection,
97100
to outputStream: inout Output,
98101
parsingDiagnosticHandler: ((Diagnostic, SourceLocation) -> Void)? = nil
99102
) throws {
@@ -108,8 +111,8 @@ public final class SwiftFormatter {
108111
assumingFileURL: url,
109112
parsingDiagnosticHandler: parsingDiagnosticHandler)
110113
try format(
111-
syntax: sourceFile, operatorTable: .standardOperators, assumingFileURL: url, source: source,
112-
to: &outputStream)
114+
syntax: sourceFile, source: source, operatorTable: .standardOperators, assumingFileURL: url,
115+
selection: selection, to: &outputStream)
113116
}
114117

115118
/// Formats the given Swift syntax tree and writes the result to an output stream.
@@ -122,32 +125,26 @@ public final class SwiftFormatter {
122125
///
123126
/// - Parameters:
124127
/// - syntax: The Swift syntax tree to be converted to source code and formatted.
128+
/// - source: The original Swift source code used to build the syntax tree.
125129
/// - operatorTable: The table that defines the operators and their precedence relationships.
126130
/// This must be the same operator table that was used to fold the expressions in the `syntax`
127131
/// argument.
128132
/// - url: A file URL denoting the filename/path that should be assumed for this syntax tree,
129133
/// which is associated with any diagnostics emitted during formatting. If this is nil, a
130134
/// dummy value will be used.
135+
/// - selection: The ranges to format
131136
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will
132137
/// be written.
133138
/// - Throws: If an unrecoverable error occurs when formatting the code.
134139
public func format<Output: TextOutputStream>(
135-
syntax: SourceFileSyntax, operatorTable: OperatorTable, assumingFileURL url: URL?,
136-
to outputStream: inout Output
137-
) throws {
138-
try format(
139-
syntax: syntax, operatorTable: operatorTable, assumingFileURL: url, source: nil,
140-
to: &outputStream)
141-
}
142-
143-
private func format<Output: TextOutputStream>(
144-
syntax: SourceFileSyntax, operatorTable: OperatorTable,
145-
assumingFileURL url: URL?, source: String?, to outputStream: inout Output
140+
syntax: SourceFileSyntax, source: String, operatorTable: OperatorTable,
141+
assumingFileURL url: URL?, selection: Selection, to outputStream: inout Output
146142
) throws {
147143
let assumedURL = url ?? URL(fileURLWithPath: "source")
148144
let context = Context(
149145
configuration: configuration, operatorTable: operatorTable, findingConsumer: findingConsumer,
150-
fileURL: assumedURL, sourceFileSyntax: syntax, source: source, ruleNameCache: ruleNameCache)
146+
fileURL: assumedURL, selection: selection, sourceFileSyntax: syntax, source: source,
147+
ruleNameCache: ruleNameCache)
151148
let pipeline = FormatPipeline(context: context)
152149
let transformedSyntax = pipeline.rewrite(Syntax(syntax))
153150

@@ -158,6 +155,7 @@ public final class SwiftFormatter {
158155

159156
let printer = PrettyPrinter(
160157
context: context,
158+
source: source,
161159
node: transformedSyntax,
162160
printTokenStream: debugOptions.contains(.dumpTokenStream),
163161
whitespaceOnly: false)

Sources/SwiftFormat/API/SwiftLinter.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,18 @@ public final class SwiftLinter {
119119
/// - Throws: If an unrecoverable error occurs when formatting the code.
120120
public func lint(
121121
syntax: SourceFileSyntax,
122+
source: String,
122123
operatorTable: OperatorTable,
123124
assumingFileURL url: URL
124125
) throws {
125-
try lint(syntax: syntax, operatorTable: operatorTable, assumingFileURL: url, source: nil)
126+
try lint(syntax: syntax, operatorTable: operatorTable, assumingFileURL: url, source: source)
126127
}
127128

128129
private func lint(
129130
syntax: SourceFileSyntax,
130131
operatorTable: OperatorTable,
131132
assumingFileURL url: URL,
132-
source: String?
133+
source: String
133134
) throws {
134135
let context = Context(
135136
configuration: configuration, operatorTable: operatorTable, findingConsumer: findingConsumer,
@@ -145,6 +146,7 @@ public final class SwiftLinter {
145146
// pretty-printer.
146147
let printer = PrettyPrinter(
147148
context: context,
149+
source: source,
148150
node: Syntax(syntax),
149151
printTokenStream: debugOptions.contains(.dumpTokenStream),
150152
whitespaceOnly: true)

Sources/SwiftFormat/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_library(SwiftFormat
1414
API/Finding.swift
1515
API/FindingCategorizing.swift
1616
API/Indent.swift
17+
API/Selection.swift
1718
API/SwiftFormatError.swift
1819
API/SwiftFormatter.swift
1920
API/SwiftLinter.swift

0 commit comments

Comments
 (0)