Skip to content

Commit 1755392

Browse files
committed
Let interactions return multiple results --- aka also info messages
1 parent 77bcd55 commit 1755392

File tree

6 files changed

+58
-42
lines changed

6 files changed

+58
-42
lines changed

plugins/hls-tactics-plugin/src/Wingman/AbstractLSP.hs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import Control.Monad.IO.Class
1313
import Control.Monad.Trans (lift)
1414
import Control.Monad.Trans.Maybe (MaybeT, mapMaybeT)
1515
import qualified Data.Aeson as A
16+
import Data.Coerce
1617
import Data.Foldable (traverse_)
18+
import Data.Monoid (Last (..))
1719
import qualified Data.Text as T
20+
import Data.Traversable (for)
1821
import Data.Tuple.Extra (uncurry3)
1922
import Development.IDE (IdeState)
2023
import Development.IDE.Core.UseStale
@@ -93,26 +96,30 @@ runContinuation plId cont state (fc, b) = do
9396
env@LspEnv{..} <- buildEnv state plId fc
9497
let stale a = runStaleIde "runContinuation" state (fc_nfp le_fileContext) a
9598
args <- fetchTargetArgs @a env
96-
c_runCommand cont env args fc b >>= \case
97-
ErrorMessages errs -> do
98-
traverse_ showUserFacingMessage errs
99-
pure $ Right A.Null
100-
RawEdit edits -> do
101-
sendEdits edits
102-
pure $ Right A.Null
103-
GraftEdit gr -> do
104-
ccs <- lift getClientCapabilities
105-
TrackedStale pm _ <- mapMaybeT liftIO $ stale GetAnnotatedParsedSource
106-
case mkWorkspaceEdits le_dflags ccs (fc_uri le_fileContext) (unTrack pm) gr of
107-
Left errs ->
108-
pure $ Left $ ResponseError
109-
{ _code = InternalError
110-
, _message = T.pack $ show errs
111-
, _xdata = Nothing
112-
}
113-
Right edits -> do
114-
sendEdits edits
115-
pure $ Right A.Null
99+
res <- c_runCommand cont env args fc b
100+
101+
-- This block returns a maybe error.
102+
fmap (maybe (Right $ A.Null) Left . coerce . foldMap Last) $
103+
for res $ \case
104+
ErrorMessages errs -> do
105+
traverse_ showUserFacingMessage errs
106+
pure Nothing
107+
RawEdit edits -> do
108+
sendEdits edits
109+
pure Nothing
110+
GraftEdit gr -> do
111+
ccs <- lift getClientCapabilities
112+
TrackedStale pm _ <- mapMaybeT liftIO $ stale GetAnnotatedParsedSource
113+
case mkWorkspaceEdits le_dflags ccs (fc_uri le_fileContext) (unTrack pm) gr of
114+
Left errs ->
115+
pure $ Just $ ResponseError
116+
{ _code = InternalError
117+
, _message = T.pack $ show errs
118+
, _xdata = Nothing
119+
}
120+
Right edits -> do
121+
sendEdits edits
122+
pure $ Nothing
116123

117124

118125
------------------------------------------------------------------------------

plugins/hls-tactics-plugin/src/Wingman/AbstractLSP/TacticActions.hs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,33 @@ makeTacticInteraction cmd =
5151
pm_span <- liftMaybe $ mapAgeFrom pmmap span
5252
let t = commandTactic cmd var_name
5353

