Skip to content
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

Add Local Refactoring support #70

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ SourceKit-LSP is still in early development, so you may run into rough edges wit
| Find References | ✅ | |
| Background Indexing | ❌ | Build project to update the index using [Indexing While Building](#indexing-while-building) |
| Workspace Symbols | ❌ | |
| Refactoring | ❌ | |
| Global Rename | ❌ | |
| Local Refactoring | ✅ | |
| Formatting | ❌ | |
| Folding | ✅ | |
| Syntax Highlighting | ❌ | Not currently part of LSP. |
Expand Down
47 changes: 47 additions & 0 deletions Sources/LanguageServerProtocol/ApplyEdit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

/// Request from the server to the client to modify resources on the client side.
///
/// - Parameters:
/// - label: An optional label of the workspace edit.
/// - edit: The edits to apply.
public struct ApplyEditRequest: RequestType {
public static let method: String = "workspace/applyEdit"
public typealias Response = ApplyEditResponse?

/// An optional label of the workspace edit.
/// Used by the client's user interface for things such as
/// the stack to undo the workspace edit.
public var label: String?

/// The edits to apply.
public var edit: WorkspaceEdit

public init(label: String?, edit: WorkspaceEdit) {
self.label = label
self.edit = edit
}
}

public struct ApplyEditResponse: Codable, Hashable, ResponseType {
/// Indicates whether the edit was applied or not.
public var applied: Bool

/// An optional textual description for why the edit was not applied.
public var failureReason: String?

public init(applied: Bool, failureReason: String?) {
self.applied = applied
self.failureReason = failureReason
}
}
11 changes: 11 additions & 0 deletions Sources/LanguageServerProtocol/ClientCapabilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,23 @@ public struct TextDocumentClientCapabilities: Hashable, Codable {
///
/// If specified, the client *also* guarantees that it will handle unknown kinds gracefully.
public var valueSet: [LanguageServerProtocol.CodeActionKind]

public init(valueSet: [LanguageServerProtocol.CodeActionKind]) {
self.valueSet = valueSet
}
}

public var codeActionKind: CodeActionKind

public init(codeActionKind: CodeActionKind) {
self.codeActionKind = codeActionKind
}
}

public var codeActionLiteralSupport: CodeActionLiteralSupport? = nil

public init() {
}
}

/// Capabilities specific to `textDocument/publishDiagnostics`.
Expand Down
31 changes: 30 additions & 1 deletion Sources/LanguageServerProtocol/CodeAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import Foundation
import SKSupport

public typealias CodeActionProviderCompletion = (([CodeAction]) -> Void)
public typealias CodeActionProvider = ((CodeActionRequest, @escaping CodeActionProviderCompletion) -> Void)
Expand Down Expand Up @@ -47,6 +48,30 @@ public struct CodeActionRequest: TextDocumentRequest, Hashable {
self.context = context
self.textDocument = textDocument
}

public func injectMetadata(atResponse response: CodeActionRequestResponse?) -> CodeActionRequestResponse? {
let metadata = SourceKitLSPCommandMetadata(textDocument: textDocument)
guard let data = try? JSONEncoder().encode(metadata),
let metadataArgument = try? JSONDecoder().decode(CommandArgumentType.self, from: data) else
{
log("failed to inject metadata in codeAction response", level: .error)
return nil
}
switch response {
case .codeActions(var codeActions)?:
for i in 0..<codeActions.count {
codeActions[i].command?.arguments?.append(metadataArgument)
}
return .codeActions(codeActions)
case .commands(var commands)?:
for i in 0..<commands.count {
commands[i].arguments?.append(metadataArgument)
}
return .commands(commands)
case nil:
return nil
}
}
}

/// Wrapper type for the response of a CodeAction request.
Expand Down Expand Up @@ -123,15 +148,19 @@ public struct CodeAction: Codable, Equatable, ResponseType {
/// The diagnostics that this code action resolves, if applicable.
public var diagnostics: [Diagnostic]?

/// The workspace edit this code action performs.
public var edit: WorkspaceEdit?

/// A command this code action executes.
/// If a code action provides an edit and a command,
/// first the edit is executed and then the command.
public var command: Command?

public init(title: String, kind: CodeActionKind? = nil, diagnostics: [Diagnostic]? = nil, command: Command? = nil) {
public init(title: String, kind: CodeActionKind? = nil, diagnostics: [Diagnostic]? = nil, edit: WorkspaceEdit? = nil, command: Command? = nil) {
self.title = title
self.kind = kind
self.diagnostics = diagnostics
self.edit = edit
self.command = command
}
}
2 changes: 1 addition & 1 deletion Sources/LanguageServerProtocol/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct Command: Codable, Hashable {
}
}

