1
1
{-# LANGUAGE DisambiguateRecordFields #-}
2
2
{-# LANGUAGE OverloadedStrings #-}
3
+ {-# LANGUAGE QuasiQuotes #-}
4
+
3
5
4
6
module Completer where
5
7
@@ -8,16 +10,19 @@ import Control.Lens.Prism
8
10
import qualified Data.ByteString as ByteString
9
11
import Data.Maybe (mapMaybe )
10
12
import qualified Data.Text as T
13
+ import qualified Data.Text.Encoding as Text
11
14
import qualified Development.IDE.Plugin.Completions.Types as Ghcide
12
15
import Distribution.PackageDescription (GenericPackageDescription )
13
16
import Distribution.PackageDescription.Parsec (parseGenericPackageDescriptionMaybe )
14
17
import Ide.Plugin.Cabal.Completion.Completer.FilePath
15
18
import Ide.Plugin.Cabal.Completion.Completer.Module
16
19
import Ide.Plugin.Cabal.Completion.Completer.Paths
20
+ import Ide.Plugin.Cabal.Completion.Completer.Simple (importCompleter )
17
21
import Ide.Plugin.Cabal.Completion.Completer.Types (CompleterData (.. ))
18
22
import Ide.Plugin.Cabal.Completion.Completions
19
23
import Ide.Plugin.Cabal.Completion.Types (CabalPrefixInfo (.. ),
20
24
StanzaName )
25
+ import qualified Ide.Plugin.Cabal.Parse as Parse
21
26
import qualified Language.LSP.Protocol.Lens as L
22
27
import System.FilePath
23
28
import Test.Hls
@@ -33,7 +38,8 @@ completerTests =
33
38
directoryCompleterTests,
34
39
completionHelperTests,
35
40
filePathExposedModulesTests,
36
- exposedModuleCompleterTests
41
+ exposedModuleCompleterTests,
42
+ importCompleterTests
37
43
]
38
44
39
45
basicCompleterTests :: TestTree
@@ -290,23 +296,55 @@ exposedModuleCompleterTests =
290
296
completions @?== []
291
297
]
292
298
where
293
- simpleCompleterData :: Maybe StanzaName -> FilePath -> T. Text -> CompleterData
294
- simpleCompleterData sName dir pref = do
295
- CompleterData
296
- { cabalPrefixInfo = simpleExposedCabalPrefixInfo pref dir,
297
- getLatestGPD = do
298
- cabalContents <- ByteString. readFile $ testDataDir </> " exposed.cabal"
299
- pure $ parseGenericPackageDescriptionMaybe cabalContents,
300
- stanzaName = sName
301
- }
302
299
callModulesCompleter :: Maybe StanzaName -> (Maybe StanzaName -> GenericPackageDescription -> [FilePath ]) -> T. Text -> IO [T. Text ]
303
300
callModulesCompleter sName func prefix = do
304
301
let cData = simpleCompleterData sName testDataDir prefix
305
302
completer <- modulesCompleter func mempty cData
306
303
pure $ fmap extract completer
307
304
305
+ -- TODO: These tests are a bit barebones at the moment,
306
+ -- since we do not take cursorposition into account at this point.
307
+ importCompleterTests :: TestTree
308
+ importCompleterTests =
309
+ testGroup
310
+ " Import Completer Tests"
311
+ [ testCase " All above common sections are suggested" $ do
312
+ completions <- callImportCompleter importTestData
313
+ (" defaults" `elem` completions) @? " defaults contained"
314
+ (" test-defaults" `elem` completions) @? " test-defaults contained"
315
+ -- TODO: Only common sections defined before the current stanza may be imported
316
+ , testCase " Common sections occuring below are not suggested" $ do
317
+ completions <- callImportCompleter importTestData
318
+ (" notForLib" `elem` completions) @? " notForLib contained, this needs to be fixed"
319
+ , testCase " All common sections are suggested when curser is below them" $ do
320
+ completions <- callImportCompleter importTestData
321
+ completions @?== [" defaults" , " notForLib" ," test-defaults" ]
322
+ ]
323
+ where
324
+ callImportCompleter :: T. Text -> IO [T. Text ]
325
+ callImportCompleter filecontent = do
326
+ let cData' = simpleCompleterData Nothing testDataDir " "
327
+ let cabalFields = Parse. readCabalFields " not-real" (Text. encodeUtf8 filecontent)
328
+ case cabalFields of
329
+ Left err -> fail $ show err
330
+ Right fields -> do
331
+ let cData = cData' {getCabalFields = pure $ Just fields}
332
+ completer <- importCompleter mempty cData
333
+ pure $ fmap extract completer
334
+
335
+ simpleCompleterData :: Maybe StanzaName -> FilePath -> T. Text -> CompleterData
336
+ simpleCompleterData sName dir pref = do
337
+ CompleterData
338
+ { cabalPrefixInfo = simpleExposedCabalPrefixInfo pref dir,
339
+ getLatestGPD = do
340
+ cabalContents <- ByteString. readFile $ testDataDir </> " exposed.cabal"
341
+ pure $ parseGenericPackageDescriptionMaybe cabalContents,
342
+ getCabalFields = undefined ,
343
+ stanzaName = sName
344
+ }
345
+
308
346
mkCompleterData :: CabalPrefixInfo -> CompleterData
309
- mkCompleterData prefInfo = CompleterData {getLatestGPD = undefined , cabalPrefixInfo = prefInfo, stanzaName = Nothing }
347
+ mkCompleterData prefInfo = CompleterData {getLatestGPD = undefined , getCabalFields = undefined , cabalPrefixInfo = prefInfo, stanzaName = Nothing }
310
348
311
349
exposedTestDir :: FilePath
312
350
exposedTestDir = addTrailingPathSeparator $ testDataDir </> " src-modules"
@@ -326,3 +364,41 @@ extract :: CompletionItem -> T.Text
326
364
extract item = case item ^. L. textEdit of
327
365
Just (InL v) -> v ^. L. newText
328
366
_ -> error " "
367
+
368
+ importTestData :: T. Text
369
+ importTestData = [trimming |
370
+ cabal-version: 3.0
371
+ name: hls-cabal-plugin
372
+ version: 0.1.0.0
373
+ synopsis:
374
+ homepage:
375
+ license: MIT
376
+ license-file: LICENSE
377
+ author: Fendor
378
+
379
+ category: Development
380
+ extra-source-files: CHANGELOG.md
381
+
382
+ common defaults
383
+ default-language: GHC2021
384
+ -- Should have been in GHC2021, an oversight
385
+ default-extensions: ExplicitNamespaces
386
+
387
+ common test-defaults
388
+ ghc-options: -threaded -rtsopts -with-rtsopts=-N
389
+
390
+ library
391
+ import:
392
+ ^
393
+ exposed-modules: IDE.Plugin.Cabal
394
+ build-depends: base ^>=4.14.3.0
395
+ hs-source-dirs: src
396
+ default-language: Haskell2010
397
+
398
+ common notForLib
399
+ default-language: GHC2021
400
+
401
+ test-suite tests
402
+ import:
403
+ ^
404
+ |]
0 commit comments