Skip to content
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 support for textDocument/prepareRename (added in LSP 3.12) #179

Merged
merged 6 commits into from
Aug 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -307,6 +307,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 @@ -672,7 +697,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 @@ -2586,6 +2611,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
5 changes: 3 additions & 2 deletions src/Language/Haskell/LSP/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ data Options =
, implementationProvider :: Maybe J.GotoOptions
, 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 All @@ -116,7 +117,7 @@ data Options =
instance Default Options where
def = Options Nothing Nothing Nothing Nothing Nothing
Nothing Nothing Nothing Nothing Nothing
Nothing
Nothing Nothing

-- | A function to publish diagnostics. It aggregates all diagnostics pertaining
-- to a particular version of a document, by source, and sends a
Expand Down Expand Up @@ -851,7 +852,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