{-# LANGUAGE RankNTypes   #-}
{-# LANGUAGE TypeFamilies #-}
module Development.IDE.Core.Actions
( getAtPoint
, getDefinition
, getTypeDefinition
, highlightAtPoint
, refsAtPoint
, useE
, useNoFileE
, usesE
, workspaceSymbols
) where

import           Control.Monad.Reader
import           Control.Monad.Trans.Maybe
import qualified Data.HashMap.Strict                  as HM
import           Data.Maybe
import qualified Data.Text                            as T
import           Data.Tuple.Extra
import           Development.IDE.Core.OfInterest
import           Development.IDE.Core.PositionMapping
import           Development.IDE.Core.RuleTypes
import           Development.IDE.Core.Service
import           Development.IDE.Core.Shake
import           Development.IDE.GHC.Compat           hiding (writeHieFile)
import           Development.IDE.Graph
import qualified Development.IDE.Spans.AtPoint        as AtPoint
import           Development.IDE.Types.HscEnvEq       (hscEnv)
import           Development.IDE.Types.Location
import qualified HieDb
import           Language.LSP.Types                   (DocumentHighlight (..),
                                                       SymbolInformation (..))


-- | Eventually this will lookup/generate URIs for files in dependencies, but not in the
-- project. Right now, this is just a stub.
lookupMod
  :: HieDbWriter -- ^ access the database
  -> FilePath -- ^ The `.hie` file we got from the database
  -> ModuleName
  -> Unit
  -> Bool -- ^ Is this file a boot file?
  -> MaybeT IdeAction Uri
lookupMod :: HieDbWriter
-> FilePath -> ModuleName -> Unit -> Bool -> MaybeT IdeAction Uri
lookupMod HieDbWriter
_dbchan FilePath
_hie_f ModuleName
_mod Unit
_uid Bool
_boot = IdeAction (Maybe Uri) -> MaybeT IdeAction Uri
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (IdeAction (Maybe Uri) -> MaybeT IdeAction Uri)
-> IdeAction (Maybe Uri) -> MaybeT IdeAction Uri
forall a b. (a -> b) -> a -> b
$ Maybe Uri -> IdeAction (Maybe Uri)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Uri
forall a. Maybe a
Nothing


-- IMPORTANT NOTE : make sure all rules `useE`d by these have a "Persistent Stale" rule defined,
-- so we can quickly answer as soon as the IDE is opened
-- Even if we don't have persistent information on disk for these rules, the persistent rule
-- should just return an empty result
-- It is imperative that the result of the persistent rule succeed in such a case, or we will
-- block waiting for the rule to be properly computed.

-- | Try to get hover text for the name under point.
getAtPoint :: NormalizedFilePath -> Position -> IdeAction (Maybe (Maybe Range, [T.Text]))
getAtPoint :: NormalizedFilePath
-> Position -> IdeAction (Maybe (Maybe Range, [Text]))
getAtPoint NormalizedFilePath
file Position
pos = MaybeT IdeAction (Maybe Range, [Text])
-> IdeAction (Maybe (Maybe Range, [Text]))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT IdeAction (Maybe Range, [Text])
 -> IdeAction (Maybe (Maybe Range, [Text])))
-> MaybeT IdeAction (Maybe Range, [Text])
-> IdeAction (Maybe (Maybe Range, [Text]))
forall a b. (a -> b) -> a -> b
$ do
  ShakeExtras
ide <- MaybeT IdeAction ShakeExtras
forall r (m :: * -> *). MonadReader r m => m r
ask
  IdeOptions
opts <- IO IdeOptions -> MaybeT IdeAction IdeOptions
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO IdeOptions -> MaybeT IdeAction IdeOptions)
-> IO IdeOptions -> MaybeT IdeAction IdeOptions
forall a b. (a -> b) -> a -> b
$ ShakeExtras -> IO IdeOptions
getIdeOptionsIO ShakeExtras
ide

  (HieAstResult
hf, PositionMapping
mapping) <- GetHieAst
-> NormalizedFilePath
-> MaybeT IdeAction (HieAstResult, PositionMapping)
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> MaybeT IdeAction (v, PositionMapping)
useE GetHieAst
GetHieAst NormalizedFilePath
file
  HscEnv
