Skip to content

Commit

Permalink
Merge pull request #179 from codedownio/prepare-rename
Browse files Browse the repository at this point in the history
Add support for textDocument/prepareRename (added in LSP 3.12)
  • Loading branch information
alanz authored Aug 25, 2019
2 parents 23a59bc + 5e878d1 commit 8cb23f2
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ data LSPVersion = LSPVersion Int Int -- ^ Construct a major.minor version
-- | Capabilities for full conformance to the LSP specification up until a version.
-- Some important milestones:
--
-- * 3.12 textDocument/prepareRename request
-- * 3.11 CodeActionOptions provided by the server
-- * 3.10 hierarchical document symbols, folding ranges
-- * 3.9 completion item preselect
-- * 3.8 codeAction literals
-- * 3.7 related information in diagnostics
Expand Down Expand Up @@ -99,7 +102,7 @@ capsForVersion (LSPVersion maj min) = ClientCapabilities (Just w) (Just td) Noth
(Just (CodeLensClientCapabilities dynamicReg))
(Just (DocumentLinkClientCapabilities dynamicReg))
(since 3 6 (ColorProviderClientCapabilities dynamicReg))
(Just (RenameClientCapabilities dynamicReg))
(Just (RenameClientCapabilities dynamicReg (since 3 12 True)))
(Just (PublishDiagnosticsClientCapabilities (since 3 7 True)))
(since 3 10 foldingRangeCapability)
sync =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,13 @@ export interface TextDocumentClientCapabilities {
* Whether rename supports dynamic registration.
*/
dynamicRegistration?: boolean;
/**
* The client supports testing for validity of rename operations
* before execution.
*
* Since 3.12.0
*/
prepareSupport?: boolean;
};
/**
Expand Down Expand Up @@ -896,6 +903,7 @@ $(deriveJSON lspOptions ''ColorProviderClientCapabilities)
data RenameClientCapabilities =
RenameClientCapabilities
{ _dynamicRegistration :: Maybe Bool
, _prepareSupport :: Maybe Bool
} deriving (Show, Read, Eq)

$(deriveJSON lspOptions ''RenameClientCapabilities)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,31 @@ data DocumentLinkOptions =

deriveJSON lspOptions ''DocumentLinkOptions

-- ---------------------------------------------------------------------
{-
New in 3.12
----------
/**
* Rename options
*/
export interface RenameOptions {
/**
* Renames should be checked and tested before being executed.
*/
prepareProvider?: boolean;
}
-}

data RenameOptions =
RenameOptionsStatic Bool
| RenameOptions
{ -- |Renames should be checked and tested before being executed.
_prepareProvider :: Maybe Bool
} deriving (Show, Read, Eq)

deriveJSON lspOptions { sumEncoding = A.UntaggedValue } ''RenameOptions

-- ---------------------------------------------------------------------

