From 676396079aecbbf74956395190500fa554f8d663 Mon Sep 17 00:00:00 2001 From: CrystalSplitter Date: Mon, 4 Mar 2024 22:03:41 -0800 Subject: [PATCH] Match on functions that have apostraphes Apostraphes are of course valid haskell characters in function names. The current ParseContext did not understand this. Fixes #38 hopefully --- ghcitui.cabal | 2 ++ .../Ghcitui/Ghcid/ParseContext.hs | 16 +++++----- test/ParseContextSpec.hs | 32 +++++++++++++++++++ test/Spec.hs | 2 ++ 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 test/ParseContextSpec.hs diff --git a/ghcitui.cabal b/ghcitui.cabal index ca39dd3..42c747a 100644 --- a/ghcitui.cabal +++ b/ghcitui.cabal @@ -150,8 +150,10 @@ test-suite spec main-is: Spec.hs type: exitcode-stdio-1.0 build-depends: base >= 4.16 && < 5 + , text , ghcitui , hspec ^>= 2.11.5 other-modules: LocSpec + , ParseContextSpec , UtilSpec default-language: Haskell2010 diff --git a/lib/ghcitui-core/Ghcitui/Ghcid/ParseContext.hs b/lib/ghcitui-core/Ghcitui/Ghcid/ParseContext.hs index 483fa7c..7bb1f2f 100644 --- a/lib/ghcitui-core/Ghcitui/Ghcid/ParseContext.hs +++ b/lib/ghcitui-core/Ghcitui/Ghcid/ParseContext.hs @@ -41,9 +41,9 @@ data ParseContextOut = ParseContextOut , filepath :: !FilePath , pcSourceRange :: !Loc.SourceRange } - deriving (Show) + deriving (Eq, Show) -data ParseContextReturn = PCError ParseError | PCNoContext | PCContext ParseContextOut +data ParseContextReturn = PCError ParseError | PCNoContext | PCContext ParseContextOut deriving (Eq, Show) -- | Parse the output from ":show context" for the interpreter state. parseContext :: T.Text -> ParseContextReturn @@ -58,7 +58,7 @@ parseContext contextText = let contextTextLines = T.lines contextText in if all (`elem` ["", "()"]) contextTextLines then PCNoContext - else PCError (ParseError [i| parsing context: #{e}|]) + else PCError (ParseError [i|parsing context: #{e}|]) parseFile :: T.Text -> Either ParseError FilePath parseFile s @@ -128,15 +128,15 @@ eInfoLine :: T.Text -> Either ParseError (T.Text, T.Text) eInfoLine "" = Left $ ParseError "Could not find info line in empty string" eInfoLine contextText = note - (ParseError $ "Could not match info line: '" <> showT splits <> "'") - stopLine + (ParseError [i|Could not match info line: '#{showT splits}'|]) + mStopLine where splits = splitBy ghcidPrompt contextText - stopLineMR = foldr (\n acc -> acc <|> stopReg n) Nothing splits - stopLine = (\mr -> (mrSubs mr ! 1, mrSubs mr ! 2)) <$> stopLineMR + mStopLine = (\mr -> (mrSubs mr ! 1, mrSubs mr ! 2)) <$> mStopLineMatchRes + mStopLineMatchRes = foldr (\n acc -> acc <|> stopReg n) Nothing splits -- Match on the "Stopped in ..." line. stopReg :: T.Text -> Maybe (MatchResult T.Text) - stopReg s = s =~~ ("^[ \t]*Stopped in ([[:alnum:]_.()]+),(.*)" :: T.Text) + stopReg s = s =~~ ("^[ \t]*Stopped in ([[:alnum:]_.()]+'*),(.*)" :: T.Text) parseBreakResponse :: T.Text -> Either T.Text [Loc.ModuleLoc] parseBreakResponse t diff --git a/test/ParseContextSpec.hs b/test/ParseContextSpec.hs new file mode 100644 index 0000000..e8c24a4 --- /dev/null +++ b/test/ParseContextSpec.hs @@ -0,0 +1,32 @@ +{-# LANGUAGE OverloadedStrings #-} + +module ParseContextSpec where + +import Data.Text as T +import Test.Hspec + +import qualified Ghcitui.Ghcid.ParseContext as PC +import qualified Ghcitui.Loc as Loc + +spec :: Spec +spec = do + describe "parseContext" $ do + it "can parse the Ormolu function parseModule' (with an apostraphe)" $ do + let apostrapheFixture = + T.unlines + [ "()" + , "[src/Ormolu.hs:(257,51)-(261,35)] #~GHCID-START~#()" + , "[src/Ormolu.hs:(257,51)-(261,35)] #~GHCID-START~#--> invoke" + , " Stopped in Ormolu.parseModule', src/Ormolu.hs:(257,51)-(261,35)" + ] + let expectedLoc = + Loc.SourceRange + { Loc.startLine = Just 257 + , Loc.startCol = Just 51 + , Loc.endLine = Just 261 + , Loc.endCol = Just 35 + } + let expected = + PC.PCContext + (PC.ParseContextOut "Ormolu.parseModule'" "src/Ormolu.hs" expectedLoc) + PC.parseContext apostrapheFixture `shouldBe` expected diff --git a/test/Spec.hs b/test/Spec.hs index c56fd2e..20945d5 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -3,9 +3,11 @@ module Main where import Test.Hspec import qualified LocSpec +import qualified ParseContextSpec import qualified UtilSpec main :: IO () main = hspec $ do LocSpec.spec UtilSpec.spec + ParseContextSpec.spec