env <- HscEnvEq -> HscEnv
hscEnv (HscEnvEq -> HscEnv)
-> ((HscEnvEq, PositionMapping) -> HscEnvEq)
-> (HscEnvEq, PositionMapping)
-> HscEnv
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HscEnvEq, PositionMapping) -> HscEnvEq
forall a b. (a, b) -> a
fst ((HscEnvEq, PositionMapping) -> HscEnv)
-> MaybeT IdeAction (HscEnvEq, PositionMapping)
-> MaybeT IdeAction HscEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GhcSession
-> NormalizedFilePath
-> MaybeT IdeAction (HscEnvEq, PositionMapping)
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> MaybeT IdeAction (v, PositionMapping)
useE GhcSession
GhcSession NormalizedFilePath
file
  DocAndKindMap
dkMap <- IdeAction DocAndKindMap -> MaybeT IdeAction DocAndKindMap
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IdeAction DocAndKindMap -> MaybeT IdeAction DocAndKindMap)
-> IdeAction DocAndKindMap -> MaybeT IdeAction DocAndKindMap
forall a b. (a -> b) -> a -> b
$ DocAndKindMap
-> ((DocAndKindMap, PositionMapping) -> DocAndKindMap)
-> Maybe (DocAndKindMap, PositionMapping)
-> DocAndKindMap
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (DocMap -> KindMap -> DocAndKindMap
DKMap DocMap
forall a. Monoid a => a
mempty KindMap
forall a. Monoid a => a
mempty) (DocAndKindMap, PositionMapping) -> DocAndKindMap
forall a b. (a, b) -> a
fst (Maybe (DocAndKindMap, PositionMapping) -> DocAndKindMap)
-> IdeAction (Maybe (DocAndKindMap, PositionMapping))
-> IdeAction DocAndKindMap
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MaybeT IdeAction (DocAndKindMap, PositionMapping)
-> IdeAction (Maybe (DocAndKindMap, PositionMapping))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (GetDocMap
-> NormalizedFilePath
-> MaybeT IdeAction (DocAndKindMap, PositionMapping)
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> MaybeT IdeAction (v, PositionMapping)
useE GetDocMap
GetDocMap NormalizedFilePath
file)

  !Position
pos' <- IdeAction (Maybe Position) -> MaybeT IdeAction Position
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (Maybe Position -> IdeAction (Maybe Position)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Position -> IdeAction (Maybe Position))
-> Maybe Position -> IdeAction (Maybe Position)
forall a b. (a -> b) -> a -> b
$ PositionMapping -> Position -> Maybe Position
fromCurrentPosition PositionMapping
mapping Position
pos)
  IdeAction (Maybe (Maybe Range, [Text]))
-> MaybeT IdeAction (Maybe Range, [Text])
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (IdeAction (Maybe (Maybe Range, [Text]))
 -> MaybeT IdeAction (Maybe Range, [Text]))
-> IdeAction (Maybe (Maybe Range, [Text]))
-> MaybeT IdeAction (Maybe Range, [Text])
forall a b. (a -> b) -> a -> b
$ Maybe (Maybe Range, [Text])
-> IdeAction (Maybe (Maybe Range, [Text]))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Maybe Range, [Text])
 -> IdeAction (Maybe (Maybe Range, [Text])))
