+ in updateState (ReqApplyWorkspaceEdit req)
+
+-- | Adds the current version to the document, as tracked by the session.
+getVersionedDoc :: TextDocumentIdentifier -> Session VersionedTextDocumentIdentifier
+getVersionedDoc (TextDocumentIdentifier uri) = do
+ fs <- vfs <$> get
+ let ver =
+ case fs Map.!? uri of
+ Just (VirtualFile v _) -> Just v
+ _ -> Nothing
+ return (VersionedTextDocumentIdentifier uri ver)
+
+-- | Applys an edit to the document and returns the updated document version.
+applyEdit :: TextDocumentIdentifier -> TextEdit -> Session VersionedTextDocumentIdentifier
+applyEdit doc edit = do
+
+ verDoc <- getVersionedDoc doc
+
+ caps <- asks (capabilities . config)
+
+ let supportsDocChanges = fromMaybe False $ do
+ let LSP.ClientCapabilities mWorkspace _ _ = caps
+ LSP.WorkspaceClientCapabilities _ mEdit _ _ _ _ <- mWorkspace
+ LSP.WorkspaceEditClientCapabilities mDocChanges <- mEdit
+ mDocChanges
+
+ let wEdit = if supportsDocChanges
+ then
+ let docEdit = TextDocumentEdit verDoc (List [edit])
+ in WorkspaceEdit Nothing (Just (List [docEdit]))
+ else
+ let changes = HashMap.singleton (doc ^. uri) (List [edit])
+ in WorkspaceEdit (Just changes) Nothing
+
+ let req = RequestMessage "" (IdInt 0) WorkspaceApplyEdit (ApplyWorkspaceEditParams wEdit)
+ updateState (ReqApplyWorkspaceEdit req)
+
+ -- version may have changed
+ getVersionedDoc doc
+
+-- | Returns the completions for the position in the document.
+getCompletions :: TextDocumentIdentifier -> Position -> Session [CompletionItem]
+getCompletions doc pos = do
+ rsp <- sendRequest TextDocumentCompletion (TextDocumentPositionParams doc pos)
+
+ let exc = throw $ UnexpectedResponseError (rsp ^. LSP.id)
+ (fromJust $ rsp ^. LSP.error)
+ res = fromMaybe exc (rsp ^. result)
+ case res of
+ Completions (List items) -> return items
+ CompletionList (CompletionListType _ (List items)) -> return items