public enum CommandArgumentType: Hashable {
public enum CommandArgumentType: Hashable, ResponseType {
case null
case int(Int)
case bool(Bool)
Expand Down
2 changes: 1 addition & 1 deletion Sources/LanguageServerProtocol/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public final class LocalConnection {

var state: State = .ready

var handler: MessageHandler? = nil
public internal(set) var handler: MessageHandler? = nil

public init() {}

Expand Down
69 changes: 69 additions & 0 deletions Sources/LanguageServerProtocol/ExecuteCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//===----------------------------------------------------------------------===//
//
// 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

/// Request sent from the client to to trigger command execution on the server.
Copy link
Contributor

@literalpie literalpie Aug 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a duplicate "to" here

///
/// The execution of this request can be the result of a request that returns a command,
/// such as CodeActionsRequest and CodeLensRequest. In most cases, the server creates a WorkspaceEdit
/// structure and applies the changes to the workspace using the ApplyEditRequest.
///
/// Servers that provide command execution should set the `executeCommand` server capability.
///
/// - Parameters:
/// - command: The command to be executed.
/// - arguments: The arguments to use when executing the command.
public struct ExecuteCommandRequest: RequestType {
public static let method: String = "workspace/executeCommand"

// Note: The LSP type for this response is `Any?`.
public typealias Response = CommandArgumentType?

/// The command to be executed.
public var command: String

/// Arguments that the command should be invoked with.
public var arguments: [CommandArgumentType]?

/// The document in which the command was invoked.
public var textDocument: TextDocumentIdentifier? {
return metadata?.textDocument
}

/// Optional metadata containing SourceKit-LSP infomration about this command.
public var metadata: SourceKitLSPCommandMetadata? {
guard case .dictionary(let dictionary)? = arguments?.last else {
return nil
}
guard let data = try? JSONEncoder().encode(dictionary) else {
return nil
}
return try? JSONDecoder().decode(SourceKitLSPCommandMetadata.self, from: data)
}

public init(command: String, arguments: [CommandArgumentType]?) {
self.command = command
self.arguments = arguments
}
}

/// Represents metadata that SourceKit-LSP injects at every command returned by code actions.
/// The ExecuteCommand is not a TextDocumentRequest, so metadata is injected to allow SourceKit-LSP
/// to determine where a command should be executed.
public struct SourceKitLSPCommandMetadata: Codable, Hashable {
public var textDocument: TextDocumentIdentifier

public init(textDocument: TextDocumentIdentifier) {
self.textDocument = textDocument
}
}
1 change: 1 addition & 0 deletions Sources/LanguageServerProtocol/Messages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public let builtinRequests: [_RequestType.Type] = [
DocumentColorRequest.self,
ColorPresentationRequest.self,
CodeActionRequest.self,
ExecuteCommandRequest.self,

// MARK: LSP Extension Requests

Expand Down
18 changes: 17 additions & 1 deletion Sources/LanguageServerProtocol/ServerCapabilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public struct ServerCapabilities: Codable, Hashable {
/// Whether the server provides "textDocument/codeAction".
public var codeActionProvider: CodeActionServerCapabilities?

/// Whether the server provides "workspace/executeCommand".
public var executeCommandProvider: ExecuteCommandOptions?

// TODO: fill-in the rest.

public init(
Expand All @@ -66,7 +69,8 @@ public struct ServerCapabilities: Codable, Hashable {
foldingRangeProvider: Bool? = nil,
documentSymbolProvider: Bool? = nil,
colorProvider: Bool? = nil,
codeActionProvider: CodeActionServerCapabilities? = nil
codeActionProvider: CodeActionServerCapabilities? = nil,
executeCommandProvider: ExecuteCommandOptions? = nil
)
{
self.textDocumentSync = textDocumentSync
Expand All @@ -82,6 +86,7 @@ public struct ServerCapabilities: Codable, Hashable {
self.documentSymbolProvider = documentSymbolProvider
self.colorProvider = colorProvider
self.codeActionProvider = codeActionProvider
self.executeCommandProvider = executeCommandProvider
}

public init(from decoder: Decoder) throws {
Expand All @@ -93,6 +98,7 @@ public struct ServerCapabilities: Codable, Hashable {
self.documentSymbolProvider = try container.decodeIfPresent(Bool.self, forKey: .documentSymbolProvider)
self.colorProvider = try container.decodeIfPresent(Bool.self, forKey: .colorProvider)
self.codeActionProvider = try container.decodeIfPresent(CodeActionServerCapabilities.self, forKey: .codeActionProvider)
self.executeCommandProvider = try container.decodeIfPresent(ExecuteCommandOptions.self, forKey: .executeCommandProvider)

if let textDocumentSync = try? container.decode(TextDocumentSyncOptions.self, forKey: .textDocumentSync) {
self.textDocumentSync = textDocumentSync
Expand Down Expand Up @@ -234,3 +240,13 @@ public struct CodeActionOptions: Codable, Hashable {
self.codeActionKinds = codeActionKinds
}
}

public struct ExecuteCommandOptions: Codable, Hashable {

/// The commands to be executed on this server.
public var commands: [String]

public init(commands: [String]) {
self.commands = commands
}
}
26 changes: 26 additions & 0 deletions Sources/LanguageServerProtocol/WorkspaceEdit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

/// A workspace edit represents changes to many resources managed in the workspace.
public struct WorkspaceEdit: Codable, Hashable, ResponseType {

/// The edits to be applied to existing resources.
public var changes: [String: [TextEdit]]?

public init(changes: [URL: [TextEdit]]?) {
guard let changes = changes else {
return
}
let changesArray = changes.map { ($0.key.absoluteString, $0.value) }
self.changes = Dictionary(uniqueKeysWithValues: changesArray)
}
}
6 changes: 5 additions & 1 deletion Sources/SKTestSupport/TestJSONRPCConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public final class TestClient: LanguageServerEndpoint {
var oneShotNotificationHandlers: [((Any) -> Void)] = []

public var allowUnexpectedNotification: Bool = false
public var allowUnexpectedRequest: Bool = false

public func appendOneShotNotificationHandler<N>(_ handler: @escaping (Notification<N>) -> Void) {
oneShotNotificationHandlers.append({ anyNote in
Expand Down Expand Up @@ -125,7 +126,10 @@ public final class TestClient: LanguageServerEndpoint {
}

override public func _handleUnknown<R>(_ request: Request<R>) where R : RequestType {
fatalError()
guard allowUnexpectedRequest else {
fatalError("unexpected request \(request)")
}
request.reply(.failure(.cancelled))
}
}

Expand Down
Loading