-> Maybe (Maybe Range, [Text])
-> IdeAction (Maybe (Maybe Range, [Text]))
forall a b. (a -> b) -> a -> b
$ (Maybe Range -> Maybe Range)
-> (Maybe Range, [Text]) -> (Maybe Range, [Text])
forall a a' b. (a -> a') -> (a, b) -> (a', b)
first (PositionMapping -> Range -> Maybe Range
toCurrentRange PositionMapping
mapping (Range -> Maybe Range) -> Maybe Range -> Maybe Range
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) ((Maybe Range, [Text]) -> (Maybe Range, [Text]))
-> Maybe (Maybe Range, [Text]) -> Maybe (Maybe Range, [Text])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IdeOptions
-> HieAstResult
-> DocAndKindMap
-> HscEnv
-> Position
-> Maybe (Maybe Range, [Text])
AtPoint.atPoint IdeOptions
opts HieAstResult
hf DocAndKindMap
dkMap HscEnv
env Position
pos'

toCurrentLocations :: PositionMapping -> [Location] -> [Location]
toCurrentLocations :: PositionMapping -> [Location] -> [Location]
toCurrentLocations PositionMapping
mapping = (Location -> Maybe Location) -> [Location] -> [Location]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Location -> Maybe Location
go
  where
    go :: Location -> Maybe Location
go (Location Uri
uri Range
range) = Uri -> Range -> Location
Location Uri
uri (Range -> Location) -> Maybe Range -> Maybe Location
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PositionMapping -> Range -> Maybe Range
toCurrentRange PositionMapping
mapping Range
range

-- | useE is useful to implement functions that aren’t rules but need shortcircuiting
-- e.g. getDefinition.
useE :: IdeRule k v => k -> NormalizedFilePath -> MaybeT IdeAction (v, PositionMapping)
useE :: k -> NormalizedFilePath -> MaybeT IdeAction (v, PositionMapping)
useE k
k = IdeAction (Maybe (v, PositionMapping))
-> MaybeT IdeAction (v, PositionMapping)
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (IdeAction (Maybe (v, PositionMapping))
 -> MaybeT IdeAction (v, PositionMapping))
-> (NormalizedFilePath -> IdeAction (Maybe (v, PositionMapping)))
-> NormalizedFilePath
-> MaybeT IdeAction (v, PositionMapping)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> NormalizedFilePath -> IdeAction (Maybe (v, PositionMapping))
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> IdeAction (Maybe (v, PositionMapping))
useWithStaleFast k
k

useNoFileE :: IdeRule k v => IdeState -> k -> MaybeT IdeAction v
useNoFileE :: IdeState -> k -> MaybeT IdeAction v
useNoFileE IdeState
_ide k
k = (v, PositionMapping) -> v
forall a b. (a, b) -> a
fst ((v, PositionMapping) -> v)
-> MaybeT IdeAction (v, PositionMapping) -> MaybeT IdeAction v
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> NormalizedFilePath -> MaybeT IdeAction (v, PositionMapping)
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> MaybeT IdeAction (v, PositionMapping)
useE k
k NormalizedFilePath
emptyFilePath

usesE :: IdeRule k v => k -> [NormalizedFilePath] -> MaybeT IdeAction [(v,PositionMapping)]
usesE :: k
-> [NormalizedFilePath] -> MaybeT IdeAction [(v, PositionMapping)]
usesE k
k = IdeAction (Maybe [(v, PositionMapping)])
-> MaybeT IdeAction [(v, PositionMapping)]
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (IdeAction (Maybe [(v, PositionMapping)])
 -> MaybeT IdeAction [(v, PositionMapping)])
-> ([NormalizedFilePath]
    -> IdeAction (Maybe [(v, PositionMapping)]))
-> [NormalizedFilePath]
-> MaybeT IdeAction [(v, PositionMapping)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Maybe (v, PositionMapping)] -> Maybe [(v, PositionMapping)])
-> IdeAction [Maybe (v, PositionMapping)]
-> IdeAction (Maybe [(v, PositionMapping)])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Maybe (v, PositionMapping)] -> Maybe [(v, PositionMapping)]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence (IdeAction [Maybe (v, PositionMapping)]
 -> IdeAction (Maybe [(v, PositionMapping)]))
