Skip to content

Commit 64589d1

Browse files
committed
Implement lookupMod function
1 parent 7e56257 commit 64589d1

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

ghcide/src/Development/IDE/Core/Actions.hs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ module Development.IDE.Core.Actions
1515

1616
import Control.Monad.Reader
1717
import Control.Monad.Trans.Maybe
18+
import qualified Data.ByteString as BS
1819
import qualified Data.HashMap.Strict as HM
1920
import Data.Maybe
2021
import qualified Data.Text as T
2122
import Data.Tuple.Extra
23+
import Development.IDE.Core.Compile (loadHieFile)
2224
import Development.IDE.Core.OfInterest
2325
import Development.IDE.Core.PositionMapping
2426
import Development.IDE.Core.RuleTypes
@@ -32,18 +34,56 @@ import Development.IDE.Types.Location
3234
import qualified HieDb
3335
import Language.LSP.Protocol.Types (DocumentHighlight (..),
3436
SymbolInformation (..))
37+
import Language.LSP.Server (resRootPath)
38+
import System.Directory (doesFileExist)
39+
import System.FilePath ((</>))
3540

3641

37-
-- | Eventually this will lookup/generate URIs for files in dependencies, but not in the
38-
-- project. Right now, this is just a stub.
42+
-- | Generates URIs for files in dependencies, but not in the
43+
-- project.
3944
lookupMod
4045
:: HieDbWriter -- ^ access the database
4146
-> FilePath -- ^ The `.hie` file we got from the database
4247
-> ModuleName
4348
-> Unit
4449
-> Bool -- ^ Is this file a boot file?
4550
-> MaybeT IdeAction Uri
46-
lookupMod _dbchan _hie_f _mod _uid _boot = MaybeT $ pure Nothing
51+
lookupMod _dbchan hieFile moduleName _uid _boot = MaybeT $ do
52+
mProjectRoot <- (resRootPath =<<) <$> asks lspEnv
53+
case mProjectRoot of
54+
Nothing -> pure Nothing
55+
Just projectRoot -> do
56+
let toFilePath :: ModuleName -> FilePath
57+
toFilePath = separateDirectories . show
58+
where
59+
separateDirectories :: FilePath -> FilePath
60+
separateDirectories moduleNameString =
61+
case breakOnDot moduleNameString of
62+
[] -> ""
63+
ms -> foldr1 (</>) ms
64+
breakOnDot :: FilePath -> [FilePath]
65+
breakOnDot = words . map replaceDotWithSpace
66+
replaceDotWithSpace :: Char -> Char
67+
replaceDotWithSpace '.' = ' '
68+
replaceDotWithSpace c = c
69+
writeOutDir :: FilePath
70+
writeOutDir = projectRoot </> ".hls" </> "dependencies"
71+
writeOutFile :: FilePath
72+
writeOutFile = toFilePath moduleName ++ ".hs"
73+
writeOutPath :: FilePath
74+
writeOutPath = writeOutDir </> writeOutFile
75+
moduleUri :: Uri
76+
moduleUri = AtPoint.toUri writeOutPath
77+
fileExists <- liftIO $ doesFileExist writeOutPath
78+
if fileExists
79+
then pure $ Just moduleUri
80+
else do
81+
nc <- asks ideNc
82+
liftIO $ do
83+
moduleSource <- hie_hs_src <$> loadHieFile (mkUpdater nc) hieFile
84+
BS.writeFile writeOutPath moduleSource
85+
pure $ Just moduleUri
86+
4787

4888

4989
-- IMPORTANT NOTE : make sure all rules `useE`d by these have a "Persistent Stale" rule defined,

ghcide/src/Development/IDE/Spans/AtPoint.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Development.IDE.Spans.AtPoint (
1919
, defRowToSymbolInfo
2020
, getNamesAtPoint
2121
, toCurrentLocation
22+
, toUri
2223
, rowToLoc
2324
, nameToLocation
2425
, LookupModule

0 commit comments

Comments
 (0)