Skip to content

Commit d168667

Browse files
author
kokobd
committed
handle comments
1 parent 9237972 commit d168667

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

ghcide/src/Development/IDE/Plugin/CodeAction.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,13 @@ import Text.Regex.TDFA (mrAfter,
8080
(=~), (=~~))
8181
#if MIN_VERSION_ghc(9,2,0)
8282
import GHC (AddEpAnn (AddEpAnn),
83+
Anchor (anchor_op),
84+
AnchorOperation (..),
8385
AnnsModule (am_main),
8486
DeltaPos (..),
8587
EpAnn (..),
8688
EpaLocation (..),
89+
LEpaComment,
8790
LocatedA)
8891

8992
import Control.Monad (msum)
@@ -1453,8 +1456,16 @@ findPositionAfterModuleName ps hsmodName' = do
14531456

14541457
epaLocationToLine :: EpaLocation -> Maybe Int
14551458
epaLocationToLine (EpaSpan sp) = Just . srcLocLine . realSrcSpanEnd $ sp
1456-
epaLocationToLine (EpaDelta (SameLine _) _) = Just 0
1457-
epaLocationToLine (EpaDelta (DifferentLine line _) _) = Just line
1459+
epaLocationToLine (EpaDelta (SameLine _) priorComments) = Just $ sumCommentsOffset priorComments
1460+
epaLocationToLine (EpaDelta (DifferentLine line _) priorComments) = Just (line + sumCommentsOffset priorComments)
1461+
1462+
sumCommentsOffset :: [LEpaComment] -> Int
1463+
sumCommentsOffset = sum . fmap (\(L anchor _) -> anchorOpLine (anchor_op anchor))
1464+
1465+
anchorOpLine :: AnchorOperation -> Int
1466+
anchorOpLine UnchangedAnchor = 0
1467+
anchorOpLine (MovedAnchor (SameLine _)) = 0
1468+
anchorOpLine (MovedAnchor (DifferentLine line _)) = line
14581469
#else
14591470
whereKeywordLineOffset = do
14601471
ann <- annsA ps M.!? mkAnnKey (astA ps)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Asdf
2+
(f
3+
, where')
4+
-- hello
5+
-- world
6+
7+
where
8+
import Data.Int
9+
10+
11+
12+
f :: Int64 -> Int64
13+
f = id'
14+
where id' = id
15+
16+
g :: Int -> Int
17+
g = id
18+
19+
where' :: Int -> Int
20+
where' = id
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Asdf
2+
(f
3+
, where')
4+
-- hello
5+
-- world
6+
7+
where
8+
9+
10+
11+
f :: Int64 -> Int64
12+
f = id'
13+
where id' = id
14+
15+
g :: Int -> Int
16+
g = id
17+
18+
where' :: Int -> Int
19+
where' = id

ghcide/test/exe/Main.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,11 @@ insertImportTests = testGroup "insert import"
912912
"WhereDeclLowerInFile.hs"
913913
"WhereDeclLowerInFile.expected.hs"
914914
"import Data.Int"
915+
, checkImport
916+
"module where keyword lower in file with comments before it"
917+
"WhereDeclLowerInFileWithCommentsBeforeIt.hs"
918+
"WhereDeclLowerInFileWithCommentsBeforeIt.expected.hs"
919+
"import Data.Int"
915920
, expectFailBecause
916921
"'findNextPragmaPosition' function doesn't account for case when shebang is not placed at top of file"
917922
(checkImport

0 commit comments

Comments
 (0)