@@ -9,9 +9,12 @@ module Main (
9
9
import Completer (completerTests )
10
10
import Context (contextTests )
11
11
import Control.Lens ((^.) )
12
+ import Control.Lens.Fold ((^?) )
12
13
import Control.Monad (guard )
13
14
import qualified Data.ByteString as BS
14
15
import Data.Either (isRight )
16
+ import Data.List.Extra (nubOrdOn )
17
+ import qualified Data.Maybe as Maybe
15
18
import qualified Data.Text as T
16
19
import qualified Data.Text as Text
17
20
import Ide.Plugin.Cabal.LicenseSuggest (licenseErrorSuggestion )
@@ -30,6 +33,7 @@ main = do
30
33
, pluginTests
31
34
, completerTests
32
35
, contextTests
36
+ , codeActionTests
33
37
]
34
38
35
39
-- ------------------------------------------------------------------------
@@ -137,57 +141,83 @@ pluginTests =
137
141
unknownLicenseDiag ^. L. range @?= Range (Position 3 24 ) (Position 4 0 )
138
142
unknownLicenseDiag ^. L. severity @?= Just DiagnosticSeverity_Error
139
143
]
140
- , testGroup
141
- " Code Actions"
142
- [ runCabalTestCaseSession " BSD-3" " " $ do
143
- doc <- openDoc " licenseCodeAction.cabal" " cabal"
144
- diags <- waitForDiagnosticsFromSource doc " cabal"
145
- reduceDiag <- liftIO $ inspectDiagnostic diags [" Unknown SPDX license identifier: 'BSD3'" ]
146
- liftIO $ do
147
- length diags @?= 1
148
- reduceDiag ^. L. range @?= Range (Position 3 24 ) (Position 4 0 )
149
- reduceDiag ^. L. severity @?= Just DiagnosticSeverity_Error
150
- [codeAction] <- getLicenseAction " BSD-3-Clause" <$> getCodeActions doc (Range (Position 3 24 ) (Position 4 0 ))
151
- executeCodeAction codeAction
152
- contents <- documentContents doc
153
- liftIO $
154
- contents
155
- @?= Text. unlines
156
- [ " cabal-version: 3.0"
157
- , " name: licenseCodeAction"
158
- , " version: 0.1.0.0"
159
- , " license: BSD-3-Clause"
160
- , " "
161
- , " library"
162
- , " build-depends: base"
163
- , " default-language: Haskell2010"
164
- ]
165
- , runCabalTestCaseSession " Apache-2.0" " " $ do
166
- doc <- openDoc " licenseCodeAction2.cabal" " cabal"
167
- diags <- waitForDiagnosticsFromSource doc " cabal"
168
- -- test if it supports typos in license name, here 'apahe'
169
- reduceDiag <- liftIO $ inspectDiagnostic diags [" Unknown SPDX license identifier: 'APAHE'" ]
170
- liftIO $ do
171
- length diags @?= 1
172
- reduceDiag ^. L. range @?= Range (Position 3 25 ) (Position 4 0 )
173
- reduceDiag ^. L. severity @?= Just DiagnosticSeverity_Error
174
- [codeAction] <- getLicenseAction " Apache-2.0" <$> getCodeActions doc (Range (Position 3 24 ) (Position 4 0 ))
175
- executeCodeAction codeAction
176
- contents <- documentContents doc
177
- liftIO $
178
- contents
179
- @?= Text. unlines
180
- [ " cabal-version: 3.0"
181
- , " name: licenseCodeAction2"
182
- , " version: 0.1.0.0"
183
- , " license: Apache-2.0"
184
- , " "
185
- , " library"
186
- , " build-depends: base"
187
- , " default-language: Haskell2010"
188
- ]
189
- ]
190
144
]
145
+ -- ----------------------------------------------------------------------------
146
+ -- Code Action Tests
147
+ -- ----------------------------------------------------------------------------
148
+
149
+ codeActionTests :: TestTree
150
+ codeActionTests = testGroup " Code Actions"
151
+ [ runCabalTestCaseSession " BSD-3" " " $ do
152
+ doc <- openDoc " licenseCodeAction.cabal" " cabal"
153
+ diags <- waitForDiagnosticsFromSource doc " cabal"
154
+ reduceDiag <- liftIO $ inspectDiagnostic diags [" Unknown SPDX license identifier: 'BSD3'" ]
155
+ liftIO $ do
156
+ length diags @?= 1
157
+ reduceDiag ^. L. range @?= Range (Position 3 24 ) (Position 4 0 )
158
+ reduceDiag ^. L. severity @?= Just DiagnosticSeverity_Error
159
+ [codeAction] <- getLicenseAction " BSD-3-Clause" <$> getCodeActions doc (Range (Position 3 24 ) (Position 4 0 ))
160
+ executeCodeAction codeAction
161
+ contents <- documentContents doc
162
+ liftIO $
163
+ contents
164
+ @?= Text. unlines
165
+ [ " cabal-version: 3.0"
166
+ , " name: licenseCodeAction"
167
+ , " version: 0.1.0.0"
168
+ , " license: BSD-3-Clause"
169
+ , " "
170
+ , " library"
171
+ , " build-depends: base"
172
+ , " default-language: Haskell2010"
173
+ ]
174
+ , runCabalTestCaseSession " Apache-2.0" " " $ do
175
+ doc <- openDoc " licenseCodeAction2.cabal" " cabal"
176
+ diags <- waitForDiagnosticsFromSource doc " cabal"
177
+ -- test if it supports typos in license name, here 'apahe'
178
+ reduceDiag <- liftIO $ inspectDiagnostic diags [" Unknown SPDX license identifier: 'APAHE'" ]
179
+ liftIO $ do
180
+ length diags @?= 1
181
+ reduceDiag ^. L. range @?= Range (Position 3 25 ) (Position 4 0 )
182
+ reduceDiag ^. L. severity @?= Just DiagnosticSeverity_Error
183
+ [codeAction] <- getLicenseAction " Apache-2.0" <$> getCodeActions doc (Range (Position 3 24 ) (Position 4 0 ))
184
+ executeCodeAction codeAction
185
+ contents <- documentContents doc
186
+ liftIO $
187
+ contents
188
+ @?= Text. unlines
189
+ [ " cabal-version: 3.0"
190
+ , " name: licenseCodeAction2"
191
+ , " version: 0.1.0.0"
192
+ , " license: Apache-2.0"
193
+ , " "
194
+ , " library"
195
+ , " build-depends: base"
196
+ , " default-language: Haskell2010"
197
+ ]
198
+ , runCabalGoldenSession " Code Actions - Can fix field names" " code-actions" " FieldSuggestions" $ \ doc -> do
199
+ _ <- waitForDiagnosticsFrom doc
200
+ cas <- Maybe. mapMaybe (^? _R) <$> getAllCodeActions doc
201
+ -- Filter out the code actions we want to invoke.
202
+ -- We only want to invoke Code Actions with certain titles, and
203
+ -- we want to invoke them only once, not once for each cursor request.
204
+ -- 'getAllCodeActions' iterates over each cursor position and requests code actions.
205
+ let selectedCas = nubOrdOn (^. L. title) $ filter
206
+ (\ ca -> (ca ^. L. title) `elem`
207
+ [ " Replace with license"
208
+ , " Replace with build-type"
209
+ , " Replace with extra-doc-files"
210
+ , " Replace with ghc-options"
211
+ , " Replace with location"
212
+ , " Replace with default-language"
213
+ , " Replace with import"
214
+ , " Replace with build-depends"
215
+ , " Replace with main-is"
216
+ , " Replace with hs-source-dirs"
217
+ ]) cas
218
+ mapM_ executeCodeAction selectedCas
219
+ pure ()
220
+ ]
191
221
where
192
222
getLicenseAction :: T. Text -> [Command |? CodeAction ] -> [CodeAction ]
193
223
getLicenseAction license codeActions = do
0 commit comments