Skip to content

Commit 505f777

Browse files
Add the correct file offset to metaprogram parse errors (#1967)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent b4deb4a commit 505f777

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

plugins/hls-tactics-plugin/src/Wingman/LanguageServer/Metaprogram.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Development.IDE.Core.RuleTypes
2222
import Development.IDE.Core.Shake (IdeState (..))
2323
import Development.IDE.Core.UseStale
2424
import Development.IDE.GHC.Compat
25-
import GhcPlugins (containsSpan, realSrcLocSpan)
25+
import GhcPlugins (containsSpan, realSrcLocSpan, realSrcSpanStart)
2626
import Ide.Types
2727
import Language.LSP.Types
2828
import Prelude hiding (span)
@@ -50,8 +50,9 @@ hoverProvider state plId (HoverParams (TextDocumentIdentifier uri) (unsafeMkCurr
5050
case (find (flip containsSpan (unTrack loc) . unTrack . fst) holes) of
5151
Just (trss, program) -> do
5252
let tr_range = fmap realSrcSpanToRange trss
53+
rsl = realSrcSpanStart $ unTrack trss
5354
HoleJudgment{hj_jdg=jdg, hj_ctx=ctx} <- judgementForHole state nfp tr_range cfg
54-
z <- liftIO $ attempt_it ctx jdg $ T.unpack program
55+
z <- liftIO $ attempt_it rsl ctx jdg $ T.unpack program
5556
pure $ Hover
5657
{ _contents = HoverContents
5758
$ MarkupContent MkMarkdown

plugins/hls-tactics-plugin/src/Wingman/Metaprogramming/Parser.hs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import Wingman.Metaprogramming.Parser.Documentation
2121
import Wingman.Metaprogramming.ProofState (proofState, layout)
2222
import Wingman.Tactics
2323
import Wingman.Types
24+
import Development.IDE.GHC.Compat (RealSrcLoc, srcLocLine, srcLocCol, srcLocFile)
25+
import FastString (unpackFS)
2426

2527

2628
nullary :: T.Text -> TacticsM () -> Parser (TacticsM ())
@@ -421,17 +423,30 @@ wrapError :: String -> String
421423
wrapError err = "```\n" <> err <> "\n```\n"
422424

423425

426+
fixErrorOffset :: RealSrcLoc -> P.ParseErrorBundle a b -> P.ParseErrorBundle a b
427+
fixErrorOffset rsl (P.ParseErrorBundle ne (P.PosState a n (P.SourcePos _ line col) pos s))
428+
= P.ParseErrorBundle ne
429+
$ P.PosState a n
430+
(P.SourcePos
431+
(unpackFS $ srcLocFile rsl)
432+
((<>) line $ P.mkPos $ srcLocLine rsl - 1)
433+
((<>) col $ P.mkPos $ srcLocCol rsl - 1 + length @[] "[wingman|")
434+
)
435+
pos
436+
s
437+
424438
------------------------------------------------------------------------------
425439
-- | Attempt to run a metaprogram tactic, returning the proof state, or the
426440
-- errors.
427441
attempt_it
428-
:: Context
442+
:: RealSrcLoc
443+
-> Context
429444
-> Judgement
430445
-> String
431446
-> IO (Either String String)
432-
attempt_it ctx jdg program =
447+
attempt_it rsl ctx jdg program =
433448
case P.runParser tacticProgram "<splice>" (T.pack program) of
434-
Left peb -> pure $ Left $ wrapError $ P.errorBundlePretty peb
449+
Left peb -> pure $ Left $ wrapError $ P.errorBundlePretty $ fixErrorOffset rsl peb
435450
Right tt -> do
436451
res <- runTactic
437452
ctx

0 commit comments

Comments
 (0)