-> ([NormalizedFilePath] -> IdeAction [Maybe (v, PositionMapping)])
-> [NormalizedFilePath]
-> IdeAction (Maybe [(v, PositionMapping)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NormalizedFilePath -> IdeAction (Maybe (v, PositionMapping)))
-> [NormalizedFilePath] -> IdeAction [Maybe (v, PositionMapping)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (k -> NormalizedFilePath -> IdeAction (Maybe (v, PositionMapping))
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> IdeAction (Maybe (v, PositionMapping))
useWithStaleFast k
k)

-- | Goto Definition.
getDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [Location])
getDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [Location])
getDefinition NormalizedFilePath
file Position
pos = MaybeT IdeAction [Location] -> IdeAction (Maybe [Location])
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT IdeAction [Location] -> IdeAction (Maybe [Location]))
-> MaybeT IdeAction [Location] -> IdeAction (Maybe [Location])
forall a b. (a -> b) -> a -> b
$ do
    ide :: ShakeExtras
ide@ShakeExtras{ WithHieDb
$sel:withHieDb:ShakeExtras :: ShakeExtras -> WithHieDb
withHieDb :: WithHieDb
withHieDb, HieDbWriter
$sel:hiedbWriter:ShakeExtras :: ShakeExtras -> HieDbWriter
hiedbWriter :: HieDbWriter
hiedbWriter } <- MaybeT IdeAction ShakeExtras
forall r (m :: * -> *). MonadReader r m => m r
ask
    IdeOptions
opts <- IO IdeOptions -> MaybeT IdeAction IdeOptions
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO IdeOptions -> MaybeT IdeAction IdeOptions)
-> IO IdeOptions -> MaybeT IdeAction IdeOptions
forall a b. (a -> b) -> a -> b
$ ShakeExtras -> IO IdeOptions
getIdeOptionsIO ShakeExtras
ide
    (HAR Module
_ HieASTs a
hf RefMap a
_ Map Name [RealSrcSpan]
_ HieKind a
_, PositionMapping
mapping) <- GetHieAst
-> NormalizedFilePath
-> MaybeT IdeAction (HieAstResult, PositionMapping)
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> MaybeT IdeAction (v, PositionMapping)
useE GetHieAst
GetHieAst NormalizedFilePath
file
    (ImportMap Map ModuleName NormalizedFilePath
imports, PositionMapping
_) <- GetImportMap
-> NormalizedFilePath
-> MaybeT IdeAction (ImportMap, PositionMapping)
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> MaybeT IdeAction (v, PositionMapping)
useE GetImportMap
GetImportMap NormalizedFilePath
file
    !Position
pos' <- IdeAction (Maybe Position) -> MaybeT IdeAction Position
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (Maybe Position -> IdeAction (Maybe Position)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Position -> IdeAction (Maybe Position))
-> Maybe Position -> IdeAction (Maybe Position)
forall a b. (a -> b) -> a -> b
$ PositionMapping -> Position -> Maybe Position
fromCurrentPosition PositionMapping
mapping Position
pos)
    PositionMapping -> [Location] -> [Location]
toCurrentLocations PositionMapping
mapping ([Location] -> [Location])
-> MaybeT IdeAction [Location] -> MaybeT IdeAction [Location]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WithHieDb
-> (FilePath -> ModuleName -> Unit -> Bool -> MaybeT IdeAction Uri)
-> IdeOptions
-> Map ModuleName NormalizedFilePath
-> HieASTs a
-> Position
-> MaybeT IdeAction [Location]
forall (m :: * -> *) a.
MonadIO m =>
WithHieDb
-> LookupModule m
-> IdeOptions
-> Map ModuleName NormalizedFilePath
-> HieASTs a
-> Position
-> MaybeT m [Location]
AtPoint.gotoDefinition WithHieDb
withHieDb (HieDbWriter
-> FilePath -> ModuleName -> Unit -> Bool -> MaybeT IdeAction Uri
lookupMod HieDbWriter
hiedbWriter) IdeOptions
opts Map ModuleName NormalizedFilePath
imports HieASTs a
hf Position
pos'

getTypeDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [Location])
getTypeDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [Location])
getTypeDefinition NormalizedFilePath
file Position
pos = MaybeT IdeAction [Location] -> IdeAction (Maybe [Location])
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT IdeAction [Location] -> IdeAction (Maybe [Location]))
-> MaybeT IdeAction [Location] -> IdeAction (Maybe [Location])
forall a b. (a -> b) -> a -> b
$ do
    ide :: ShakeExtras
ide@ShakeExtras{ WithHieDb
withHieDb :: WithHieDb
$sel:withHieDb:ShakeExtras :: ShakeExtras -> WithHieDb
withHieDb, HieDbWriter
hiedbWriter :: HieDbWriter
$sel:hiedbWriter:ShakeExtras :: ShakeExtras -> HieDbWriter
hiedbWriter } <- MaybeT IdeAction ShakeExtras
forall r (m :: * -> *). MonadReader r m => m r
ask
    IdeOptions
opts <- IO IdeOptions -> MaybeT IdeAction IdeOptions
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO IdeOptions -> MaybeT IdeAction IdeOptions)
-> IO IdeOptions -> MaybeT IdeAction IdeOptions
forall a b. (a -> b) -> a -> b
$ ShakeExtras -> IO IdeOptions
getIdeOptionsIO ShakeExtras
ide
    (HieAstResult
hf, PositionMapping
mapping) <- GetHieAst
-> NormalizedFilePath
-> MaybeT IdeAction (HieAstResult, PositionMapping)
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> MaybeT IdeAction (v, PositionMapping)
useE GetHieAst
GetHieAst NormalizedFilePath
file
    !Position
pos' <- IdeAction (Maybe Position) -> MaybeT IdeAction Position
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (Maybe Position -> IdeAction (Maybe Position)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Position -> IdeAction (Maybe Position))
-> Maybe Position -> IdeAction (Maybe Position)
forall a b. (a -> b) -> a -> b
$ PositionMapping -> Position -> Maybe Position
fromCurrentPosition PositionMapping
mapping Position
pos)
    PositionMapping -> [Location] -> [Location]
toCurrentLocations PositionMapping
mapping ([Location] -> [Location])
-> MaybeT IdeAction [Location] -> MaybeT IdeAction [Location]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WithHieDb
-> (FilePath -> ModuleName -> Unit -> Bool -> MaybeT IdeAction Uri)
-> IdeOptions
-> HieAstResult
-> Position
-> MaybeT IdeAction [Location]
forall (m :: * -> *).
MonadIO m =>
WithHieDb
-> LookupModule m
-> IdeOptions
-> HieAstResult
-> Position
-> MaybeT m [Location]
AtPoint.gotoTypeDefinition WithHieDb
withHieDb (HieDbWriter
-> FilePath -> ModuleName -> Unit -> Bool -> MaybeT IdeAction Uri
lookupMod HieDbWriter
hiedbWriter) IdeOptions
opts HieAstResult
hf Position
pos'

highlightAtPoint :: NormalizedFilePath -> Position -> IdeAction (Maybe [DocumentHighlight])
highlightAtPoint :: NormalizedFilePath
-> Position -> IdeAction (Maybe [DocumentHighlight])
highlightAtPoint NormalizedFilePath
file Position
pos = MaybeT IdeAction [DocumentHighlight]
-> IdeAction (Maybe [DocumentHighlight])
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT IdeAction [DocumentHighlight]
 -> IdeAction (Maybe [DocumentHighlight]))
-> MaybeT IdeAction [DocumentHighlight]
-> IdeAction (Maybe [DocumentHighlight])
forall a b. (a -> b) -> a -> b
$ do
    (HAR Module
_ HieASTs a
hf RefMap a
rf Map Name [RealSrcSpan]
_ HieKind a
_,PositionMapping
mapping) <- GetHieAst
-> NormalizedFilePath
-> MaybeT IdeAction (HieAstResult, PositionMapping)
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> MaybeT IdeAction (v, PositionMapping)
useE GetHieAst
GetHieAst NormalizedFilePath
file
    !Position
