File: DeclarationsSectionTranslator.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (432 lines) | stat: -rw-r--r-- 20,650 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
 This source file is part of the Swift.org open source project

 Copyright (c) 2021-2024 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 Swift project authors
*/

import Foundation
import SymbolKit

/// Translates a symbol's declaration into a render node's Declarations section.
struct DeclarationsSectionTranslator: RenderSectionTranslator {
    func translateSection(
        for symbol: Symbol,
        renderNode: inout RenderNode,
        renderNodeTranslator: inout RenderNodeTranslator
    ) -> VariantCollection<CodableContentSection?>? {
        translateSectionToVariantCollection(documentationDataVariants: symbol.declarationVariants) { trait, declaration -> RenderSection? in
            guard !declaration.isEmpty else {
                return nil
            }

            /// Convert a ``SymbolGraph`` declaration fragment into a ``DeclarationRenderSection/Token``
            /// by resolving any symbol USRs to the appropriate reference link.
            func translateFragment(
                _ fragment: SymbolGraph.Symbol.DeclarationFragments.Fragment,
                highlight: Bool
            ) -> DeclarationRenderSection.Token {
                let reference: ResolvedTopicReference?
                if let preciseIdentifier = fragment.preciseIdentifier,
                    let resolved = renderNodeTranslator.context.localOrExternalReference(
                        symbolID: preciseIdentifier
                    )
                {
                    reference = resolved
                    renderNodeTranslator.collectedTopicReferences.append(resolved)
                } else {
                    reference = nil
                }

                // Add the declaration token
                return DeclarationRenderSection.Token(
                    fragment: fragment,
                    identifier: reference?.absoluteString,
                    highlight: highlight
                )
            }

            /// Convenience wrapper for `translateFragment(_:highlight:)` that can be used in
            /// an iterator mapping.
            func translateFragment(
                _ fragment: SymbolGraph.Symbol.DeclarationFragments.Fragment
            ) -> DeclarationRenderSection.Token {
                return translateFragment(fragment, highlight: false)
            }

            /// Translate a whole ``SymbolGraph`` declaration to a ``DeclarationRenderSection``
            /// declaration and highlight any tokens that aren't shared with a sequence of common tokens.
            func translateDeclaration(
                _ declaration: [SymbolGraph.Symbol.DeclarationFragments.Fragment],
                commonFragments: [SymbolGraph.Symbol.DeclarationFragments.Fragment]
            ) -> [DeclarationRenderSection.Token] {
                var translatedDeclaration: [DeclarationRenderSection.Token] = []
                var commonIndex = 0
                for fragment in declaration {
                    if commonIndex < commonFragments.count, fragment == commonFragments[commonIndex] {
                        // fragment is common to all declarations, render plain
                        translatedDeclaration.append(translateFragment(fragment))
                        commonIndex += 1
                    } else {
                        // fragment is unique to this declaration, render highlighted
                        translatedDeclaration.append(translateFragment(fragment, highlight: true))
                    }
                }

                return postProcessTokens(translatedDeclaration)
            }

            typealias OverloadDeclaration = (
                declaration: [SymbolGraph.Symbol.DeclarationFragments.Fragment],
                reference: ResolvedTopicReference
            )

            func renderOtherDeclarationsTokens(
                from overloadDeclarations: [OverloadDeclaration],
                displayIndex: Int,
                commonFragments: [SymbolGraph.Symbol.DeclarationFragments.Fragment]
            ) -> DeclarationRenderSection.OtherDeclarations {
                var otherDeclarations: [DeclarationRenderSection.OtherDeclarations.Declaration] = []

                for overloadDeclaration in overloadDeclarations {
                    let translatedDeclaration = translateDeclaration(
                        overloadDeclaration.declaration,
                        commonFragments: commonFragments)
                    otherDeclarations.append(.init(
                        tokens: translatedDeclaration,
                        identifier: overloadDeclaration.reference.absoluteString))

                    // Add a topic reference to the overload
                    renderNodeTranslator.collectedTopicReferences.append(
                        overloadDeclaration.reference
                    )
                }

                return .init(declarations: otherDeclarations, displayIndex: displayIndex)
            }

            func collectOverloadDeclarations(from overloads: Symbol.Overloads) -> [OverloadDeclaration]? {
                let declarations = overloads.references.compactMap { overloadReference -> OverloadDeclaration? in
                    guard let overload = try? renderNodeTranslator.context
                        .entity(with: overloadReference).semantic as? Symbol
                    else {
                        return nil
                    }

                    let declarationFragments = overload.declarationVariants[trait]?.values
                        .first?
                        .declarationFragments
                    precondition(
                        declarationFragments != nil,
                        "Overloaded symbols must have declaration fragments."
                    )
                    return declarationFragments.map({
                        (declaration: $0, reference: overloadReference)
                    })
                }

                guard !declarations.isEmpty else {
                    return nil
                }
                return declarations
            }

            func sortPlatformNames(_ platforms: [PlatformName?]) -> [PlatformName?] {
                platforms.sorted { (lhs, rhs) -> Bool in
                    guard let lhsValue = lhs, let rhsValue = rhs else {
                        return lhs == nil
                    }
                    return lhsValue.rawValue < rhsValue.rawValue
                }
            }

            var declarations: [DeclarationRenderSection] = []
            let languages = [
                trait.interfaceLanguage ?? renderNodeTranslator.identifier.sourceLanguage.id
            ]
            for pair in declaration {
                let (platforms, declaration) = pair

                let renderedTokens: [DeclarationRenderSection.Token]
                let otherDeclarations: DeclarationRenderSection.OtherDeclarations?

                // If this symbol has overloads, render their declarations as well.
                if let overloads = symbol.overloadsVariants[trait],
                    let overloadDeclarations = collectOverloadDeclarations(from: overloads)
                {
                    // Pre-process the declarations by splitting text fragments apart to increase legibility
                    let mainDeclaration = declaration.declarationFragments.flatMap(preProcessFragment(_:))
                    let processedOverloadDeclarations = overloadDeclarations.map({
                        OverloadDeclaration($0.declaration.flatMap(preProcessFragment(_:)), $0.reference)
                    })
                    let preProcessedDeclarations = [mainDeclaration] + processedOverloadDeclarations.map(\.declaration)

                    // Collect the "common fragments" so we can highlight the ones that are different
                    // in each declaration
                    let commonFragments = longestCommonSubsequence(preProcessedDeclarations)

                    renderedTokens = translateDeclaration(
                        mainDeclaration,
                        commonFragments: commonFragments
                    )
                    otherDeclarations = renderOtherDeclarationsTokens(
                        from: processedOverloadDeclarations,
                        displayIndex: overloads.displayIndex,
                        commonFragments: commonFragments)
                } else {
                    renderedTokens = declaration.declarationFragments.map(translateFragment)
                    otherDeclarations = nil
                }

                declarations.append(
                    DeclarationRenderSection(
                        languages: languages,
                        platforms: sortPlatformNames(platforms),
                        tokens: renderedTokens,
                        otherDeclarations: otherDeclarations
                    )
                )
            }

            if let alternateDeclarations = symbol.alternateDeclarationVariants[trait] {
                for pair in alternateDeclarations {
                    let (platforms, decls) = pair
                    let platformNames = sortPlatformNames(platforms)
                    for alternateDeclaration in decls {
                        let renderedTokens = alternateDeclaration.declarationFragments.map(translateFragment)

                        declarations.append(
                            DeclarationRenderSection(
                                languages: languages,
                                platforms: platformNames,
                                tokens: renderedTokens
                            )
                        )
                    }
                }
            }

            return DeclarationsRenderSection(declarations: declarations)
        }
    }
}

