Skip to content

Commit 911d10f

Browse files
committed
fix #2612. apply fixities to parsed source before sent to apply-refact
1 parent 5714207 commit 911d10f

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

Diff for: plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import Development.IDE.Core.Rules (defineNoFil
5252
usePropertyAction)
5353
import Development.IDE.Core.Shake (getDiagnostics)
5454
import Refact.Apply
55+
import qualified Refact.Fixity as Refact
5556

5657
#ifdef HLINT_ON_GHC_LIB
5758
import Data.List (nub)
@@ -555,7 +556,8 @@ applyHint ide nfp mhint =
555556
-- apply-refact uses RigidLayout
556557
let rigidLayout = deltaOptions RigidLayout
557558
(anns', modu') <-
558-
ExceptT $ return $ postParseTransform (Right (anns, [], dflags, modu)) rigidLayout
559+
ExceptT $ mapM (uncurry Refact.applyFixities)
560+
$ postParseTransform (Right (anns, [], dflags, modu)) rigidLayout
559561
liftIO $ (Right <$> withRuntimeLibdir (applyRefactorings' position commands anns' modu'))
560562
`catches` errorHandlers
561563
#endif

Diff for: plugins/hls-hlint-plugin/test/Main.hs

+31-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Main
88

99
import Control.Lens ((^.))
1010
import Data.Aeson (Value (..), object, toJSON, (.=))
11+
import Data.Functor (void)
1112
import Data.List (find)
1213
import qualified Data.Map as Map
1314
import Data.Maybe (fromJust, isJust)
@@ -30,26 +31,40 @@ tests = testGroup "hlint" [
3031
suggestionsTests
3132
, configTests
3233
, ignoreHintTests
34+
, applyHintTests
3335
]
3436

3537
getIgnoreHintText :: T.Text -> T.Text
3638
getIgnoreHintText name = "Ignore hint \"" <> name <> "\" in this module"
3739

40+
getApplyHintText :: T.Text -> T.Text
41+
getApplyHintText name = "Apply hint \"Avoid reverse\"" -- "Apply hint \"" <> name <> "\""
42+
3843
ignoreHintTests :: TestTree
3944
ignoreHintTests = testGroup "hlint ignore hint tests"
4045
[
41-
ignoreGoldenTest
46+
ignoreHintGoldenTest
4247
"Ignore hint in this module inserts -Wno-unrecognised-pragmas and hlint ignore pragma if warn unrecognized pragmas is off"
4348
"UnrecognizedPragmasOff"
4449
(Point 3 8)
4550
"Eta reduce"
46-
, ignoreGoldenTest
51+
, ignoreHintGoldenTest
4752
"Ignore hint in this module inserts only hlint ignore pragma if warn unrecognized pragmas is on"
4853
"UnrecognizedPragmasOn"
4954
(Point 3 9)
5055
"Eta reduce"
5156
]
5257

58+
applyHintTests :: TestTree
59+
applyHintTests = testGroup "hlint apply hint tests"
60+
[
61+
applyHintGoldenTest
62+
"[#2612] Apply hint works when operator fixities go right-to-left"
63+
"RightToLeftFixities"
64+
(Point 6 13)
65+
"Avoid reverse"
66+
]
67+
5368
suggestionsTests :: TestTree
5469
suggestionsTests =
5570
testGroup "hlint suggestions" [
@@ -378,13 +393,23 @@ makeCodeActionFoundAtString :: Point -> String
378393
makeCodeActionFoundAtString Point {..} =
379394
"CodeAction found at line: " <> show line <> ", column: " <> show column
380395

381-
ignoreGoldenTest :: TestName -> FilePath -> Point -> T.Text -> TestTree
382-
ignoreGoldenTest testCaseName goldenFilename point hintName =
396+
ignoreHintGoldenTest :: TestName -> FilePath -> Point -> T.Text -> TestTree
397+
ignoreHintGoldenTest testCaseName goldenFilename point hintName =
398+
goldenTest testCaseName goldenFilename point (getIgnoreHintText hintName)
399+
400+
applyHintGoldenTest :: TestName -> FilePath -> Point -> T.Text -> TestTree
401+
applyHintGoldenTest testCaseName goldenFilename point hintName =
402+
goldenTest testCaseName goldenFilename point (getApplyHintText hintName)
403+
404+
goldenTest :: TestName -> FilePath -> Point -> T.Text -> TestTree
405+
goldenTest testCaseName goldenFilename point hintText =
383406
setupGoldenHlintTest testCaseName goldenFilename $ \document -> do
384407
waitForDiagnosticsFromSource document "hlint"
385408
actions <- getCodeActions document $ pointToRange point
386-
case find ((== Just (getIgnoreHintText hintName)) . getCodeActionTitle) actions of
387-
Just (InR codeAction) -> executeCodeAction codeAction
409+
case find ((== Just hintText) . getCodeActionTitle) actions of
410+
Just (InR codeAction) -> do
411+
executeCodeAction codeAction
412+
void $ skipManyTill anyMessage $ getDocumentEdit document
388413
_ -> liftIO $ assertFailure $ makeCodeActionNotFoundAtString point
389414

390415
setupGoldenHlintTest :: TestName -> FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module RightToLeftFixities where
2+
import Data.List (sortOn)
3+
import Control.Arrow ((&&&))
4+
import Data.Ord (Down(Down))
5+
functionB :: [String] -> [(Char,Int)]
6+
functionB = sortOn (Down . snd) . map (head &&& length) . id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module RightToLeftFixities where
2+
import Data.List (sortOn)
3+
import Control.Arrow ((&&&))
4+
import Data.Ord (Down(Down))
5+
functionB :: [String] -> [(Char,Int)]
6+
functionB = reverse . sortOn snd . map (head &&& length) . id

0 commit comments

Comments
 (0)