pos' <- IdeAction (Maybe Position) -> MaybeT IdeAction Position
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (Maybe Position -> IdeAction (Maybe Position)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Position -> IdeAction (Maybe Position))
-> Maybe Position -> IdeAction (Maybe Position)
forall a b. (a -> b) -> a -> b
$ PositionMapping -> Position -> Maybe Position
fromCurrentPosition PositionMapping
mapping Position
pos)
    let toCurrentHighlight :: DocumentHighlight -> Maybe DocumentHighlight
toCurrentHighlight (DocumentHighlight Range
range Maybe DocumentHighlightKind
t) = (Range -> Maybe DocumentHighlightKind -> DocumentHighlight)
-> Maybe DocumentHighlightKind -> Range -> DocumentHighlight
forall a b c. (a -> b -> c) -> b -> a -> c
flip Range -> Maybe DocumentHighlightKind -> DocumentHighlight
DocumentHighlight Maybe DocumentHighlightKind
t (Range -> DocumentHighlight)
-> Maybe Range -> Maybe DocumentHighlight
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PositionMapping -> Range -> Maybe Range
toCurrentRange PositionMapping
mapping Range
range
    (DocumentHighlight -> Maybe DocumentHighlight)
-> [DocumentHighlight] -> [DocumentHighlight]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe DocumentHighlight -> Maybe DocumentHighlight
toCurrentHighlight ([DocumentHighlight] -> [DocumentHighlight])
-> MaybeT IdeAction [DocumentHighlight]
-> MaybeT IdeAction [DocumentHighlight]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>HieASTs a
-> RefMap a -> Position -> MaybeT IdeAction [DocumentHighlight]
forall (m :: * -> *) a.
Monad m =>
HieASTs a -> RefMap a -> Position -> MaybeT m [DocumentHighlight]
AtPoint.documentHighlight HieASTs a
hf RefMap a
rf Position
pos'

-- Refs are not an IDE action, so it is OK to be slow and (more) accurate
refsAtPoint :: NormalizedFilePath -> Position -> Action [Location]
refsAtPoint :: NormalizedFilePath -> Position -> Action [Location]
refsAtPoint NormalizedFilePath
file Position
pos = do
    ShakeExtras{WithHieDb
withHieDb :: WithHieDb
$sel:withHieDb:ShakeExtras :: ShakeExtras -> WithHieDb
withHieDb} <- Action ShakeExtras
getShakeExtras
    [NormalizedFilePath]
fs <- HashMap NormalizedFilePath FileOfInterestStatus
-> [NormalizedFilePath]
forall k v. HashMap k v -> [k]
HM.keys (HashMap NormalizedFilePath FileOfInterestStatus
 -> [NormalizedFilePath])
-> Action (HashMap NormalizedFilePath FileOfInterestStatus)
-> Action [NormalizedFilePath]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Action (HashMap NormalizedFilePath FileOfInterestStatus)
getFilesOfInterestUntracked
    HashMap NormalizedFilePath (HieAstResult, PositionMapping)
asts <- [(NormalizedFilePath, (HieAstResult, PositionMapping))]
-> HashMap NormalizedFilePath (HieAstResult, PositionMapping)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HM.fromList ([(NormalizedFilePath, (HieAstResult, PositionMapping))]
 -> HashMap NormalizedFilePath (HieAstResult, PositionMapping))
-> ([Maybe (HieAstResult, PositionMapping)]
    -> [(NormalizedFilePath, (HieAstResult, PositionMapping))])
-> [Maybe (HieAstResult, PositionMapping)]
-> HashMap NormalizedFilePath (HieAstResult, PositionMapping)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((NormalizedFilePath, Maybe (HieAstResult, PositionMapping))
 -> Maybe (NormalizedFilePath, (HieAstResult, PositionMapping)))
-> [(NormalizedFilePath, Maybe (HieAstResult, PositionMapping))]
-> [(NormalizedFilePath, (HieAstResult, PositionMapping))]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (NormalizedFilePath, Maybe (HieAstResult, PositionMapping))
-> Maybe (NormalizedFilePath, (HieAstResult, PositionMapping))
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ([(NormalizedFilePath, Maybe (HieAstResult, PositionMapping))]
 -> [(NormalizedFilePath, (HieAstResult, PositionMapping))])
