Skip to content

Commit 1a8a260

Browse files
committed
Use stale type lens
1 parent 1d07fbe commit 1a8a260

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

ghcide/src/Development/IDE/Plugin/TypeLenses.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import Development.IDE (GhcSession (..),
2828
HscEnvEq (hscEnv),
2929
RuleResult, Rules, define,
3030
srcSpanToRange,
31-
usePropertyAction)
31+
usePropertyAction,
32+
useWithStale)
3233
import Development.IDE.Core.Compile (TcModuleResult (..))
3334
import Development.IDE.Core.Rules (IdeState, runAction)
3435
import Development.IDE.Core.RuleTypes (GetBindings (GetBindings),
@@ -108,10 +109,12 @@ codeLensProvider ideState pId CodeLensParams{_textDocument = TextDocumentIdentif
108109
mode <- liftIO $ runAction "codeLens.config" ideState $ usePropertyAction #mode pId properties
109110
fmap (Right . List) $ case uriToFilePath' uri of
110111
Just (toNormalizedFilePath' -> filePath) -> liftIO $ do
111-
env <- fmap hscEnv <$> runAction "codeLens.GhcSession" ideState (use GhcSession filePath)
112-
tmr <- runAction "codeLens.TypeCheck" ideState (use TypeCheck filePath)
113-
bindings <- runAction "codeLens.GetBindings" ideState (use GetBindings filePath)
114-
gblSigs <- runAction "codeLens.GetGlobalBindingTypeSigs" ideState (use GetGlobalBindingTypeSigs filePath)
112+
-- Using stale results means that we can almost always return a value. In practice
113+
-- this means the lenses don't 'flicker'
114+
env <- fmap (hscEnv . fst) <$> runAction "codeLens.GhcSession" ideState (useWithStale GhcSession filePath)
115+
tmr <- fmap fst <$> runAction "codeLens.TypeCheck" ideState (useWithStale TypeCheck filePath)
116+
bindings <- fmap fst <$> runAction "codeLens.GetBindings" ideState (useWithStale GetBindings filePath)
117+
gblSigs <- fmap fst <$> runAction "codeLens.GetGlobalBindingTypeSigs" ideState (useWithStale GetGlobalBindingTypeSigs filePath)
115118

116119
diag <- atomically $ getDiagnostics ideState
117120
hDiag <- atomically $ getHiddenDiagnostics ideState

ghcide/test/exe/Main.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,18 @@ addSigLensesTests =
969969
[ sigSession "with GHC warnings" True "diagnostics" "" (second Just $ head cases) []
970970
, sigSession "without GHC warnings" False "diagnostics" "" (second (const Nothing) $ head cases) []
971971
]
972+
, testSession "keep stale lens" $ do
973+
let content = T.unlines
974+
[ "module Stale where"
975+
, "f = _"
976+
]
977+
doc <- createDoc "Stale.hs" "haskell" content
978+
oldLens <- getCodeLenses doc
979+
liftIO $ length oldLens @?= 1
980+
let edit = TextEdit (mkRange 0 4 0 5) "" -- Remove the `_`
981+
_ <- applyEdit doc edit
982+
newLens <- getCodeLenses doc
983+
liftIO $ newLens @?= oldLens
972984
]
973985

974986
linkToLocation :: [LocationLink] -> [Location]

0 commit comments

Comments
 (0)