+
+-- | Returns the references for the position in the document.
+getReferences :: TextDocumentIdentifier -- ^ The document to lookup in.
+ -> Position -- ^ The position to lookup.
+ -> Bool -- ^ Whether to include declarations as references.
+ -> Session [Location] -- ^ The locations of the references.
+getReferences doc pos inclDecl =
+ let ctx = ReferenceContext inclDecl
+ params = ReferenceParams doc pos ctx
+ in getResponseResult <$> request TextDocumentReferences params
+
+-- | Returns the definition(s) for the term at the specified position.
+getDefinitions :: TextDocumentIdentifier -- ^ The document the term is in.
+ -> Position -- ^ The position the term is at.
+ -> Session [Location] -- ^ The location(s) of the definitions
+getDefinitions doc pos = do
+ let params = TextDocumentPositionParams doc pos
+ rsp <- request TextDocumentDefinition params :: Session DefinitionResponse
+ case getResponseResult rsp of
+ SingleLoc loc -> pure [loc]
+ MultiLoc locs -> pure locs
+
+-- | Returns the type definition(s) for the term at the specified position.
+getTypeDefinitions :: TextDocumentIdentifier -- ^ The document the term is in.
+ -> Position -- ^ The position the term is at.
+ -> Session [Location] -- ^ The location(s) of the definitions
+getTypeDefinitions doc pos =
+ let params = TextDocumentPositionParams doc pos
+ in getResponseResult <$> request TextDocumentTypeDefinition params
+
+-- | Renames the term at the specified position.
+rename :: TextDocumentIdentifier -> Position -> String -> Session ()
+rename doc pos newName = do
+ let params = RenameParams doc pos (T.pack newName)
+ rsp <- request TextDocumentRename params :: Session RenameResponse
+ let wEdit = getResponseResult rsp
+ req = RequestMessage "" (IdInt 0) WorkspaceApplyEdit (ApplyWorkspaceEditParams wEdit)
+ updateState (ReqApplyWorkspaceEdit req)
+
+-- | Returns the hover information at the specified position.
+getHover :: TextDocumentIdentifier -> Position -> Session (Maybe Hover)
+getHover doc pos =
+ let params = TextDocumentPositionParams doc pos
+ in getResponseResult <$> request TextDocumentHover params
+
+-- | Returns the highlighted occurences of the term at the specified position
+getHighlights :: TextDocumentIdentifier -> Position -> Session [DocumentHighlight]
+getHighlights doc pos =
+ let params = TextDocumentPositionParams doc pos
+ in getResponseResult <$> request TextDocumentDocumentHighlight params
+
+-- | Checks the response for errors and throws an exception if needed.
+-- Returns the result if successful.
+getResponseResult :: ResponseMessage a -> a
+getResponseResult rsp = fromMaybe exc (rsp ^. result)
+ where exc = throw $ UnexpectedResponseError (rsp ^. LSP.id)
+ (fromJust $ rsp ^. LSP.error)
+
+-- | Applies formatting to the specified document.
+formatDoc :: TextDocumentIdentifier -> FormattingOptions -> Session ()
+formatDoc doc opts = do
+ let params = DocumentFormattingParams doc opts
+ edits <- getResponseResult <$> request TextDocumentFormatting params
+ applyTextEdits doc edits
+
+-- | Applies formatting to the specified range in a document.
+formatRange :: TextDocumentIdentifier -> FormattingOptions -> Range -> Session ()
+formatRange doc opts range = do
+ let params = DocumentRangeFormattingParams doc range opts
+ edits <- getResponseResult <$> request TextDocumentRangeFormatting params
+ applyTextEdits doc edits
+
+applyTextEdits :: TextDocumentIdentifier -> List TextEdit -> Session ()
+applyTextEdits doc edits =
+ let wEdit = WorkspaceEdit (Just (HashMap.singleton (doc ^. uri) edits)) Nothing
+ req = RequestMessage "" (IdInt 0) WorkspaceApplyEdit (ApplyWorkspaceEditParams wEdit)
+ in updateState (ReqApplyWorkspaceEdit req)
+
+-- | Returns the code lenses for the specified document.
+getCodeLenses :: TextDocumentIdentifier -> Session [CodeLens]
+getCodeLenses tId = do
+ rsp <- request TextDocumentCodeLens (CodeLensParams tId) :: Session CodeLensResponse
+ case getResponseResult rsp of
+ List res -> pure res