fileprivate extension DeclarationRenderSection.Token {
    /// Whether this token is a text token with a changed highlight.
    var isHighlightedText: Bool {
        self.highlight == .changed && self.kind == .text
    }

    /// Whether this token has any highlight applied to it.
    var isHighlighted: Bool {
        self.highlight != nil
    }

    /// Create a new ``Kind/text`` token with the given text and highlight.
    init(plainText text: String, highlight: Highlight? = nil) {
        self.init(text: text, kind: .text, highlight: highlight)
    }
}

/// "Post-process" a sequence of declaration tokens by recombining text tokens and cleaning up
/// highlighted spans of tokens.
///
/// The output of `preProcessFragment` is suitable for differencing, but causes unexpected results
/// when handed directly to Swift-DocC-Render and applied to its declaration formatter. This
/// function recombines adjacent tokens so that the declaration formatter can correctly see the
/// structure of a declaration and format it accordingly.
///
/// In addition, this function takes the opportunity to trim whitespace at the beginning and end of
/// highlighted spans of tokens, to beautify the resulting declaration.
fileprivate func postProcessTokens(
    _ tokens: [DeclarationRenderSection.Token]
) -> [DeclarationRenderSection.Token] {
    var processedTokens: [DeclarationRenderSection.Token] = []

    // This function iterates a list of tokens, keeping track of a "previous token" and comparing it
    // against the "current token" to perform the following transformations:
    //
    // 1. Trim whitespace from the end of a highlighted token if the following token is not
    //    highlighted.
    // 2. Trim whitespace from the beginning of a highlighted token if the previous token was not
    //    highlighted.
    // 3. Concatenate plain-text tokens if they are both highlighted or both not highlighted.

    /// If the given tokens are both plain-text tokens with the same highlight, return a token with
    /// both tokens' texts.
    func concatenateTokens(
        _ lhs: DeclarationRenderSection.Token,
        _ rhs: DeclarationRenderSection.Token
    ) -> DeclarationRenderSection.Token? {
        guard lhs.kind == .text, rhs.kind == .text, lhs.highlight == rhs.highlight else {
            return nil
        }
        var result = lhs
        result.text += rhs.text
        return result
    }

    guard var previousToken = tokens.first else { return [] }
    for var currentToken in tokens.dropFirst() {
        // First, check whether the tokens we have start or end a highlighted span, and whether
        // that span started or ended (respectively) with whitespace. If so, break off that
        // whitespace into a new, unhighlighted token, and save any excess tokens accordingly.
        // This is complicated by the fact that the whitespace token could be all whitespace,
        // removing the need to create a new token. Both of these branches also "fall through"
        // to the concatenation check below, in case the creation of a new unhighlighted token
        // allows it to be combined with the current token.

        // If the previous token was a highlighted plain-text token that ended with whitespace,
        // and the current token is not highlighted, then remove the highlighting for the
        // whitespace.
        if previousToken.isHighlightedText,
            !currentToken.isHighlighted,
            previousToken.text.last?.isWhitespace == true
        {
            if previousToken.text.allSatisfy(\.isWhitespace) {
                // if the last token was all whitespace, just convert it to be unhighlighted
                previousToken.highlight = nil
            } else {
                // otherwise, split the trailing whitespace into a new token
                let trimmedText = previousToken.text.removingTrailingWhitespace()
                let trailingWhitespace = previousToken.text.suffix(
                    previousToken.text.count - trimmedText.count
                )
                previousToken.text = trimmedText
                processedTokens.append(previousToken)

                previousToken = .init(plainText: String(trailingWhitespace))
            }
        } else if !previousToken.isHighlighted,
            currentToken.isHighlightedText,
            currentToken.text.first?.isWhitespace == true
        {
            // Vice versa: If the current token is a highlighted plain-text token that begins
            // with whitespace, and the previous token is not highlighted, then remove the
            // highlighting for that whitespace.

            if currentToken.text.allSatisfy(\.isWhitespace) {
                // if this token is all whitespace, just convert it to be unhighlighted
                currentToken.highlight = nil
            } else {
                // otherwise, split the leading whitespace into a new token
                let trimmedText = currentToken.text.removingLeadingWhitespace()
                let leadingWhitespace = currentToken.text.prefix(
                    currentToken.text.count - trimmedText.count
                )
                currentToken.text = trimmedText

                // if we can combine the whitespace with the previous token, do that
                let whitespaceToken = DeclarationRenderSection.Token(plainText: String(leadingWhitespace))
                if let combinedToken = concatenateTokens(previousToken, whitespaceToken) {
                    previousToken = combinedToken
                } else {
                    processedTokens.append(previousToken)
                    previousToken = whitespaceToken
                }
            }
        }

        if let combinedToken = concatenateTokens(previousToken, currentToken) {
            // if we could combine the tokens, save it to the current token so it becomes the
            // "previous" token at the end of the loop
            currentToken = combinedToken
        } else {
            // otherwise, just save off the previous token so we can store the next
            // one for the next iteration
            processedTokens.append(previousToken)
        }
        previousToken = currentToken
    }
    processedTokens.append(previousToken)

    return processedTokens
}

