|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftSyntax |
| 14 | + |
| 15 | +extension SyntaxProtocol { |
| 16 | + static func completions(at keyPath: AnyKeyPath, visitedNodeKinds: inout [SyntaxProtocol.Type]) -> (completions: Set<TokenKind>, hasRequiredToken: Bool) { |
| 17 | + visitedNodeKinds.append(Self.self) |
| 18 | + if let keyPath = keyPath as? KeyPath<Self, TokenSyntax> { |
| 19 | + return (Set(keyPath.tokenChoices), true) |
| 20 | + } else if let keyPath = keyPath as? KeyPath<Self, TokenSyntax?> { |
| 21 | + return (Set(keyPath.tokenChoices), false) |
| 22 | + } else if let value = type(of: keyPath).valueType as? SyntaxOrOptionalProtocol.Type { |
| 23 | + switch value.syntaxOrOptionalProtocolType { |
| 24 | + case .optional(let syntaxType): |
| 25 | + return (syntaxType.completionsAtStartOfNode(visitedNodeKinds: &visitedNodeKinds).completions, false) |
| 26 | + case .nonOptional(let syntaxType): |
| 27 | + return (syntaxType.completionsAtStartOfNode(visitedNodeKinds: &visitedNodeKinds).completions, !syntaxType.structure.isCollection) |
| 28 | + } |
| 29 | + } else { |
| 30 | + assertionFailure("Unexpected keypath") |
| 31 | + return ([], false) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + static func completionsAtStartOfNode(visitedNodeKinds: inout [SyntaxProtocol.Type]) -> (completions: Set<TokenKind>, hasRequiredToken: Bool) { |
| 36 | + if visitedNodeKinds.contains(where: { $0 == Self.self }) { |
| 37 | + return ([], true) |
| 38 | + } |
| 39 | + if self == Syntax.self { |
| 40 | + return ([], true) |
| 41 | + } |
| 42 | + |
| 43 | + var hasRequiredToken: Bool |
| 44 | + var completions: Set<TokenKind> = [] |
| 45 | + |
| 46 | + switch self.structure { |
| 47 | + case .layout(let keyPaths): |
| 48 | + hasRequiredToken = false // Only relevant if keyPaths is empty and the loop below isn't traversed |
| 49 | + for keyPath in keyPaths { |
| 50 | + let res = self.completions(at: keyPath, visitedNodeKinds: &visitedNodeKinds) |
| 51 | + completions.formUnion(res.completions) |
| 52 | + hasRequiredToken = hasRequiredToken || res.hasRequiredToken |
| 53 | + if hasRequiredToken { |
| 54 | + break |
| 55 | + } |
| 56 | + } |
| 57 | + case .collection(let collectionElementType): |
| 58 | + return collectionElementType.completionsAtStartOfNode(visitedNodeKinds: &visitedNodeKinds) |
| 59 | + case .choices(let choices): |
| 60 | + hasRequiredToken = true |
| 61 | + for choice in choices { |
| 62 | + switch choice { |
| 63 | + case .node(let nodeType): |
| 64 | + let res = nodeType.completionsAtStartOfNode(visitedNodeKinds: &visitedNodeKinds) |
| 65 | + completions.formUnion(res.completions) |
| 66 | + hasRequiredToken = hasRequiredToken && res.hasRequiredToken |
| 67 | + case .token(let tokenKind): |
| 68 | + completions.insert(tokenKind) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return (completions, hasRequiredToken) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +extension SyntaxProtocol { |
| 78 | + func completions(after keyPath: AnyKeyPath) -> Set<TokenKind> { |
| 79 | + var hasRequiredToken: Bool = false |
| 80 | + |
| 81 | + var completions: Set<TokenKind> = [] |
| 82 | + var visitedNodeKinds: [SyntaxProtocol.Type] = [] |
| 83 | + if case .layout(let childrenKeyPaths) = self.kind.syntaxNodeType.structure, |
| 84 | + let index = childrenKeyPaths.firstIndex(of: keyPath) |
| 85 | + { |
| 86 | + for keyPath in childrenKeyPaths[(index + 1)...] { |
| 87 | + let res = self.kind.syntaxNodeType.completions(at: keyPath, visitedNodeKinds: &visitedNodeKinds) |
| 88 | + completions.formUnion(res.completions) |
| 89 | + hasRequiredToken = res.hasRequiredToken |
| 90 | + if hasRequiredToken { |
| 91 | + break |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + if !hasRequiredToken, let parent = parent, let keyPathInParent = self.keyPathInParent { |
| 96 | + completions.formUnion(parent.completions(after: keyPathInParent)) |
| 97 | + } |
| 98 | + return completions |
| 99 | + } |
| 100 | + |
| 101 | + public func completions(at position: AbsolutePosition) -> Set<TokenKind> { |
| 102 | + if position <= self.positionAfterSkippingLeadingTrivia { |
| 103 | + var visitedNodeKinds: [SyntaxProtocol.Type] = [] |
| 104 | + return Self.completionsAtStartOfNode(visitedNodeKinds: &visitedNodeKinds).completions |
| 105 | + } |
| 106 | + let finder = TokenFinder(targetPosition: position) |
| 107 | + finder.walk(self) |
| 108 | + guard let found = finder.found?.previousToken(viewMode: .sourceAccurate), let parent = found.parent, let keyPathInParent = found.keyPathInParent else { |
| 109 | + return [] |
| 110 | + } |
| 111 | + return parent.completions(after: keyPathInParent) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/// Finds the first token whose text (ignoring trivia) starts after targetPosition. |
| 116 | +class TokenFinder: SyntaxAnyVisitor { |
| 117 | + var targetPosition: AbsolutePosition |
| 118 | + var found: TokenSyntax? = nil |
| 119 | + |
| 120 | + init(targetPosition: AbsolutePosition) { |
| 121 | + self.targetPosition = targetPosition |
| 122 | + super.init(viewMode: .sourceAccurate) |
| 123 | + } |
| 124 | + |
| 125 | + override func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind { |
| 126 | + if found != nil || node.endPosition < targetPosition { |
| 127 | + return .skipChildren |
| 128 | + } else { |
| 129 | + return .visitChildren |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + override func visit(_ node: TokenSyntax) -> SyntaxVisitorContinueKind { |
| 134 | + if targetPosition <= node.positionAfterSkippingLeadingTrivia, found == nil { |
| 135 | + found = node |
| 136 | + } |
| 137 | + return .skipChildren |
| 138 | + } |
| 139 | +} |
0 commit comments