Skip to content

Commit c0c1e5e

Browse files
authored
Merge pull request #1991 from natikgadzhi/source-location-converter-file-to-filename
Renamed file to fileTree in SourceLocationConverter
2 parents dd6a976 + 8a01dd6 commit c0c1e5e

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

Sources/SwiftSyntax/SourceLocation.swift

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fileprivate struct SourceLocationDirectiveArguments {
158158
/// vice-versa. The ``AbsolutePosition``s must be originating from nodes that are
159159
/// part of the same tree that was used to initialize this class.
160160
public final class SourceLocationConverter {
161-
private let file: String
161+
private let fileName: String
162162
/// The source of the file, modeled as data so it can contain invalid UTF-8.
163163
private let source: [UInt8]
164164
/// Array of lines and the position at the start of the line.
@@ -183,11 +183,11 @@ public final class SourceLocationConverter {
183183
/// name that contains string interpolation.
184184
///
185185
/// - Parameters:
186-
/// - file: The file path associated with the syntax tree.
186+
/// - fileName: The file path associated with the syntax tree.
187187
/// - tree: The root of the syntax tree to convert positions to line/columns for.
188-
public init(file: String, tree: some SyntaxProtocol) {
188+
public init(fileName: String, tree: some SyntaxProtocol) {
189189
precondition(tree.parent == nil, "SourceLocationConverter must be passed the root of the syntax tree")
190-
self.file = file
190+
self.fileName = fileName
191191
self.source = tree.syntaxTextBytes
192192
(self.lines, endOfFile) = computeLines(tree: Syntax(tree))
193193
precondition(tree.totalLength.utf8Length == endOfFile.utf8Offset)
@@ -206,16 +206,33 @@ public final class SourceLocationConverter {
206206
}
207207
}
208208

209+
/// Create a new ``SourceLocationConverter`` to convert between ``AbsolutePosition``
210+
/// and ``SourceLocation`` in a syntax tree.
211+
///
212+
/// This initializer is deprecated. Please use `init(fileName:source:)` instead.
213+
///
214+
/// This converter ignores any malformed `#sourceLocation` directives, e.g.
215+
/// `#sourceLocation` directives with a non-decimal line number or with a file
216+
/// name that contains string interpolation.
217+
///
218+
/// - Parameters:
219+
/// - file: The file path associated with the syntax tree.
220+
/// - tree: The root of the syntax tree to convert positions to line/columns for.
221+
@available(*, deprecated, message: "Use init(fileName:tree:) instead")
222+
public convenience init(file: String, tree: SyntaxProtocol) {
223+
self.init(fileName: file, tree: tree)
224+
}
225+
209226
/// - Important: This initializer does not take `#sourceLocation` directives
210227
/// into account and doesn’t produce `presumedFile` and
211228
/// `presumedLine`.
212229
///
213230
/// - Parameters:
214231
/// - file: The file path associated with the syntax tree.
215232
/// - source: The source code to convert positions to line/columns for.
216-
@available(*, deprecated, message: "Use init(file:tree:) instead")
233+
@available(*, deprecated, message: "Use init(fileName:tree:) instead")
217234
public init(file: String, source: String) {
218-
self.file = file
235+
self.fileName = file
219236
self.source = Array(source.utf8)
220237
(self.lines, endOfFile) = self.source.withUnsafeBufferPointer { buf in
221238
return computeLines(SyntaxText(buffer: buf))
@@ -285,7 +302,7 @@ public final class SourceLocationConverter {
285302
// Clamp the given position to the end of file if needed.
286303
let pos = min(position, endOfFile)
287304
if pos.utf8Offset < 0 {
288-
return SourceLocation(line: 1, column: 1, offset: 0, file: self.file)
305+
return SourceLocation(line: 1, column: 1, offset: 0, file: self.fileName)
289306
}
290307

291308
precondition(!lines.isEmpty)
@@ -318,7 +335,7 @@ public final class SourceLocationConverter {
318335
line: line,
319336
column: column,
320337
offset: pos.utf8Offset,
321-
file: self.file
338+
file: self.fileName
322339
)
323340
}
324341

0 commit comments

Comments
 (0)