@@ -27,6 +27,7 @@ import Data.ByteString (ByteString)
27
27
import qualified Data.ByteString.Char8 as B
28
28
import Data.List.NonEmpty (NonEmpty (.. ),
29
29
fromList )
30
+ import Data.Maybe (catMaybes )
30
31
import Data.String (IsString )
31
32
import qualified Data.Text as T
32
33
import Data.Text.Encoding (encodeUtf8 )
@@ -37,18 +38,17 @@ import Development.IDE.Core.Rules (runAction)
37
38
import Development.IDE.Core.RuleTypes (GetFileContents (.. ))
38
39
import Distribution.Client.Add as Add
39
40
import Distribution.Compat.Prelude (Generic )
40
- import Distribution.PackageDescription (GenericPackageDescription ,
41
+ import Distribution.PackageDescription (ComponentName ,
42
+ GenericPackageDescription ,
43
+ PackageDescription (.. ),
41
44
packageDescription ,
42
45
specVersion )
43
46
import Distribution.PackageDescription.Configuration (flattenPackageDescription )
44
47
import Distribution.PackageDescription.Quirks (patchQuirks )
45
48
import qualified Distribution.Pretty as Pretty
46
- import Distribution.Simple.BuildTarget (BuildTarget ,
47
- buildTargetComponentName ,
48
- readBuildTargets )
49
49
import Distribution.Simple.Utils (safeHead )
50
- import Distribution.Verbosity ( silent ,
51
- verboseNoStderr )
50
+ import Distribution.Types.Component ( Component ( .. ) ,
51
+ componentName )
52
52
import Ide.Logger
53
53
import Ide.Plugin.Cabal.Completion.Types (ParseCabalFields (.. ),
54
54
ParseCabalFile (.. ))
@@ -76,7 +76,6 @@ import Language.LSP.Protocol.Types (ApplyWorkspaceEd
76
76
import System.Directory (doesFileExist ,
77
77
listDirectory )
78
78
import System.FilePath (dropFileName ,
79
- makeRelative ,
80
79
splitPath ,
81
80
takeExtension ,
82
81
(</>) )
@@ -134,42 +133,34 @@ addDependencySuggestCodeAction
134
133
:: PluginId
135
134
-> VersionedTextDocumentIdentifier -- ^ Cabal's versioned text identifier
136
135
-> [(T. Text , T. Text )] -- ^ A dependency-version suggestion pairs
137
- -> FilePath -- ^ Path to the haskell file (source of diagnostics)
138
136
-> FilePath -- ^ Path to the cabal file (that will be edited)
139
137
-> GenericPackageDescription
140
138
-> 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
150
140
-- | 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.
152
142
-- 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
165
156
166
157
mkCodeAction :: FilePath -> Maybe String -> (T. Text , T. Text ) -> CodeAction
167
158
mkCodeAction cabalFilePath target (suggestedDep, suggestedVersion) =
168
159
let
169
- versionTitle = if T. null suggestedVersion then T. empty else " version " <> suggestedVersion
160
+ versionTitle = if T. null suggestedVersion then T. empty else " - " <> suggestedVersion
170
161
targetTitle = case target of
171
162
Nothing -> T. empty
172
- Just t -> " target " <> T. pack t
163
+ Just t -> " at " <> T. pack t
173
164
title = " Add dependency " <> suggestedDep <> versionTitle <> targetTitle
174
165
version = if T. null suggestedVersion then Nothing else Just suggestedVersion
175
166
0 commit comments