Skip to content

Commit 8cb23f2

Browse files
authored
Merge pull request #179 from codedownio/prepare-rename
Add support for textDocument/prepareRename (added in LSP 3.12)
2 parents 23a59bc + 5e878d1 commit 8cb23f2

File tree

7 files changed

+93
-3
lines changed

7 files changed

+93
-3
lines changed

haskell-lsp-types/src/Language/Haskell/LSP/Types/Capabilities.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ data LSPVersion = LSPVersion Int Int -- ^ Construct a major.minor version
2121
-- | Capabilities for full conformance to the LSP specification up until a version.
2222
-- Some important milestones:
2323
--
24+
-- * 3.12 textDocument/prepareRename request
25+
-- * 3.11 CodeActionOptions provided by the server
26+
-- * 3.10 hierarchical document symbols, folding ranges
2427
-- * 3.9 completion item preselect
2528
-- * 3.8 codeAction literals
2629
-- * 3.7 related information in diagnostics
@@ -99,7 +102,7 @@ capsForVersion (LSPVersion maj min) = ClientCapabilities (Just w) (Just td) Noth
99102
(Just (CodeLensClientCapabilities dynamicReg))
100103
(Just (DocumentLinkClientCapabilities dynamicReg))
101104
(since 3 6 (ColorProviderClientCapabilities dynamicReg))
102-
(Just (RenameClientCapabilities dynamicReg))
105+
(Just (RenameClientCapabilities dynamicReg (since 3 12 True)))
103106
(Just (PublishDiagnosticsClientCapabilities (since 3 7 True)))
104107
(since 3 10 foldingRangeCapability)
105108
sync =

haskell-lsp-types/src/Language/Haskell/LSP/Types/ClientCapabilities.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,13 @@ export interface TextDocumentClientCapabilities {
564564
* Whether rename supports dynamic registration.
565565
*/
566566
dynamicRegistration?: boolean;
567+
/**
568+
* The client supports testing for validity of rename operations
569+
* before execution.
570+
*
571+
* Since 3.12.0
572+
*/
573+
prepareSupport?: boolean;
567574
};
568575
569576
/**
@@ -896,6 +903,7 @@ $(deriveJSON lspOptions ''ColorProviderClientCapabilities)
896903
data RenameClientCapabilities =
897904
RenameClientCapabilities
898905
{ _dynamicRegistration :: Maybe Bool
906+
, _prepareSupport :: Maybe Bool
899907
} deriving (Show, Read, Eq)
900908

901909
$(deriveJSON lspOptions ''RenameClientCapabilities)

haskell-lsp-types/src/Language/Haskell/LSP/Types/DataTypesJSON.hs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,31 @@ data DocumentLinkOptions =
332332

333333
deriveJSON lspOptions ''DocumentLinkOptions
334334

335+
-- ---------------------------------------------------------------------
336+
{-
337+
New in 3.12
338+
----------
339+
340+
/**
341+
* Rename options
342+
*/
343+
export interface RenameOptions {
344+
/**
345+
* Renames should be checked and tested before being executed.
346+
*/
347+
prepareProvider?: boolean;
348+
}
349+
-}
350+
351+
data RenameOptions =
352+
RenameOptionsStatic Bool
353+
| RenameOptions
354+
{ -- |Renames should be checked and tested before being executed.
355+
_prepareProvider :: Maybe Bool
356+
} deriving (Show, Read, Eq)
357+
358+
deriveJSON lspOptions { sumEncoding = A.UntaggedValue } ''RenameOptions
359+
335360
-- ---------------------------------------------------------------------
336361

