@@ -5,6 +5,8 @@ module Main
5
5
) where
6
6
7
7
import Control.Lens ((<&>) , (^.) )
8
+ import Data.Aeson
9
+ import Data.Foldable
8
10
import qualified Data.Text as T
9
11
import Ide.Plugin.Pragmas
10
12
import qualified Language.LSP.Types.Lens as L
@@ -25,6 +27,7 @@ tests =
25
27
, codeActionTests'
26
28
, completionTests
27
29
, completionSnippetTests
30
+ , dontSuggestCompletionTests
28
31
]
29
32
30
33
codeActionTests :: TestTree
@@ -127,29 +130,80 @@ completionSnippetTests :: TestTree
127
130
completionSnippetTests =
128
131
testGroup " expand snippet to pragma" $
129
132
validPragmas <&>
130
- (\ (insertText, label, detail, _) ->
131
- let input = T. toLower $ T. init label
133
+ (\ (insertText, label, detail, appearWhere) ->
134
+ let inputPrefix =
135
+ case appearWhere of
136
+ NewLine -> " "
137
+ CanInline -> " something "
138
+ input = inputPrefix <> (T. toLower $ T. init label)
132
139
in completionTest (T. unpack label)
133
140
" Completion.hs" input label (Just Snippet )
134
141
(Just $ " {-# " <> insertText <> " #-}" ) (Just detail)
135
142
[0 , 0 , 0 , 34 , 0 , fromIntegral $ T. length input])
136
143
137
- completionTest :: String -> String -> T. Text -> T. Text -> Maybe InsertTextFormat -> Maybe T. Text -> Maybe T. Text -> [UInt ] -> TestTree
138
- completionTest testComment fileName te' label textFormat insertText detail [a, b, c, d, x, y] =
144
+ dontSuggestCompletionTests :: TestTree
145
+ dontSuggestCompletionTests =
146
+ testGroup " do not suggest pragmas" $
147
+ let replaceFuncBody newBody = Just $ mkEdit (8 ,6 ) (8 ,8 ) newBody
148
+ writeInEmptyLine txt = Just $ mkEdit (3 ,0 ) (3 ,0 ) txt
149
+ generalTests = [ provideNoCompletionsTest " in imports" " Completion.hs" (Just $ mkEdit (3 ,0 ) (3 ,0 ) " import WA" ) (Position 3 8 )
150
+ , provideNoCompletionsTest " when no word has been typed" " Completion.hs" Nothing (Position 3 0 )
151
+ , provideNoCompletionsTest " when expecting auto complete on modules" " Completion.hs" (Just $ mkEdit (8 ,6 ) (8 ,8 ) " Data.Maybe.WA" ) (Position 8 19 )
152
+ ]
153
+ individualPragmaTests = validPragmas <&> \ (insertText,label,detail,appearWhere) ->
154
+ let completionPrompt = T. toLower $ T. init label
155
+ promptLen = fromIntegral (T. length completionPrompt)
156
+ in case appearWhere of
157
+ CanInline ->
158
+ provideNoUndesiredCompletionsTest (" at new line: " <> T. unpack label) " Completion.hs" (Just label) (writeInEmptyLine completionPrompt) (Position 3 0 )
159
+ NewLine ->
160
+ provideNoUndesiredCompletionsTest (" inline: " <> T. unpack label) " Completion.hs" (Just label) (replaceFuncBody completionPrompt) (Position 8 (6 + promptLen))
161
+ in generalTests ++ individualPragmaTests
162
+
163
+ mkEdit :: (UInt ,UInt ) -> (UInt ,UInt ) -> T. Text -> TextEdit
164
+ mkEdit (startLine, startCol) (endLine, endCol) newText =
165
+ TextEdit (Range (Position startLine startCol) (Position endLine endCol)) newText
166
+
167
+ completionTest :: String -> FilePath -> T. Text -> T. Text -> Maybe InsertTextFormat -> Maybe T. Text -> Maybe T. Text -> [UInt ] -> TestTree
168
+ completionTest testComment fileName replacementText expectedLabel expectedFormat expectedInsertText detail [delFromLine, delFromCol, delToLine, delToCol, completeAtLine, completeAtCol] =
139
169
testCase testComment $ runSessionWithServer pragmasPlugin testDataDir $ do
140
170
doc <- openDoc fileName " haskell"
141
171
_ <- waitForDiagnostics
142
- let te = TextEdit (Range (Position a b ) (Position c d )) te'
172
+ let te = TextEdit (Range (Position delFromLine delFromCol ) (Position delToLine delToCol )) replacementText
143
173
_ <- applyEdit doc te
144
- compls <- getCompletions doc (Position x y )
145
- item <- getCompletionByLabel label compls
174
+ compls <- getCompletions doc (Position completeAtLine completeAtCol )
175
+ item <- getCompletionByLabel expectedLabel compls
146
176
liftIO $ do
147
- item ^. L. label @?= label
177
+ item ^. L. label @?= expectedLabel
148
178
item ^. L. kind @?= Just CiKeyword
149
- item ^. L. insertTextFormat @?= textFormat
150
- item ^. L. insertText @?= insertText
179
+ item ^. L. insertTextFormat @?= expectedFormat
180
+ item ^. L. insertText @?= expectedInsertText
151
181
item ^. L. detail @?= detail
152
182
183
+ provideNoCompletionsTest :: String -> FilePath -> Maybe TextEdit -> Position -> TestTree
184
+ provideNoCompletionsTest testComment fileName mTextEdit pos =
185
+ provideNoUndesiredCompletionsTest testComment fileName Nothing mTextEdit pos
186
+
187
+ provideNoUndesiredCompletionsTest :: String -> FilePath -> Maybe T. Text -> Maybe TextEdit -> Position -> TestTree
188
+ provideNoUndesiredCompletionsTest testComment fileName mUndesiredLabel mTextEdit pos =
189
+ testCase testComment $ runSessionWithServer pragmasPlugin testDataDir $ do
190
+ doc <- openDoc fileName " haskell"
191
+ _ <- waitForDiagnostics
192
+ _ <- sendConfigurationChanged disableGhcideCompletions
193
+ mapM_ (applyEdit doc) mTextEdit
194
+ compls <- getCompletions doc pos
195
+ liftIO $ case mUndesiredLabel of
196
+ Nothing -> compls @?= []
197
+ Just undesiredLabel -> do
198
+ case find (\ c -> c ^. L. label == undesiredLabel) compls of
199
+ Just c -> assertFailure $
200
+ " Did not expect a completion with label=" <> T. unpack undesiredLabel
201
+ <> " , got completion: " <> show c
202
+ Nothing -> pure ()
203
+
204
+ disableGhcideCompletions :: Value
205
+ disableGhcideCompletions = object [ " haskell" .= object [" plugin" .= object [ " ghcide-completions" .= object [" globalOn" .= False ]]] ]
206
+
153
207
goldenWithPragmas :: TestName -> FilePath -> (TextDocumentIdentifier -> Session () ) -> TestTree
154
208
goldenWithPragmas title path = goldenWithHaskellDoc pragmasPlugin title testDataDir path " expected" " hs"
155
209
0 commit comments