@@ -20,7 +20,7 @@ import Control.Monad (filterM, void)
20
20
import Control.Monad.IO.Class (liftIO )
21
21
import Data.String (IsString )
22
22
import qualified Data.Text as T
23
- import Development.IDE (IdeState )
23
+ import Development.IDE (IdeState , runIdeAction )
24
24
import Distribution.PackageDescription.Quirks (patchQuirks )
25
25
import Ide.PluginUtils (mkLspCommand )
26
26
import Ide.Types (CommandFunction ,
@@ -47,7 +47,7 @@ import Data.Maybe (fromJust)
47
47
import Distribution.Client.Add as Add
48
48
import Distribution.Compat.Prelude (Generic )
49
49
import Distribution.PackageDescription (packageDescription ,
50
- specVersion )
50
+ specVersion , GenericPackageDescription )
51
51
import Distribution.PackageDescription.Configuration (flattenPackageDescription )
52
52
import Distribution.Pretty (pretty )
53
53
import Distribution.Simple.BuildTarget (BuildTarget ,
@@ -68,34 +68,30 @@ import Text.Regex.TDFA
68
68
-- sorted from the closest to the farthest.
69
69
-- Gives all found paths all the way to the root directory.
70
70
findResponsibleCabalFile :: FilePath -> IO [FilePath ]
71
- findResponsibleCabalFile uriPath = do
71
+ findResponsibleCabalFile haskellFilePath = do
72
72
contents <- mapM (unsafeInterleaveIO . listDirectory) allDirPaths
73
73
let objectWithPaths = concat $ zipWith (\ path content -> map (path </> ) content) allDirPaths contents
74
74
let objectCabalExtension = filter (\ c -> takeExtension c == " .cabal" ) objectWithPaths
75
75
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
78
78
allDirPaths = scanl1 (</>) (splitPath dirPath)
79
79
80
80
81
81
-- | Gives a code action that calls the command,
82
82
-- if a suggestion for a missing dependency is found.
83
83
-- Disabled action if no cabal files given.
84
84
-- 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]
96
92
where
97
93
buildTargetToStringRepr target = render $ pretty $ buildTargetComponentName target
98
- mkCodeAction cabalFile target (suggestedDep, suggestedVersion) =
94
+ mkCodeAction cabalFilePath target (suggestedDep, suggestedVersion) =
99
95
let
100
96
versionTitle = if T. null suggestedVersion then T. empty else " version " <> suggestedVersion
101
97
targetTitle = case target of
@@ -105,7 +101,7 @@ hiddenPackageAction plId maxCompletions uri diag cabalFiles =
105
101
106
102
version = if T. null suggestedVersion then Nothing else Just suggestedVersion
107
103
108
- params = CabalAddCommandParams {cabalPath = cabalFile , buildTarget = target, dependency = suggestedDep, version= version}
104
+ params = CabalAddCommandParams {cabalPath = cabalFilePath , buildTarget = target, dependency = suggestedDep, version= version}
109
105
command = mkLspCommand plId (CommandId cabalAddCommand) " Execute Code Action" (Just [toJSON params])
110
106
in CodeAction title (Just CodeActionKind_QuickFix ) (Just [] ) Nothing Nothing Nothing (Just command) Nothing
111
107
@@ -153,15 +149,10 @@ readCabalFile fileName = do
153
149
then snd . patchQuirks <$> B. readFile fileName
154
150
else error (" Failed to read cabal file at " <> fileName)
155
151
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
163
154
let haskellFileRelativePath = makeRelative (dropFileName cabalFilePath) haskellFilePath
164
- readBuildTargets (verboseNoStderr silent) (flattenPackageDescription packDescr ) [haskellFileRelativePath]
155
+ readBuildTargets (verboseNoStderr silent) (flattenPackageDescription gpd ) [haskellFileRelativePath]
165
156
166
157
167
158
-- | Constructs prerequisets for the @executeConfig@
0 commit comments