337362
{-
@@ -699,7 +724,7 @@ data InitializeResponseCapabilitiesInner =
699724
-- | The server provides document formatting on typing.
700725
, _documentOnTypeFormattingProvider :: Maybe DocumentOnTypeFormattingOptions
701726
-- | The server provides rename support.
702-
, _renameProvider :: Maybe Bool
727+
, _renameProvider :: Maybe RenameOptions
703728
-- | The server provides document link support.
704729
, _documentLinkProvider :: Maybe DocumentLinkOptions
705730
-- | The server provides color provider support. Since LSP 3.6
@@ -2613,6 +2638,51 @@ deriveJSON lspOptions ''RenameParams
26132638
type RenameRequest = RequestMessage ClientMethod RenameParams WorkspaceEdit
26142639
type RenameResponse = ResponseMessage WorkspaceEdit
26152640

2641+
-- ---------------------------------------------------------------------
2642+
{-
2643+
Prepare Rename Request
2644+
2645+
Since version 3.12.0
2646+
2647+
The prepare rename request is sent from the client to the server to setup
2648+
and test the validity of a rename operation at a given location.
2649+
2650+
Request:
2651+
2652+
method: ‘textDocument/prepareRename’
2653+
params: TextDocumentPositionParams
2654+
2655+
Response:
2656+
2657+
result: Range | { range: Range, placeholder: string } | null describing
2658+
the range of the string to rename and optionally a placeholder
2659+
text of the string content to be renamed. If null is returned
2660+
then it is deemed that a ‘textDocument/rename’ request is not
2661+
valid at the given position.
2662+
error: code and message set in case an exception happens during the
2663+
prepare rename request.
2664+
2665+
-}
2666+
2667+
-- {\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"textDocument/rename\",\"params\":{\"textDocument\":{\"uri\":\"file:///home/alanz/mysrc/github/alanz/haskell-lsp/src/HieVscode.hs\"},\"position\":{\"line\":37,\"character\":17},\"newName\":\"getArgs'\"}}
2668+
2669+
data RangeWithPlaceholder =
2670+
RangeWithPlaceholder
2671+
{
2672+
_range :: Range
2673+
, _placeholder :: Text
2674+
}
2675+
2676+
deriveJSON lspOptions { sumEncoding = A.UntaggedValue } ''RangeWithPlaceholder
2677+
2678+
data RangeOrRangeWithPlaceholder = RangeWithPlaceholderValue RangeWithPlaceholder
2679+
| RangeValue Range
2680+
2681+
deriveJSON lspOptions { sumEncoding = A.UntaggedValue } ''RangeOrRangeWithPlaceholder
2682+
2683+
type PrepareRenameRequest = RequestMessage ClientMethod TextDocumentPositionParams Range
2684+
type PrepareRenameResponse = ResponseMessage (Maybe RangeOrRangeWithPlaceholder)
2685+
26162686
-- ---------------------------------------------------------------------
26172687
{-
26182688
New in 3.0

haskell-lsp-types/src/Language/Haskell/LSP/Types/Message.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ data ClientMethod =
112112
| TextDocumentRangeFormatting
113113
| TextDocumentOnTypeFormatting
114114
| TextDocumentRename
115+
| TextDocumentPrepareRename
115116
| TextDocumentFoldingRange
116117
-- A custom message type. It is not enforced that this starts with $/.
117118
| CustomClientMethod Text
@@ -158,6 +159,7 @@ instance A.FromJSON ClientMethod where
158159
parseJSON (A.String "textDocument/rangeFormatting") = return TextDocumentRangeFormatting
159160
parseJSON (A.String "textDocument/onTypeFormatting") = return TextDocumentOnTypeFormatting
160161
parseJSON (A.String "textDocument/rename") = return TextDocumentRename
162+
parseJSON (A.String "textDocument/prepareRename") = return TextDocumentPrepareRename
161163
parseJSON (A.String "textDocument/foldingRange") = return TextDocumentFoldingRange
162164
parseJSON (A.String "window/progress/cancel") = return WindowProgressCancel
163165
parseJSON (A.String x) = return (CustomClientMethod x)
@@ -202,6 +204,7 @@ instance A.ToJSON ClientMethod where
202204
toJSON TextDocumentRangeFormatting = A.String "textDocument/rangeFormatting"
203205
toJSON TextDocumentOnTypeFormatting = A.String "textDocument/onTypeFormatting"
204206
toJSON TextDocumentRename = A.String "textDocument/rename"
207+
toJSON TextDocumentPrepareRename = A.String "textDocument/prepareRename"
205208
toJSON TextDocumentFoldingRange = A.String "textDocument/foldingRange"
206209
toJSON TextDocumentDocumentLink = A.String "textDocument/documentLink"
207210
toJSON DocumentLinkResolve = A.String "documentLink/resolve"

haskell-lsp-types/src/Language/Haskell/LSP/Types/MessageFuncs.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,7 @@ fmClientRenameRequest :: J.LspId -> J.RenameParams -> J.RenameRequest
329329
fmClientRenameRequest rid params
330330
= J.RequestMessage "2.0" rid J.TextDocumentRename params
331331

332+
-- * :leftwards_arrow_with_hook: [textDocument/prepareRename](#textDocument_prepareRename)
333+
fmClientPrepareRenameRequest :: J.LspId -> J.TextDocumentPositionParams -> J.PrepareRenameRequest
334+
fmClientPrepareRenameRequest rid params
335+
= J.RequestMessage "2.0" rid J.TextDocumentPrepareRename params

src/Language/Haskell/LSP/Core.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ data Options =
108108
, codeActionProvider :: Maybe J.CodeActionOptions
109109
, codeLensProvider :: Maybe J.CodeLensOptions
110110
, documentOnTypeFormattingProvider :: Maybe J.DocumentOnTypeFormattingOptions
111+
, renameProvider :: Maybe J.RenameOptions
111112
, documentLinkProvider :: Maybe J.DocumentLinkOptions
112113
, colorProvider :: Maybe J.ColorOptions
113114
, foldingRangeProvider :: Maybe J.FoldingRangeOptions
@@ -852,7 +853,7 @@ initializeRequestHandler' onStartup mHandler tvarCtx req@(J.RequestMessage _ ori
852853
, J._documentFormattingProvider = supported (documentFormattingHandler h)
853854
, J._documentRangeFormattingProvider = supported (documentRangeFormattingHandler h)
854855
, J._documentOnTypeFormattingProvider = documentOnTypeFormattingProvider o
855-
, J._renameProvider = supported (renameHandler h)
856+
, J._renameProvider = renameProvider o
856857
, J._documentLinkProvider = documentLinkProvider o
857858
, J._colorProvider = colorProvider o
858859
, J._foldingRangeProvider = foldingRangeProvider o

test/MethodSpec.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ clientMethods = [
5555
,"textDocument/documentLink"
5656
,"documentLink/resolve"
5757
,"textDocument/rename"
58+
,"textDocument/prepareRename"
5859
]
5960

6061
serverMethods :: [T.Text]

0 commit comments

Comments
 (0)