Skip to content

Commit 7e48bfa

Browse files
committed
Clean up refine imports plugin
1 parent 86fbd8e commit 7e48bfa

File tree

2 files changed

+39
-50
lines changed

2 files changed

+39
-50
lines changed

plugins/hls-refine-imports-plugin/hls-refine-imports-plugin.cabal

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
cabal-version: 2.2
2-
name: hls-refine-imports-plugin
3-
version: 1.0.0.0
4-
synopsis: Refine imports plugin for Haskell Language Server
5-
license: Apache-2.0
6-
license-file: LICENSE
7-
author: rayshih
8-
maintainer: mnf.shih@gmail.com
9-
category: Development
10-
build-type: Simple
11-
extra-source-files:
12-
LICENSE
1+
cabal-version: 2.2
2+
name: hls-refine-imports-plugin
3+
version: 1.0.0.0
4+
synopsis: Refine imports plugin for Haskell Language Server
5+
license: Apache-2.0
6+
license-file: LICENSE
7+
author: rayshih
8+
maintainer: mnf.shih@gmail.com
9+
category: Development
10+
build-type: Simple
11+
extra-source-files: LICENSE
1312

1413
library
15-
exposed-modules: Ide.Plugin.RefineImports
16-
hs-source-dirs: src
14+
exposed-modules: Ide.Plugin.RefineImports
15+
hs-source-dirs: src
1716
build-depends:
1817
, aeson
19-
, base >=4.12 && <5
18+
, base >=4.12 && <5
2019
, containers
2120
, deepseq
2221
, ghc
23-
, ghcide ^>=1.3
24-
, hls-plugin-api ^>=1.1
25-
, lsp
26-
, lsp-types
22+
, ghcide ^>=1.3
23+
, hls-explicit-imports-plugin ^>=1.0.0.1
2724
, hls-graph
25+
, hls-plugin-api ^>=1.1
26+
, lsp
2827
, text
2928
, unordered-containers
30-
, hls-explicit-imports-plugin ^>= 1.0.0.1
3129

32-
default-language: Haskell2010
33-
default-extensions: DataKinds, TypeOperators
30+
default-language: Haskell2010
31+
default-extensions:
32+
DataKinds
33+
TypeOperators
3434

3535
test-suite tests
3636
type: exitcode-stdio-1.0
@@ -40,7 +40,6 @@ test-suite tests
4040
ghc-options: -threaded -rtsopts -with-rtsopts=-N
4141
build-depends:
4242
, base
43-
, bytestring
4443
, filepath
4544
, hls-refine-imports-plugin
4645
, hls-test-utils

plugins/hls-refine-imports-plugin/test/Main.hs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
{-# LANGUAGE TypeOperators #-}
55
{-# LANGUAGE ViewPatterns #-}
66

7-
module Main (main) where
7+
module Main
8+
( main
9+
) where
810

9-
import qualified Data.ByteString.Lazy as LBS
1011
import Data.Foldable (find, forM_)
1112
import Data.Text (Text)
1213
import qualified Data.Text as T
13-
import Data.Text.Encoding (encodeUtf8)
1414
import qualified Ide.Plugin.RefineImports as RefineImports
1515
import System.FilePath ((<.>), (</>))
1616
import Test.Hls
@@ -23,24 +23,17 @@ main = defaultTestRunner $
2323
, codeLensGoldenTest "UsualCase" 1
2424
]
2525

26-
plugin :: PluginDescriptor IdeState
27-
plugin = RefineImports.descriptor "refineImports"
26+
refineImportsPlugin :: PluginDescriptor IdeState
27+
refineImportsPlugin = RefineImports.descriptor "refineImports"
2828

2929
-- code action tests
3030

3131
codeActionGoldenTest :: FilePath -> Int -> Int -> TestTree
32-
codeActionGoldenTest fp l c = goldenGitDiff (fp <> " (golden)") goldenFilePath $
33-
runSessionWithServer plugin testDataDir $ do
34-
doc <- openDoc hsFilePath "haskell"
35-
actions <- getCodeActions doc (pointRange l c)
36-
case find ((== Just "Refine all imports") . caTitle) actions of
37-
Just (InR x) -> do
38-
executeCodeAction x
39-
LBS.fromStrict . encodeUtf8 <$> documentContents doc
40-
_ -> liftIO $ assertFailure "Unable to find CodeAction"
41-
where
42-
hsFilePath = fp <.> "hs"
43-
goldenFilePath = testDataDir </> fp <.> "expected" <.> "hs"
32+
codeActionGoldenTest fp l c = goldenWithRefineImports fp $ \doc -> do
33+
actions <- getCodeActions doc (pointRange l c)
34+
case find ((== Just "Refine all imports") . caTitle) actions of
35+
Just (InR x) -> executeCodeAction x
36+
_ -> liftIO $ assertFailure "Unable to find CodeAction"
4437

4538
caTitle :: (Command |? CodeAction) -> Maybe Text
4639
caTitle (InR CodeAction {_title}) = Just _title
@@ -50,16 +43,10 @@ caTitle _ = Nothing
5043
-- code lens tests
5144

5245
codeLensGoldenTest :: FilePath -> Int -> TestTree
53-
codeLensGoldenTest fp codeLensIdx = goldenGitDiff (fp <> " (golden)") goldenFilePath $
54-
runSessionWithServer plugin testDataDir $ do
55-
doc <- openDoc hsFilePath "haskell"
56-
codeLens <- (!! codeLensIdx) <$> getCodeLensesBy isRefineImports doc
57-
mapM_ executeCmd
58-
[c | CodeLens{_command = Just c} <- [codeLens]]
59-
LBS.fromStrict . encodeUtf8 <$> documentContents doc
60-
where
61-
hsFilePath = fp <.> "hs"
62-
goldenFilePath = testDataDir </> fp <.> "expected" <.> "hs"
46+
codeLensGoldenTest fp codeLensIdx = goldenWithRefineImports fp $ \doc -> do
47+
codeLens <- (!! codeLensIdx) <$> getCodeLensesBy isRefineImports doc
48+
mapM_ executeCmd
49+
[c | CodeLens{_command = Just c} <- [codeLens]]
6350

6451
getCodeLensesBy :: (CodeLens -> Bool) -> TextDocumentIdentifier -> Session [CodeLens]
6552
getCodeLensesBy f doc = filter f <$> getCodeLenses doc
@@ -79,6 +66,9 @@ executeCmd cmd = do
7966

8067
-- helpers
8168

69+
goldenWithRefineImports :: FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree
70+
goldenWithRefineImports fp = goldenWithHaskellDoc refineImportsPlugin (fp <> " (golden)") testDataDir fp "expected" "hs"
71+
8272
testDataDir :: String
8373
testDataDir = "test" </> "testdata"
8474

0 commit comments

Comments
 (0)