Skip to content

Add LSP support for showing Macro Expansions #1436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/LanguageServerProtocol/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ add_library(LanguageServerProtocol STATIC
Requests/RegisterCapabilityRequest.swift
Requests/RenameRequest.swift
Requests/SelectionRangeRequest.swift
Requests/ShowDocumentRequest.swift
Requests/ShowMessageRequest.swift
Requests/ShutdownRequest.swift
Requests/SignatureHelpRequest.swift
Expand Down
1 change: 1 addition & 0 deletions Sources/LanguageServerProtocol/Messages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public let builtinRequests: [_RequestType.Type] = [
RegisterCapabilityRequest.self,
RenameRequest.self,
SelectionRangeRequest.self,
ShowDocumentRequest.self,
ShowMessageRequest.self,
ShutdownRequest.self,
SignatureHelpRequest.self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//

/// Request from the server to the client to show a document on the client
/// side.
public struct ShowDocumentRequest: RequestType {
public static let method: String = "window/showDocument"
public typealias Response = ShowDocumentResponse

/// The uri to show.
public var uri: DocumentURI

/// An optional boolean indicates to show the resource in an external
/// program. To show, for example, `https://www.swift.org/ in the default WEB
/// browser set `external` to `true`.
public var external: Bool?

/// An optional boolean to indicate whether the editor showing the document
/// should take focus or not. Clients might ignore this property if an
/// external program is started.
public var takeFocus: Bool?

/// An optional selection range if the document is a text document. Clients
/// might ignore the property if an external program is started or the file
/// is not a text file.
public var selection: Range<Position>?

public init(uri: DocumentURI, external: Bool? = nil, takeFocus: Bool? = nil, selection: Range<Position>? = nil) {
self.uri = uri
self.external = external
self.takeFocus = takeFocus
self.selection = selection
}
}

public struct ShowDocumentResponse: Codable, Hashable, ResponseType {
/// A boolean indicating if the show was successful.
public var success: Bool

public init(success: Bool) {
self.success = success
}
}
3 changes: 3 additions & 0 deletions Sources/SKCore/ExperimentalFeatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ public enum ExperimentalFeature: String, Codable, Sendable, CaseIterable {

/// Add `--experimental-prepare-for-indexing` to the `swift build` command run to prepare a target for indexing.
case swiftpmPrepareForIndexing = "swiftpm-prepare-for-indexing"

/// Enable showing macro expansions via `ShowDocumentRequest`
case showMacroExpansions = "show-macro-expansions"
}
7 changes: 4 additions & 3 deletions Sources/SKSupport/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ extension AbsolutePath {
}
}

