44
55module Ide.Plugin.SelectionRange (descriptor ) where
66
7- import Control.Monad.IO.Class (liftIO )
8- import Control.Monad.Trans.Maybe (MaybeT (MaybeT ),
9- runMaybeT )
10- import Data.Foldable (find )
11- import qualified Data.Map.Strict as Map
12- import Data.Maybe (fromMaybe , mapMaybe )
13- import Development.IDE (GetHieAst (GetHieAst ),
14- HieAstResult (HAR , hieAst ),
15- IdeAction ,
16- IdeState (shakeExtras ),
17- Range (Range ),
18- fromNormalizedFilePath ,
19- realSrcSpanToRange ,
20- runIdeAction ,
21- toNormalizedFilePath' ,
22- uriToFilePath' )
23- import Development.IDE.Core.Actions (useE )
24- import Development.IDE.Core.PositionMapping (PositionMapping ,
25- fromCurrentPosition ,
26- toCurrentRange )
27- import Development.IDE.GHC.Compat (HieAST (Node ), Span ,
28- getAsts )
29- import Development.IDE.GHC.Compat.Util (mkFastString )
30- import Ide.Types (PluginDescriptor (pluginHandlers ),
31- PluginId ,
32- defaultPluginDescriptor ,
33- mkPluginHandler )
34- import Language.LSP.Server (LspM )
35- import Language.LSP.Types (List (List ),
36- NormalizedFilePath ,
37- Position , ResponseError ,
38- SMethod (STextDocumentSelectionRange ),
39- SelectionRange (.. ),
40- SelectionRangeParams (.. ),
41- TextDocumentIdentifier (TextDocumentIdentifier ))
42- import Prelude hiding (span )
7+ import Control.Monad.IO.Class (liftIO )
8+ import Control.Monad.Reader (MonadReader (ask ))
9+ import Control.Monad.Trans.Maybe (MaybeT (MaybeT ),
10+ runMaybeT )
11+ import Data.Foldable (find )
12+ import qualified Data.Map.Strict as Map
13+ import Data.Maybe (fromMaybe , mapMaybe )
14+ import qualified Data.Text as T
15+ import Development.IDE (GetHieAst (GetHieAst ),
16+ HieAstResult (HAR , hieAst ),
17+ IdeAction ,
18+ IdeState (shakeExtras ),
19+ Range (Range ),
20+ fromNormalizedFilePath ,
21+ logDebug ,
22+ realSrcSpanToRange ,
23+ runIdeAction ,
24+ toNormalizedFilePath' ,
25+ uriToFilePath' )
26+ import Development.IDE.Core.Actions (useE )
27+ import Development.IDE.Core.PositionMapping (PositionMapping ,
28+ fromCurrentPosition ,
29+ toCurrentRange )
30+ import Development.IDE.Core.Shake (ShakeExtras (ShakeExtras , logger ))
31+ import Development.IDE.GHC.Compat (HieAST (Node , nodeChildren , nodeInfo ),
32+ NodeInfo (nodeAnnotations ),
33+ Span , getAsts )
34+ import Development.IDE.GHC.Compat.Util (mkFastString )
35+ import Ide.Plugin.SelectionRange.ASTPreProcess (preProcessAST )
36+ import Ide.Types (PluginDescriptor (pluginHandlers ),
37+ PluginId ,
38+ defaultPluginDescriptor ,
39+ mkPluginHandler )
40+ import Language.LSP.Server (LspM )
41+ import Language.LSP.Types (List (List ),
42+ NormalizedFilePath ,
43+ Position ,
44+ ResponseError ,
45+ SMethod (STextDocumentSelectionRange ),
46+ SelectionRange (.. ),
47+ SelectionRangeParams (.. ),
48+ TextDocumentIdentifier (TextDocumentIdentifier ))
49+ import Prelude hiding (span )
4350
4451descriptor :: PluginId -> PluginDescriptor IdeState
4552descriptor plId = (defaultPluginDescriptor plId)
@@ -62,8 +69,15 @@ getSelectionRanges file positions = fmap (fromMaybe []) <$> runMaybeT $ do
6269 (HAR {hieAst}, positionMapping) <- useE GetHieAst file
6370 positions' <- MaybeT . pure $ traverse (fromCurrentPosition positionMapping) positions
6471 ast <- MaybeT . pure $ getAsts hieAst Map. !? (mkFastString . fromNormalizedFilePath) file
72+
73+ -- FIXME: remove the debug logs when it's done
74+ ShakeExtras {logger} <- ask
75+ let children = nodeAnnotations . nodeInfo <$> nodeChildren ast
76+ liftIO $ logDebug logger $ " children: " <> T. pack (show children)
77+
78+ let ast' = preProcessAST ast
6579 MaybeT . pure . traverse (toCurrentSelectionRange positionMapping) $
66- findSelectionRangesByPositions (astPathsLeafToRoot ast) positions'
80+ findSelectionRangesByPositions (astPathsLeafToRoot ast' ) positions'
6781
6882-- | Like 'toCurrentPosition', but works on 'SelectionRange'
6983toCurrentSelectionRange :: PositionMapping -> SelectionRange -> Maybe SelectionRange
0 commit comments