@@ -27,13 +27,10 @@ import Ide.Types (CommandFunction,
27
27
CommandId (CommandId ),
28
28
PluginId )
29
29
import Language.LSP.Protocol.Types (CodeAction (CodeAction ),
30
- CodeActionDisabled (CodeActionDisabled ),
31
30
CodeActionKind (CodeActionKind_QuickFix ),
32
31
Diagnostic (.. ),
33
32
Null (Null ),
34
- Uri (.. ),
35
- type (|? ) (InR ),
36
- uriToFilePath )
33
+ type (|? ) (InR ))
37
34
import System.Directory (doesFileExist ,
38
35
listDirectory )
39
36
@@ -43,7 +40,6 @@ import Data.ByteString (ByteString)
43
40
import qualified Data.ByteString.Char8 as B
44
41
import Data.List.NonEmpty (NonEmpty (.. ),
45
42
fromList )
46
- import Data.Maybe (fromJust )
47
43
import Distribution.Client.Add as Add
48
44
import Distribution.Compat.Prelude (Generic )
49
45
import Distribution.PackageDescription (packageDescription ,
@@ -60,22 +56,28 @@ import System.FilePath (dropFileName,
60
56
splitPath ,
61
57
takeExtension ,
62
58
(</>) )
63
- import System.IO.Unsafe (unsafeInterleaveIO )
64
59
import Text.PrettyPrint (render )
65
60
import Text.Regex.TDFA
61
+ import Distribution.Simple.Utils (safeHead )
66
62
67
- -- | Given a path to a haskell file, finds all cabal files paths
68
- -- sorted from the closest to the farthest .
69
- -- Gives all found paths all the way to the root directory .
70
- findResponsibleCabalFile :: FilePath -> IO [ FilePath ]
63
+
64
+ -- | Given a path to a haskell file, returns the closest cabal file .
65
+ -- If cabal file wasn't found, dives Nothing .
66
+ findResponsibleCabalFile :: FilePath -> IO ( Maybe FilePath )
71
67
findResponsibleCabalFile haskellFilePath = do
72
- contents <- mapM (unsafeInterleaveIO . listDirectory) allDirPaths
73
- let objectWithPaths = concat $ zipWith (\ path content -> map (path </> ) content) allDirPaths contents
74
- let objectCabalExtension = filter (\ c -> takeExtension c == " .cabal" ) objectWithPaths
75
- cabalFiles <- filterM (\ c -> doesFileExist c) objectCabalExtension
76
- pure $ reverse cabalFiles -- sorted from closest to the haskellFilePath
77
- where dirPath = dropFileName haskellFilePath
78
- allDirPaths = scanl1 (</>) (splitPath dirPath)
68
+ let dirPath = dropFileName haskellFilePath
69
+ allDirPaths = reverse $ scanl1 (</>) (splitPath dirPath) -- sorted from most to least specific
70
+ go allDirPaths
71
+ where
72
+ go [] = pure Nothing
73
+ go (path: ps) = do
74
+ objects <- listDirectory path
75
+ let objectsWithPaths = map (\ obj -> path <> obj) objects
76
+ objectsCabalExtension = filter (\ c -> takeExtension c == " .cabal" ) objectsWithPaths
77
+ cabalFiles <- filterM (\ c -> doesFileExist c) objectsCabalExtension
78
+ case safeHead cabalFiles of
79
+ Nothing -> go ps
80
+ Just cabalFile -> pure $ Just cabalFile
79
81
80
82
81
83
-- | Gives a code action that calls the command,
0 commit comments