/// The directory to write generated module interfaces
public var defaultDirectoryForGeneratedInterfaces: AbsolutePath {
try! AbsolutePath(validating: NSTemporaryDirectory()).appending(component: "GeneratedInterfaces")
/// The default directory to write generated files
/// `<TEMPORARY_DIRECTORY>/sourcekit-lsp/`
public var defaultDirectoryForGeneratedFiles: AbsolutePath {
try! AbsolutePath(validating: NSTemporaryDirectory()).appending(component: "sourcekit-lsp")
}
5 changes: 5 additions & 0 deletions Sources/SourceKitLSP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ target_sources(SourceKitLSP PRIVATE
Swift/DiagnosticReportManager.swift
Swift/DocumentFormatting.swift
Swift/DocumentSymbols.swift
Swift/ExpandMacroCommand.swift
Swift/FoldingRange.swift
Swift/MacroExpansion.swift
Swift/OpenInterface.swift
Swift/Refactoring.swift
Swift/RefactoringEdit.swift
Swift/RefactorCommand.swift
Swift/RelatedIdentifiers.swift
Swift/RewriteSourceKitPlaceholders.swift
Swift/SemanticRefactorCommand.swift
Expand Down
20 changes: 16 additions & 4 deletions Sources/SourceKitLSP/SourceKitLSPServer+Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,20 @@ extension SourceKitLSPServer {
/// Options for code-completion.
public var completionOptions: SKCompletionOptions

/// Override the default directory where generated interfaces will be stored
public var generatedInterfacesPath: AbsolutePath
/// Override the default directory where generated files will be stored
public var generatedFilesPath: AbsolutePath

/// Path to the generated interfaces
/// `<generatedFilesPath>/GeneratedInterfaces/`
public var generatedInterfacesPath: AbsolutePath {
generatedFilesPath.appending(component: "GeneratedInterfaces")
}

/// Path to the generated macro expansions
/// `<generatedFilesPath>`/GeneratedMacroExpansions/
public var generatedMacroExpansionsPath: AbsolutePath {
generatedFilesPath.appending(component: "GeneratedMacroExpansions")
}

/// The time that `SwiftLanguageService` should wait after an edit before starting to compute diagnostics and
/// sending a `PublishDiagnosticsNotification`.
Expand All @@ -64,7 +76,7 @@ extension SourceKitLSPServer {
compilationDatabaseSearchPaths: [RelativePath] = [],
indexOptions: IndexOptions = .init(),
completionOptions: SKCompletionOptions = .init(),
generatedInterfacesPath: AbsolutePath = defaultDirectoryForGeneratedInterfaces,
generatedFilesPath: AbsolutePath = defaultDirectoryForGeneratedFiles,
swiftPublishDiagnosticsDebounceDuration: TimeInterval = 2, /* 2s */
workDoneProgressDebounceDuration: Duration = .seconds(0),
experimentalFeatures: Set<ExperimentalFeature> = [],
Expand All @@ -75,7 +87,7 @@ extension SourceKitLSPServer {
self.compilationDatabaseSearchPaths = compilationDatabaseSearchPaths
self.indexOptions = indexOptions
self.completionOptions = completionOptions
self.generatedInterfacesPath = generatedInterfacesPath
self.generatedFilesPath = generatedFilesPath
self.swiftPublishDiagnosticsDebounceDuration = swiftPublishDiagnosticsDebounceDuration
self.experimentalFeatures = experimentalFeatures
self.workDoneProgressDebounceDuration = workDoneProgressDebounceDuration
Expand Down
80 changes: 80 additions & 0 deletions Sources/SourceKitLSP/Swift/ExpandMacroCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import LanguageServerProtocol
import SourceKitD

public struct ExpandMacroCommand: RefactorCommand {
typealias Response = MacroExpansion

public static let identifier: String = "expand.macro.command"

/// The name of this refactoring action.
public var title = "Expand Macro"

/// The sourcekitd identifier of the refactoring action.
public var actionString = "source.refactoring.kind.expand.macro"

/// The range to expand.
public var positionRange: Range<Position>

/// The text document related to the refactoring action.
public var textDocument: TextDocumentIdentifier

public init(positionRange: Range<Position>, textDocument: TextDocumentIdentifier) {
self.positionRange = positionRange
self.textDocument = textDocument
}

public init?(fromLSPDictionary dictionary: [String: LSPAny]) {
guard case .dictionary(let documentDict)? = dictionary[CodingKeys.textDocument.stringValue],
case .string(let title)? = dictionary[CodingKeys.title.stringValue],
case .string(let actionString)? = dictionary[CodingKeys.actionString.stringValue],
case .dictionary(let rangeDict)? = dictionary[CodingKeys.positionRange.stringValue]
else {
return nil
}
guard let positionRange = Range<Position>(fromLSPDictionary: rangeDict),
let textDocument = TextDocumentIdentifier(fromLSPDictionary: documentDict)
else {
return nil
}

self.init(
title: title,
actionString: actionString,
positionRange: positionRange,
textDocument: textDocument
)
}

public init(
title: String,
actionString: String,
positionRange: Range<Position>,
textDocument: TextDocumentIdentifier
) {
self.title = title
self.actionString = actionString
self.positionRange = positionRange
self.textDocument = textDocument
}

public func encodeToLSPAny() -> LSPAny {
return .dictionary([
CodingKeys.title.stringValue: .string(title),
CodingKeys.actionString.stringValue: .string(actionString),
CodingKeys.positionRange.stringValue: positionRange.encodeToLSPAny(),
CodingKeys.textDocument.stringValue: textDocument.encodeToLSPAny(),
])
}
}
132 changes: 132 additions & 0 deletions Sources/SourceKitLSP/Swift/MacroExpansion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation
import LSPLogging
import LanguageServerProtocol
import SourceKitD

/// Detailed information about the result of a macro expansion operation.
///
/// Wraps the information returned by sourcekitd's `semantic_refactoring`
/// request, such as the necessary macro expansion edits.
struct MacroExpansion: RefactoringResponse {
/// The title of the refactoring action.
var title: String

/// The URI of the file where the macro is used
var uri: DocumentURI

/// The resulting array of `RefactoringEdit` of a semantic refactoring request
var edits: [RefactoringEdit]

init(title: String, uri: DocumentURI, refactoringEdits: [RefactoringEdit]) {
self.title = title
self.uri = uri
self.edits = refactoringEdits.compactMap { refactoringEdit in
if refactoringEdit.bufferName == nil && !refactoringEdit.newText.isEmpty {
logger.fault("Unable to retrieve some parts of the expansion")
return nil
}

return refactoringEdit
}
}
}

extension SwiftLanguageService {
/// Handles the `ExpandMacroCommand`.
///
/// Makes a request to sourcekitd and wraps the result into a `MacroExpansion`
/// and then makes a `ShowDocumentRequest` to the client side for each
/// expansion to be displayed.
///
/// - Parameters:
/// - expandMacroCommand: The `ExpandMacroCommand` that triggered this request.
///
/// - Returns: A `[RefactoringEdit]` with the necessary edits and buffer name as a `LSPAny`
func expandMacro(
_ expandMacroCommand: ExpandMacroCommand
) async throws -> LSPAny {
guard let sourceKitLSPServer else {
// `SourceKitLSPServer` has been destructed. We are tearing down the
// language server. Nothing left to do.
throw ResponseError.unknown("Connection to the editor closed")
}

guard let sourceFileURL = expandMacroCommand.textDocument.uri.fileURL else {
throw ResponseError.unknown("Given URI is not a file URL")
}

let expansion = try await self.refactoring(expandMacroCommand)

for macroEdit in expansion.edits {
if let bufferName = macroEdit.bufferName {
// buffer name without ".swift"
let macroExpansionBufferDirectoryName =
bufferName.hasSuffix(".swift")
? String(bufferName.dropLast(6))
: bufferName

let macroExpansionBufferDirectoryURL = self.generatedMacroExpansionsPath
.appendingPathComponent(macroExpansionBufferDirectoryName)
do {
try FileManager.default.createDirectory(
at: macroExpansionBufferDirectoryURL,
withIntermediateDirectories: true
)
} catch {
throw ResponseError.unknown(
"Failed to create directory for macro expansion buffer at path: \(macroExpansionBufferDirectoryURL.path)"
)
}

// name of the source file
let macroExpansionFileName = sourceFileURL.deletingPathExtension().lastPathComponent

// github permalink notation for position range
let macroExpansionPositionRangeIndicator =
"L\(macroEdit.range.lowerBound.line)C\(macroEdit.range.lowerBound.utf16index)-L\(macroEdit.range.upperBound.line)C\(macroEdit.range.upperBound.utf16index)"

let macroExpansionFilePath =
macroExpansionBufferDirectoryURL
.appendingPathComponent(
"\(macroExpansionFileName)_\(macroExpansionPositionRangeIndicator).\(sourceFileURL.pathExtension)"
)

do {
try macroEdit.newText.write(to: macroExpansionFilePath, atomically: true, encoding: .utf8)
} catch {
throw ResponseError.unknown(
"Unable to write macro expansion to file path: \"\(macroExpansionFilePath.path)\""
)
}

Task {
let req = ShowDocumentRequest(uri: DocumentURI(macroExpansionFilePath), selection: macroEdit.range)

let response = await orLog("Sending ShowDocumentRequest to Client") {
try await sourceKitLSPServer.sendRequestToClient(req)
}

if let response, !response.success {
logger.error("client refused to show document for \(expansion.title, privacy: .public)")
}
}
} else if !macroEdit.newText.isEmpty {
logger.fault("Unable to retrieve some parts of macro expansion")
}
}

return expansion.edits.encodeToLSPAny()
}
}
31 changes: 31 additions & 0 deletions Sources/SourceKitLSP/Swift/RefactorCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import LanguageServerProtocol
import SourceKitD

/// A protocol to be utilised by all commands that are served by sourcekitd refactorings.
protocol RefactorCommand: SwiftCommand {
/// The response type of the refactor command
associatedtype Response: RefactoringResponse

/// The sourcekitd identifier of the refactoring action.
var actionString: String { get set }

/// The range to refactor.
var positionRange: Range<Position> { get set }

/// The text document related to the refactoring action.
var textDocument: TextDocumentIdentifier { get set }

init(title: String, actionString: String, positionRange: Range<Position>, textDocument: TextDocumentIdentifier)
}
Loading