Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for splice plugin with GHC 9.10 #4452

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ jobs:
name: Test hls-eval-plugin
run: cabal test hls-eval-plugin-tests || cabal test hls-eval-plugin-tests

# TODO enable when it supports 9.10
- if: matrix.test && matrix.ghc != '9.10'
- if: matrix.test
name: Test hls-splice-plugin
run: cabal test hls-splice-plugin-tests || cabal test hls-splice-plugin-tests

Expand Down
822 changes: 822 additions & 0 deletions 9.10

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/support/plugin-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ For example, a plugin to provide a formatter which has itself been abandoned has
| `hls-floskell-plugin` | 3 | 9.10.1 |
| `hls-stan-plugin` | 3 | |
| `hls-retrie-plugin` | 3 | 9.10.1 |
| `hls-splice-plugin` | 3 | 9.10.1 |
| `hls-splice-plugin` | 3 | |
6 changes: 3 additions & 3 deletions haskell-language-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -949,13 +949,13 @@ flag splice
manual: True

common splice
if flag(splice) && impl(ghc < 9.10)
if flag(splice)
build-depends: haskell-language-server:hls-splice-plugin
cpp-options: -Dhls_splice

library hls-splice-plugin
import: defaults, pedantic, warnings
if !(flag(splice) && impl(ghc < 9.10))
if !(flag(splice))
buildable: False
exposed-modules:
Ide.Plugin.Splice
Expand Down Expand Up @@ -984,7 +984,7 @@ library hls-splice-plugin

test-suite hls-splice-plugin-tests
import: defaults, pedantic, test-defaults, warnings
if !(flag(splice) && impl(ghc < 9.10))
if !(flag(splice))
buildable: False
type: exitcode-stdio-1.0
hs-source-dirs: plugins/hls-splice-plugin/test
Expand Down
133 changes: 127 additions & 6 deletions plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE BlockArguments #-}

module Ide.Plugin.Splice (descriptor) where

