-
Notifications
You must be signed in to change notification settings - Fork 247
Support for formatting a selection #708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6640067
88beb85
0c0977d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
import SwiftSyntax | ||
|
||
/// The selection as given on the command line - an array of offets and lengths | ||
public enum Selection { | ||
case infinite | ||
case ranges([Range<AbsolutePosition>]) | ||
|
||
/// Create a selection from an array of utf8 ranges. An empty array means an infinite selection. | ||
public init(offsetRanges: [Range<Int>]) { | ||
if offsetRanges.isEmpty { | ||
self = .infinite | ||
} else { | ||
let ranges = offsetRanges.map { | ||
AbsolutePosition(utf8Offset: $0.lowerBound) ..< AbsolutePosition(utf8Offset: $0.upperBound) | ||
} | ||
self = .ranges(ranges) | ||
} | ||
} | ||
|
||
public func contains(_ position: AbsolutePosition) -> Bool { | ||
switch self { | ||
case .infinite: | ||
return true | ||
case .ranges(let ranges): | ||
return ranges.contains { $0.contains(position) } | ||
} | ||
} | ||
|
||
public func overlapsOrTouches(_ range: Range<AbsolutePosition>) -> Bool { | ||
switch self { | ||
case .infinite: | ||
return true | ||
case .ranges(let ranges): | ||
return ranges.contains { $0.overlapsOrTouches(range) } | ||
} | ||
} | ||
} | ||
|
||
|
||
public extension Syntax { | ||
/// - Returns: `true` if the node is _completely_ inside any range in the selection | ||
func isInsideSelection(_ selection: Selection) -> Bool { | ||
switch selection { | ||
case .infinite: | ||
return true | ||
case .ranges(let ranges): | ||
return ranges.contains { return $0.lowerBound <= position && endPosition <= $0.upperBound } | ||
ahoppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
|
@@ -70,6 +70,7 @@ public final class SwiftFormatter { | |
try format( | ||
source: String(contentsOf: url, encoding: .utf8), | ||
assumingFileURL: url, | ||
selection: .infinite, | ||
to: &outputStream, | ||
parsingDiagnosticHandler: parsingDiagnosticHandler) | ||
} | ||
|
@@ -86,6 +87,7 @@ public final class SwiftFormatter { | |
/// - url: A file URL denoting the filename/path that should be assumed for this syntax tree, | ||
/// which is associated with any diagnostics emitted during formatting. If this is nil, a | ||
/// dummy value will be used. | ||
/// - selection: The ranges to format | ||
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will | ||
/// be written. | ||
/// - parsingDiagnosticHandler: An optional callback that will be notified if there are any | ||
|
@@ -94,6 +96,7 @@ public final class SwiftFormatter { | |
public func format<Output: TextOutputStream>( | ||
source: String, | ||
assumingFileURL url: URL?, | ||
selection: Selection, | ||
to outputStream: inout Output, | ||
parsingDiagnosticHandler: ((Diagnostic, SourceLocation) -> Void)? = nil | ||
) throws { | ||
|
@@ -108,8 +111,8 @@ public final class SwiftFormatter { | |
assumingFileURL: url, | ||
parsingDiagnosticHandler: parsingDiagnosticHandler) | ||
try format( | ||
syntax: sourceFile, operatorTable: .standardOperators, assumingFileURL: url, source: source, | ||
to: &outputStream) | ||
syntax: sourceFile, source: source, operatorTable: .standardOperators, assumingFileURL: url, | ||
selection: selection, to: &outputStream) | ||
} | ||
|
||
/// Formats the given Swift syntax tree and writes the result to an output stream. | ||
|
@@ -122,32 +125,26 @@ public final class SwiftFormatter { | |
/// | ||
/// - Parameters: | ||
/// - syntax: The Swift syntax tree to be converted to source code and formatted. | ||
/// - source: The original Swift source code used to build the syntax tree. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I’m not a fan of having a public API that requires the user to provide both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the syntax tree hold on to the source (I didn't see that when I did this two months ago 🙃, but I can look...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like having to regenerate the original source text if we already have it to start (which, of course, we do). And eventually we'll have a transformed syntax tree, and we still need to use the original source along with that. I think passing both makes the most sense. |
||
/// - operatorTable: The table that defines the operators and their precedence relationships. | ||
/// This must be the same operator table that was used to fold the expressions in the `syntax` | ||
/// argument. | ||
/// - url: A file URL denoting the filename/path that should be assumed for this syntax tree, | ||
/// which is associated with any diagnostics emitted during formatting. If this is nil, a | ||
/// dummy value will be used. | ||
/// - selection: The ranges to format | ||
/// - outputStream: A value conforming to `TextOutputStream` to which the formatted output will | ||
/// be written. | ||
/// - Throws: If an unrecoverable error occurs when formatting the code. | ||
public func format<Output: TextOutputStream>( | ||
syntax: SourceFileSyntax, operatorTable: OperatorTable, assumingFileURL url: URL?, | ||
to outputStream: inout Output | ||
) throws { | ||
try format( | ||
syntax: syntax, operatorTable: operatorTable, assumingFileURL: url, source: nil, | ||
to: &outputStream) | ||
} | ||
|
||
private func format<Output: TextOutputStream>( | ||
syntax: SourceFileSyntax, operatorTable: OperatorTable, | ||
assumingFileURL url: URL?, source: String?, to outputStream: inout Output | ||
syntax: SourceFileSyntax, source: String, operatorTable: OperatorTable, | ||
assumingFileURL url: URL?, selection: Selection, to outputStream: inout Output | ||
) throws { | ||
let assumedURL = url ?? URL(fileURLWithPath: "source") | ||
let context = Context( | ||
configuration: configuration, operatorTable: operatorTable, findingConsumer: findingConsumer, | ||
fileURL: assumedURL, sourceFileSyntax: syntax, source: source, ruleNameCache: ruleNameCache) | ||
fileURL: assumedURL, selection: selection, sourceFileSyntax: syntax, source: source, | ||
ruleNameCache: ruleNameCache) | ||
let pipeline = FormatPipeline(context: context) | ||
let transformedSyntax = pipeline.rewrite(Syntax(syntax)) | ||
|
||
|
@@ -158,6 +155,7 @@ public final class SwiftFormatter { | |
|
||
let printer = PrettyPrinter( | ||
context: context, | ||
source: source, | ||
node: transformedSyntax, | ||
printTokenStream: debugOptions.contains(.dumpTokenStream), | ||
whitespaceOnly: false) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,17 +119,18 @@ public final class SwiftLinter { | |
/// - Throws: If an unrecoverable error occurs when formatting the code. | ||
public func lint( | ||
syntax: SourceFileSyntax, | ||
source: String, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar here. It feels redundant to pass both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here. Since the whitespace linting uses |
||
operatorTable: OperatorTable, | ||
assumingFileURL url: URL | ||
) throws { | ||
try lint(syntax: syntax, operatorTable: operatorTable, assumingFileURL: url, source: nil) | ||
try lint(syntax: syntax, operatorTable: operatorTable, assumingFileURL: url, source: source) | ||
} | ||
|
||
private func lint( | ||
syntax: SourceFileSyntax, | ||
operatorTable: OperatorTable, | ||
assumingFileURL url: URL, | ||
source: String? | ||
source: String | ||
) throws { | ||
let context = Context( | ||
configuration: configuration, operatorTable: operatorTable, findingConsumer: findingConsumer, | ||
|
@@ -145,6 +146,7 @@ public final class SwiftLinter { | |
// pretty-printer. | ||
let printer = PrettyPrinter( | ||
context: context, | ||
source: source, | ||
node: Syntax(syntax), | ||
printTokenStream: debugOptions.contains(.dumpTokenStream), | ||
whitespaceOnly: true) | ||
|
Uh oh!
There was an error while loading. Please reload this page.