@@ -45,6 +45,8 @@ import Language.LSP.Types.Capabilities
45
45
import OccName
46
46
import Prelude hiding (span )
47
47
import System.Timeout
48
+ import Control.Exception (evaluate )
49
+ import Data.Bifunctor (Bifunctor (bimap ))
48
50
49
51
50
52
descriptor :: PluginId -> PluginDescriptor IdeState
@@ -59,19 +61,18 @@ descriptor plId = (defaultPluginDescriptor plId)
59
61
, pluginHandlers = mkPluginHandler STextDocumentCodeAction codeActionProvider
60
62
}
61
63
64
+
62
65
tacticDesc :: T. Text -> T. Text
63
66
tacticDesc name = " fill the hole using the " <> name <> " tactic"
64
67
65
68
66
-
67
69
------------------------------------------------------------------------------
68
70
-- | The name of the command for the LS.
69
71
tcCommandName :: TacticCommand -> T. Text
70
72
tcCommandName = T. pack . show
71
73
72
74
73
75
74
-
75
76
codeActionProvider :: PluginMethodHandler IdeState TextDocumentCodeAction
76
77
codeActionProvider state plId (CodeActionParams _ _ (TextDocumentIdentifier uri) range _ctx)
77
78
| Just nfp <- uriToNormalizedFilePath $ toNormalizedUri uri =
@@ -98,24 +99,30 @@ tacticCmd tac state (TacticParams uri range var_name)
98
99
let span = rangeToRealSrcSpan (fromNormalizedFilePath nfp) range'
99
100
pm <- MaybeT $ useAnnotatedSource " tacticsCmd" state nfp
100
101
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
108
106
109
107
case res of
110
108
Left err -> pure $ Left err
111
109
Right medit -> do
112
110
forM_ medit $ \ edit ->
113
- sendRequest SWorkspaceApplyEdit (ApplyWorkspaceEditParams Nothing edit) (\ _ -> pure () )
111
+ sendRequest
112
+ SWorkspaceApplyEdit
113
+ (ApplyWorkspaceEditParams Nothing edit)
114
+ (const $ pure () )
114
115
pure $ Right Null
115
116
tacticCmd _ _ _ =
116
117
pure $ Left $ mkErr InvalidRequest " Bad URI"
117
118
118
119
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
+
119
126
mkErr :: ErrorCode -> T. Text -> ResponseError
120
127
mkErr code err = ResponseError code err Nothing
121
128
@@ -129,14 +136,14 @@ joinNote _ (Just a) = a
129
136
-- | Turn a 'RunTacticResults' into concrete edits to make in the source
130
137
-- document.
131
138
mkWorkspaceEdits
132
- :: RunTacticResults
133
- -> RealSrcSpan
139
+ :: RealSrcSpan
134
140
-> DynFlags
135
141
-> ClientCapabilities
136
142
-> Uri
137
143
-> Annotated ParsedSource
144
+ -> RunTacticResults
138
145
-> Either ResponseError (Maybe WorkspaceEdit )
139
- mkWorkspaceEdits rtr span dflags clientCapabilities uri pm = do
146
+ mkWorkspaceEdits span dflags clientCapabilities uri pm rtr = do
140
147
let g = graftHole (RealSrcSpan span ) rtr
141
148
response = transform dflags clientCapabilities uri g pm
142
149
in case response of
0 commit comments