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

Fix progress counting #1789

Merged
merged 2 commits into from
May 4, 2021
Merged
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
4 changes: 3 additions & 1 deletion ghcide/ghcide.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ test-suite ghcide-tests
tasty-hunit,
tasty-quickcheck,
tasty-rerun,
text
text,
unordered-containers,
if (impl(ghc >= 8.6))
build-depends:
record-dot-preprocessor,
Expand All @@ -379,6 +380,7 @@ test-suite ghcide-tests
Development.IDE.Test.Runfiles
Experiments
Experiments.Types
Progress
default-extensions:
BangPatterns
DeriveFunctor
Expand Down
14 changes: 11 additions & 3 deletions ghcide/src/Development/IDE/Core/ProgressReporting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module Development.IDE.Core.ProgressReporting
-- utilities, reexported for use in Core.Shake
, mRunLspT
, mRunLspTCallback
-- for tests
, recordProgress
, InProgress(..)
)
where

Expand All @@ -18,7 +21,6 @@ import Control.Monad.Trans.Class (lift)
import Data.Foldable (for_)
import Data.Functor (($>))
import qualified Data.HashMap.Strict as HMap
import Data.Maybe (isJust)
import qualified Data.Text as T
import Data.Unique
import Development.IDE.GHC.Orphans ()
Expand Down Expand Up @@ -76,8 +78,14 @@ data InProgress = InProgress
recordProgress :: NormalizedFilePath -> (Int -> Int) -> InProgress -> InProgress
recordProgress file shift InProgress{..} = case HMap.alterF alter file current of
((prev, new), m') ->
let todo' = if isJust prev then todo else todo + 1
done' = if new == 0 then done+1 else done
let (done',todo') =
case (prev,new) of
(Nothing,0) -> (done+1, todo+1)
(Nothing,_) -> (done, todo+1)
(Just 0, 0) -> (done , todo)
(Just 0, _) -> (done-1, todo)
(Just _, 0) -> (done+1, todo)
(Just _, _) -> (done , todo)
in InProgress todo' done' m'
where
alter x = let x' = maybe (shift 0) shift x in ((x,x'), Just x')
Expand Down
2 changes: 2 additions & 0 deletions ghcide/test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import qualified Language.LSP.Types as LSP
import Data.IORef.Extra (atomicModifyIORef_)
import qualified Development.IDE.Plugin.HLS.GhcIde as Ghcide
import Text.Regex.TDFA ((=~))
import qualified Progress

waitForProgressBegin :: Session ()
waitForProgressBegin = skipManyTill anyMessage $ satisfyMaybe $ \case
Expand Down Expand Up @@ -5463,6 +5464,7 @@ unitTests = do
actualOrder <- liftIO $ readIORef orderRef

liftIO $ actualOrder @?= reverse [(1::Int)..20]
, Progress.tests
]

testIde :: IDE.Arguments -> Session () -> IO ()
Expand Down
28 changes: 28 additions & 0 deletions ghcide/test/exe/Progress.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Progress (tests) where

import Development.IDE.Core.ProgressReporting
import Test.Tasty
import Test.Tasty.HUnit
import qualified Data.HashMap.Strict as Map

tests :: TestTree
tests = testGroup "Progress"
[ reportProgressTests
]

reportProgressTests :: TestTree
reportProgressTests = testGroup "recordProgress"
[ test "addNew" addNew
, test "increase" increase
, test "decrease" decrease
, test "done" done
]
where
p0 = InProgress 0 0 mempty
addNew = recordProgress "A" succ p0
increase = recordProgress "A" succ addNew
decrease = recordProgress "A" succ increase
done = recordProgress "A" pred decrease
model InProgress{..} =
(done, todo) @?= (length (filter (==0) (Map.elems current)), Map.size current)
test name p = testCase name $ model p