Skip to content

Renamed file to fileTree in SourceLocationConverter #1991

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions Sources/SwiftSyntax/SourceLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fileprivate struct SourceLocationDirectiveArguments {
/// vice-versa. The ``AbsolutePosition``s must be originating from nodes that are
/// part of the same tree that was used to initialize this class.
public final class SourceLocationConverter {
private let file: String
private let fileName: String
/// The source of the file, modeled as data so it can contain invalid UTF-8.
private let source: [UInt8]
/// Array of lines and the position at the start of the line.
Expand All @@ -183,11 +183,11 @@ public final class SourceLocationConverter {
/// name that contains string interpolation.
///
/// - Parameters:
/// - file: The file path associated with the syntax tree.
/// - fileName: The file path associated with the syntax tree.
/// - tree: The root of the syntax tree to convert positions to line/columns for.
public init(file: String, tree: some SyntaxProtocol) {
public init(fileName: String, tree: some SyntaxProtocol) {
precondition(tree.parent == nil, "SourceLocationConverter must be passed the root of the syntax tree")
self.file = file
self.fileName = fileName
self.source = tree.syntaxTextBytes
(self.lines, endOfFile) = computeLines(tree: Syntax(tree))
precondition(tree.totalLength.utf8Length == endOfFile.utf8Offset)
Expand All @@ -206,16 +206,33 @@ public final class SourceLocationConverter {
}
}

/// Create a new ``SourceLocationConverter`` to convert between ``AbsolutePosition``
/// and ``SourceLocation`` in a syntax tree.
///
/// This initializer is deprecated. Please use `init(fileName:source:)` instead.
///
/// This converter ignores any malformed `#sourceLocation` directives, e.g.
/// `#sourceLocation` directives with a non-decimal line number or with a file
/// name that contains string interpolation.
///
/// - Parameters:
/// - file: The file path associated with the syntax tree.
/// - tree: The root of the syntax tree to convert positions to line/columns for.
@available(*, deprecated, message: "Use init(fileName:tree:) instead")
public convenience init(file: String, tree: SyntaxProtocol) {
self.init(fileName: file, tree: tree)
}

/// - Important: This initializer does not take `#sourceLocation` directives
/// into account and doesn’t produce `presumedFile` and
/// `presumedLine`.
///
/// - Parameters:
/// - file: The file path associated with the syntax tree.
/// - source: The source code to convert positions to line/columns for.
@available(*, deprecated, message: "Use init(file:tree:) instead")
@available(*, deprecated, message: "Use init(fileName:tree:) instead")
public init(file: String, source: String) {
self.file = file
self.fileName = file
self.source = Array(source.utf8)
(self.lines, endOfFile) = self.source.withUnsafeBufferPointer { buf in
return computeLines(SyntaxText(buffer: buf))
Expand Down Expand Up @@ -285,7 +302,7 @@ public final class SourceLocationConverter {
// Clamp the given position to the end of file if needed.
let pos = min(position, endOfFile)
if pos.utf8Offset < 0 {
return SourceLocation(line: 1, column: 1, offset: 0, file: self.file)
return SourceLocation(line: 1, column: 1, offset: 0, file: self.fileName)
}

precondition(!lines.isEmpty)
Expand Down Expand Up @@ -318,7 +335,7 @@ public final class SourceLocationConverter {
line: line,
column: column,
offset: pos.utf8Offset,
file: self.file
file: self.fileName
)
}

Expand Down