11{-# LANGUAGE GADTs #-}
22{-# LANGUAGE OverloadedLists #-}
33{-# LANGUAGE RecordWildCards #-}
4- {-# LANGUAGE CPP #-}
54{-# OPTIONS_GHC -Wno-overlapping -patterns #-}
65
76module Ide.Plugin.Class.CodeLens where
87
9- import Control.Lens ((^.) )
10- import Control.Monad.IO.Class (liftIO )
8+ import Control.Lens ((^.) )
9+ import Control.Monad.IO.Class (liftIO )
1110import Data.Aeson
12- import Data.Maybe (mapMaybe , maybeToList )
13- import qualified Data.Text as T
11+ import Data.Maybe (mapMaybe , maybeToList )
12+ import qualified Data.Text as T
1413import Development.IDE
14+ import Development.IDE.Core.PositionMapping
1515import Development.IDE.GHC.Compat
1616import Development.IDE.GHC.Compat.Util
1717import GHC.LanguageExtensions.Type
1818import Ide.Plugin.Class.Types
1919import Ide.Plugin.Class.Utils
2020import Ide.PluginUtils
2121import Ide.Types
22- import Language.LSP.Server (sendRequest )
22+ import Language.LSP.Server (sendRequest )
2323import Language.LSP.Types
24- import qualified Language.LSP.Types.Lens as J
24+ import qualified Language.LSP.Types.Lens as J
2525
2626codeLens :: PluginMethodHandler IdeState TextDocumentCodeLens
2727codeLens state plId CodeLensParams {.. } = pluginResponse $ do
2828 nfp <- getNormalizedFilePath uri
29- tmr <- handleMaybeM " Unable to typecheck"
29+ ( tmr, _) <- handleMaybeM " Unable to typecheck"
3030 $ liftIO
3131 $ runAction " classplugin.TypeCheck" state
32- $ use TypeCheck nfp
32+ -- Using stale results means that we can almost always return a value. In practice
33+ -- this means the lenses don't 'flicker'
34+ $ useWithStale TypeCheck nfp
3335
3436 -- All instance binds
35- InstanceBindTypeSigsResult allBinds <-
37+ ( InstanceBindTypeSigsResult allBinds, mp) <-
3638 handleMaybeM " Unable to get InstanceBindTypeSigsResult"
3739 $ liftIO
3840 $ runAction " classplugin.GetInstanceBindTypeSigs" state
39- $ use GetInstanceBindTypeSigs nfp
41+ -- Using stale results means that we can almost always return a value. In practice
42+ -- this means the lenses don't 'flicker'
43+ $ useWithStale GetInstanceBindTypeSigs nfp
4044
4145 pragmaInsertion <- insertPragmaIfNotPresent state nfp InstanceSigs
4246
@@ -53,7 +57,7 @@ codeLens state plId CodeLensParams{..} = pluginResponse $ do
5357 makeLens (range, title) =
5458 generateLens plId range title
5559 $ workspaceEdit pragmaInsertion
56- $ makeEdit range title
60+ $ makeEdit range title mp
5761 codeLens = makeLens <$> mapMaybe getRangeWithSig targetSigs
5862
5963 pure $ List codeLens
@@ -97,13 +101,9 @@ codeLens state plId CodeLensParams{..} = pluginResponse $ do
97101 -- that are nonsense for displaying code lenses.
98102 --
99103 -- See https://github.com/haskell/haskell-language-server/issues/3319
100- #if MIN_VERSION_ghc(9,5,0)
101- | not $ isGenerated (mg_ext fun_matches)
102- #else
103- | not $ isGenerated (mg_origin fun_matches)
104- #endif
105- -> Just $ L l fun_id
106- _ -> Nothing
104+ | not $ isGenerated (groupOrigin fun_matches)
105+ -> Just $ L l fun_id
106+ _ -> Nothing
107107 -- Existed signatures' name
108108 sigNames = concat $ mapMaybe (\ (L _ r) -> getSigName r) cid_sigs
109109 toBindInfo (L l (L l' _)) = BindInfo
@@ -130,12 +130,14 @@ codeLens state plId CodeLensParams{..} = pluginResponse $ do
130130 let cmd = mkLspCommand plId typeLensCommandId title (Just [toJSON edit])
131131 in CodeLens range (Just cmd) Nothing
132132
133- makeEdit :: Range -> T. Text -> [TextEdit ]
134- makeEdit range bind =
133+ makeEdit :: Range -> T. Text -> PositionMapping -> [TextEdit ]
134+ makeEdit range bind mp =
135135 let startPos = range ^. J. start
136136 insertChar = startPos ^. J. character
137137 insertRange = Range startPos startPos
138- in [TextEdit insertRange (bind <> " \n " <> T. replicate (fromIntegral insertChar) " " )]
138+ in case toCurrentRange mp insertRange of
139+ Just rg -> [TextEdit rg (bind <> " \n " <> T. replicate (fromIntegral insertChar) " " )]
140+ Nothing -> []
139141
140142codeLensCommandHandler :: CommandFunction IdeState WorkspaceEdit
141143codeLensCommandHandler _ wedit = do
0 commit comments