-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathSyntax+Classifications.swift
70 lines (64 loc) · 3.02 KB
/
Syntax+Classifications.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 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
//
//===----------------------------------------------------------------------===//
#if compiler(>=6)
public import SwiftSyntax
#else
import SwiftSyntax
#endif
extension SyntaxProtocol {
/// Sequence of ``SyntaxClassifiedRange``s for this syntax node.
///
/// The provided classified ranges are consecutive and cover the full source
/// text of the node. The ranges may also span multiple tokens, if multiple
/// consecutive tokens would have the same classification then a single classified
/// range is provided for all of them.
public var classifications: SyntaxClassifications {
return SyntaxClassifications(_syntaxNode, in: self.range)
}
/// Sequence of ``SyntaxClassifiedRange``s contained in this syntax node within
/// a source range.
///
/// The provided classified ranges may extend beyond the provided `range`.
/// Active classifications (non-`none`) will extend the range to include the
/// full classified range (e.g. from the beginning of the comment block), while
/// `none` classified ranges will extend to the beginning or end of the token
/// that the `range` touches.
/// It is guaranteed that no classified range will be provided that doesn't
/// intersect the provided `range`.
///
/// - Parameters:
/// - in: The range to pull ``SyntaxClassifiedRange``s from.
/// - Returns: Sequence of ``SyntaxClassifiedRange``s.
public func classifications(in range: Range<AbsolutePosition>) -> SyntaxClassifications {
return SyntaxClassifications(_syntaxNode, in: range)
}
/// The ``SyntaxClassifiedRange`` for a relative byte offset.
/// - Parameters:
/// - at: The relative to the node byte offset.
/// - Returns: The ``SyntaxClassifiedRange`` for the offset or nil if the source text
/// at the given offset is unclassified.
@available(*, deprecated, message: "Use classification(at: AbsolutePosition) instead.")
public func classification(at offset: Int) -> SyntaxClassifiedRange? {
return classification(at: AbsolutePosition(utf8Offset: offset + self.position.utf8Offset))
}
/// The ``SyntaxClassifiedRange`` for an absolute position.
/// - Parameters:
/// - at: The absolute position.
/// - Returns: The ``SyntaxClassifiedRange`` for the position or `nil`` if the source text
/// at the given position is unclassified.
public func classification(at position: AbsolutePosition) -> SyntaxClassifiedRange? {
let range = Range(position: position, length: SourceLength(utf8Length: 1))
let classifications = SyntaxClassifications(_syntaxNode, in: range)
var iterator = classifications.makeIterator()
return iterator.next()
}
}