From 56dafdeade26cbe6a511c6200bd76f9d0ee4012f Mon Sep 17 00:00:00 2001 From: Snowy <3mille.prenom.nom@gmail.com> Date: Sat, 2 Oct 2021 15:07:36 +0200 Subject: [PATCH 1/3] Add RenameRequest and PrepareRenameRequest --- .../Requests/PrepareRenameRequest.swift | 88 +++++++++++++++++++ .../Requests/RenameRequest.swift | 45 ++++++++++ .../CodingTests.swift | 9 ++ 3 files changed, 142 insertions(+) create mode 100644 Sources/LanguageServerProtocol/Requests/PrepareRenameRequest.swift create mode 100644 Sources/LanguageServerProtocol/Requests/RenameRequest.swift diff --git a/Sources/LanguageServerProtocol/Requests/PrepareRenameRequest.swift b/Sources/LanguageServerProtocol/Requests/PrepareRenameRequest.swift new file mode 100644 index 00000000..7951fef3 --- /dev/null +++ b/Sources/LanguageServerProtocol/Requests/PrepareRenameRequest.swift @@ -0,0 +1,88 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +/// Request to test the validity of a rename operation at the given location. +/// +/// Looks up the symbol at the given position and returns the range of the symbol the user is renaming, +/// or null if there is no symbol to be renamed at the given position +/// +/// Servers that provide rename preparation should set`prepareProvider` to true in the `renameProvider` server capability. +/// +/// - Parameters: +/// - textDocument: The document in which to lookup the symbol location. +/// - position: The document location at which to lookup symbol information. +/// +/// - Returns: A range for the symbol, and optionally a placeholder text of the string content to be renamed. +public struct PrepareRenameRequest: TextDocumentRequest, Hashable { + public static let method: String = "textDocument/prepareRename" + public typealias Response = PrepareRenameResponse? + + /// The document in which to lookup the symbol location. + public var textDocument: TextDocumentIdentifier + + /// The document location at which to lookup symbol information. + public var position: Position + + public init(textDocument: TextDocumentIdentifier, position: Position) { + self.textDocument = textDocument + self.position = position + } +} + +public struct PrepareRenameResponse: ResponseType, Hashable { + /// The range of the symbol + @CustomCodable + public var range: Range + + /// A placeholder text of the string content to be renamed + public var placeholder: String? + + public init(range: Range, placeholder: String? = nil) { + self.range = range + self.placeholder = placeholder + } + + public init(from decoder: Decoder) throws { + // Try decoding as PrepareRenameResponse + do { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.range = try container.decode(PositionRange.self, forKey: .range).wrappedValue + self.placeholder = try container.decode(String.self, forKey: .placeholder) + return + } catch {} + + // Try decoding as PositionRange + do { + self.range = try PositionRange(from: decoder).wrappedValue + self.placeholder = nil + return + } catch {} + + let context = DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Expected PrepareRenameResponse or PositionRange") + throw DecodingError.dataCorrupted(context) + } + + public func encode(to encoder: Encoder) throws { + if let placeholder = placeholder { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(_range, forKey: .range) + try container.encode(placeholder, forKey: .placeholder) + } else { + try _range.encode(to: encoder) + } + } + + private enum CodingKeys: String, CodingKey { + case range + case placeholder + } +} diff --git a/Sources/LanguageServerProtocol/Requests/RenameRequest.swift b/Sources/LanguageServerProtocol/Requests/RenameRequest.swift new file mode 100644 index 00000000..ee3e1332 --- /dev/null +++ b/Sources/LanguageServerProtocol/Requests/RenameRequest.swift @@ -0,0 +1,45 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +/// Request to compute a workspace range to rename a symbol. The change is then performed client side. +/// +/// Looks up the symbol at the given position and returns the range of the symbol the user is renaming, +/// or null if there is no symbol to be renamed at the given position +/// +/// Servers that allow renaming should set the `renameProvider` server capability. +/// +/// - Parameters: +/// - textDocument: The document in which the selected symbol is. +/// - position: The document location at which the selected symbol is. +/// - newName: The new name of the symbol. +/// +/// - Returns: A workspace edit +public struct RenameRequest: TextDocumentRequest, Hashable { + public static let method: String = "textDocument/rename" + public typealias Response = WorkspaceEdit? + + /// The document in which the selected symbol is. + public var textDocument: TextDocumentIdentifier + + /// The document location at which the selected symbol is. + public var position: Position + + /// The new name of the symbol. If the given name is not valid the request must return + /// a ResponseError with an appropriate message set. + public var newName: String + + public init(textDocument: TextDocumentIdentifier, position: Position, newName: String) { + self.textDocument = textDocument + self.position = position + self.newName = newName + } +} diff --git a/Tests/LanguageServerProtocolTests/CodingTests.swift b/Tests/LanguageServerProtocolTests/CodingTests.swift index 9df028ba..b0383904 100644 --- a/Tests/LanguageServerProtocolTests/CodingTests.swift +++ b/Tests/LanguageServerProtocolTests/CodingTests.swift @@ -303,6 +303,15 @@ final class CodingTests: XCTestCase { checkCoding(CompletionItemDocumentation.string("Some documentation"), json: """ "Some documentation" """) + + checkCoding(PrepareRenameResponse(range: range), json: rangejson) + + checkCoding(PrepareRenameResponse(range: range, placeholder: "somePlaceholder"), json: """ + { + "placeholder" : "somePlaceholder", + "range" : \(rangejson.indented(2, skipFirstLine: true)) + } + """) checkCoding(LocationsOrLocationLinksResponse.locations([Location(uri: uri, range: range)]), json: """ [ From c56e5fb728b7c1c28725573406ecf8b7f3af6a58 Mon Sep 17 00:00:00 2001 From: Snowy <3mille.prenom.nom@gmail.com> Date: Sat, 2 Oct 2021 15:35:00 +0200 Subject: [PATCH 2/3] Register rename requests --- Sources/LanguageServerProtocol/CMakeLists.txt | 2 ++ Sources/LanguageServerProtocol/Messages.swift | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Sources/LanguageServerProtocol/CMakeLists.txt b/Sources/LanguageServerProtocol/CMakeLists.txt index ca3405fe..c4242ed9 100644 --- a/Sources/LanguageServerProtocol/CMakeLists.txt +++ b/Sources/LanguageServerProtocol/CMakeLists.txt @@ -43,8 +43,10 @@ add_library(LanguageServerProtocol Requests/InitializeRequest.swift Requests/InlayHintsRequest.swift Requests/PollIndexRequest.swift + Requests/PrepareRenameRequest.swift Requests/ReferencesRequest.swift Requests/RegisterCapabilityRequest.swift + Requests/RenameRequest.swift Requests/ShowMessageRequest.swift Requests/ShutdownRequest.swift Requests/SymbolInfoRequest.swift diff --git a/Sources/LanguageServerProtocol/Messages.swift b/Sources/LanguageServerProtocol/Messages.swift index dcb12ca1..3791b8c8 100644 --- a/Sources/LanguageServerProtocol/Messages.swift +++ b/Sources/LanguageServerProtocol/Messages.swift @@ -43,6 +43,8 @@ public let builtinRequests: [_RequestType.Type] = [ CodeActionRequest.self, ExecuteCommandRequest.self, ApplyEditRequest.self, + PrepareRenameRequest.self, + RenameRequest.self, RegisterCapabilityRequest.self, UnregisterCapabilityRequest.self, From b70615c3757b7d8b04c457a93b3525405e7dc417 Mon Sep 17 00:00:00 2001 From: Emil <3mille.prenom.nom@gmail.com> Date: Mon, 4 Oct 2021 15:48:13 +0200 Subject: [PATCH 3/3] Add missing space in doc comment Co-authored-by: Alex Hoppen --- .../LanguageServerProtocol/Requests/PrepareRenameRequest.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/LanguageServerProtocol/Requests/PrepareRenameRequest.swift b/Sources/LanguageServerProtocol/Requests/PrepareRenameRequest.swift index 7951fef3..6ee75f60 100644 --- a/Sources/LanguageServerProtocol/Requests/PrepareRenameRequest.swift +++ b/Sources/LanguageServerProtocol/Requests/PrepareRenameRequest.swift @@ -15,7 +15,7 @@ /// Looks up the symbol at the given position and returns the range of the symbol the user is renaming, /// or null if there is no symbol to be renamed at the given position /// -/// Servers that provide rename preparation should set`prepareProvider` to true in the `renameProvider` server capability. +/// Servers that provide rename preparation should set `prepareProvider` to true in the `renameProvider` server capability. /// /// - Parameters: /// - textDocument: The document in which to lookup the symbol location.