Expand Down Expand Up @@ -53,6 +54,14 @@
import qualified Language.LSP.Protocol.Lens as J
import Language.LSP.Protocol.Message
import Language.LSP.Protocol.Types
import Debug.Trace
import Development.IDE.GHC.Compat.Util (FastString, fsLit)
import GHC.Types.SrcLoc (BufPos (..), BufSpan (..))
import System.IO.Unsafe (unsafePerformIO)
import System.Process.Extra (readProcess)
import GHC (EpaLocation'(EpaSpan))

Check failure on line 62 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / flags (9.8, ubuntu-latest)

Module ‘GHC’ does not export ‘EpaLocation'’.

Check failure on line 62 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / test (9.8, ubuntu-latest, true)

Module ‘GHC’ does not export ‘EpaLocation'’.

Check failure on line 62 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / test (9.6, macOS-latest, false)

Module ‘GHC’ does not export ‘EpaLocation'’

Check failure on line 62 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / test (9.6, windows-latest, true)

Module ‘GHC’ does not export ‘EpaLocation'’

Check failure on line 62 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / test (9.4, ubuntu-latest, true)

Module ‘GHC’ does not export ‘EpaLocation'’

Check failure on line 62 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / test (9.8, macOS-latest, false)

Module ‘GHC’ does not export ‘EpaLocation'’.

Check failure on line 62 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / test (9.4, macOS-latest, false)

Module ‘GHC’ does not export ‘EpaLocation'’

Check failure on line 62 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / test (9.4, windows-latest, true)

Module ‘GHC’ does not export ‘EpaLocation'’

Check failure on line 62 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / test (9.6, ubuntu-latest, true)

Module ‘GHC’ does not export ‘EpaLocation'’

Check failure on line 62 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / test (9.8, windows-latest, true)

Module ‘GHC’ does not export ‘EpaLocation'’.
import Ide.PluginUtils (diffText, WithDeletions (..))
import Control.Monad

#if !MIN_VERSION_base(4,20,0)
import Data.Foldable (Foldable (foldl'))
Expand Down Expand Up @@ -133,7 +142,7 @@
let Splices {..} = tmrTopLevelSplices
let exprSuperSpans =
listToMaybe $ findSubSpansDesc srcSpan exprSplices
_patSuperSpans =
patSuperSpans =
listToMaybe $ findSubSpansDesc srcSpan patSplices
typeSuperSpans =
listToMaybe $ findSubSpansDesc srcSpan typeSplices
Expand All @@ -156,10 +165,72 @@
maybe (throwError $ PluginInternalError "No splice information found") (either (throwError . PluginInternalError . T.pack) pure) $
case spliceContext of
Expr -> graftSpliceWith exprSuperSpans
Pat ->

graftSpliceWith _patSuperSpans

-- Pat -> graftSpliceWith patSuperSpans
Pat -> patSuperSpans <&> \(_, expanded) ->
-- basically just the old code inlined and with some debug tracing added
let edit0 = do
let src = printA ps
(a', _, _) <- runTransformFromT 0 $ do
val'0 <- Development.IDE.GHC.ExactPrint.annotate dflags True $ maybeParensAST expanded
-- on 9.10, this becomes `UnhelpfulSpan UnhelpfulNoLocationInfo`
-- but seems a red herring - adding the old span manually makes no difference
-- let L (EpAnn _sp0 a0 cs0) x = val'0
-- let sp' = mkSrcSpan (mkSrcLoc (fsLit "RealSrcSpan SrcSpanPoint \"ghc-exactprint\" -1 0 Nothing") 1 1) ((mkSrcLoc (fsLit "unused") 1 6))
-- -- let sp'' = sp
-- let val'' =
-- traceShow (getLoc val'') $
-- traceShow (getLoc val'0) $
-- L (EpAnn (EpaSpan sp') a0 cs0) x

let val' = val'0
pure $
-- traceShow (printA expanded) $ -- same, but weird: `"\n\n\n\n\n \"str\""`
-- traceShow (printA $ maybeParensAST expanded) $
-- traceShow (printA val') $
everywhere'
( mkT \case
L src _ :: LocatedAn l ast | locA src `eqSrcSpan` dst ->
-- prints the same, but has different spans
-- traceShow (printA val') $ -- same old and new - " \"str\""
-- traceShow (locA src) $ -- same
-- traceShow (getLoc val') $
-- traceShow dst $ -- same except `Nothing` for `BufSpan`
val'
l -> l
)
ps
-- this differs - 9.10 version lacks the space
-- how does that happen? src spans are the same as for the expr replaced...
let res = printA a'
pure $
-- trace (renderWithContext defaultSDocContext $ ppr a') $
-- traceShow a' $
-- traceShow res $

-- trace (pShow $ showsMod a' "") $
traceFile traceFileName (pShowNoColor $ showsMod a' "") $

-- traceShow (src,res) $
-- same apart from `_newText = "f \"str\"= putStrLn \"is str\""`
-- where previously there was correctly an extra space
diffText clientCapabilities (verTxtDocId, T.pack src) (T.pack res) IncludeDeletions
edit = edit0
dst = (RealSrcSpan spliceSpan Nothing)
in
traceShow () $

Check failure on line 220 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Error in expandTHSplice in module Ide.Plugin.Splice: Avoid restricted function ▫︎ Found: "traceShow" ▫︎ Note: may break the code
-- same on both GHC versions - weird leading newlines but no whitespace at end
-- traceShow (printA expanded) $
-- traceShow (g dflags) $
-- traceShow spliceSpan $
traceShowId $

Check failure on line 225 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Error in expandTHSplice in module Ide.Plugin.Splice: Avoid restricted function ▫︎ Found: "traceShowId" ▫︎ Note: may break the code
edit
-- matchSplice _ (SplicePat _ spl) = Just spl
-- matchSplice _ _ = Nothing
-- expandSplice _ =
-- #if MIN_VERSION_ghc(9,5,0)
-- fmap (first (Left . unLoc . utsplice_result . snd )) .
-- #endif
-- rnSplicePat
HsType -> graftSpliceWith typeSuperSpans
HsDecl ->
declSuperSpans <&> \(_, expanded) ->
Expand All @@ -181,7 +252,7 @@
=<< MaybeT
(runAction "expandTHSplice.TypeCheck" ideState $ use TypeCheck fp)
)
<|> lift (runExceptT $ expandManually fp)
-- <|> lift (runExceptT $ expandManually fp)

case eedits of
Left err -> do
Expand All @@ -195,13 +266,63 @@
Nothing -> pure $ Right $ InR Null
Just (Left err) -> pure $ Left err
Just (Right edit) -> do
-- huh, edit unnecessarily goes all the way to end of line - why?
-- liftIO $ print edit
_ <- pluginSendRequest SMethod_WorkspaceApplyEdit (ApplyWorkspaceEditParams Nothing edit) (\_ -> pure ())
pure $ Right $ InR Null

where
range = realSrcSpanToRange spliceSpan
srcSpan = RealSrcSpan spliceSpan Nothing

traceFileName :: [Char]
traceFileName =
#if MIN_VERSION_ghc(9,9,0)
"9.10"
#else
"9.8"
#endif

showsModSimple :: ParsedSource -> ShowS
showsModSimple = gshowsWith
(\(s :: SrcSpan) ->
-- ("george-src-span-placeholder" <>)
shows s
)
showsMod :: Data a => a -> ShowS
showsMod =
( \t ->
showChar '('
. (showString . showConstr . toConstr $ t)
. (foldr (.) id . gmapQ ((showChar ' ' .) . showsMod) $ t)
. showChar ')'
)
`extQ` (shows :: String -> ShowS)
`extQ` (\(s :: SrcSpan) -> shows s)
`extQ` (\(s :: FastString) -> shows s)

gshowsWith :: (Data a, Typeable b) => (b -> ShowS) -> a -> ShowS
gshowsWith f =
( \t ->
showChar '('
. (showString . showConstr . toConstr $ t)
. (foldr (.) id . gmapQ ((showChar ' ' .) . gshowsWith f) $ t)
. showChar ')'
)
`extQ` (shows :: String -> ShowS)
`extQ` f

pPrint :: (Show a) => a -> IO ()
pPrint = putStrLn <=< readProcess "pretty-simple" [] . show
{-# NOINLINE pShow #-}
pShow :: String -> String
pShow = unsafePerformIO . readProcess "pretty-simple" []

Check failure on line 319 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Error in pShow in module Ide.Plugin.Splice: Avoid restricted function ▫︎ Found: "unsafePerformIO" ▫︎ Note: may break the code
{-# NOINLINE pShowNoColor #-}
pShowNoColor :: String -> String
pShowNoColor = unsafePerformIO . readProcess "pretty-simple" ["-cno-color"]

Check failure on line 322 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Error in pShowNoColor in module Ide.Plugin.Splice: Avoid restricted function ▫︎ Found: "unsafePerformIO" ▫︎ Note: may break the code
{-# NOINLINE traceFile #-}
traceFile :: String -> String -> a -> a
traceFile fp s x = unsafePerformIO $ writeFile fp s >> pure x

Check failure on line 325 in plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Error in traceFile in module Ide.Plugin.Splice: Avoid restricted function ▫︎ Found: "unsafePerformIO" ▫︎ Note: may break the code

setupHscEnv
:: IdeState
Expand Down
79 changes: 40 additions & 39 deletions plugins/hls-splice-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,46 @@ splicePlugin = mkPluginTestDescriptor' Splice.descriptor "splice"

tests :: TestTree
tests = testGroup "splice"
[ goldenTest "TSimpleExp" Inplace 6 15
, goldenTest "TSimpleExp" Inplace 6 24
, goldenTest "TTypeAppExp" Inplace 7 5
, goldenTest "TErrorExp" Inplace 6 15
, goldenTest "TErrorExp" Inplace 6 51
, goldenTest "TQQExp" Inplace 6 17
, goldenTest "TQQExp" Inplace 6 25
, goldenTest "TQQExpError" Inplace 6 13
, goldenTest "TQQExpError" Inplace 6 22
, testGroup "Pattern Splices"
[ goldenTest "TSimplePat" Inplace 6 3
, goldenTest "TSimplePat" Inplace 6 22
, goldenTest "TSimplePat" Inplace 6 3
, goldenTest "TSimplePat" Inplace 6 22
, goldenTest "TErrorPat" Inplace 6 3
, goldenTest "TErrorPat" Inplace 6 18
, goldenTest "TQQPat" Inplace 6 3
, goldenTest "TQQPat" Inplace 6 11
, goldenTest "TQQPatError" Inplace 6 3
, goldenTest "TQQPatError" Inplace 6 11
]
, goldenTest "TSimpleType" Inplace 5 12
, goldenTest "TSimpleType" Inplace 5 22
, goldenTest "TTypeTypeError" Inplace 7 12
, goldenTest "TTypeTypeError" Inplace 7 52
, goldenTest "TQQType" Inplace 8 19
, goldenTest "TQQType" Inplace 8 28
, goldenTest "TQQTypeTypeError" Inplace 8 19
, goldenTest "TQQTypeTypeError" Inplace 8 28
, goldenTest "TSimpleDecl" Inplace 8 1
, goldenTest "TQQDecl" Inplace 5 1
, goldenTestWithEdit "TTypeKindError" (
if ghcVersion >= GHC96 then
"96-expected"
else
"expected"
) Inplace 7 9
, goldenTestWithEdit "TDeclKindError" "expected" Inplace 8 1
]
[goldenTest "TQQPat" Inplace 6 11] -- a useful simple test to focus on
-- [ goldenTest "TSimpleExp" Inplace 6 15
-- , goldenTest "TSimpleExp" Inplace 6 24
-- , goldenTest "TTypeAppExp" Inplace 7 5
-- , goldenTest "TErrorExp" Inplace 6 15
-- , goldenTest "TErrorExp" Inplace 6 51
-- , goldenTest "TQQExp" Inplace 6 17
-- , goldenTest "TQQExp" Inplace 6 25
-- , goldenTest "TQQExpError" Inplace 6 13
-- , goldenTest "TQQExpError" Inplace 6 22
-- , testGroup "Pattern Splices"
-- [ goldenTest "TSimplePat" Inplace 6 3
-- , goldenTest "TSimplePat" Inplace 6 22
-- , goldenTest "TSimplePat" Inplace 6 3
-- , goldenTest "TSimplePat" Inplace 6 22
-- , goldenTest "TErrorPat" Inplace 6 3
-- , goldenTest "TErrorPat" Inplace 6 18
-- , goldenTest "TQQPat" Inplace 6 3
-- , goldenTest "TQQPat" Inplace 6 11
-- , goldenTest "TQQPatError" Inplace 6 3
-- , goldenTest "TQQPatError" Inplace 6 11
-- ]
-- , goldenTest "TSimpleType" Inplace 5 12
-- , goldenTest "TSimpleType" Inplace 5 22
-- , goldenTest "TTypeTypeError" Inplace 7 12
-- , goldenTest "TTypeTypeError" Inplace 7 52
-- , goldenTest "TQQType" Inplace 8 19
-- , goldenTest "TQQType" Inplace 8 28
-- , goldenTest "TQQTypeTypeError" Inplace 8 19
-- , goldenTest "TQQTypeTypeError" Inplace 8 28
-- , goldenTest "TSimpleDecl" Inplace 8 1
-- , goldenTest "TQQDecl" Inplace 5 1
-- , goldenTestWithEdit "TTypeKindError" (
-- if ghcVersion >= GHC96 then
-- "96-expected"
-- else
-- "expected"
-- ) Inplace 7 9
-- , goldenTestWithEdit "TDeclKindError" "expected" Inplace 8 1
-- ]

goldenTest :: FilePath -> ExpandStyle -> Int -> Int -> TestTree
goldenTest fp tc line col =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ module TErrorExp where
import Language.Haskell.TH ( tupE, litE, integerL )

main :: IO ()
main = return (42, ())
main = return 42, ())
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ module TErrorPat where
import Language.Haskell.TH ( conP )

f :: () -> ()
f True = x
f True




= x
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{-# LANGUAGE QuasiQuotes #-}
module TQQDecl where
import QQ (str)

foo :: String
foo = "foo"
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module TQQPat where
import QQ

f :: String -> IO ()
f "str" = putStrLn "is str"
f "str"= putStrLn "is str"
f _ = putStrLn " not str"
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module TQQPatError where
import QQ

f :: () -> IO ()
f "str" = putStrLn "is str"
f "str"= putStrLn "is str"
f _ = putStrLn " not str"
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
{-# LANGUAGE QuasiQuotes #-}
module TSimpleDecl where
import Language.Haskell.TH ( mkName, clause, normalB, funD, sigD )

-- Foo
-- Bar
foo :: Int
foo = 42
-- Bar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ module TSimplePat where
import Language.Haskell.TH ( varP, mkName )

f :: x -> x
f x = x
f x




= x
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
module TSimpleType where
import Language.Haskell.TH ( tupleT )

main :: IO ()
main :: IO )
main = return ()
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ module TTypeTypeError where
import Language.Haskell.TH ( appT, numTyLit, litT, conT )
import Data.Proxy ( Proxy )

main :: IO (Proxy 42)
main :: IO Proxy 42)
main = return ()
Loading