54-
res <-
55-
liftIO $ runTactic (cfg_timeout_seconds le_config * seconds) hj_ctx hj_jdg t >>= \case
56-
Left err -> pure $ ErrorMessages $ pure $ mkUserFacingMessage err
57-
Right rtr ->
58-
case rtr_extract rtr of
59-
L _ (HsVar _ (L _ rdr)) | isHole (occName rdr) ->
60-
pure $ ErrorMessages [NothingToDo]
61-
_ -> do
62-
for_ (rtr_other_solns rtr) $ \soln -> do
63-
traceMX "other solution" $ syn_val soln
64-
traceMX "with score" $ scoreSolution soln (rtr_jdg rtr) []
65-
traceMX "solution" $ rtr_extract rtr
66-
pure $ GraftEdit $ graftHole (RealSrcSpan $ unTrack pm_span) rtr
67-
68-
pure res
69-
-- pure $ case res of
70-
-- Nothing -> ErrorMessages $ pure TimedOut
71-
-- Just c -> c
54+
liftIO $ runTactic (cfg_timeout_seconds le_config * seconds) hj_ctx hj_jdg t >>= \case
55+
Left err -> pure $ pure $ ErrorMessages $ pure $ mkUserFacingMessage err
56+
Right rtr ->
57+
case rtr_extract rtr of
58+
L _ (HsVar _ (L _ rdr)) | isHole (occName rdr) ->
59+
pure
60+
$ addTimeoutMessage rtr
61+
$ pure
62+
$ ErrorMessages
63+
$ pure NothingToDo
64+
_ -> do
65+
for_ (rtr_other_solns rtr) $ \soln -> do
66+
traceMX "other solution" $ syn_val soln
67+
traceMX "with score" $ scoreSolution soln (rtr_jdg rtr) []
68+
traceMX "solution" $ rtr_extract rtr
69+
pure
70+
$ addTimeoutMessage rtr
71+
$ pure
72+
$ GraftEdit
73+
$ graftHole (RealSrcSpan $ unTrack pm_span) rtr
74+
75+
76+
addTimeoutMessage :: RunTacticResults -> [ContinuationResult] -> [ContinuationResult]
77+
addTimeoutMessage rtr = mappend
78+
[ ErrorMessages $ pure TimedOut
79+
| rtr_timed_out rtr
80+
]
7281

7382

7483
------------------------------------------------------------------------------

plugins/hls-tactics-plugin/src/Wingman/AbstractLSP/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ data Continuation sort target payload = Continuation
113113
-> TargetArgs target
114114
-> FileContext
115115
-> payload
116-
-> MaybeT (LspM Plugin.Config) ContinuationResult
116+
-> MaybeT (LspM Plugin.Config) [ContinuationResult]
117117
}
118118

119119

plugins/hls-tactics-plugin/src/Wingman/EmptyCase.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ emptyCaseInteraction = Interaction $
8484
, edits
8585
)
8686
)
87-
$ (\ _ _ _ we -> pure $ RawEdit we)
87+
$ (\ _ _ _ we -> pure $ pure $ RawEdit we)
8888

8989

9090
scrutinzedType :: EmptyCaseSort Type -> Maybe Type

plugins/hls-tactics-plugin/src/Wingman/Machinery.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ runTactic duration ctx jdg t = do
123123
$ runStreamingTacticT t jdg tacticState
124124
(in_chan, out_chan) <- newChan
125125
timed_out <-
126-
fmap isJust $ timeout duration $ consume stream $ \case
126+
fmap (not. isJust) $ timeout duration $ consume stream $ \case
127127
Left _ -> pure ()
128128
Right proof -> writeChan in_chan $ Just proof
129129
writeChan in_chan Nothing

plugins/hls-tactics-plugin/src/Wingman/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ data UserFacingMessage
552552
instance Show UserFacingMessage where
553553
show NotEnoughGas = "Wingman ran out of gas when trying to find a solution. \nTry increasing the `auto_gas` setting."
554554
show TacticErrors = "Wingman couldn't find a solution"
555-
show TimedOut = "Wingman timed out while trying to find a solution"
555+
show TimedOut = "Wingman timed out while finding a solution. \nYou might get a better result if you increase the timeout duration."
556556
show NothingToDo = "Nothing to do"
557557
show (InfrastructureError t) = "Internal error: " <> T.unpack t
558558

0 commit comments

Comments
 (0)