Skip to content

Commit ba5048c

Browse files
committed
rule usage, refactoring
1 parent 78f6589 commit ba5048c

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import qualified Language.LSP.VFS as VFS
4949

5050
import qualified Data.Text ()
5151
import qualified Ide.Plugin.Cabal.CabalAdd as CabalAdd
52+
import Debug.Trace
53+
import Distribution.PackageDescription.Configuration (flattenPackageDescription)
5254

5355
data Log
5456
= LogModificationTime NormalizedFilePath FileVersion
@@ -257,13 +259,24 @@ licenseSuggestCodeAction ideState _ (CodeActionParams _ _ (TextDocumentIdentifie
257259
cabalAddCodeAction :: Recorder (WithPriority Log) -> PluginMethodHandler IdeState 'LSP.Method_TextDocumentCodeAction
258260
cabalAddCodeAction recorder state plId (CodeActionParams _ _ (TextDocumentIdentifier uri) _ CodeActionContext{_diagnostics=diags}) = do
259261
maxCompls <- fmap maxCompletions . liftIO $ runAction "cabal-plugin.cabalAdd" state getClientConfigAction
260-
let mbUriPath = uriToFilePath uri
261-
case mbUriPath of
262+
263+
let mbHaskellFilePath = uriToFilePath uri
264+
case mbHaskellFilePath of
262265
Nothing -> pure $ InL []
263-
Just uriPath -> do
264-
cabalFiles <- liftIO $ CabalAdd.findResponsibleCabalFile uriPath
265-
actions <- liftIO $ mapM (\diag -> CabalAdd.hiddenPackageAction plId maxCompls uri diag cabalFiles) diags
266-
pure $ InL $ fmap InR (concat actions)
266+
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)
272+
case mGPD of
273+
Nothing -> pure $ InL []
274+
Just (gpd, _) -> do
275+
actions <- liftIO $ mapM (\diag -> CabalAdd.hiddenPackageAction plId maxCompls diag haskellFilePath cabalFilePath gpd) diags
276+
pure $ InL $ fmap InR (concat actions)
277+
where
278+
noCabalFileAction = CodeAction "No .cabal file found" (Just CodeActionKind_QuickFix) (Just []) Nothing
279+
(Just (CodeActionDisabled "No .cabal file found")) Nothing Nothing Nothing
267280

268281
-- ----------------------------------------------------------------
269282
-- Cabal file of Interest rules and global variable

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Control.Monad (filterM, void)
2020
import Control.Monad.IO.Class (liftIO)
2121
import Data.String (IsString)
2222
import qualified Data.Text as T
23-
import Development.IDE (IdeState)
23+
import Development.IDE (IdeState, runIdeAction)
2424
import Distribution.PackageDescription.Quirks (patchQuirks)
2525
import Ide.PluginUtils (mkLspCommand)
2626
import Ide.Types (CommandFunction,
@@ -47,7 +47,7 @@ import Data.Maybe (fromJust)
4747
import Distribution.Client.Add as Add
4848
import Distribution.Compat.Prelude (Generic)
4949
import Distribution.PackageDescription (packageDescription,
50-
specVersion)
50+
specVersion, GenericPackageDescription)
5151
import Distribution.PackageDescription.Configuration (flattenPackageDescription)
5252
import Distribution.Pretty (pretty)
5353
import Distribution.Simple.BuildTarget (BuildTarget,
@@ -68,34 +68,30 @@ import Text.Regex.TDFA
6868
-- sorted from the closest to the farthest.
6969
-- Gives all found paths all the way to the root directory.
7070
findResponsibleCabalFile :: FilePath -> IO [FilePath]
71-
findResponsibleCabalFile uriPath = do
71+
findResponsibleCabalFile haskellFilePath = do
7272
contents <- mapM (unsafeInterleaveIO . listDirectory) allDirPaths
7373
let objectWithPaths = concat $ zipWith (\path content -> map (path </>) content) allDirPaths contents
7474
let objectCabalExtension = filter (\c -> takeExtension c == ".cabal") objectWithPaths
7575
cabalFiles <- filterM (\c -> doesFileExist c) objectCabalExtension
76-
pure $ reverse cabalFiles -- sorted from closest to the uriPath
77-
where dirPath = dropFileName uriPath
76+
pure $ reverse cabalFiles -- sorted from closest to the haskellFilePath
77+
where dirPath = dropFileName haskellFilePath
7878
allDirPaths = scanl1 (</>) (splitPath dirPath)
7979

8080

8181
-- | Gives a code action that calls the command,
8282
-- if a suggestion for a missing dependency is found.
8383
-- Disabled action if no cabal files given.
8484
-- Conducts IO action on a cabal file to find build targets.
85-
hiddenPackageAction :: PluginId -> Int -> Uri -> Diagnostic -> [FilePath] -> IO [CodeAction]
86-
hiddenPackageAction plId maxCompletions uri diag cabalFiles =
87-
case cabalFiles of
88-
[] -> pure [CodeAction "No .cabal file found" (Just CodeActionKind_QuickFix) (Just []) Nothing
89-
(Just (CodeActionDisabled "No .cabal file found")) Nothing Nothing Nothing]
90-
(cabalFile:_) -> do
91-
buildTargets <- liftIO $ getBuildTargets cabalFile (fromJust $ uriToFilePath uri)
92-
case buildTargets of
93-
[] -> pure $ mkCodeAction cabalFile Nothing <$> hiddenPackageSuggestion maxCompletions (_message diag)
94-
targets -> pure $ concat [mkCodeAction cabalFile (Just $ buildTargetToStringRepr target) <$>
95-
hiddenPackageSuggestion maxCompletions (_message diag) | target <- targets]
85+
hiddenPackageAction :: PluginId -> Int -> Diagnostic -> FilePath -> FilePath -> GenericPackageDescription -> IO [CodeAction]
86+
hiddenPackageAction plId maxCompletions diag haskellFilePath cabalFilePath gpd = do
87+
buildTargets <- liftIO $ getBuildTargets gpd cabalFilePath haskellFilePath
88+
case buildTargets of
89+
[] -> pure $ mkCodeAction cabalFilePath Nothing <$> hiddenPackageSuggestion maxCompletions (_message diag)
90+
targets -> pure $ concat [mkCodeAction cabalFilePath (Just $ buildTargetToStringRepr target) <$>
91+
hiddenPackageSuggestion maxCompletions (_message diag) | target <- targets]
9692
where
9793
buildTargetToStringRepr target = render $ pretty $ buildTargetComponentName target
98-
mkCodeAction cabalFile target (suggestedDep, suggestedVersion) =
94+
mkCodeAction cabalFilePath target (suggestedDep, suggestedVersion) =
9995
let
10096
versionTitle = if T.null suggestedVersion then T.empty else " version " <> suggestedVersion
10197
targetTitle = case target of
@@ -105,7 +101,7 @@ hiddenPackageAction plId maxCompletions uri diag cabalFiles =
105101

106102
version = if T.null suggestedVersion then Nothing else Just suggestedVersion
107103

108-
params = CabalAddCommandParams {cabalPath = cabalFile, buildTarget = target, dependency = suggestedDep, version=version}
104+
params = CabalAddCommandParams {cabalPath = cabalFilePath, buildTarget = target, dependency = suggestedDep, version=version}
109105
command = mkLspCommand plId (CommandId cabalAddCommand) "Execute Code Action" (Just [toJSON params])
110106
in CodeAction title (Just CodeActionKind_QuickFix) (Just []) Nothing Nothing Nothing (Just command) Nothing
111107

@@ -153,15 +149,10 @@ readCabalFile fileName = do
153149
then snd . patchQuirks <$> B.readFile fileName
154150
else error ("Failed to read cabal file at " <> fileName)
155151

156-
getBuildTargets :: FilePath -> FilePath -> IO [BuildTarget]
157-
getBuildTargets cabalFilePath haskellFilePath = do
158-
cabalContents <- readCabalFile cabalFilePath
159-
(_, packDescr) <- case parseCabalFile cabalFilePath cabalContents of
160-
Left err -> error err
161-
Right pair -> pure pair
162-
152+
getBuildTargets :: GenericPackageDescription -> FilePath -> FilePath -> IO [BuildTarget]
153+
getBuildTargets gpd cabalFilePath haskellFilePath = do
163154
let haskellFileRelativePath = makeRelative (dropFileName cabalFilePath) haskellFilePath
164-
readBuildTargets (verboseNoStderr silent) (flattenPackageDescription packDescr) [haskellFileRelativePath]
155+
readBuildTargets (verboseNoStderr silent) (flattenPackageDescription gpd) [haskellFileRelativePath]
165156

166157

167158
-- | Constructs prerequisets for the @executeConfig@

plugins/hls-cabal-plugin/test/cabal-add-testdata/hidden-package/hidden-package.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ executable hidden-package
1212
default-language: Haskell2010
1313

1414

15-
library hidden-package
15+
library
1616
build-depends: base >= 4 && < 5
1717
ghc-options: -Wall

0 commit comments

Comments
 (0)