@@ -15,10 +15,12 @@ module Development.IDE.Core.Actions
15
15
16
16
import Control.Monad.Reader
17
17
import Control.Monad.Trans.Maybe
18
+ import qualified Data.ByteString as BS
18
19
import qualified Data.HashMap.Strict as HM
19
20
import Data.Maybe
20
21
import qualified Data.Text as T
21
22
import Data.Tuple.Extra
23
+ import Development.IDE.Core.Compile (loadHieFile )
22
24
import Development.IDE.Core.OfInterest
23
25
import Development.IDE.Core.PositionMapping
24
26
import Development.IDE.Core.RuleTypes
@@ -32,18 +34,56 @@ import Development.IDE.Types.Location
32
34
import qualified HieDb
33
35
import Language.LSP.Protocol.Types (DocumentHighlight (.. ),
34
36
SymbolInformation (.. ))
37
+ import Language.LSP.Server (resRootPath )
38
+ import System.Directory (doesFileExist )
39
+ import System.FilePath ((</>) )
35
40
36
41
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.
39
44
lookupMod
40
45
:: HieDbWriter -- ^ access the database
41
46
-> FilePath -- ^ The `.hie` file we got from the database
42
47
-> ModuleName
43
48
-> Unit
44
49
-> Bool -- ^ Is this file a boot file?
45
50
-> 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
+
47
87
48
88
49
89
-- IMPORTANT NOTE : make sure all rules `useE`d by these have a "Persistent Stale" rule defined,
0 commit comments