-
Notifications
You must be signed in to change notification settings - Fork 274
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 ExecuteCommand/ApplyEdit #114
Merged
+406
−10
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cef9167
Add ExecuteCommand/ApplyEdit
rockbruno e71965f
Inject metadata at commands
rockbruno 7180d66
Avoid using decoders for retrieving dictionary data
rockbruno c20fe44
Convert CommandArgumentType -> LSPAny references
rockbruno 20c026c
Cleaning Code
rockbruno 3536217
Remove @testable from test class
rockbruno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? = nil, 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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. | ||
/// | ||
/// 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 = LSPAny? | ||
|
||
/// The command to be executed. | ||
public var command: String | ||
|
||
/// Arguments that the command should be invoked with. | ||
public var arguments: [LSPAny]? | ||
|
||
public init(command: String, arguments: [LSPAny]?) { | ||
self.command = command | ||
self.arguments = arguments | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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: [URL: [TextEdit]]? | ||
|
||
public init(changes: [URL: [TextEdit]]?) { | ||
self.changes = changes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2018 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 Foundation | ||
import SKSupport | ||
|
||
/// 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 sourcekitlsp_textDocument: TextDocumentIdentifier | ||
|
||
public init?(fromLSPDictionary dictionary: [String: LSPAny]) { | ||
let textDocumentKey = CodingKeys.sourcekitlsp_textDocument.stringValue | ||
let urlKey = TextDocumentIdentifier.CodingKeys.url.stringValue | ||
guard case .dictionary(let textDocumentDict)? = dictionary[textDocumentKey], | ||
case .string(let urlString)? = textDocumentDict[urlKey], | ||
let url = URL(string: urlString) else | ||
{ | ||
return nil | ||
} | ||
let textDocument = TextDocumentIdentifier(url) | ||
self.init(textDocument: textDocument) | ||
} | ||
|
||
public init(textDocument: TextDocumentIdentifier) { | ||
self.sourcekitlsp_textDocument = textDocument | ||
} | ||
|
||
public func encodeToLSPAny() -> LSPAny { | ||
let textDocumentArgument = LSPAny.dictionary( | ||
[TextDocumentIdentifier.CodingKeys.url.stringValue: .string(sourcekitlsp_textDocument.url.absoluteString)] | ||
) | ||
return .dictionary([CodingKeys.sourcekitlsp_textDocument.stringValue: textDocumentArgument]) | ||
} | ||
} | ||
|
||
extension CodeActionRequest { | ||
public func injectMetadata(toResponse response: CodeActionRequestResponse?) -> CodeActionRequestResponse? { | ||
let metadata = SourceKitLSPCommandMetadata(textDocument: textDocument) | ||
let metadataArgument = metadata.encodeToLSPAny() | ||
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 | ||
} | ||
} | ||
} | ||
|
||
extension ExecuteCommandRequest { | ||
/// The document in which the command was invoked. | ||
public var textDocument: TextDocumentIdentifier? { | ||
return metadata?.sourcekitlsp_textDocument | ||
} | ||
|
||
/// Optional metadata containing SourceKit-LSP information about this command. | ||
public var metadata: SourceKitLSPCommandMetadata? { | ||
guard case .dictionary(let dictionary)? = arguments?.last else { | ||
return nil | ||
} | ||
guard let metadata = SourceKitLSPCommandMetadata(fromLSPDictionary: dictionary) else { | ||
log("failed to decode lsp metadata in executeCommand request", level: .error) | ||
return nil | ||
} | ||
return metadata | ||
} | ||
|
||
/// Returns this Command's arguments without SourceKit-LSP's injected metadata, if it exists. | ||
public var argumentsWithoutSourceKitMetadata: [LSPAny]? { | ||
guard metadata != nil else { | ||
return arguments | ||
} | ||
return arguments?.dropLast() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should strip the metadata off before sending it to clang. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, since at this point the correct server can be determined we can strip it for Swift as well. Should make things more predictable 👍