Skip to content

Commit 05c315a

Browse files
committed
eval: fix multiline outputs
1 parent 94a93da commit 05c315a

5 files changed

Lines changed: 26 additions & 6 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,20 @@ showDiff (Second w) = "NOW " <> w
6868
showDiff (Both w _) = w
6969

7070
-- | Compare the expected output recorded in the 'EvalExpr' with the actual
71-
-- output @out@. When diffing is enabled and there is a recorded output, return
72-
-- a line-by-line diff (see 'showDiffs'); otherwise return @out@ unchanged.
71+
-- output @out@, returning the result as one 'T.Text' per line. When diffing is
72+
-- enabled and there is a recorded output, return a line-by-line diff (see
73+
-- 'showDiffs'); otherwise return @out@ unchanged.
74+
--
75+
-- @out@ is normalised to one element per line first: a multi-line result
76+
-- arrives here as a single element with embedded newlines, whereas the recorded
77+
-- output is already split per line, and 'getDiff' compares element-wise. Without
78+
-- this, identical multi-line results would be reported as entirely changed.
7379
evalExprCheck :: Bool -> (Section, EvalExpr) -> [T.Text] -> [T.Text]
7480
evalExprCheck diff (section, evalExpr) out
75-
| not diff || null (evalExprOutput evalExpr) || sectionLanguage section == Plain = out
76-
| otherwise = showDiffs $ getDiff (map T.pack $ evalExprOutput evalExpr) out
81+
| not diff || null (evalExprOutput evalExpr) || sectionLanguage section == Plain = outLines
82+
| otherwise = showDiffs $ getDiff (map T.pack $ evalExprOutput evalExpr) outLines
83+
where
84+
outLines = concatMap T.lines out
7785

7886
-- | The number of (expression lines, result lines) an 'EvalExpr' occupies.
7987
evalExprLengths :: EvalExpr -> (Int, Int)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,7 @@ runEvalExprs recorder EvalConfig{..} e evalExprs = do
400400
rs <- runEvalExpr e df evalExpr
401401
dbg $ LogRunEvalExprResults rs
402402

403-
let checkedResult = evalExprCheck eval_cfg_diff (section, evalExpr) rs
404-
let resultLines = concatMap T.lines checkedResult
403+
let resultLines = evalExprCheck eval_cfg_diff (section, evalExpr) rs
405404

406405
let edit = asEdit (sectionFormat section) evalExpr (map pad resultLines)
407406
dbg $ LogRunEvalExprEdits edit

plugins/hls-eval-plugin/test/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ tests =
153153
, goldenWithEval "Variable 'it' works" "TIt" "hs"
154154
, testGroup "configuration"
155155
[ goldenWithEval' "Give 'WAS' by default" "TDiff" "hs" "expected.default"
156+
, goldenWithEval "Refreshing an identical multi-line result is a no-op" "TDiffMultiline" "hs"
156157
, goldenWithEvalConfig' "Give the result only if diff is off" "TDiff" "hs" "expected.no-diff" diffOffConfig
157158
, goldenWithEvalConfig' "Evaluates to exception (not marked)" "TException" "hs" "expected.nomark" (exceptionConfig False)
158159
, goldenWithEvalConfig' "Evaluates to exception (with mark)" "TException" "hs" "expected.marked" (exceptionConfig True)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module TDiffMultiline where
2+
3+
-- |
4+
-- >>> putStrLn "Hello," >> pure "World!"
5+
-- Hello,
6+
-- "World!"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module TDiffMultiline where
2+
3+
-- |
4+
-- >>> putStrLn "Hello," >> pure "World!"
5+
-- Hello,
6+
-- "World!"

0 commit comments

Comments
 (0)