File: RewriteSourceKitPlaceholders.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (179 lines) | stat: -rw-r--r-- 5,445 bytes parent folder | download
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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
//
//===----------------------------------------------------------------------===//

import Foundation
import SKLogging
@_spi(RawSyntax) import SwiftSyntax

/// Translate SourceKit placeholder syntax — `<#foo#>` — in `input` to LSP
/// placeholder syntax: `${n:foo}`.
///
/// If `clientSupportsSnippets` is `false`, the placeholder is rendered as an
/// empty string, to prevent the client from inserting special placeholder
/// characters as if they were literal text.
@_spi(Testing)
public func rewriteSourceKitPlaceholders(in input: String, clientSupportsSnippets: Bool) -> String {
  var result = ""
  var nextPlaceholderNumber = 1
  // Current stack of nested placeholders, most nested last. Each element needs
  // to be rendered inside the element before it.
  var placeholders: [(number: Int, contents: String)] = []
  let tokens = tokenize(input)
  for token in tokens {
    switch token {
    case let .text(text):
      if placeholders.isEmpty {
        result += text
      } else {
        placeholders.latest.contents += text
      }

    case .escapeInsidePlaceholder(let character):
      if placeholders.isEmpty {
        result.append(character)
      } else {
        // A closing brace is only escaped _inside_ a placeholder; otherwise the client would include the backslashes
        // literally.
        placeholders.latest.contents += [#"\"#, character]
      }

    case .placeholderOpen:
      placeholders.append((number: nextPlaceholderNumber, contents: ""))
      nextPlaceholderNumber += 1

    case .placeholderClose:
      guard let (number, placeholderBody) = placeholders.popLast() else {
        logger.fault("Invalid placeholder in \(input)")
        return input
      }
      guard let displayName = nameForSnippet(placeholderBody) else {
        logger.fault("Failed to decode placeholder \(placeholderBody) in \(input)")
        return input
      }
      let placeholder =
        clientSupportsSnippets
        ? formatLSPPlaceholder(displayName, number: number)
        : ""
      if placeholders.isEmpty {
        result += placeholder
      } else {
        placeholders.latest.contents += placeholder
      }
    }
  }

  return result
}

/// Scan `input` to identify special elements within: curly braces, which may
/// need to be escaped; and SourceKit placeholder open/close delimiters.
private func tokenize(_ input: String) -> [SnippetToken] {
  var index = input.startIndex
  var isAtEnd: Bool { index == input.endIndex }
  func match(_ char: Character) -> Bool {
    if isAtEnd || input[index] != char {
      return false
    } else {
      input.formIndex(after: &index)
      return true
    }
  }
  func next() -> Character? {
    guard !isAtEnd else { return nil }
    defer { input.formIndex(after: &index) }
    return input[index]
  }

  var tokens: [SnippetToken] = []
  var text = ""
  while let char = next() {
    switch char {
    case "<":
      if match("#") {
        tokens.append(.text(text))
        text.removeAll()
        tokens.append(.placeholderOpen)
      } else {
        text.append(char)
      }

    case "#":
      if match(">") {
        tokens.append(.text(text))
        text.removeAll()
        tokens.append(.placeholderClose)
      } else {
        text.append(char)
      }

    case "$", "}", "\\":
      tokens.append(.text(text))
      text.removeAll()
      tokens.append(.escapeInsidePlaceholder(char))

    case let c:
      text.append(c)
    }
  }

  tokens.append(.text(text))

  return tokens
}

/// A syntactical element inside a SourceKit snippet.
private enum SnippetToken {
  /// A placeholder delimiter.
  case placeholderOpen, placeholderClose
  /// '$', '}' or '\', which need to be escaped when used inside a placeholder.
  case escapeInsidePlaceholder(Character)
  /// Any other consecutive run of characters from the input, which needs no
  /// special treatment.
  case text(String)
}

/// Given the interior text of a SourceKit placeholder, extract a display name
/// suitable for a LSP snippet.
private func nameForSnippet(_ body: String) -> String? {
  var text = rewrappedAsPlaceholder(body)
  return text.withSyntaxText {
    guard let data = RawEditorPlaceholderData(syntaxText: $0) else {
      return nil
    }
    return String(syntaxText: data.typeForExpansionText ?? data.displayText)
  }
}

private let placeholderStart = "<#"
private let placeholderEnd = "#>"
private func rewrappedAsPlaceholder(_ body: String) -> String {
  return placeholderStart + body + placeholderEnd
}

/// Wrap `body` in LSP snippet placeholder syntax, using `number` as the
/// placeholder's index in the snippet.
private func formatLSPPlaceholder(_ body: String, number: Int) -> String {
  "${\(number):\(body)}"
}

private extension Array {
  /// Mutable access to the final element of an array.
  ///
  /// - precondition: The array must not be empty.
  var latest: Element {
    get { self.last! }
    _modify {
      let index = self.index(before: self.endIndex)
      yield &self[index]
    }
  }
}