Skip to content

Commit 078ec02

Browse files
authored
Add textDocument/prepareRename request (#30)
* Add textDocument/prepareRename request NOTE: the type of response is not modelled fully as it's a bit involved. I hope supporting additional response shapes can be done in a follow up if needed. * Fix textDocument/prepareRename default behavior notImplemented cannot be used because the server will fail on rename. None cannot be used because it marks an invalid rename. We need to respond with a {defaultBehavior=true} so that those servers that didn't override prepareRename still have the same rename behavior as before. NOTE: I needed to model the response fully to make things work. * misc: Fix formatting
1 parent 114bd9c commit 078ec02

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/LanguageServerProtocol.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ module Server =
193193
"textDocument/completion", requestHandling (fun s p -> s.TextDocumentCompletion(p))
194194
"completionItem/resolve", requestHandling (fun s p -> s.CompletionItemResolve(p))
195195
"textDocument/rename", requestHandling (fun s p -> s.TextDocumentRename(p))
196+
"textDocument/prepareRename", requestHandling (fun s p -> s.TextDocumentPrepareRename(p))
196197
"textDocument/definition", requestHandling (fun s p -> s.TextDocumentDefinition(p))
197198
"textDocument/typeDefinition", requestHandling (fun s p -> s.TextDocumentTypeDefinition(p))
198199
"textDocument/implementation", requestHandling (fun s p -> s.TextDocumentImplementation(p))

src/Server.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ type LspServer() =
9292
abstract member TextDocumentRename: RenameParams -> AsyncLspResult<WorkspaceEdit option>
9393
default __.TextDocumentRename(_) = notImplemented
9494

95+
/// The prepare rename request is sent from the client to the server to setup and test the validity of a rename operation at a given location.
96+
/// If None is returned then it is deemed that a ‘textDocument/rename’ request is not valid at the given position.
97+
abstract member TextDocumentPrepareRename: PrepareRenameParams -> AsyncLspResult<PrepareRenameResult option>
98+
99+
default __.TextDocumentPrepareRename(_) =
100+
AsyncLspResult.success (Some(PrepareRenameResult.Default { DefaultBehavior = true }))
101+
95102
/// The goto definition request is sent from the client to the server to resolve the definition location of
96103
/// a symbol at a given text document position.
97104
abstract member TextDocumentDefinition: TextDocumentPositionParams -> AsyncLspResult<GotoResult option>

src/Types.fs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,30 @@ type RenameParams =
17861786
member this.TextDocument = this.TextDocument
17871787
member this.Position = this.Position
17881788

1789+
type PrepareRenameParams =
1790+
{ /// The document to rename.
1791+
TextDocument: TextDocumentIdentifier
1792+
1793+
/// The position at which this request was sent.
1794+
Position: Position }
1795+
interface ITextDocumentPositionParams with
1796+
member this.TextDocument = this.TextDocument
1797+
member this.Position = this.Position
1798+
1799+
type DefaultBehavior = { DefaultBehavior: bool }
1800+
1801+
type RangeWithPlaceholder = { Range: Range; Placeholder: string }
1802+
1803+
[<ErasedUnion>]
1804+
[<RequireQualifiedAccess>]
1805+
type PrepareRenameResult =
1806+
/// A range of the string to rename.
1807+
| Range of Range
1808+
/// A range of the string to rename and a placeholder text of the string content to be renamed.
1809+
| RangeWithPlaceholder of RangeWithPlaceholder
1810+
/// The rename position is valid and the client should use its default behavior to compute the rename range.
1811+
| Default of DefaultBehavior
1812+
17891813
[<ErasedUnion>]
17901814
[<RequireQualifiedAccess>]
17911815
type GotoResult =

0 commit comments

Comments
 (0)