Skip to content

Commit

Permalink
Make src dirs a configurable option for users (#25)
Browse files Browse the repository at this point in the history
Allow for passing in possible source directory roots as arguments.

For projects that use different source directories this can improve the
hie file lookup if hiedb with `--src-base-dirs` is not available.
  • Loading branch information
josephsumabat authored Dec 23, 2023
1 parent a161178 commit 6334335
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 14 deletions.
11 changes: 11 additions & 0 deletions app/App/Arguments.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Options.Applicative
import StaticLS.StaticEnv.Options
import System.Environment
import System.Exit
import Text.Parsec hiding (many, option)

data PrgOptions = PrgOptions
{ staticEnvOpts :: StaticEnvOptions
Expand Down Expand Up @@ -69,3 +70,13 @@ staticEnvOptParser =
<> help "Path to hiefiles generated by -fwrite-ide-info"
<> showDefault
)
<*> listOption
( long "srcDirs"
<> metavar "TARGET1,TARGET2,TARGET3..."
<> value defaultSrcDirs
<> help "Path to directories containing source code. Comma delimited strings"
<> showDefault
)
where
-- Parse a list of comma delimited strings
listOption = option $ eitherReader (either (Left . show) Right . runParser (sepEndBy (many alphaNum) (char ',')) () "")
3 changes: 2 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ dependencies:
- filepath >= 1.4.1 && < 1.5
- ghc >= 9.4.4 && < 9.7
- ghc-paths >= 0.1.0 && < 0.2
- ghcide >= 1.9.1 && < 2.1.0
- ghcide >= 1.9.1 && < 2.5.0
- hiedb >= 0.4.2 && < 0.5
- lsp >= 1.6.0 && < 1.7
- lsp-types >= 1.6.0 && < 1.7
- mtl >= 2.2.2 && < 2.4
- parsec >= 3.1.0 && < 3.2
- sqlite-simple >= 0.4.18 && < 0.5
- template-haskell >= 2.19.0 && < 2.21
- text >= 2.0.1 && < 2.1
Expand Down
8 changes: 2 additions & 6 deletions src/StaticLS/HIE/File.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,13 @@ modToHieFilePath modName =
runMaybeT $ do
Right unitId <- liftIO (HieDb.resolveUnitId hieDb modName)
Just hieModRow <- liftIO $ HieDb.lookupHieFile hieDb modName unitId
pure $ hieModRow.hieModuleHieFile
pure hieModRow.hieModuleHieFile

-----------------------------------------------------------------------------------
-- File/Directory method for getting hie files - faster but somewhat "hacky"
-- Useful as a fallback
-----------------------------------------------------------------------------------

-- TODO: make this configurable (use cabal?)
srcDirs :: [FilePath]
srcDirs = ["src/", "lib/", "app/", "test/"]

hieFilePathToSrcFilePathFromFile :: (HasStaticEnv m, MonadIO m) => HieFilePath -> MaybeT m SrcFilePath
hieFilePathToSrcFilePathFromFile hiePath = do
hieFile <- exceptToMaybeT $ getHieFile hiePath
Expand All @@ -143,7 +139,7 @@ srcFilePathToHieFilePathFromFile srcPath = do
absoluteRoot <- liftIO $ Dir.makeAbsolute staticEnv.wsRoot
let hieDir = staticEnv.hieFilesPath
absoluteHieDir = absoluteRoot </> hieDir
absoluteSrcDirs = (absoluteRoot </>) <$> srcDirs
absoluteSrcDirs = (absoluteRoot </>) <$> staticEnv.srcDirs
absoluteSrcPath <- liftIO $ Dir.makeAbsolute srcPath

-- Drop all src directory prefixes
Expand Down
9 changes: 6 additions & 3 deletions src/StaticLS/StaticEnv.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import Control.Monad.Trans.Except (ExceptT (..))
import Control.Monad.Trans.Maybe (MaybeT (..), exceptToMaybeT)
import Control.Monad.Trans.Reader (ReaderT (..))
import Database.SQLite.Simple (SQLError)
import qualified GHC
import qualified GHC.Paths as GHC
import qualified GHC.Types.Name.Cache as GHC
import qualified HieDb
import StaticLS.StaticEnv.Options (StaticEnvOptions (..))
import System.FilePath ((</>))
Expand All @@ -51,7 +48,10 @@ data StaticEnv = StaticEnv
, hieFilesPath :: HieFilePath
, wsRoot :: FilePath
-- ^ workspace root
, srcDirs :: [FilePath]
-- ^ directories to search for source code in order of priority
}
deriving (Eq, Show)

type StaticLs = ReaderT StaticEnv IO

Expand All @@ -65,13 +65,16 @@ initStaticEnv wsRoot staticEnvOptions =
do
let databasePath = fmap (wsRoot </>) (Just staticEnvOptions.optionHieDbPath)
hieFilesPath = wsRoot </> staticEnvOptions.optionHieFilesPath
srcDirs = fmap (wsRoot </>) staticEnvOptions.optionSrcDirs

let serverStaticEnv =
StaticEnv
{ hieDbPath = databasePath
, hieFilesPath = hieFilesPath
, wsRoot = wsRoot
, srcDirs = srcDirs
}

pure serverStaticEnv

-- | Run an hiedb action in an exceptT
Expand Down
7 changes: 7 additions & 0 deletions src/StaticLS/StaticEnv/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module StaticLS.StaticEnv.Options (
defaultStaticEnvOptions,
defaultHieDb,
defaultHieFiles,
defaultSrcDirs,
StaticEnvOptions (..),
)
where
Expand All @@ -13,17 +14,23 @@ data StaticEnvOptions = StaticEnvOptions
, optionHieFilesPath :: FilePath
-- ^ Relative path to hie files directory
-- hie files are required for all functionality
, optionSrcDirs :: [FilePath]
}
deriving (Show, Eq)

