Skip to content

Commit 0563434

Browse files
committed
Clean up tacticCmd
1 parent 41ebd2e commit 0563434

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

plugins/hls-tactics-plugin/src/Ide/Plugin/Tactic.hs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import Language.LSP.Types.Capabilities
4545
import OccName
4646
import Prelude hiding (span)
4747
import System.Timeout
48+
import Control.Exception (evaluate)
49+
import Data.Bifunctor (Bifunctor(bimap))
4850

4951

5052
descriptor :: PluginId -> PluginDescriptor IdeState
@@ -59,19 +61,18 @@ descriptor plId = (defaultPluginDescriptor plId)
5961
, pluginHandlers = mkPluginHandler STextDocumentCodeAction codeActionProvider
6062
}
6163

64+
6265
tacticDesc :: T.Text -> T.Text
6366
tacticDesc name = "fill the hole using the " <> name <> " tactic"
6467

6568

66-
6769
------------------------------------------------------------------------------
6870
-- | The name of the command for the LS.
6971
tcCommandName :: TacticCommand -> T.Text
7072
tcCommandName = T.pack . show
7173

7274

7375

74-
7576
codeActionProvider :: PluginMethodHandler IdeState TextDocumentCodeAction
7677
codeActionProvider state plId (CodeActionParams _ _ (TextDocumentIdentifier uri) range _ctx)
7778
| Just nfp <- uriToNormalizedFilePath $ toNormalizedUri uri =
@@ -98,24 +99,30 @@ tacticCmd tac state (TacticParams uri range var_name)
9899
let span = rangeToRealSrcSpan (fromNormalizedFilePath nfp) range'
99100
pm <- MaybeT $ useAnnotatedSource "tacticsCmd" state nfp
100101

101-
x <- lift $ timeout 2e8 $ pure $
102-
case runTactic ctx jdg $ tac $ mkVarOcc $ T.unpack var_name of
103-
Left err ->
104-
Left $ mkErr InvalidRequest $ T.pack $ show err
105-
Right rtr ->
106-
mkWorkspaceEdits rtr span dflags clientCapabilities uri pm
107-
pure $ joinNote (mkErr InvalidRequest "timed out") x
102+
timingOut 2e8 $ join $
103+
bimap (mkErr InvalidRequest . T.pack . show)
104+
(mkWorkspaceEdits span dflags clientCapabilities uri pm)
105+
$ runTactic ctx jdg $ tac $ mkVarOcc $ T.unpack var_name
108106

109107
case res of
110108
Left err -> pure $ Left err
111109
Right medit -> do
112110
forM_ medit $ \edit ->
113-
sendRequest SWorkspaceApplyEdit (ApplyWorkspaceEditParams Nothing edit) (\_ -> pure ())
111+
sendRequest
112+
SWorkspaceApplyEdit
113+
(ApplyWorkspaceEditParams Nothing edit)
114+
(const $ pure ())
114115
pure $ Right Null
115116
tacticCmd _ _ _ =
116117
pure $ Left $ mkErr InvalidRequest "Bad URI"
117118

118119

120+
timingOut :: Int -> (Either ResponseError a) -> MaybeT IO (Either ResponseError a)
121+
timingOut t m = do
122+
x <- lift $ timeout t $ evaluate m
123+
pure $ joinNote (mkErr InvalidRequest "timed out") x
124+
125+
119126
mkErr :: ErrorCode -> T.Text -> ResponseError
120127
mkErr code err = ResponseError code err Nothing
121128

@@ -129,14 +136,14 @@ joinNote _ (Just a) = a
129136
-- | Turn a 'RunTacticResults' into concrete edits to make in the source
130137
-- document.
131138
mkWorkspaceEdits
132-
:: RunTacticResults
133-
-> RealSrcSpan
139+
:: RealSrcSpan
134140
-> DynFlags
135141
-> ClientCapabilities
136142
-> Uri
137143
-> Annotated ParsedSource
144+
-> RunTacticResults
138145
-> Either ResponseError (Maybe WorkspaceEdit)
139-
mkWorkspaceEdits rtr span dflags clientCapabilities uri pm = do
146+
mkWorkspaceEdits span dflags clientCapabilities uri pm rtr = do
140147
let g = graftHole (RealSrcSpan span) rtr
141148
response = transform dflags clientCapabilities uri g pm
142149
in case response of

plugins/hls-tactics-plugin/src/Ide/Plugin/Tactic/LanguageServer.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ getSpanAndTypeAtHole amapping range hf = do
132132
pure (nodeSpan ast', ty)
133133

134134

135-
136135
liftMaybe :: Monad m => Maybe a -> MaybeT m a
137136
liftMaybe a = MaybeT $ pure a
138137

0 commit comments

Comments
 (0)