{-
Expand Down Expand Up @@ -699,7 +724,7 @@ data InitializeResponseCapabilitiesInner =
-- | The server provides document formatting on typing.
, _documentOnTypeFormattingProvider :: Maybe DocumentOnTypeFormattingOptions
-- | The server provides rename support.
, _renameProvider :: Maybe Bool
, _renameProvider :: Maybe RenameOptions
-- | The server provides document link support.
, _documentLinkProvider :: Maybe DocumentLinkOptions
-- | The server provides color provider support. Since LSP 3.6
Expand Down Expand Up @@ -2613,6 +2638,51 @@ deriveJSON lspOptions ''RenameParams
type RenameRequest = RequestMessage ClientMethod RenameParams WorkspaceEdit
type RenameResponse = ResponseMessage WorkspaceEdit

-- ---------------------------------------------------------------------
{-
Prepare Rename Request
Since version 3.12.0
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.
Request:
method: ‘textDocument/prepareRename’
params: TextDocumentPositionParams
Response:
result: Range | { range: Range, placeholder: string } | null describing
the range of the string to rename and optionally a placeholder
text of the string content to be renamed. If null is returned
then it is deemed that a ‘textDocument/rename’ request is not
valid at the given position.
error: code and message set in case an exception happens during the
prepare rename request.
-}

-- {\"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'\"}}

data RangeWithPlaceholder =
RangeWithPlaceholder
{
_range :: Range
, _placeholder :: Text
}

deriveJSON lspOptions { sumEncoding = A.UntaggedValue } ''RangeWithPlaceholder

data RangeOrRangeWithPlaceholder = RangeWithPlaceholderValue RangeWithPlaceholder
| RangeValue Range

deriveJSON lspOptions { sumEncoding = A.UntaggedValue } ''RangeOrRangeWithPlaceholder

type PrepareRenameRequest = RequestMessage ClientMethod TextDocumentPositionParams Range
type PrepareRenameResponse = ResponseMessage (Maybe RangeOrRangeWithPlaceholder)

-- ---------------------------------------------------------------------
{-
New in 3.0
Expand Down
3 changes: 3 additions & 0 deletions haskell-lsp-types/src/Language/Haskell/LSP/Types/Message.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ data ClientMethod =
| TextDocumentRangeFormatting
| TextDocumentOnTypeFormatting
| TextDocumentRename
| TextDocumentPrepareRename
| TextDocumentFoldingRange
-- A custom message type. It is not enforced that this starts with $/.
| CustomClientMethod Text
Expand Down Expand Up @@ -158,6 +159,7 @@ instance A.FromJSON ClientMethod where
parseJSON (A.String "textDocument/rangeFormatting") = return TextDocumentRangeFormatting
parseJSON (A.String "textDocument/onTypeFormatting") = return TextDocumentOnTypeFormatting
parseJSON (A.String "textDocument/rename") = return TextDocumentRename
parseJSON (A.String "textDocument/prepareRename") = return TextDocumentPrepareRename
parseJSON (A.String "textDocument/foldingRange") = return TextDocumentFoldingRange
parseJSON (A.String "window/progress/cancel") = return WindowProgressCancel
parseJSON (A.String x) = return (CustomClientMethod x)
Expand Down Expand Up @@ -202,6 +204,7 @@ instance A.ToJSON ClientMethod where
toJSON TextDocumentRangeFormatting = A.String "textDocument/rangeFormatting"
toJSON TextDocumentOnTypeFormatting = A.String "textDocument/onTypeFormatting"
toJSON TextDocumentRename = A.String "textDocument/rename"
toJSON TextDocumentPrepareRename = A.String "textDocument/prepareRename"
toJSON TextDocumentFoldingRange = A.String "textDocument/foldingRange"
toJSON TextDocumentDocumentLink = A.String "textDocument/documentLink"
toJSON DocumentLinkResolve = A.String "documentLink/resolve"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,7 @@ fmClientRenameRequest :: J.LspId -> J.RenameParams -> J.RenameRequest
fmClientRenameRequest rid params
= J.RequestMessage "2.0" rid J.TextDocumentRename params

-- * :leftwards_arrow_with_hook: [textDocument/prepareRename](#textDocument_prepareRename)
fmClientPrepareRenameRequest :: J.LspId -> J.TextDocumentPositionParams -> J.PrepareRenameRequest
fmClientPrepareRenameRequest rid params
= J.RequestMessage "2.0" rid J.TextDocumentPrepareRename params
3 changes: 2 additions & 1 deletion src/Language/Haskell/LSP/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ data Options =
, codeActionProvider :: Maybe J.CodeActionOptions
, codeLensProvider :: Maybe J.CodeLensOptions
, documentOnTypeFormattingProvider :: Maybe J.DocumentOnTypeFormattingOptions
, renameProvider :: Maybe J.RenameOptions
, documentLinkProvider :: Maybe J.DocumentLinkOptions
, colorProvider :: Maybe J.ColorOptions
, foldingRangeProvider :: Maybe J.FoldingRangeOptions
Expand Down Expand Up @@ -852,7 +853,7 @@ initializeRequestHandler' onStartup mHandler tvarCtx req@(J.RequestMessage _ ori
, J._documentFormattingProvider = supported (documentFormattingHandler h)
, J._documentRangeFormattingProvider = supported (documentRangeFormattingHandler h)
, J._documentOnTypeFormattingProvider = documentOnTypeFormattingProvider o
, J._renameProvider = supported (renameHandler h)
, J._renameProvider = renameProvider o
, J._documentLinkProvider = documentLinkProvider o
, J._colorProvider = colorProvider o
, J._foldingRangeProvider = foldingRangeProvider o
Expand Down
1 change: 1 addition & 0 deletions test/MethodSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ clientMethods = [
,"textDocument/documentLink"
,"documentLink/resolve"
,"textDocument/rename"
,"textDocument/prepareRename"
]

serverMethods :: [T.Text]
Expand Down

0 comments on commit 8cb23f2

Please sign in to comment.