-> ([Maybe (HieAstResult, PositionMapping)]
    -> [(NormalizedFilePath, Maybe (HieAstResult, PositionMapping))])
-> [Maybe (HieAstResult, PositionMapping)]
-> [(NormalizedFilePath, (HieAstResult, PositionMapping))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NormalizedFilePath]
-> [Maybe (HieAstResult, PositionMapping)]
-> [(NormalizedFilePath, Maybe (HieAstResult, PositionMapping))]
forall a b. [a] -> [b] -> [(a, b)]
zip [NormalizedFilePath]
fs ([Maybe (HieAstResult, PositionMapping)]
 -> HashMap NormalizedFilePath (HieAstResult, PositionMapping))
-> Action [Maybe (HieAstResult, PositionMapping)]
-> Action
     (HashMap NormalizedFilePath (HieAstResult, PositionMapping))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GetHieAst
-> [NormalizedFilePath]
-> Action [Maybe (HieAstResult, PositionMapping)]
forall k v.
IdeRule k v =>
k -> [NormalizedFilePath] -> Action [Maybe (v, PositionMapping)]
usesWithStale GetHieAst
GetHieAst [NormalizedFilePath]
fs
    WithHieDb
-> NormalizedFilePath
-> Position
-> FOIReferences
-> Action [Location]
forall (m :: * -> *).
MonadIO m =>
WithHieDb
-> NormalizedFilePath -> Position -> FOIReferences -> m [Location]
AtPoint.referencesAtPoint WithHieDb
withHieDb NormalizedFilePath
file Position
pos (HashMap NormalizedFilePath (HieAstResult, PositionMapping)
-> FOIReferences
AtPoint.FOIReferences HashMap NormalizedFilePath (HieAstResult, PositionMapping)
asts)

workspaceSymbols :: T.Text -> IdeAction (Maybe [SymbolInformation])
workspaceSymbols :: Text -> IdeAction (Maybe [SymbolInformation])
workspaceSymbols Text
query = MaybeT IdeAction [SymbolInformation]
-> IdeAction (Maybe [SymbolInformation])
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT IdeAction [SymbolInformation]
 -> IdeAction (Maybe [SymbolInformation]))
-> MaybeT IdeAction [SymbolInformation]
-> IdeAction (Maybe [SymbolInformation])
forall a b. (a -> b) -> a -> b
$ do
  ShakeExtras{WithHieDb
withHieDb :: WithHieDb
$sel:withHieDb:ShakeExtras :: ShakeExtras -> WithHieDb
withHieDb} <- MaybeT IdeAction ShakeExtras
forall r (m :: * -> *). MonadReader r m => m r
ask
  [Res DefRow]
res <- IO [Res DefRow] -> MaybeT IdeAction [Res DefRow]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [Res DefRow] -> MaybeT IdeAction [Res DefRow])
-> IO [Res DefRow] -> MaybeT IdeAction [Res DefRow]
forall a b. (a -> b) -> a -> b
$ (HieDb -> IO [Res DefRow]) -> IO [Res DefRow]
WithHieDb
withHieDb (\HieDb
hieDb -> HieDb -> FilePath -> IO [Res DefRow]
HieDb.searchDef HieDb
hieDb (FilePath -> IO [Res DefRow]) -> FilePath -> IO [Res DefRow]
forall a b. (a -> b) -> a -> b
$ Text -> FilePath
T.unpack Text
query)
  [SymbolInformation] -> MaybeT IdeAction [SymbolInformation]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([SymbolInformation] -> MaybeT IdeAction [SymbolInformation])
-> [SymbolInformation] -> MaybeT IdeAction [SymbolInformation]
forall a b. (a -> b) -> a -> b
$ (Res DefRow -> Maybe SymbolInformation)
-> [Res DefRow] -> [SymbolInformation]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Res DefRow -> Maybe SymbolInformation
AtPoint.defRowToSymbolInfo [Res DefRow]
res