Skip to content

Commit 6b6c405

Browse files
konnAilrun
andauthored
Doctest comment parsing using module annotations in Eval Plugin (#1232)
* WIP: Comment parsing using module annotations * Line Comment parsers (wip) * Line comment implemented (block comment not implemented) * Completely switches to Megaparsec * T27 must be fixed * We can always assume that comment starts with "--" with no space prepended * must be horizontal space, not ANY whitespace * Block parser (WIP) * We don't need whole range; position suffices * Brutal parsing for block haddock comments * Brutal line parsing * unset Opt_Haddock * Wrong debug messages * Redundant debug output * Hacks for indentation levels and LHS * Updates block comment logic in Literate Haskell * Updates doctests * Allows doctest without newline at the end * Precise handling of line ending * Corrects last-line block eval handling * Makes normal line parsing LHS sensitive * Removes outdated note on block comments in a single line * Wait a moment before executing each code lenses * Sorting tests in order * Sorts lenses in order * Reverted to use executCmd * Changes sorting logic * Fixes test case: trailing space * Dummy commit to re-invoke CI * expect fail CPP Eval on Windows * Corrects typo * Test for #1258 * Corrects test header * Ad-hoc treatment for ending brace in nested comment block * `goldenTest` function from Eval plugin doesn't support multiple tests in the same block but in separate group * Dummy commit to rerun CI * Stop using CPP and use `knownBrokenForGhcVersions` and `knownBrokenOnWindows` * Nested `expectedFailure` didn't work as expected * Abolishes `Parser` type synonym * Removes unneccesary comment evals * Skip failed curentRange resolution Co-authored-by: Junyoung/Clare Jang <[email protected]>
1 parent b97b469 commit 6b6c405

32 files changed

+1082
-862
lines changed

ghcide/src/Development/IDE/Core/Rules.hs

+4-1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ withOptHaddock = withOption Opt_Haddock
339339
withOption :: GeneralFlag -> ModSummary -> ModSummary
340340
withOption opt ms = ms{ms_hspp_opts= gopt_set (ms_hspp_opts ms) opt}
341341

342+
withoutOption :: GeneralFlag -> ModSummary -> ModSummary
343+
withoutOption opt ms = ms{ms_hspp_opts= gopt_unset (ms_hspp_opts ms) opt}
344+
342345
-- | Given some normal parse errors (first) and some from Haddock (second), merge them.
343346
-- Ignore Haddock errors that are in both. Demote Haddock-only errors to warnings.
344347
mergeParseErrorsHaddock :: [FileDiagnostic] -> [FileDiagnostic] -> [FileDiagnostic]
@@ -360,7 +363,7 @@ getParsedModuleWithCommentsRule = defineEarlyCutoff $ \GetParsedModuleWithCommen
360363
sess <- use_ GhcSession file
361364
opt <- getIdeOptions
362365

363-
let ms' = withOption Opt_KeepRawTokenStream ms
366+
let ms' = withoutOption Opt_Haddock $ withOption Opt_KeepRawTokenStream ms
364367

365368
liftIO $ getParsedModuleDefinition (hscEnv sess) opt file ms'
366369

ghcide/src/Development/IDE/GHC/Orphans.hs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import qualified StringBuffer as SB
2222
import Data.Text (Text)
2323
import Data.String (IsString(fromString))
2424
import Retrie.ExactPrint (Annotated)
25+
import Data.List (foldl')
2526

2627

2728
-- Orphan instances for types from the GHC API.

plugins/hls-eval-plugin/hls-eval-plugin.cabal

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ library
3131
Ide.Plugin.Eval.CodeLens
3232
Ide.Plugin.Eval.GHC
3333
Ide.Plugin.Eval.Parse.Option
34-
Ide.Plugin.Eval.Parse.Parser
35-
Ide.Plugin.Eval.Parse.Section
36-
Ide.Plugin.Eval.Parse.Token
34+
Ide.Plugin.Eval.Parse.Comments
3735
Ide.Plugin.Eval.Types
3836
Ide.Plugin.Eval.Util
3937

@@ -44,6 +42,7 @@ library
4442
, deepseq
4543
, Diff
4644
, directory
45+
, dlist
4746
, extra
4847
, filepath
4948
, ghc
@@ -54,6 +53,8 @@ library
5453
, haskell-lsp
5554
, haskell-lsp-types
5655
, hls-plugin-api
56+
, lens
57+
, megaparsec >= 0.9
5758
, parser-combinators
5859
, pretty-simple
5960
, QuickCheck
@@ -63,6 +64,7 @@ library
6364
, text
6465
, time
6566
, transformers
67+
, mtl
6668
, unordered-containers
6769

6870
ghc-options: -Wall -Wno-name-shadowing

plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs

+14-13
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ import GhcMonad (Ghc, GhcMonad, liftIO)
1515
import Ide.Plugin.Eval.Types (
1616
Language (Plain),
1717
Loc,
18-
Located (Located),
1918
Section (sectionLanguage),
20-
Test (Example, Property, testOutput),
19+
Test (..),
2120
Txt,
2221
locate,
23-
locate0,
22+
locate0, Located(..)
2423
)
2524
import InteractiveEval (runDecls)
2625
import Unsafe.Coerce (unsafeCoerce)
26+
import Control.Lens ((^.))
27+
import Language.Haskell.LSP.Types.Lens (start, line)
2728

2829
-- | Return the ranges of the expression and result parts of the given test
29-
testRanges :: Loc Test -> (Range, Range)
30-
testRanges (Located line tst) =
31-
let startLine = line
30+
testRanges :: Test -> (Range, Range)
31+
testRanges tst =
32+
let startLine = testRange tst ^. start.line
3233
(exprLines, resultLines) = testLenghts tst
3334
resLine = startLine + exprLines
3435
in ( Range
@@ -44,7 +45,7 @@ testRanges (Located line tst) =
4445
-}
4546

4647
-- |The document range where the result of the test is defined
47-
resultRange :: Loc Test -> Range
48+
resultRange :: Test -> Range
4849
resultRange = snd . testRanges
4950

5051
-- TODO: handle BLANKLINE
@@ -66,18 +67,18 @@ testCheck (section, test) out
6667
| otherwise = showDiffs $ getDiff (map T.pack $ testOutput test) out
6768

6869
testLenghts :: Test -> (Int, Int)
69-
testLenghts (Example e r) = (NE.length e, length r)
70-
testLenghts (Property _ r) = (1, length r)
70+
testLenghts (Example e r _) = (NE.length e, length r)
71+
testLenghts (Property _ r _) = (1, length r)
7172

7273
-- |A one-line Haskell statement
7374
type Statement = Loc String
7475

75-
asStatements :: Loc Test -> [Statement]
76-
asStatements lt = locate (asStmts <$> lt)
76+
asStatements :: Test -> [Statement]
77+
asStatements lt = locate $ Located (testRange lt ^. start.line) (asStmts lt)
7778

7879
asStmts :: Test -> [Txt]
79-
asStmts (Example e _) = NE.toList e
80-
asStmts (Property t _) =
80+
asStmts (Example e _ _) = NE.toList e
81+
asStmts (Property t _ _) =
8182
["prop11 = " ++ t, "(propEvaluation prop11 :: IO String)"]
8283

8384
-- |Evaluate an expression (either a pure expression or an IO a)

0 commit comments

Comments
 (0)