@@ -114,17 +114,25 @@ data CabalAddCommandParams =
114
114
115
115
instance Logger. Pretty CabalAddCommandParams where
116
116
pretty CabalAddCommandParams {.. } =
117
- " CabalAdd parameters:" <+> vcat
118
- [ " cabal path:" <+> pretty cabalPath
119
- , " target:" <+> pretty buildTarget
120
- , " dependendency:" <+> pretty dependency
121
- , " version:" <+> pretty version
117
+ " CabalAdd parameters:" Logger. <+> Logger. vcat
118
+ [ " cabal path:" Logger. <+> Logger. pretty cabalPath
119
+ , " target:" Logger. <+> Logger. pretty buildTarget
120
+ , " dependendency:" Logger. <+> Logger. pretty dependency
121
+ , " version:" Logger. <+> Logger. pretty version
122
122
]
123
123
124
- -- | Gives a code action that calls the command,
125
- -- if a suggestion for a missing dependency is found.
126
- -- Disabled action if no cabal files given.
127
- -- Conducts IO action on a cabal file to find build targets.
124
+ -- | Creates a code action that calls the `cabalAddCommand`,
125
+ -- using dependency-version suggestion pairs as input.
126
+ --
127
+ -- Returns disabled action if no cabal files given.
128
+ --
129
+ -- Takes haskell file and cabal file paths to create a relative path
130
+ -- to the haskell file, which is used to get a `BuildTarget`.
131
+ -- In current implementation the dependency is being added to the main found
132
+ -- build target, but if there will be a way to get all build targets from a file
133
+ -- it will be possible to support addition to a build target of choice.
134
+ --
135
+ -- The cabal file path is also used to make the text `edit` down the line.
128
136
addDependencySuggestCodeAction
129
137
:: Logger. Recorder (Logger. WithPriority Log )
130
138
-> PluginId
@@ -142,8 +150,17 @@ addDependencySuggestCodeAction recorder plId verTxtDocId suggestions haskellFile
142
150
targets -> pure $ concat [mkCodeAction cabalFilePath (Just $ buildTargetToStringRepr target) <$>
143
151
suggestions | target <- targets]
144
152
where
153
+ -- | Note the use of `pretty` funciton.
154
+ -- It converts the `BuildTarget` to an acceptable string representation.
155
+ -- It will be used in as the input for `cabal-add`'s `executeConfig`.
145
156
buildTargetToStringRepr target = render $ pretty $ buildTargetComponentName target
146
157
158
+ -- | Gives the build targets that are used in the `CabalAdd`.
159
+ -- Note the unorthodox usage of `readBuildTargets`:
160
+ -- If the relative path to the haskell file is provided,
161
+ -- the `readBuildTargets` will return a main build target.
162
+ -- This behaviour is acceptable for now, but changing to a way of getting
163
+ -- all build targets in a file is advised.
147
164
getBuildTargets :: GenericPackageDescription -> FilePath -> FilePath -> IO [BuildTarget ]
148
165
getBuildTargets gpd cabalFilePath haskellFilePath = do
149
166
let haskellFileRelativePath = makeRelative (dropFileName cabalFilePath) haskellFilePath
@@ -205,10 +222,10 @@ getDependencyEdit :: MonadIO m => Logger.Recorder (Logger.WithPriority Log) -> (
205
222
FilePath -> Maybe String -> NonEmpty String -> ExceptT PluginError m WorkspaceEdit
206
223
getDependencyEdit recorder env cabalFilePath buildTarget dependency = do
207
224
let (state, caps, verTxtDocId) = env
208
- runActionE " cabal.cabal-add" state $ do
209
- contents <- useWithStaleE GetFileContents $ toNormalizedFilePath cabalFilePath
210
- inFields <- useWithStaleE ParseCabalFields $ toNormalizedFilePath cabalFilePath
211
- inPackDescr <- useWithStaleE ParseCabalFile $ toNormalizedFilePath cabalFilePath
225
+ (mbCnfOrigContents, mbFields, mbPackDescr) <- liftIO $ runAction " cabal.cabal-add" state $ do
226
+ contents <- Development.IDE. useWithStale GetFileContents $ toNormalizedFilePath cabalFilePath
227
+ inFields <- Development.IDE. useWithStale ParseCabalFields $ toNormalizedFilePath cabalFilePath
228
+ inPackDescr <- Development.IDE. useWithStale ParseCabalFile $ toNormalizedFilePath cabalFilePath
212
229
let mbCnfOrigContents = case snd . fst <$> contents of
213
230
Just (Just txt) -> Just $ encodeUtf8 txt
214
231
_ -> Nothing
0 commit comments