Skip to content

Commit b5956d6

Browse files
committed
better findResponsibleCabalFile
1 parent ba5048c commit b5956d6

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ cabalAddCodeAction recorder state plId (CodeActionParams _ _ (TextDocumentIdenti
264264
case mbHaskellFilePath of
265265
Nothing -> pure $ InL []
266266
Just haskellFilePath -> do
267-
cabalFiles <- liftIO $ CabalAdd.findResponsibleCabalFile haskellFilePath
268-
case cabalFiles of
269-
[] -> pure $ InL $ fmap InR [noCabalFileAction]
270-
(cabalFilePath:_) -> do
271-
mGPD <- liftIO $ runIdeAction "cabal-plugin.modulesCompleter.gpd" (shakeExtras state) $ useWithStaleFast ParseCabalFile $ toNormalizedFilePath (head cabalFiles)
267+
mbCabalFile <- liftIO $ CabalAdd.findResponsibleCabalFile haskellFilePath
268+
case mbCabalFile of
269+
Nothing -> pure $ InL $ fmap InR [noCabalFileAction]
270+
Just cabalFilePath -> do
271+
mGPD <- liftIO $ runIdeAction "cabal-plugin.modulesCompleter.gpd" (shakeExtras state) $ useWithStaleFast ParseCabalFile $ toNormalizedFilePath cabalFilePath
272272
case mGPD of
273273
Nothing -> pure $ InL []
274274
Just (gpd, _) -> do

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/CabalAdd.hs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,10 @@ import Ide.Types (CommandFunction,
2727
CommandId (CommandId),
2828
PluginId)
2929
import Language.LSP.Protocol.Types (CodeAction (CodeAction),
30-
CodeActionDisabled (CodeActionDisabled),
3130
CodeActionKind (CodeActionKind_QuickFix),
3231
Diagnostic (..),
3332
Null (Null),
34-
Uri (..),
35-
type (|?) (InR),
36-
uriToFilePath)
33+
type (|?) (InR))
3734
import System.Directory (doesFileExist,
3835
listDirectory)
3936

@@ -43,7 +40,6 @@ import Data.ByteString (ByteString)
4340
import qualified Data.ByteString.Char8 as B
4441
import Data.List.NonEmpty (NonEmpty (..),
4542
fromList)
46-
import Data.Maybe (fromJust)
4743
import Distribution.Client.Add as Add
4844
import Distribution.Compat.Prelude (Generic)
4945
import Distribution.PackageDescription (packageDescription,
@@ -60,22 +56,28 @@ import System.FilePath (dropFileName,
6056
splitPath,
6157
takeExtension,
6258
(</>))
63-
import System.IO.Unsafe (unsafeInterleaveIO)
6459
import Text.PrettyPrint (render)
6560
import Text.Regex.TDFA
61+
import Distribution.Simple.Utils (safeHead)
6662

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)
7167
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
7981

8082

8183
-- | Gives a code action that calls the command,

0 commit comments

Comments
 (0)