13
13
import SwiftSyntax
14
14
15
15
extension DeclModifierListSyntax {
16
-
17
- func has( modifier: String ) -> Bool {
18
- return contains { $0. name. text == modifier }
19
- }
20
-
21
- func has( modifier: TokenKind ) -> Bool {
22
- return contains { $0. name. tokenKind == modifier }
23
- }
24
-
25
16
/// Returns the declaration's access level modifier, if present.
26
17
var accessLevelModifier : DeclModifierSyntax ? {
27
18
for modifier in self {
@@ -35,41 +26,33 @@ extension DeclModifierListSyntax {
35
26
return nil
36
27
}
37
28
38
- /// Returns modifier list without the given modifier.
39
- func remove( name: String ) -> DeclModifierListSyntax {
40
- return filter { $0. name. text != name }
29
+ /// Returns true if the modifier list contains any of the keywords in the given set.
30
+ func contains( anyOf keywords: Set < Keyword > ) -> Bool {
31
+ return contains {
32
+ switch $0. name. tokenKind {
33
+ case . keyword( let keyword) : return keywords. contains ( keyword)
34
+ default : return false
35
+ }
36
+ }
41
37
}
42
38
43
- /// Returns a formatted declaration modifier token with the given name.
44
- func createModifierToken( name: String ) -> DeclModifierSyntax {
45
- let id = TokenSyntax . identifier ( name, trailingTrivia: . spaces( 1 ) )
46
- let newModifier = DeclModifierSyntax ( name: id, detail: nil )
47
- return newModifier
39
+ /// Removes any of the modifiers in the given set from the modifier list, mutating it in-place.
40
+ mutating func remove( anyOf keywords: Set < Keyword > ) {
41
+ self = filter {
42
+ switch $0. name. tokenKind {
43
+ case . keyword( let keyword) : return !keywords. contains ( keyword)
44
+ default : return true
45
+ }
46
+ }
48
47
}
49
48
50
- /// Inserts the given modifier into the list at a specific index.
51
- ///
52
- /// If the modifier is being inserted at the front of the list, the current front element's
53
- /// leading trivia will be moved to the new element to preserve any leading comments and newlines.
54
- mutating func triviaPreservingInsert(
55
- _ modifier: DeclModifierSyntax , at index: SyntaxChildrenIndex
56
- ) {
57
- var modifier = modifier
58
- modifier. trailingTrivia = [ . spaces( 1 ) ]
59
-
60
- guard index == self . startIndex else {
61
- self . insert ( modifier, at: index)
62
- return
63
- }
64
- guard var firstMod = first, let firstTok = firstMod. firstToken ( viewMode: . sourceAccurate) else {
65
- self . insert ( modifier, at: index)
66
- return
49
+ /// Returns a copy of the modifier list with any of the modifiers in the given set removed.
50
+ func removing( anyOf keywords: Set < Keyword > ) -> DeclModifierListSyntax {
51
+ return filter {
52
+ switch $0. name. tokenKind {
53
+ case . keyword( let keyword) : return !keywords. contains ( keyword)
54
+ default : return true
55
+ }
67
56
}
68
-
69
- modifier. leadingTrivia = firstTok. leadingTrivia
70
- firstMod. leadingTrivia = [ ]
71
- firstMod. trailingTrivia = [ . spaces( 1 ) ]
72
- self [ self . startIndex] = firstMod
73
- self . insert ( modifier, at: self . startIndex)
74
57
}
75
58
}
0 commit comments