-
-
Notifications
You must be signed in to change notification settings - Fork 391
Add diff option for eval plugin #2622
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ec969da
Add diff option for eval plugin
Ailrun 2fe4b85
Add a test for the diff option
Ailrun b6c017d
Merge branch 'master' into option-to-disable-diff
Ailrun eecbe79
Merge branch 'master' into option-to-disable-diff
jneira 89c4df7
Merge branch 'master' into option-to-disable-diff
Ailrun 8964fff
Merge branch 'master' into option-to-disable-diff
Ailrun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,120 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
{-# OPTIONS_GHC -Wwarn -fno-warn-orphans #-} | ||
-- | Expression execution | ||
module Ide.Plugin.Eval.Code (Statement, testRanges, resultRange, evalSetup, propSetup, testCheck, asStatements,myExecStmt) where | ||
import Control.Lens ((^.)) | ||
import Control.Monad.IO.Class | ||
import Data.Algorithm.Diff (Diff, PolyDiff (..), getDiff) | ||
import qualified Data.List.NonEmpty as NE | ||
import Data.String (IsString) | ||
import qualified Data.Text as T | ||
import Development.IDE.GHC.Compat | ||
import Development.IDE.Types.Location (Position (..), Range (..)) | ||
import GHC (ExecOptions, ExecResult (..), | ||
execStmt) | ||
import Ide.Plugin.Eval.Types (Language (Plain), Loc, | ||
Located (..), | ||
Section (sectionLanguage), | ||
Test (..), Txt, locate, | ||
locate0) | ||
import Language.LSP.Types.Lens (line, start) | ||
import System.IO.Extra (newTempFile, readFile') | ||
-- | Return the ranges of the expression and result parts of the given test | ||
testRanges :: Test -> (Range, Range) | ||
testRanges tst = | ||
let startLine = testRange tst ^. start.line | ||
(fromIntegral -> exprLines, fromIntegral -> resultLines) = testLengths tst | ||
resLine = startLine + exprLines | ||
in ( Range | ||
(Position startLine 0) | ||
--(Position (startLine + exprLines + resultLines) 0), | ||
(Position resLine 0) | ||
, Range (Position resLine 0) (Position (resLine + resultLines) 0) | ||
) | ||
{- |The document range where a test is defined | ||
testRange :: Loc Test -> Range | ||
testRange = fst . testRanges | ||
-} | ||
-- |The document range where the result of the test is defined | ||
resultRange :: Test -> Range | ||
resultRange = snd . testRanges | ||
-- TODO: handle BLANKLINE | ||
{- | ||
>>> showDiffs $ getDiff ["abc","def","ghi","end"] ["abc","def","Z","ZZ","end"] | ||
["abc","def","WAS ghi","NOW Z","NOW ZZ","end"] | ||
-} | ||
showDiffs :: (Semigroup a, IsString a) => [Diff a] -> [a] | ||
showDiffs = map showDiff | ||
showDiff :: (Semigroup a, IsString a) => Diff a -> a | ||
showDiff (First w) = "WAS " <> w | ||
showDiff (Second w) = "NOW " <> w | ||
showDiff (Both w _) = w | ||
testCheck :: (Section, Test) -> [T.Text] -> [T.Text] | ||
testCheck (section, test) out | ||
| null (testOutput test) || sectionLanguage section == Plain = out | ||
| otherwise = showDiffs $ getDiff (map T.pack $ testOutput test) out | ||
testLengths :: Test -> (Int, Int) | ||
testLengths (Example e r _) = (NE.length e, length r) | ||
testLengths (Property _ r _) = (1, length r) | ||
-- |A one-line Haskell statement | ||
type Statement = Loc String | ||
asStatements :: Test -> [Statement] | ||
asStatements lt = locate $ Located (fromIntegral $ testRange lt ^. start.line) (asStmts lt) | ||
asStmts :: Test -> [Txt] | ||
asStmts (Example e _ _) = NE.toList e | ||
asStmts (Property t _ _) = | ||
["prop11 = " ++ t, "(propEvaluation prop11 :: IO String)"] | ||
-- |GHC declarations required for expression evaluation | ||
evalSetup :: Ghc () | ||
evalSetup = do | ||
preludeAsP <- parseImportDecl "import qualified Prelude as P" | ||
context <- getContext | ||
setContext (IIDecl preludeAsP : context) | ||
-- | A wrapper of 'InteractiveEval.execStmt', capturing the execution result | ||
myExecStmt :: String -> ExecOptions -> Ghc (Either String (Maybe String)) | ||
myExecStmt stmt opts = do | ||
(temp, purge) <- liftIO newTempFile | ||
evalPrint <- head <$> runDecls ("evalPrint x = P.writeFile "<> show temp <> " (P.show x)") | ||
modifySession $ \hsc -> hsc {hsc_IC = setInteractivePrintName (hsc_IC hsc) evalPrint} | ||
result <- execStmt stmt opts >>= \case | ||
ExecComplete (Left err) _ -> pure $ Left $ show err | ||
ExecComplete (Right _) _ -> liftIO $ Right . (\x -> if null x then Nothing else Just x) <$> readFile' temp | ||
ExecBreak{} -> pure $ Right $ Just "breakpoints are not supported" | ||
liftIO purge | ||
pure result | ||
{- |GHC declarations required to execute test properties | ||
Example: | ||
prop> \(l::[Bool]) -> reverse (reverse l) == l | ||
+++ OK, passed 100 tests. | ||
prop> \(l::[Bool]) -> reverse l == l | ||
*** Failed! Falsified (after 6 tests and 2 shrinks): | ||
[True,False] | ||
-} | ||
propSetup :: [Loc [Char]] | ||
propSetup = | ||
locate0 | ||
[ ":set -XScopedTypeVariables -XExplicitForAll" | ||
, "import qualified Test.QuickCheck as Q11" | ||
, "propEvaluation p = Q11.quickCheckWithResult Q11.stdArgs p >>= error . Q11.output" -- uses `error` to get a multi-line display | ||
] | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
{-# OPTIONS_GHC -Wwarn -fno-warn-orphans #-} | ||
|
||
-- | Expression execution | ||
module Ide.Plugin.Eval.Code (Statement, testRanges, resultRange, evalSetup, propSetup, testCheck, asStatements,myExecStmt) where | ||
|
||
import Control.Lens ((^.)) | ||
import Control.Monad.IO.Class | ||
import Data.Algorithm.Diff (Diff, PolyDiff (..), getDiff) | ||
import qualified Data.List.NonEmpty as NE | ||
import Data.String (IsString) | ||
import qualified Data.Text as T | ||
import Development.IDE.GHC.Compat | ||
import Development.IDE.Types.Location (Position (..), Range (..)) | ||
import GHC (ExecOptions, ExecResult (..), | ||
execStmt) | ||
import Ide.Plugin.Eval.Types (Language (Plain), Loc, | ||
Located (..), | ||
Section (sectionLanguage), | ||
Test (..), Txt, locate, | ||
locate0) | ||
import Language.LSP.Types.Lens (line, start) | ||
import System.IO.Extra (newTempFile, readFile') | ||
|
||
-- | Return the ranges of the expression and result parts of the given test | ||
testRanges :: Test -> (Range, Range) | ||
testRanges tst = | ||
let startLine = testRange tst ^. start.line | ||
(fromIntegral -> exprLines, fromIntegral -> resultLines) = testLengths tst | ||
resLine = startLine + exprLines | ||
in ( Range | ||
(Position startLine 0) | ||
--(Position (startLine + exprLines + resultLines) 0), | ||
(Position resLine 0) | ||
, Range (Position resLine 0) (Position (resLine + resultLines) 0) | ||
) | ||
|
||
{- |The document range where a test is defined | ||
testRange :: Loc Test -> Range | ||
testRange = fst . testRanges | ||
-} | ||
|
||
-- |The document range where the result of the test is defined | ||
resultRange :: Test -> Range | ||
resultRange = snd . testRanges | ||
|
||
-- TODO: handle BLANKLINE | ||
{- | ||
>>> showDiffs $ getDiff ["abc","def","ghi","end"] ["abc","def","Z","ZZ","end"] | ||
["abc","def","WAS ghi","NOW Z","NOW ZZ","end"] | ||
-} | ||
showDiffs :: (Semigroup a, IsString a) => [Diff a] -> [a] | ||
showDiffs = map showDiff | ||
|
||
showDiff :: (Semigroup a, IsString a) => Diff a -> a | ||
showDiff (First w) = "WAS " <> w | ||
showDiff (Second w) = "NOW " <> w | ||
showDiff (Both w _) = w | ||
|
||
testCheck :: Bool -> (Section, Test) -> [T.Text] -> [T.Text] | ||
testCheck diff (section, test) out | ||
| not diff || null (testOutput test) || sectionLanguage section == Plain = out | ||
| otherwise = showDiffs $ getDiff (map T.pack $ testOutput test) out | ||
|
||
testLengths :: Test -> (Int, Int) | ||
testLengths (Example e r _) = (NE.length e, length r) | ||
testLengths (Property _ r _) = (1, length r) | ||
|
||
-- |A one-line Haskell statement | ||
type Statement = Loc String | ||
|
||
asStatements :: Test -> [Statement] | ||
asStatements lt = locate $ Located (fromIntegral $ testRange lt ^. start.line) (asStmts lt) | ||
|
||
asStmts :: Test -> [Txt] | ||
asStmts (Example e _ _) = NE.toList e | ||
asStmts (Property t _ _) = | ||
["prop11 = " ++ t, "(propEvaluation prop11 :: IO String)"] | ||
|
||
|
||
-- |GHC declarations required for expression evaluation | ||
evalSetup :: Ghc () | ||
evalSetup = do | ||
preludeAsP <- parseImportDecl "import qualified Prelude as P" | ||
context <- getContext | ||
setContext (IIDecl preludeAsP : context) | ||
|
||
-- | A wrapper of 'InteractiveEval.execStmt', capturing the execution result | ||
myExecStmt :: String -> ExecOptions -> Ghc (Either String (Maybe String)) | ||
myExecStmt stmt opts = do | ||
(temp, purge) <- liftIO newTempFile | ||
evalPrint <- head <$> runDecls ("evalPrint x = P.writeFile "<> show temp <> " (P.show x)") | ||
modifySession $ \hsc -> hsc {hsc_IC = setInteractivePrintName (hsc_IC hsc) evalPrint} | ||
result <- execStmt stmt opts >>= \case | ||
ExecComplete (Left err) _ -> pure $ Left $ show err | ||
ExecComplete (Right _) _ -> liftIO $ Right . (\x -> if null x then Nothing else Just x) <$> readFile' temp | ||
ExecBreak{} -> pure $ Right $ Just "breakpoints are not supported" | ||
liftIO purge | ||
pure result | ||
|
||
{- |GHC declarations required to execute test properties | ||
|
||
Example: | ||
|
||
prop> \(l::[Bool]) -> reverse (reverse l) == l | ||
+++ OK, passed 100 tests. | ||
|
||
prop> \(l::[Bool]) -> reverse l == l | ||
*** Failed! Falsified (after 6 tests and 2 shrinks): | ||
[True,False] | ||
-} | ||
propSetup :: [Loc [Char]] | ||
propSetup = | ||
locate0 | ||
[ ":set -XScopedTypeVariables -XExplicitForAll" | ||
, "import qualified Test.QuickCheck as Q11" | ||
, "propEvaluation p = Q11.quickCheckWithResult Q11.stdArgs p >>= error . Q11.output" -- uses `error` to get a multi-line display | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE OverloadedLabels #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Ide.Plugin.Eval.Config | ||
( properties | ||
, getDiffProperty | ||
) where | ||
|
||
import Ide.Plugin.Config (Config) | ||
import Ide.Plugin.Properties | ||
import Ide.PluginUtils (usePropertyLsp) | ||
import Ide.Types (PluginId) | ||
import Language.LSP.Server (MonadLsp) | ||
|
||
properties :: Properties '[ 'PropertyKey "diff" 'TBoolean] | ||
properties = emptyProperties | ||
& defineBooleanProperty #diff | ||
"Enable the diff output (WAS/NOW) of eval lenses" True | ||
|
||
getDiffProperty :: (MonadLsp Config m) => PluginId -> m Bool | ||
getDiffProperty plId = usePropertyLsp #diff plId properties |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is completely altered because of
\r
(it looks like #2597 introducing some\r
s. C.C. @bradrn).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that’s really bad. The pre-commit hook should not change semantics. This is a
stylish-haskell
bug, I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bradrn It didn't change the semantics. It just added
\r
on each line, which gives this "huge" git diff. (As my setting auto-removes\r
s)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarifying — I had thought you meant something different by ‘completely altered’.
I’m a bit confused, though. What exactly changed the file? My unrelated PR (merged after this one was submitted) shouldn’t affect this code, unless you merge it… in which case it shouldn’t show up in this commit. Did you run the pre-commit hook by any chance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bradrn I always rebase my PR even after its submission, so this PR is indeed after your PR. (the last was 8 hours ago: #2622 (comment))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, makes sense, thanks!