defaultHieDb :: FilePath
defaultHieDb = ".hiedb"

defaultHieFiles :: FilePath
defaultHieFiles = ".hiefiles"

defaultSrcDirs :: [FilePath]
defaultSrcDirs = ["src/", "lib/", "app/", "test/"]

defaultStaticEnvOptions :: StaticEnvOptions
defaultStaticEnvOptions =
StaticEnvOptions
{ optionHieDbPath = defaultHieDb
, optionHieFilesPath = defaultHieFiles
, optionSrcDirs = defaultSrcDirs
}
11 changes: 7 additions & 4 deletions static-ls.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.5.
-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack

Expand Down Expand Up @@ -69,11 +69,12 @@ library
, filepath >=1.4.1 && <1.5
, ghc >=9.4.4 && <9.7
, ghc-paths >=0.1.0 && <0.2
, ghcide >=1.9.1 && <2.1.0
, ghcide >=1.9.1 && <2.5.0
, hiedb >=0.4.2 && <0.5
, lsp >=1.6.0 && <1.7
, lsp-types >=1.6.0 && <1.7
, mtl >=2.2.2 && <2.4
, parsec >=3.1.0 && <3.2
, sqlite-simple >=0.4.18 && <0.5
, template-haskell >=2.19.0 && <2.21
, text >=2.0.1 && <2.1
Expand Down Expand Up @@ -107,12 +108,13 @@ executable static-ls
, filepath >=1.4.1 && <1.5
, ghc >=9.4.4 && <9.7
, ghc-paths >=0.1.0 && <0.2
, ghcide >=1.9.1 && <2.1.0
, ghcide >=1.9.1 && <2.5.0
, hiedb >=0.4.2 && <0.5
, lsp >=1.6.0 && <1.7
, lsp-types >=1.6.0 && <1.7
, mtl >=2.2.2 && <2.4
, optparse-applicative >=0.17.0.0 && <0.19
, parsec >=3.1.0 && <3.2
, sqlite-simple >=0.4.18 && <0.5
, static-ls
, template-haskell >=2.19.0 && <2.21
Expand Down Expand Up @@ -158,12 +160,13 @@ test-suite static-ls-test
, filepath >=1.4.1 && <1.5
, ghc >=9.4.4 && <9.7
, ghc-paths >=0.1.0 && <0.2
, ghcide >=1.9.1 && <2.1.0
, ghcide >=1.9.1 && <2.5.0
, hiedb >=0.4.2 && <0.5
, hspec ==2.*
, lsp >=1.6.0 && <1.7
, lsp-types >=1.6.0 && <1.7
, mtl >=2.2.2 && <2.4
, parsec >=3.1.0 && <3.2
, sqlite-simple >=0.4.18 && <0.5
, static-ls
, template-haskell >=2.19.0 && <2.21
Expand Down
2 changes: 2 additions & 0 deletions test/StaticLS/HIE/FileSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ spec = do
StaticEnvOptions
{ optionHieDbPath = ""
, optionHieFilesPath = ""
, optionSrcDirs = []
}
staticEnv <- Test.initStaticEnvOpts emptyOpts
hieFile <-
Expand All @@ -68,6 +69,7 @@ spec = do
StaticEnvOptions
{ optionHieDbPath = ""
, optionHieFilesPath = ""
, optionSrcDirs = []
}
staticEnv <- Test.initStaticEnvOpts emptyOpts
hieFile <-
Expand Down
3 changes: 3 additions & 0 deletions test/StaticLS/IDE/DefinitionSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ spec =
StaticEnvOptions
{ optionHieDbPath = ""
, optionHieFilesPath = "test/TestData/.hiefiles"
, optionSrcDirs = defaultSrcDirs
}
staticEnv <- Test.initStaticEnvOpts emptyOpts
locs <- runStaticLs staticEnv $ uncurry getDefinition Test.myFunRef1TdiAndPosition
Expand All @@ -38,6 +39,7 @@ spec =
StaticEnvOptions
{ optionHieDbPath = "./TestData/not-a-real-hiedb-file"
, optionHieFilesPath = "test/TestData/.hiefiles"
, optionSrcDirs = defaultSrcDirs
}
staticEnv <- Test.initStaticEnvOpts emptyOpts
locs <- runStaticLs staticEnv $ uncurry getDefinition Test.myFunRef1TdiAndPosition
Expand All @@ -50,6 +52,7 @@ spec =
StaticEnvOptions
{ optionHieDbPath = ""
, optionHieFilesPath = ""
, optionSrcDirs = []
}
staticEnv <- Test.initStaticEnvOpts emptyOpts
locs <- runStaticLs staticEnv $ uncurry getDefinition Test.myFunRef1TdiAndPosition
Expand Down
4 changes: 4 additions & 0 deletions test/TestImport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ testHieDir = "test/TestData/.hiefiles"
testHieDbDir :: FilePath
testHieDbDir = "test/TestData/.hiedb"

testSrcDirs :: [FilePath]
testSrcDirs = StaticEnv.defaultSrcDirs

defaultTestStaticEnvOptions :: StaticEnvOptions
defaultTestStaticEnvOptions =
StaticEnvOptions
{ optionHieDbPath = testHieDbDir
, optionHieFilesPath = testHieDir
, optionSrcDirs = testSrcDirs
}

initStaticEnvOpts :: StaticEnvOptions -> IO StaticEnv
Expand Down

0 comments on commit 6334335

Please sign in to comment.