Skip to content

Commit 3fcc812

Browse files
committed
haskell#3017 Fix CodeAction title, adjust tests
1 parent e24f590 commit 3fcc812

File tree

7 files changed

+55
-17
lines changed

7 files changed

+55
-17
lines changed

plugins/hls-class-plugin/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
The class plugin provides handy operations about class, includes:
44

55
1. Code action to add minimal class definition methods.
6-
2. Type lens about missing type signatures for instance methods.
6+
2. Code action to all missing class methods.
7+
3. Type lens about missing type signatures for instance methods.
78

89
## Demo
910

plugins/hls-class-plugin/hls-class-plugin.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,4 @@ test-suite tests
7777
, hls-test-utils ^>=1.4
7878
, lens
7979
, lsp-types
80+
, text

plugins/hls-class-plugin/src/Ide/Plugin/Class/CodeAction.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,12 @@ foo range sigs = ("all missing methods", methodGroups)
230230
, inRange range (getSrcSpan $ bindName sig)
231231
]
232232

233-
234233
-- Return (name text, signature text)
235234
minDefToMethodGroups :: Range -> [InstanceBindTypeSig] -> BooleanFormula Name -> [Suggestion]
236235
minDefToMethodGroups range sigs minDef = suggestions
237236
where
238237
makeSuggestion methodGroup =
239-
let name = mconcat $ intersperse "," $ fst <$> methodGroup
238+
let name = mconcat $ intersperse "," $ (\x -> "'" <> x <> "'") . fst <$> methodGroup
240239
in (name, methodGroup)
241240

242241
suggestions = makeSuggestion <$> go minDef

plugins/hls-class-plugin/test/Main.hs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Main
1212
import Control.Lens (Prism', prism', (^.), (^..), (^?))
1313
import Control.Monad (void)
1414
import Data.Maybe
15+
import qualified Data.Text as T
1516
import qualified Ide.Plugin.Class as Class
1617
import qualified Language.LSP.Types.Lens as J
1718
import System.FilePath
@@ -31,23 +32,21 @@ tests = testGroup
3132
codeActionTests :: TestTree
3233
codeActionTests = testGroup
3334
"code actions"
34-
[ testCase "Produces addMinimalMethodPlaceholders code actions for one instance" $ do
35-
runSessionWithServer classPlugin testDataDir $ do
36-
doc <- openDoc "T1.hs" "haskell"
37-
_ <- waitForDiagnosticsFromSource doc "typecheck"
38-
caResults <- getAllCodeActions doc
39-
liftIO $ map (^? _CACodeAction . J.title) caResults
40-
@?=
41-
[ Just "Add placeholders for '=='"
42-
, Just "Add placeholders for '==' with signature(s)"
43-
, Just "Add placeholders for '/='"
44-
, Just "Add placeholders for '/=' with signature(s)"
45-
]
35+
[ expectCodeActionsAvailable "Produces addMinimalMethodPlaceholders code actions for one instance" "T1"
36+
[ "Add placeholders for '=='"
37+
, "Add placeholders for '==' with signature(s)"
38+
, "Add placeholders for '/='"
39+
, "Add placeholders for '/=' with signature(s)"
40+
, "Add placeholders for all missing methods"
41+
, "Add placeholders for all missing methods with signature(s)"
42+
]
4643
, goldenWithClass "Creates a placeholder for '=='" "T1" "eq" $ \(eqAction:_) -> do
4744
executeCodeAction eqAction
4845
, goldenWithClass "Creates a placeholder for '/='" "T1" "ne" $ \(_:_:neAction:_) -> do
4946
executeCodeAction neAction
50-
, goldenWithClass "Creates a placeholder for 'fmap'" "T2" "fmap" $ \(_:_:_:_:fmapAction:_) -> do
47+
, goldenWithClass "Creates a placeholder for both '==' and '/='" "T1" "all" $ \(_:_:_:_:allMethodsAction:_) -> do
48+
executeCodeAction allMethodsAction
49+
, goldenWithClass "Creates a placeholder for 'fmap'" "T2" "fmap" $ \(_:_:_:_:_:_:fmapAction:_) -> do
5150
executeCodeAction fmapAction
5251
, goldenWithClass "Creates a placeholder for multiple methods 1" "T3" "1" $ \(mmAction:_) -> do
5352
executeCodeAction mmAction
@@ -70,6 +69,11 @@ codeActionTests = testGroup
7069
executeCodeAction eqWithSig
7170
, goldenWithClass "Only insert pragma once" "InsertPragmaOnce" "" $ \(_:multi:_) -> do
7271
executeCodeAction multi
72+
, expectCodeActionsAvailable "No code action available when minimal requirements meet" "MinimalDefinitionMeet" []
73+
, expectCodeActionsAvailable "Add placeholders for all missing methods is unavailable when all methods are required" "AllMethodsRequired"
74+
[ "Add placeholders for 'f','g'"
75+
, "Add placeholders for 'f','g' with signature(s)"
76+
]
7377
]
7478

7579
codeLensTests :: TestTree
@@ -99,7 +103,6 @@ _CACodeAction = prism' InR $ \case
99103
InR action -> Just action
100104
_ -> Nothing
101105

102-
103106
goldenCodeLens :: TestName -> FilePath -> Int -> TestTree
104107
goldenCodeLens title path idx =
105108
goldenWithHaskellDoc classPlugin title testDataDir path "expected" "hs" $ \doc -> do
@@ -115,5 +118,17 @@ goldenWithClass title path desc act =
115118
act actions
116119
void $ skipManyTill anyMessage (getDocumentEdit doc)
117120

121+
expectCodeActionsAvailable :: TestName -> FilePath -> [T.Text] -> TestTree
122+
expectCodeActionsAvailable title path actionTitles =
123+
testCase title $ do
124+
runSessionWithServer classPlugin testDataDir $ do
125+
doc <- openDoc (path <.> "hs") "haskell"
126+
_ <- waitForDiagnosticsFromSource doc "typecheck"
127+
caResults <- getAllCodeActions doc
128+
liftIO $ map (^? _CACodeAction . J.title) caResults
129+
@?= expectedActions
130+
where
131+
expectedActions = Just <$> actionTitles
132+
118133
testDataDir :: FilePath
119134
testDataDir = "test" </> "testdata"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module AllMethodsRequired where
2+
3+
class Test a where
4+
f :: a
5+
g :: a
6+
{-# MINIMAL f,g #-}
7+
8+
instance Test [a] where
9+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module MinimalDefinitionMeet where
2+
3+
data X = X
4+
5+
instance Eq X where
6+
(==) = _
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module T1 where
2+
3+
data X = X
4+
5+
instance Eq X where
6+
(==) = _
7+
(/=) = _

0 commit comments

Comments
 (0)