@@ -208,6 +208,7 @@ import Language.Haskell.LSP.Types (
208
208
_textDocument
209
209
),
210
210
Command (_arguments , _title ),
211
+ Position (.. ),
211
212
ServerMethod (
212
213
WorkspaceApplyEdit
213
214
),
@@ -414,14 +415,47 @@ runEvalCmd lsp st EvalParams{..} =
414
415
(st, fp)
415
416
tests
416
417
417
- let workspaceEditsMap = Map. fromList [(_uri, List edits)]
418
+ let workspaceEditsMap = Map. fromList [(_uri, List $ addFinalReturn mdlText edits)]
418
419
let workspaceEdits = WorkspaceEdit (Just workspaceEditsMap) Nothing
419
420
420
421
return (WorkspaceApplyEdit , ApplyWorkspaceEditParams workspaceEdits)
421
422
in perf " evalCmd" $
422
423
withIndefiniteProgress lsp " Evaluating" Cancellable $
423
424
response' cmd
424
425
426
+ {-
427
+ >>> import Language.Haskell.LSP.Types(applyTextEdit)
428
+ >>> aTest s = let Right [sec] = allSections (tokensFrom s) in head. sectionTests $ sec
429
+ >>> mdl = "module Test where\n-- >>> 2+2"
430
+
431
+ To avoid https://github.com/haskell/haskell-language-server/issues/1213, `addFinalReturn` adds, if necessary, a final empty line to the document before inserting the tests' results.
432
+
433
+ >>> let [e1,e2] = addFinalReturn mdl [asEdit (aTest mdl) ["4"]] in applyTextEdit e2 (applyTextEdit e1 mdl)
434
+ "module Test where\n-- >>> 2+2\n4\n"
435
+
436
+ >>> applyTextEdit (head $ addFinalReturn mdl [asEdit (aTest mdl) ["4"]]) mdl
437
+ "module Test where\n-- >>> 2+2\n"
438
+
439
+ >>> addFinalReturn mdl [asEdit (aTest mdl) ["4"]]
440
+ [TextEdit {_range = Range {_start = Position {_line = 1, _character = 10}, _end = Position {_line = 1, _character = 10}}, _newText = "\n"},TextEdit {_range = Range {_start = Position {_line = 2, _character = 0}, _end = Position {_line = 2, _character = 0}}, _newText = "4\n"}]
441
+
442
+ >>> asEdit (aTest mdl) ["4"]
443
+ TextEdit {_range = Range {_start = Position {_line = 2, _character = 0}, _end = Position {_line = 2, _character = 0}}, _newText = "4\n"}
444
+ -}
445
+ addFinalReturn :: Text -> [TextEdit ] -> [TextEdit ]
446
+ addFinalReturn mdlText edits
447
+ | not (null edits) && not (T. null mdlText) && T. last mdlText /= ' \n ' =
448
+ finalReturn mdlText : edits
449
+ | otherwise = edits
450
+
451
+ finalReturn :: Text -> TextEdit
452
+ finalReturn txt =
453
+ let ls = T. lines txt
454
+ l = length ls - 1
455
+ c = T. length . last $ ls
456
+ p = Position l c
457
+ in TextEdit (Range p p) " \n "
458
+
425
459
moduleText :: (IsString e , MonadIO m ) => LspFuncs c -> Uri -> ExceptT e m Text
426
460
moduleText lsp uri =
427
461
handleMaybeM " mdlText" $
@@ -455,7 +489,7 @@ runTests e@(_st, _) tests = do
455
489
456
490
let checkedResult = testCheck (section, unLoc test) rs
457
491
458
- let edit = TextEdit (resultRange test) ( T. unlines . map pad $ checkedResult)
492
+ let edit = asEdit test ( map pad checkedResult)
459
493
dbg " TEST EDIT" edit
460
494
return edit
461
495
@@ -467,6 +501,9 @@ runTests e@(_st, _) tests = do
467
501
" Add QuickCheck to your cabal dependencies to run this test."
468
502
runTest e df test = evals e df (asStatements test)
469
503
504
+ asEdit :: Loc Test -> [Text ] -> TextEdit
505
+ asEdit test resultLines = TextEdit (resultRange test) (T. unlines resultLines)
506
+
470
507
{-
471
508
The result of evaluating a test line can be:
472
509
* a value
0 commit comments