Skip to content

Commit 7c5c43b

Browse files
committed
fixed build targets lookup
1 parent 9f4d673 commit 7c5c43b

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,8 @@ cabalAddCodeAction state plId (CodeActionParams _ _ (TextDocumentIdentifier uri)
322322
case mbGPD of
323323
Nothing -> pure $ InL []
324324
Just (gpd, _) -> do
325-
actions <- liftIO $ CabalAdd.addDependencySuggestCodeAction plId verTxtDocId
326-
suggestions
327-
haskellFilePath cabalFilePath
328-
gpd
325+
actions <- liftIO $ CabalAdd.addDependencySuggestCodeAction plId verTxtDocId suggestions
326+
cabalFilePath gpd
329327
pure $ InL $ fmap InR actions
330328

331329

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

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import Data.ByteString (ByteString)
2727
import qualified Data.ByteString.Char8 as B
2828
import Data.List.NonEmpty (NonEmpty (..),
2929
fromList)
30+
import Data.Maybe (catMaybes)
3031
import Data.String (IsString)
3132
import qualified Data.Text as T
3233
import Data.Text.Encoding (encodeUtf8)
@@ -37,18 +38,17 @@ import Development.IDE.Core.Rules (runAction)
3738
import Development.IDE.Core.RuleTypes (GetFileContents (..))
3839
import Distribution.Client.Add as Add
3940
import Distribution.Compat.Prelude (Generic)
40-
import Distribution.PackageDescription (GenericPackageDescription,
41+
import Distribution.PackageDescription (ComponentName,
42+
GenericPackageDescription,
43+
PackageDescription (..),
4144
packageDescription,
4245
specVersion)
4346
import Distribution.PackageDescription.Configuration (flattenPackageDescription)
4447
import Distribution.PackageDescription.Quirks (patchQuirks)
4548
import qualified Distribution.Pretty as Pretty
46-
import Distribution.Simple.BuildTarget (BuildTarget,
47-
buildTargetComponentName,
48-
readBuildTargets)
4949
import Distribution.Simple.Utils (safeHead)
50-
import Distribution.Verbosity (silent,
51-
verboseNoStderr)
50+
import Distribution.Types.Component (Component (..),
51+
componentName)
5252
import Ide.Logger
5353
import Ide.Plugin.Cabal.Completion.Types (ParseCabalFields (..),
5454
ParseCabalFile (..))
@@ -76,7 +76,6 @@ import Language.LSP.Protocol.Types (ApplyWorkspaceEd
7676
import System.Directory (doesFileExist,
7777
listDirectory)
7878
import System.FilePath (dropFileName,
79-
makeRelative,
8079
splitPath,
8180
takeExtension,
8281
(</>))
@@ -134,42 +133,34 @@ addDependencySuggestCodeAction
134133
:: PluginId
135134
-> VersionedTextDocumentIdentifier -- ^ Cabal's versioned text identifier
136135
-> [(T.Text, T.Text)] -- ^ A dependency-version suggestion pairs
137-
-> FilePath -- ^ Path to the haskell file (source of diagnostics)
138136
-> FilePath -- ^ Path to the cabal file (that will be edited)
139137
-> GenericPackageDescription
140138
-> IO [CodeAction]
141-
addDependencySuggestCodeAction plId verTxtDocId suggestions haskellFilePath cabalFilePath gpd = do
142-
buildTargets <- liftIO $ getBuildTargets gpd cabalFilePath haskellFilePath
143-
case buildTargets of
144-
-- If there are no build targets found, run `cabal-add` command with default behaviour
145-
[] -> pure $ mkCodeAction cabalFilePath Nothing <$> suggestions
146-
-- Otherwise provide actions for all found targets
147-
targets -> pure $ concat [mkCodeAction cabalFilePath (Just $ buildTargetToStringRepr target) <$>
148-
suggestions | target <- targets]
149-
where
139+
addDependencySuggestCodeAction plId verTxtDocId suggestions cabalFilePath gpd = do
150140
-- | Note the use of `pretty` function.
151-
-- It converts the `BuildTarget` to an acceptable string representation.
141+
-- It converts the `ComponentName` to an acceptable string representation.
152142
-- It will be used in as the input for `cabal-add`'s `executeConfig`.
153-
buildTargetToStringRepr target = render $ Pretty.pretty $ buildTargetComponentName target
154-
155-
-- | Gives the build targets that are used in the `CabalAdd`.
156-
-- Note the unorthodox usage of `readBuildTargets`:
157-
-- If the relative path to the haskell file is provided,
158-
-- the `readBuildTargets` will return a main build target.
159-
-- This behaviour is acceptable for now, but changing to a way of getting
160-
-- all build targets in a file is advised.
161-
getBuildTargets :: GenericPackageDescription -> FilePath -> FilePath -> IO [BuildTarget]
162-
getBuildTargets gpd cabalFilePath haskellFilePath = do
163-
let haskellFileRelativePath = makeRelative (dropFileName cabalFilePath) haskellFilePath
164-
readBuildTargets (verboseNoStderr silent) (flattenPackageDescription gpd) [haskellFileRelativePath]
143+
pure $ concat [mkCodeAction cabalFilePath (Just $ render $ Pretty.pretty cNames) <$>
144+
suggestions | cNames <- getBuildTargetComponentNames gpd]
145+
where
146+
getBuildTargetComponentNames :: GenericPackageDescription -> [ComponentName]
147+
getBuildTargetComponentNames gpd = map componentName components
148+
where PackageDescription{..} = flattenPackageDescription gpd
149+
components = catMaybes $
150+
[CLib <$> library] <>
151+
map (Just . CLib) subLibraries <>
152+
map (Just . CFLib) foreignLibs <>
153+
map (Just . CExe) executables <>
154+
map (Just . CTest) testSuites <>
155+
map (Just . CBench) benchmarks
165156

166157
mkCodeAction :: FilePath -> Maybe String -> (T.Text, T.Text) -> CodeAction
167158
mkCodeAction cabalFilePath target (suggestedDep, suggestedVersion) =
168159
let
169-
versionTitle = if T.null suggestedVersion then T.empty else " version " <> suggestedVersion
160+
versionTitle = if T.null suggestedVersion then T.empty else "-" <> suggestedVersion
170161
targetTitle = case target of
171162
Nothing -> T.empty
172-
Just t -> " target " <> T.pack t
163+
Just t -> " at " <> T.pack t
173164
title = "Add dependency " <> suggestedDep <> versionTitle <> targetTitle
174165
version = if T.null suggestedVersion then Nothing else Just suggestedVersion
175166

0 commit comments

Comments
 (0)