Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit c5cb9e0

Browse files
committed
Improve behaviour of addPackage for hpack
Now addPackage will either add the package to all components the module is part of or only to the main "dependencies" object.
1 parent 01b452b commit c5cb9e0

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

src/Haskell/Ide/Engine/Plugin/Package.hs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ findPackageType rootDir = do
136136
return $ fromMaybe NoPackage $ asum [HpackPackage <$> mHpack, CabalPackage <$> mCabal]
137137

138138
-- | Edit a hpack package to add the given package to the package.yaml.
139+
-- If package.yaml is not in an expected format, will fail fatally.
139140
--
140141
-- Currently does not preserve format.
141142
-- Keep an eye out on this other GSOC project!
@@ -155,19 +156,29 @@ editHpackPackage fp modulePath pkgName = do
155156

156157
case Y.decodeThrow contents :: Maybe Object of
157158
Just obj -> do
159+
-- Map over all major components, such as "executable", "executables",
160+
-- "tests" and "benchmarks". Note, that "library" is a major component,
161+
-- but its structure is different and can not be mapped over in the same way.
162+
--
163+
-- Only adds the package if the declared "source-dirs" field is part of the
164+
-- module path, or if no "source-dirs" is declared.
158165
let compsMapped = mapComponentTypes (ensureObject $ mapComponents (ensureObject $ mapCompDependencies addDep)) obj
159166

160-
let addDepToMainLib = fromMaybe True $ do
161-
Object lib <- HM.lookup "library" compsMapped
162-
sourceDirs <- HM.lookup "source-dirs" lib
163-
return $ isInSourceDir sourceDirs
167+
-- Is there a global "dependencies" yaml object?
168+
let addDepToMainDep = fromMaybe False $ do
169+
Array _ <- HM.lookup "dependencies" compsMapped
170+
return True
164171

165-
let newPkg = if addDepToMainLib
166-
then mapMainDependencies addDep compsMapped
167-
else compsMapped
172+
-- Either add the package to only the top-level "dependencies",
173+
-- or to all main components of which the given module is part of.
174+
let newPkg
175+
| addDepToMainDep = mapMainDependencies addDep obj
176+
-- Map over the library component at last, since it has different structure.
177+
| otherwise = mapLibraryDependency addDep compsMapped
168178

169-
newPkgText = T.decodeUtf8 $ Y.encode newPkg
179+
let newPkgText = T.decodeUtf8 $ Y.encode newPkg
170180

181+
-- Construct the WorkSpaceEdit
171182
let numOldLines = length $ T.lines $ T.decodeUtf8 contents
172183
range = J.Range (J.Position 0 0) (J.Position numOldLines 0)
173184
textEdit = J.TextEdit range newPkgText
@@ -187,8 +198,14 @@ editHpackPackage fp modulePath pkgName = do
187198
mapMainDependencies :: (Value -> Value) -> Object -> Object
188199
mapMainDependencies f o =
189200
let g :: T.Text -> Value -> Value
190-
g "dependencies" x = f x
191-
g "library" (Y.Object o') = Y.Object (mapMainDependencies f o')
201+
g "dependencies" = f
202+
g _ = id
203+
in HM.mapWithKey g o
204+
205+
mapLibraryDependency :: (Value -> Value) -> Object -> Object
206+
mapLibraryDependency f o =
207+
let g :: T.Text -> Value -> Value
208+
g "library" (Y.Object o') = Y.Object (mapCompDependencies f o')
192209
g _ x = x
193210
in HM.mapWithKey g o
194211

test/functional/FunctionalCodeActionsSpec.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,8 @@ spec = describe "code actions" $ do
258258
executeCodeAction action
259259

260260
contents <- getDocumentEdit . TextDocumentIdentifier =<< getDocUri "package.yaml"
261-
liftIO $ do
262-
T.lines contents !! 23 `shouldSatisfy` T.isSuffixOf "zlib"
263-
T.lines contents !! 5 `shouldNotSatisfy` T.isSuffixOf "zlib"
264-
T.lines contents !! 6 `shouldNotSatisfy` T.isSuffixOf "zlib"
261+
liftIO $
262+
T.lines contents !! 22 `shouldSatisfy` T.isSuffixOf "zlib"
265263

266264
-- -----------------------------------
267265

test/testdata/addPackageTest/hybrid-exe/package.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ extra-source-files:
1919
# common to point users to the README.md file.
2020
description: Please see the README on GitHub at <https://github.com/githubuser/asdf#readme>
2121

22-
dependencies:
23-
- base >= 4.7 && < 5
24-
2522
library:
2623
source-dirs: src
2724

test/unit/PackagePluginSpec.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ packageSpec = do
153153
, " - -rtsopts\n"
154154
, " - -with-rtsopts=-N\n"
155155
, " dependencies:\n"
156-
, " - zlib\n"
157156
, " - asdf\n"
158157
, "description: Please see the README on GitHub at <https://github.com/githubuser/asdf#readme>\n"
159158
]
@@ -203,13 +202,11 @@ packageSpec = do
203202
res = IdeResultOk
204203
$ WorkspaceEdit (Just $ H.singleton uri textEdits) Nothing
205204
textEdits = List
206-
[ TextEdit (Range (Position 0 0) (Position 37 0)) $ T.concat
205+
[ TextEdit (Range (Position 0 0) (Position 34 0)) $ T.concat
207206
[ "library:\n"
208207
, " source-dirs: src\n"
209208
, "copyright: 2018 Author name here\n"
210209
, "maintainer: [email protected]\n"
211-
, "dependencies:\n"
212-
, "- base >= 4.7 && < 5\n"
213210
, "name: asdf\n"
214211
, "version: 0.1.0.0\n"
215212
, "extra-source-files:\n"

0 commit comments

Comments
 (0)