Skip to content

Commit 7a00e43

Browse files
eddiemundojneira
authored andcommitted
Fix extension pragma inserted below ghc options pragma haskell#2364 (haskell#2392)
* new parser for stuff before first declaration * remove unused pragmas, modify haddock comment on parser * working but need to clean lots of little things and add more tests * uncomment completions functions and tests (was trying to see why the test timeout), merge textedits to get around lsp-test applying text edits in reverse order, inserting pragma between lines fixes, some tests * add line splitting tests, fix line splitting errors and among other things, add docs * change comments, add cpp for setting use_pos_prags bit in PState * add safeImportsOn to compat, fix ghc versions * fix compat * fix compat * fix compat 3 * fix compat 4 * fix compat 5 * fix test * fix compat 6 * add back some tests and investigate haskell#2375 later Co-authored-by: Javier Neira <[email protected]>
1 parent c853759 commit 7a00e43

File tree

39 files changed

+754
-65
lines changed

39 files changed

+754
-65
lines changed

ghcide/src/Development/IDE/GHC/Compat/Env.hs

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Development.IDE.GHC.Compat.Env (
3232
-- * DynFlags Helper
3333
setBytecodeLinkerOptions,
3434
setInterpreterLinkerOptions,
35+
Development.IDE.GHC.Compat.Env.safeImportsOn,
3536
-- * Ways
3637
Ways,
3738
Way,
@@ -178,6 +179,13 @@ homeUnitId_ =
178179
thisPackage
179180
#endif
180181

182+
safeImportsOn :: DynFlags -> Bool
183+
safeImportsOn =
184+
#if MIN_VERSION_ghc(9,2,0)
185+
Session.safeImportsOn
186+
#else
187+
DynFlags.safeImportsOn
188+
#endif
181189

182190
#if MIN_VERSION_ghc(9,0,0) && !MIN_VERSION_ghc(9,2,0)
183191
type HomeUnit = Unit

ghcide/src/Development/IDE/GHC/Compat/Parser.hs

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ module Development.IDE.GHC.Compat.Parser (
1313
Anno.ApiAnns(..),
1414
#else
1515
ApiAnns,
16+
#endif
17+
#if MIN_VERSION_ghc(9,0,0)
18+
PsSpan(..),
1619
#endif
1720
mkHsParsedModule,
1821
mkParsedModule,
@@ -24,6 +27,7 @@ module Development.IDE.GHC.Compat.Parser (
2427

2528
#if MIN_VERSION_ghc(9,0,0)
2629
import qualified GHC.Parser.Lexer as Lexer
30+
import GHC.Types.SrcLoc (PsSpan (..))
2731
#if MIN_VERSION_ghc(9,2,0)
2832
import qualified GHC.Driver.Config as Config
2933
import GHC.Parser.Lexer hiding (initParserState)

ghcide/src/Development/IDE/GHC/Compat/Util.hs

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ module Development.IDE.GHC.Compat.Util (
6767
StringBuffer(..),
6868
hGetStringBuffer,
6969
stringToStringBuffer,
70+
nextChar,
71+
atEnd
7072
) where
7173

7274
#if MIN_VERSION_ghc(9,0,0)

plugins/hls-pragmas-plugin/hls-pragmas-plugin.cabal

+2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ library
2424
, base >=4.12 && <5
2525
, extra
2626
, fuzzy
27+
, ghc
2728
, ghcide >=1.2 && <1.6
2829
, hls-plugin-api >=1.1 && <1.3
2930
, lens
3031
, lsp
3132
, text
3233
, transformers
3334
, unordered-containers
35+
, containers
3436

3537
default-language: Haskell2010
3638

plugins/hls-pragmas-plugin/src/Ide/Plugin/Pragmas.hs

+479-61
Large diffs are not rendered by default.

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,24 @@ tests =
2727
codeActionTests :: TestTree
2828
codeActionTests =
2929
testGroup "code actions"
30-
[ codeActionTest "adds LANGUAGE with no other pragmas at start ignoring later INLINE pragma" "AddPragmaIgnoreInline" [("Add \"TupleSections\"", "Contains TupleSections code action")]
31-
, codeActionTest "adds LANGUAGE after shebang preceded by other LANGUAGE and GHC_OPTIONS" "AddPragmaAfterShebangPrecededByLangAndOptsGhc" [("Add \"TupleSections\"", "Contains TupleSections code action")]
32-
, codeActionTest "adds LANGUAGE after shebang with other Language preceding shebang" "AddPragmaAfterShebangPrecededByLangAndOptsGhc" [("Add \"TupleSections\"", "Contains TupleSections code action")]
30+
[
31+
codeActionTest "Block comment then line comment doesn't split line" "BlockCommentThenLineComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
32+
, codeActionTest "Block comment then single-line block comment doesn't split line" "BlockCommentThenSingleLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
33+
, codeActionTest "Block comment then multi-line block comment doesn't split line" "BlockCommentThenMultiLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
34+
, codeActionTest "Block comment then line haddock splits line" "BlockCommentThenLineHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
35+
, codeActionTest "Block comment then single-line block haddock splits line" "BlockCommentThenSingleLineBlockHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
36+
, codeActionTest "Block comment then multi-line block haddock splits line" "BlockCommentThenMultiLineBlockHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
37+
, codeActionTest "Pragma then line comment doesn't split line" "PragmaThenLineComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
38+
, codeActionTest "Pragma then single-line block comment doesn't split line" "PragmaThenSingleLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
39+
, codeActionTest "Pragma then multi-line block comment splits line" "PragmaThenMultiLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
40+
, codeActionTest "Pragma then line haddock splits line" "PragmaThenLineHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
41+
, codeActionTest "Pragma then single-line block haddock splits line" "PragmaThenSingleLineBlockHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
42+
, codeActionTest "Pragma then multi-line block haddock splits line" "PragmaThenMultiLineBlockHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
43+
, codeActionTest "Pragma then single-line block haddock single-line block comment splits line" "PragmaThenSingleLineBlockHaddockSingleLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
44+
, codeActionTest "Block comment then single-line block haddock single-line block comment splits line" "BlockCommentThenSingleLineBlockHaddockSingleLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
45+
, codeActionTest "Pragma then line haddock then newline line comment splits line" "PragmaThenLineHaddockNewlineLineComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
46+
, codeActionTest "does not add pragma after OPTIONS_GHC pragma located after a declaration" "OptionsGhcAfterDecl" [("Add \"TupleSections\"", "Contains TupleSections code action")]
47+
, codeActionTest "adds LANGUAGE with no other pragmas at start ignoring later INLINE pragma" "AddPragmaIgnoreInline" [("Add \"TupleSections\"", "Contains TupleSections code action")]
3348
, codeActionTest "adds LANGUAGE before Doc comments after interchanging pragmas" "BeforeDocInterchanging" [("Add \"NamedFieldPuns\"", "Contains NamedFieldPuns code action")]
3449
, codeActionTest "Add language after altering OPTIONS_GHC and Language" "AddLanguagePragmaAfterInterchaningOptsGhcAndLangs" [("Add \"TupleSections\"", "Contains TupleSections code action")]
3550
, codeActionTest "Add language after pragmas with non standard space between prefix and name" "AddPragmaWithNonStandardSpacingInPrecedingPragmas" [("Add \"TupleSections\"", "Contains TupleSections code action")]
@@ -67,7 +82,8 @@ codeActionTest testComment fp actions =
6782
codeActionTests' :: TestTree
6883
codeActionTests' =
6984
testGroup "additional code actions"
70-
[ goldenWithPragmas "no duplication" "NamedFieldPuns" $ \doc -> do
85+
[
86+
goldenWithPragmas "no duplication" "NamedFieldPuns" $ \doc -> do
7187
_ <- waitForDiagnosticsFrom doc
7288
cas <- map fromAction <$> getCodeActions doc (Range (Position 8 9) (Position 8 9))
7389
liftIO $ length cas == 1 @? "Expected one code action, but got: " <> show cas
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{- block comment -} -- line comment
2+
{-# LANGUAGE TupleSections #-}
3+
4+
module BlockCommentThenLineComment where
5+
6+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{- block comment -} -- line comment
2+
3+
module BlockCommentThenLineComment where
4+
5+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{- block comment -}
2+
{-# LANGUAGE TupleSections #-}
3+
-- | line haddock
4+
5+
module BlockCommentThenLineHaddock where
6+
7+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{- block comment -} -- | line haddock
2+
3+
module BlockCommentThenLineHaddock where
4+
5+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{- block comment -} {- multi
2+
line
3+
block
4+
comment
5+
-}
6+
{-# LANGUAGE TupleSections #-}
7+
8+
module BlockCommentThenMultiLineBlockComment where
9+
10+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{- block comment -} {- multi
2+
line
3+
block
4+
comment
5+
-}
6+
7+
module BlockCommentThenMultiLineBlockComment where
8+
9+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{- block comment -}
2+
{-# LANGUAGE TupleSections #-}
3+
{-| multi
4+
line
5+
block
6+
haddock
7+
-}
8+
9+
module BlockCommentThenMultiLineBlockHaddock where
10+
import GHC.SourceGen (multiIf)
11+
import Diagrams (block)
12+
13+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{- block comment -} {-| multi
2+
line
3+
block
4+
haddock
5+
-}
6+
7+
module BlockCommentThenMultiLineBlockHaddock where
8+
import GHC.SourceGen (multiIf)
9+
import Diagrams (block)
10+
11+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{- block comment -} {- single line block comment -}
2+
{-# LANGUAGE TupleSections #-}
3+
4+
module BlockCommentThenSingleLineBlockComment where
5+
6+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{- block comment -} {- single line block comment -}
2+
3+
module BlockCommentThenSingleLineBlockComment where
4+
5+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{- block comment -}
2+
{-# LANGUAGE TupleSections #-}
3+
{-| single line block haddock -}
4+
5+
module BlockCommentThenSingleLineBlockHaddock where
6+
7+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{- block comment -} {-| single line block haddock -}
2+
3+
module BlockCommentThenSingleLineBlockHaddock where
4+
5+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{- block comment -}
2+
{-# LANGUAGE TupleSections #-}
3+
{-| single line block haddock -} {- single line block comment -}
4+
5+
module BlockCommentThenSingleLineBlockHaddockSingleLineBlockComment where
6+
7+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{- block comment -} {-| single line block haddock -} {- single line block comment -}
2+
3+
module BlockCommentThenSingleLineBlockHaddockSingleLineBlockComment where
4+
5+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE TupleSections #-}
2+
data Something = Something {
3+
foo :: !String,
4+
bar :: !Int
5+
}
6+
7+
tupleSection = (1, ) <$> Just 2
8+
9+
{-# OPTIONS_GHC addOne #-}
10+
addOne :: Int -> Int
11+
addOne x = x + 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
data Something = Something {
2+
foo :: !String,
3+
bar :: !Int
4+
}
5+
6+
tupleSection = (1, ) <$> Just 2
7+
8+
{-# OPTIONS_GHC addOne #-}
9+
addOne :: Int -> Int
10+
addOne x = x + 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE TypeApplications #-} {-| haddock -}
2+
3+
module PragmaFollowedByBlockHaddock where
4+
5+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{-# LANGUAGE TypeApplications #-} -- line comment
2+
{-# LANGUAGE TupleSections #-}
3+
4+
module PragmaThenLineComment where
5+
6+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE TypeApplications #-} -- line comment
2+
3+
module PragmaThenLineComment where
4+
5+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{-# LANGUAGE TypeApplications #-}
2+
{-# LANGUAGE TupleSections #-}
3+
-- | line haddock
4+
5+
module PragmaThenLineHaddock where
6+
7+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE TypeApplications #-} -- | line haddock
2+
3+
module PragmaThenLineHaddock where
4+
5+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{-# LANGUAGE TypeApplications #-}
2+
{-# LANGUAGE TupleSections #-}
3+
-- | line haddock
4+
-- line comment
5+
6+
module PragmaThenLineHaddockNewlineLineComment where
7+
8+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{-# LANGUAGE TypeApplications #-} -- | line haddock
2+
-- line comment
3+
4+
module PragmaThenLineHaddockNewlineLineComment where
5+
6+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE TypeApplications #-}
2+
{-# LANGUAGE TupleSections #-}
3+
{- multi
4+
line
5+
block
6+
comment
7+
-}
8+
9+
module PragmaThenSingleLineBlockComment where
10+
11+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{-# LANGUAGE TypeApplications #-} {- multi
2+
line
3+
block
4+
comment
5+
-}
6+
7+
module PragmaThenSingleLineBlockComment where
8+
9+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE TypeApplications #-}
2+
{-# LANGUAGE TupleSections #-}
3+
{-| multi
4+
line
5+
block
6+
haddock
7+
-}
8+
9+
module PragmaThenMultiLineBlockHaddock where
10+
11+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{-# LANGUAGE TypeApplications #-} {-| multi
2+
line
3+
block
4+
haddock
5+
-}
6+
7+
module PragmaThenMultiLineBlockHaddock where
8+
9+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{-# LANGUAGE TypeApplications #-} {- single line block comment -}
2+
{-# LANGUAGE TupleSections #-}
3+
4+
module PragmaThenSingleLineBlockComment where
5+
6+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE TypeApplications #-} {- single line block comment -}
2+
3+
module PragmaThenSingleLineBlockComment where
4+
5+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{-# LANGUAGE TypeApplications #-}
2+
{-# LANGUAGE TupleSections #-}
3+
{-| single line block haddock -}
4+
5+
module PragmaThenSingleLineBlockHaddock where
6+
7+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE TypeApplications #-} {-| single line block haddock -}
2+
3+
module PragmaThenSingleLineBlockHaddock where
4+
5+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{-# LANGUAGE TypeApplications #-}
2+
{-# LANGUAGE TupleSections #-}
3+
{-| single line block haddock -} {- single line block comment -}
4+
5+
module PragmaThenSingleLineBlockHaddockSingleLineBlockComment where
6+
7+
a = (1,)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE TypeApplications #-} {-| single line block haddock -} {- single line block comment -}
2+
3+
module PragmaThenSingleLineBlockHaddockSingleLineBlockComment where
4+
5+
a = (1,)

0 commit comments

Comments
 (0)