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

Commit 0517eaa

Browse files
committed
Implement the ancestors function
1 parent c45714e commit 0517eaa

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{-# LANGUAGE LambdaCase #-}
33
{-# LANGUAGE GADTs #-}
44

5-
module Haskell.Ide.Engine.Cradle (findLocalCradle, isStackCradle) where
5+
module Haskell.Ide.Engine.Cradle where
66

77
import HIE.Bios as BIOS
88
import HIE.Bios.Types as BIOS
@@ -14,7 +14,7 @@ import qualified Data.List.NonEmpty as NonEmpty
1414
import Data.List.NonEmpty (NonEmpty)
1515
import System.FilePath
1616
import qualified Data.Map as M
17-
import Data.List (inits, sortOn, find)
17+
import Data.List (sortOn, find)
1818
import Data.Maybe (listToMaybe, mapMaybe, isJust)
1919
import Data.Ord (Down(..))
2020
import Data.Foldable (toList)
@@ -58,21 +58,14 @@ isStackCradle = (`elem` ["stack", "Cabal-Helper-Stack"])
5858
-- This guessing has no guarantees and may change at any time.
5959
findCabalHelperEntryPoint :: FilePath -> IO (Maybe (Ex ProjLoc))
6060
findCabalHelperEntryPoint fp = do
61-
projs <- concat <$> mapM findProjects subdirs
61+
projs <- concat <$> mapM findProjects (ancestors (takeDirectory fp))
6262
case filter (\p -> isCabalNewProject p || isStackProject p) projs of
6363
(x:_) -> return $ Just x
6464
[] -> case filter isCabalOldProject projs of
6565
(x:_) -> return $ Just x
6666
[] -> return Nothing
6767

6868
where
69-
-- | Subdirectories of a given FilePath.
70-
-- Directory closest to the FilePath `fp` is the head,
71-
-- followed by one directory taken away.
72-
subdirs :: [FilePath]
73-
subdirs = reverse . map joinPath . tail . inits
74-
$ splitDirectories (takeDirectory fp)
75-
7669
isStackProject (Ex ProjLocStackYaml {}) = True
7770
isStackProject _ = False
7871

@@ -374,3 +367,26 @@ fixCradle cradle =
374367
where
375368
addOption fp (BIOS.ComponentOptions os ds) =
376369
BIOS.ComponentOptions (os ++ [fp]) ds
370+
371+
-- | Obtain all ancestors from a given directory.
372+
--
373+
-- >>> ancestors "a/b/c/d/e"
374+
-- [ "a/b/c/d/e", "a/b/c/d", "a/b/c", "a/b", "a", "." ]
375+
--
376+
-- >>> ancestors "/a/b/c/d/e"
377+
-- [ "/a/b/c/d/e", "/a/b/c/d", "/a/b/c", "/a/b", "/a", "/" ]
378+
--
379+
-- >>> ancestors "/a/b.hs"
380+
-- [ "/a/b.hs", "/a", "/" ]
381+
--
382+
-- >>> ancestors "a/b.hs"
383+
-- [ "a/b.hs", "a", "." ]
384+
--
385+
-- >>> ancestors "a/b/"
386+
-- [ "a/b" ]
387+
ancestors :: FilePath -> [FilePath]
388+
ancestors dir
389+
| subdir `equalFilePath` dir = [dir]
390+
| otherwise = dir : ancestors subdir
391+
where
392+
subdir = takeDirectory dir

0 commit comments

Comments
 (0)