Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Fix multi source directories #1577

Merged
merged 5 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions haskell-ide-engine.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ test-suite unit-test
, ghc
, haskell-ide-engine
, haskell-lsp-types == 0.19.*
, hie-bios
, hie-test-utils
, hie-plugin-api
, hoogle > 5.0.11
Expand Down
40 changes: 20 additions & 20 deletions hie-plugin-api/Haskell/Ide/Engine/Cradle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,8 @@ partOfComponent ::
-- | Component to check whether the given FilePath is part of it.
ChComponentInfo ->
Bool
partOfComponent fp' comp
| inTargets (ciSourceDirs comp) fp' (getTargets comp fp')
= True
| otherwise
= False
partOfComponent fp' comp =
inTargets (ciSourceDirs comp) fp' (getTargets comp fp')
where
-- Check if the FilePath is in an executable or setup's main-is field
inMainIs :: FilePath -> Bool
Expand All @@ -698,11 +695,15 @@ partOfComponent fp' comp
| otherwise = False

inTargets :: [FilePath] -> FilePath -> [String] -> Bool
inTargets sourceDirs fp targets
| Just relative <- relativeTo fp sourceDirs
= any (`elem` targets) [getModuleName relative, fp] || inMainIs relative
| otherwise
= False
inTargets sourceDirs fp targets =
let candidates = relativeTo fp sourceDirs
in any (existsInTargets targets fp) candidates

existsInTargets :: [String] -> FilePath -> FilePath -> Bool
existsInTargets targets absFp relFp = or
[ any (`elem` targets) [getModuleName relFp, absFp]
, inMainIs relFp
]

getModuleName :: FilePath -> String
getModuleName fp = map
Expand Down Expand Up @@ -846,24 +847,23 @@ ancestors dir
subdir = takeDirectory dir

-- | Assuming a FilePath @"src\/Lib\/Lib.hs"@ and a list of directories
-- such as @["src", "app"]@, returns either the given FilePath
-- such as @["src", "app"]@, returns the given FilePath
-- with a matching directory stripped away.
-- If there are multiple matches, e.g. multiple directories are a prefix
-- of the given FilePath, return the first match in the list.
-- Returns Nothing, if not a single
-- given directory is a prefix of the FilePath.
-- of the given FilePath we return all matches.
-- Returns an empty list if no prefix matches the given FilePath.
--
-- >>> relativeTo "src/Lib/Lib.hs" ["src"]
-- Just "Lib/Lib.hs"
-- ["Lib/Lib.hs"]
--
-- >>> relativeTo "src/Lib/Lib.hs" ["app"]
-- Nothing
-- []
--
-- >>> relativeTo "src/Lib/Lib.hs" ["src", "src/Lib"]
-- Just "Lib/Lib.hs"
relativeTo :: FilePath -> [FilePath] -> Maybe FilePath
relativeTo file sourceDirs = listToMaybe
$ mapMaybe (`stripFilePath` file) sourceDirs
-- ["Lib/Lib.hs", "Lib.hs"]
relativeTo :: FilePath -> [FilePath] -> [FilePath]
relativeTo file sourceDirs =
mapMaybe (`stripFilePath` file) sourceDirs

-- | Returns a user facing display name for the cradle type,
-- e.g. "Stack project" or "GHC session"
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/cabal-helper/implicit-exe/implicit-exe.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ build-type: Simple
library
exposed-modules: Lib
hs-source-dirs: src
build-depends: base >=4.8 && <4.14
build-depends: base
default-language: Haskell2010


executable implicit-exe
main-is: src/Exe.hs
build-depends: base >=4.8 && <4.14, implicit-exe
build-depends: base, implicit-exe
default-language: Haskell2010
6 changes: 3 additions & 3 deletions test/testdata/cabal-helper/mono-repo/A/A.cabal
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
cabal-version: >=2.0
cabal-version: >=1.10
name: A
version: 0.1.0.0
build-type: Simple

library
exposed-modules: MyLib
build-depends: base >=4.9 && < 5
build-depends: base
default-language: Haskell2010

executable A
main-is: Main.hs
other-modules: MyLib
build-depends: base >= 4.9 && < 5, A
build-depends: base, A
default-language: Haskell2010
6 changes: 3 additions & 3 deletions test/testdata/cabal-helper/mono-repo/B/B.cabal
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
cabal-version: >=2.0
cabal-version: >=1.10
name: B
version: 0.1.0.0
build-type: Simple

library
exposed-modules: MyLib
build-depends: base >= 4.9 && < 5
build-depends: base
default-language: Haskell2010

executable B
main-is: Main.hs
other-modules: MyLib
build-depends: base >= 4.9 && < 5, B
build-depends: base, B
default-language: Haskell2010
4 changes: 2 additions & 2 deletions test/testdata/cabal-helper/mono-repo/C/C.cabal
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
cabal-version: >=2.0
cabal-version: >=1.10
name: C
version: 0.1.0.0
build-type: Simple

library
exposed-modules: MyLib
build-depends: base>= 4.9 && < 5
build-depends: base
default-language: Haskell2010
2 changes: 2 additions & 0 deletions test/testdata/cabal-helper/multi-source-dirs/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cabal-version: >=1.10
name: multi-source-dirs
version: 0.1.0.0
license-file: LICENSE
build-type: Simple

library
exposed-modules: Lib, BetterLib
hs-source-dirs: src, src/input
build-depends: base
default-language: Haskell2010
5 changes: 5 additions & 0 deletions test/testdata/cabal-helper/multi-source-dirs/src/BetterLib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module BetterLib where


foo = 3
bar = "String"
6 changes: 6 additions & 0 deletions test/testdata/cabal-helper/multi-source-dirs/src/input/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Lib where

foobar = 15

fizbuzz :: Int -> String
fizbuzz n = "Fizz"
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ build-type: Simple

library
exposed-modules: MyLib
build-depends: base >=4.12 && <4.13
build-depends: base
default-language: Haskell2010
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ build-type: Simple

library
exposed-modules: MyLib
build-depends: base >=4.12 && <4.13
build-depends: base
default-language: Haskell2010
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ build-type: Simple

library
exposed-modules: PluginLib
build-depends: base >=4.12 && <4.13
build-depends: base
default-language: Haskell2010
4 changes: 2 additions & 2 deletions test/testdata/cabal-helper/sub-package/sub-package.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ build-type: Simple

library
exposed-modules: MyLib
build-depends: base >=4.12 && <4.13, plugins-api
build-depends: base, plugins-api
hs-source-dirs: src
default-language: Haskell2010

executable sub-package
main-is: Main.hs
build-depends: base >=4.12 && <4.13, sub-package
build-depends: base, sub-package
hs-source-dirs: app
default-language: Haskell2010
Loading