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

Commit f291d99

Browse files
committed
Handle multiple source directories correctly
Change existing behaviour to find all source directories which are a prefix of the given filepath.
1 parent 6785cce commit f291d99

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

hie-plugin-api/Haskell/Ide/Engine/Cradle.hs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -684,11 +684,8 @@ partOfComponent ::
684684
-- | Component to check whether the given FilePath is part of it.
685685
ChComponentInfo ->
686686
Bool
687-
partOfComponent fp' comp
688-
| inTargets (ciSourceDirs comp) fp' (getTargets comp fp')
689-
= True
690-
| otherwise
691-
= False
687+
partOfComponent fp' comp =
688+
inTargets (ciSourceDirs comp) fp' (getTargets comp fp')
692689
where
693690
-- Check if the FilePath is in an executable or setup's main-is field
694691
inMainIs :: FilePath -> Bool
@@ -698,11 +695,15 @@ partOfComponent fp' comp
698695
| otherwise = False
699696

700697
inTargets :: [FilePath] -> FilePath -> [String] -> Bool
701-
inTargets sourceDirs fp targets
702-
| Just relative <- relativeTo fp sourceDirs
703-
= any (`elem` targets) [getModuleName relative, fp] || inMainIs relative
704-
| otherwise
705-
= False
698+
inTargets sourceDirs fp targets =
699+
let candidates = relativeTo fp sourceDirs
700+
in any (existsInTargets targets fp) candidates
701+
702+
existsInTargets :: [String] -> FilePath -> FilePath -> Bool
703+
existsInTargets targets absFp relFp = or
704+
[ any (`elem` targets) [getModuleName relFp, absFp]
705+
, inMainIs relFp
706+
]
706707

707708
getModuleName :: FilePath -> String
708709
getModuleName fp = map
@@ -846,24 +847,23 @@ ancestors dir
846847
subdir = takeDirectory dir
847848

848849
-- | Assuming a FilePath @"src\/Lib\/Lib.hs"@ and a list of directories
849-
-- such as @["src", "app"]@, returns either the given FilePath
850+
-- such as @["src", "app"]@, returns the given FilePath
850851
-- with a matching directory stripped away.
851852
-- If there are multiple matches, e.g. multiple directories are a prefix
852-
-- of the given FilePath, return the first match in the list.
853-
-- Returns Nothing, if not a single
854-
-- given directory is a prefix of the FilePath.
853+
-- of the given FilePath we return all matches.
854+
-- Returns an empty list if no prefix matches the given FilePath.
855855
--
856856
-- >>> relativeTo "src/Lib/Lib.hs" ["src"]
857-
-- Just "Lib/Lib.hs"
857+
-- ["Lib/Lib.hs"]
858858
--
859859
-- >>> relativeTo "src/Lib/Lib.hs" ["app"]
860-
-- Nothing
860+
-- []
861861
--
862862
-- >>> relativeTo "src/Lib/Lib.hs" ["src", "src/Lib"]
863-
-- Just "Lib/Lib.hs"
864-
relativeTo :: FilePath -> [FilePath] -> Maybe FilePath
865-
relativeTo file sourceDirs = listToMaybe
866-
$ mapMaybe (`stripFilePath` file) sourceDirs
863+
-- ["Lib/Lib.hs", "Lib.hs"]
864+
relativeTo :: FilePath -> [FilePath] -> [FilePath]
865+
relativeTo file sourceDirs =
866+
mapMaybe (`stripFilePath` file) sourceDirs
867867

868868
-- | Returns a user facing display name for the cradle type,
869869
-- e.g. "Stack project" or "GHC session"

0 commit comments

Comments
 (0)