|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 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 Foundation |
| 14 | +import LSPLogging |
| 15 | +import LanguageServerProtocol |
| 16 | +import SourceKitD |
| 17 | + |
| 18 | +/// Detailed information about the result of a macro expansion operation. |
| 19 | +/// |
| 20 | +/// Wraps the information returned by sourcekitd's `semantic_refactoring` |
| 21 | +/// request, such as the necessary macro expansion edits. |
| 22 | +struct MacroExpansion: RefactoringResponse { |
| 23 | + /// The title of the refactoring action. |
| 24 | + var title: String |
| 25 | + |
| 26 | + /// The URI of the file where the macro is used |
| 27 | + var uri: DocumentURI |
| 28 | + |
| 29 | + /// The resulting array of `RefactoringEdit` of a semantic refactoring request |
| 30 | + var edits: [RefactoringEdit] |
| 31 | + |
| 32 | + init(title: String, uri: DocumentURI, refactoringEdits: [RefactoringEdit]) { |
| 33 | + self.title = title |
| 34 | + self.uri = uri |
| 35 | + self.edits = refactoringEdits.compactMap { refactoringEdit in |
| 36 | + if refactoringEdit.bufferName == nil && !refactoringEdit.newText.isEmpty { |
| 37 | + logger.fault("Unable to retrieve some parts of the expansion") |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + return refactoringEdit |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +extension SwiftLanguageService { |
| 47 | + /// Handles the `ExpandMacroCommand`. |
| 48 | + /// |
| 49 | + /// Makes a request to sourcekitd and wraps the result into a `MacroExpansion` |
| 50 | + /// and then makes a `ShowDocumentRequest` to the client side for each |
| 51 | + /// expansion to be displayed. |
| 52 | + /// |
| 53 | + /// - Parameters: |
| 54 | + /// - expandMacroCommand: The `ExpandMacroCommand` that triggered this request. |
| 55 | + /// |
| 56 | + /// - Returns: A `[RefactoringEdit]` with the necessary edits and buffer name as a `LSPAny` |
| 57 | + func expandMacro( |
| 58 | + _ expandMacroCommand: ExpandMacroCommand |
| 59 | + ) async throws -> LSPAny { |
| 60 | + guard let sourceKitLSPServer else { |
| 61 | + // `SourceKitLSPServer` has been destructed. We are tearing down the |
| 62 | + // language server. Nothing left to do. |
| 63 | + throw ResponseError.unknown("Connection to the editor closed") |
| 64 | + } |
| 65 | + |
| 66 | + guard let sourceFileURL = expandMacroCommand.textDocument.uri.fileURL else { |
| 67 | + throw ResponseError.unknown("Given URI is not a file URL") |
| 68 | + } |
| 69 | + |
| 70 | + let expansion = try await self.refactoring(expandMacroCommand) |
| 71 | + |
| 72 | + for macroEdit in expansion.edits { |
| 73 | + if let bufferName = macroEdit.bufferName { |
| 74 | + // buffer name without ".swift" |
| 75 | + let macroExpansionBufferDirectoryName = |
| 76 | + bufferName.hasSuffix(".swift") |
| 77 | + ? String(bufferName.dropLast(6)) |
| 78 | + : bufferName |
| 79 | + |
| 80 | + let macroExpansionBufferDirectoryURL = self.generatedMacroExpansionsPath |
| 81 | + .appendingPathComponent(macroExpansionBufferDirectoryName) |
| 82 | + do { |
| 83 | + try FileManager.default.createDirectory( |
| 84 | + at: macroExpansionBufferDirectoryURL, |
| 85 | + withIntermediateDirectories: true |
| 86 | + ) |
| 87 | + } catch { |
| 88 | + throw ResponseError.unknown( |
| 89 | + "Failed to create directory for macro expansion buffer at path: \(macroExpansionBufferDirectoryURL.path)" |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + let macroExpansionFileName = sourceFileURL.deletingPathExtension().lastPathComponent // name of the source file |
| 94 | + let macroExpansionPositionRangeIndicator = |
| 95 | + "L\(macroEdit.range.lowerBound.line)C\(macroEdit.range.lowerBound.utf16index)-L\(macroEdit.range.upperBound.line)C\(macroEdit.range.upperBound.utf16index)" // github permalink notation for position range |
| 96 | + |
| 97 | + let macroExpansionFilePath = |
| 98 | + macroExpansionBufferDirectoryURL |
| 99 | + .appendingPathComponent( |
| 100 | + "\(macroExpansionFileName)_\(macroExpansionPositionRangeIndicator).\(sourceFileURL.pathExtension)" |
| 101 | + ) |
| 102 | + |
| 103 | + do { |
| 104 | + try macroEdit.newText.write(to: macroExpansionFilePath, atomically: true, encoding: .utf8) |
| 105 | + } catch { |
| 106 | + throw ResponseError.unknown( |
| 107 | + "Unable to write macro expansion to file path: \"\(macroExpansionFilePath.path)\"" |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + Task { |
| 112 | + let req = ShowDocumentRequest(uri: DocumentURI(macroExpansionFilePath), selection: macroEdit.range) |
| 113 | + |
| 114 | + let response = await orLog("Sending ShowDocumentRequest to Client") { |
| 115 | + try await sourceKitLSPServer.sendRequestToClient(req) |
| 116 | + } |
| 117 | + |
| 118 | + if let response, !response.success { |
| 119 | + logger.error("client refused to show document for \(expansion.title, privacy: .public)") |
| 120 | + } |
| 121 | + } |
| 122 | + } else if !macroEdit.newText.isEmpty { |
| 123 | + logger.fault("Unable to retrieve some parts of macro expansion") |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return expansion.edits.encodeToLSPAny() |
| 128 | + } |
| 129 | +} |
0 commit comments