/// "Pre-process" a declaration fragment by eagerly splitting apart text fragments into chunks that
/// are more likely to be held in common with other declarations.
///
/// This method exists to clean up the diff visualization in circumstances where parts of a text
/// fragment are shared in common, but have been combined with other text that is not shared. For
/// example, a declaration such as `myFunc<T>(param: T)` will have a text fragment `>(` before the
/// parameter list, which is technically not shared with a similar declaration `myFunc(param: Int)`
/// which only has a `(` fragment.
///
/// Text should be broken up in three scenarios:
/// 1. Before a parenthesis or comma, as in the previous example,
/// 2. Before and after whitespace, to increase the chances of matching non-whitespace tokens with
///    each other, and
/// 3. After a `?` character, to eagerly separate optional parameter types from their surrounding
///    syntax.
///
/// > Note: Any adjacent text fragments that are both shared or highlighted should be recombined
/// > after translation, to allow Swift-DocC-Render to correctly format Swift declarations into
/// > multiple lines. This is performed as part of `translateDeclaration(_:commonFragments:)` above.
fileprivate func preProcessFragment(
    _ fragment: SymbolGraph.Symbol.DeclarationFragments.Fragment
) -> [SymbolGraph.Symbol.DeclarationFragments.Fragment] {
    guard fragment.kind == .text, !fragment.spelling.isEmpty else {
        return [fragment]
    }
    var textPartitions: [Substring] = []
    var substringIndex = fragment.spelling.startIndex
    var currentElement = fragment.spelling[substringIndex]

    func areInSameChunk(_ currentElement: Character, _ nextElement: Character) -> Bool {
        if "(),".contains(nextElement) {
            // an open paren means we have a token like `>(` which should be split
            // a close paren means we have a token like ` = nil)` which should be split
            // a comma is similar to the close paren situation
            return false
        } else if currentElement.isWhitespace != nextElement.isWhitespace {
            // break whitespace into their own blocks
            return false
        } else if currentElement == "?" {
            // if we have a token like `?>` or similar we should break the fragment
            return false
        } else {
            return true
        }
    }

    // FIXME: replace this with `chunked(by:)` if we add swift-algorithms as a dependency
    for (nextIndex, nextElement) in fragment.spelling.indexed().dropFirst() {
        if !areInSameChunk(currentElement, nextElement) {
            textPartitions.append(fragment.spelling[substringIndex..<nextIndex])
            substringIndex = nextIndex
        }
        currentElement = nextElement
    }

    if substringIndex != fragment.spelling.endIndex {
        textPartitions.append(fragment.spelling[substringIndex...])
    }

    return textPartitions.map({ .init(kind: .text, spelling: String($0), preciseIdentifier: nil) })
}

/// Calculate the "longest common subsequence" of a list of sequences.
///
/// The longest common subsequence (LCS) of a set of sequences is the sequence of items that
/// appears in all the input sequences in the same order. For example given the sequences `ABAC`
/// and `CAACB`, the letters `AAC` appear in the same order in both of them, even though the letter
/// `B` interrupts the sequence in the first one.
fileprivate func longestCommonSubsequence<Element: Equatable>(_ sequences: [[Element]]) -> [Element] {
    guard var result = sequences.first else { return [] }

    for other in sequences.dropFirst() {
        // This implementation uses the Swift standard library's `CollectionDifference` API to
        // calculate the difference between two sequences, then back-computes the LCS by applying
        // just the calculated "removals" to the first sequence. Then, in the next loop iteration,
        // recalculate the LCS between the result and the next input sequence. By the time all the
        // input sequences have been iterated, we have a common subsequence from all the inputs.
        for case .remove(let offset, _, _) in other.difference(from: result).removals.reversed() {
            result.remove(at: result.startIndex + offset)
        }
    }

    return result
}