Skip to content

Commit

Permalink
Fix extension pragma inserted below ghc options pragma #2364 (#2392)
Browse files Browse the repository at this point in the history
* 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 #2375 later

Co-authored-by: Javier Neira <[email protected]>
  • Loading branch information
eddiemundo and jneira authored Nov 30, 2021
1 parent ca07742 commit 083f542
Show file tree
Hide file tree
Showing 39 changed files with 754 additions and 65 deletions.
8 changes: 8 additions & 0 deletions ghcide/src/Development/IDE/GHC/Compat/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Development.IDE.GHC.Compat.Env (
-- * DynFlags Helper
setBytecodeLinkerOptions,
setInterpreterLinkerOptions,
Development.IDE.GHC.Compat.Env.safeImportsOn,
-- * Ways
Ways,
Way,
Expand Down Expand Up @@ -178,6 +179,13 @@ homeUnitId_ =
thisPackage
#endif

safeImportsOn :: DynFlags -> Bool
safeImportsOn =
#if MIN_VERSION_ghc(9,2,0)
Session.safeImportsOn
#else
DynFlags.safeImportsOn
#endif

#if MIN_VERSION_ghc(9,0,0) && !MIN_VERSION_ghc(9,2,0)
type HomeUnit = Unit
Expand Down
4 changes: 4 additions & 0 deletions ghcide/src/Development/IDE/GHC/Compat/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module Development.IDE.GHC.Compat.Parser (
Anno.ApiAnns(..),
#else
ApiAnns,
#endif
#if MIN_VERSION_ghc(9,0,0)
PsSpan(..),
#endif
mkHsParsedModule,
mkParsedModule,
Expand All @@ -24,6 +27,7 @@ module Development.IDE.GHC.Compat.Parser (

#if MIN_VERSION_ghc(9,0,0)
import qualified GHC.Parser.Lexer as Lexer
import GHC.Types.SrcLoc (PsSpan (..))
#if MIN_VERSION_ghc(9,2,0)
import qualified GHC.Driver.Config as Config
import GHC.Parser.Lexer hiding (initParserState)
Expand Down
2 changes: 2 additions & 0 deletions ghcide/src/Development/IDE/GHC/Compat/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ module Development.IDE.GHC.Compat.Util (
StringBuffer(..),
hGetStringBuffer,
stringToStringBuffer,
nextChar,
atEnd
) where

#if MIN_VERSION_ghc(9,0,0)
Expand Down
2 changes: 2 additions & 0 deletions plugins/hls-pragmas-plugin/hls-pragmas-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ library
, base >=4.12 && <5
, extra
, fuzzy
, ghc
, ghcide >=1.2 && <1.6
, hls-plugin-api >=1.1 && <1.3
, lens
, lsp
, text
, transformers
, unordered-containers
, containers

default-language: Haskell2010

Expand Down
540 changes: 479 additions & 61 deletions plugins/hls-pragmas-plugin/src/Ide/Plugin/Pragmas.hs

Large diffs are not rendered by default.

24 changes: 20 additions & 4 deletions plugins/hls-pragmas-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,24 @@ tests =
codeActionTests :: TestTree
codeActionTests =
testGroup "code actions"
[ codeActionTest "adds LANGUAGE with no other pragmas at start ignoring later INLINE pragma" "AddPragmaIgnoreInline" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "adds LANGUAGE after shebang preceded by other LANGUAGE and GHC_OPTIONS" "AddPragmaAfterShebangPrecededByLangAndOptsGhc" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "adds LANGUAGE after shebang with other Language preceding shebang" "AddPragmaAfterShebangPrecededByLangAndOptsGhc" [("Add \"TupleSections\"", "Contains TupleSections code action")]
[
codeActionTest "Block comment then line comment doesn't split line" "BlockCommentThenLineComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Block comment then single-line block comment doesn't split line" "BlockCommentThenSingleLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Block comment then multi-line block comment doesn't split line" "BlockCommentThenMultiLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Block comment then line haddock splits line" "BlockCommentThenLineHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Block comment then single-line block haddock splits line" "BlockCommentThenSingleLineBlockHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Block comment then multi-line block haddock splits line" "BlockCommentThenMultiLineBlockHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Pragma then line comment doesn't split line" "PragmaThenLineComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Pragma then single-line block comment doesn't split line" "PragmaThenSingleLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Pragma then multi-line block comment splits line" "PragmaThenMultiLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Pragma then line haddock splits line" "PragmaThenLineHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Pragma then single-line block haddock splits line" "PragmaThenSingleLineBlockHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Pragma then multi-line block haddock splits line" "PragmaThenMultiLineBlockHaddock" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Pragma then single-line block haddock single-line block comment splits line" "PragmaThenSingleLineBlockHaddockSingleLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Block comment then single-line block haddock single-line block comment splits line" "BlockCommentThenSingleLineBlockHaddockSingleLineBlockComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Pragma then line haddock then newline line comment splits line" "PragmaThenLineHaddockNewlineLineComment" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "does not add pragma after OPTIONS_GHC pragma located after a declaration" "OptionsGhcAfterDecl" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "adds LANGUAGE with no other pragmas at start ignoring later INLINE pragma" "AddPragmaIgnoreInline" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "adds LANGUAGE before Doc comments after interchanging pragmas" "BeforeDocInterchanging" [("Add \"NamedFieldPuns\"", "Contains NamedFieldPuns code action")]
, codeActionTest "Add language after altering OPTIONS_GHC and Language" "AddLanguagePragmaAfterInterchaningOptsGhcAndLangs" [("Add \"TupleSections\"", "Contains TupleSections code action")]
, codeActionTest "Add language after pragmas with non standard space between prefix and name" "AddPragmaWithNonStandardSpacingInPrecedingPragmas" [("Add \"TupleSections\"", "Contains TupleSections code action")]
Expand Down Expand Up @@ -67,7 +82,8 @@ codeActionTest testComment fp actions =
codeActionTests' :: TestTree
codeActionTests' =
testGroup "additional code actions"
[ goldenWithPragmas "no duplication" "NamedFieldPuns" $ \doc -> do
[
goldenWithPragmas "no duplication" "NamedFieldPuns" $ \doc -> do
_ <- waitForDiagnosticsFrom doc
cas <- map fromAction <$> getCodeActions doc (Range (Position 8 9) (Position 8 9))
liftIO $ length cas == 1 @? "Expected one code action, but got: " <> show cas
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{- block comment -} -- line comment
{-# LANGUAGE TupleSections #-}

module BlockCommentThenLineComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{- block comment -} -- line comment

module BlockCommentThenLineComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{- block comment -}
{-# LANGUAGE TupleSections #-}
-- | line haddock

module BlockCommentThenLineHaddock where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{- block comment -} -- | line haddock

module BlockCommentThenLineHaddock where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{- block comment -} {- multi
line
block
comment
-}
{-# LANGUAGE TupleSections #-}

module BlockCommentThenMultiLineBlockComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{- block comment -} {- multi
line
block
comment
-}

module BlockCommentThenMultiLineBlockComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{- block comment -}
{-# LANGUAGE TupleSections #-}
{-| multi
line
block
haddock
-}

module BlockCommentThenMultiLineBlockHaddock where
import GHC.SourceGen (multiIf)
import Diagrams (block)

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{- block comment -} {-| multi
line
block
haddock
-}

module BlockCommentThenMultiLineBlockHaddock where
import GHC.SourceGen (multiIf)
import Diagrams (block)

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{- block comment -} {- single line block comment -}
{-# LANGUAGE TupleSections #-}

module BlockCommentThenSingleLineBlockComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{- block comment -} {- single line block comment -}

module BlockCommentThenSingleLineBlockComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{- block comment -}
{-# LANGUAGE TupleSections #-}
{-| single line block haddock -}

module BlockCommentThenSingleLineBlockHaddock where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{- block comment -} {-| single line block haddock -}

module BlockCommentThenSingleLineBlockHaddock where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{- block comment -}
{-# LANGUAGE TupleSections #-}
{-| single line block haddock -} {- single line block comment -}

module BlockCommentThenSingleLineBlockHaddockSingleLineBlockComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{- block comment -} {-| single line block haddock -} {- single line block comment -}

module BlockCommentThenSingleLineBlockHaddockSingleLineBlockComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{-# LANGUAGE TupleSections #-}
data Something = Something {
foo :: !String,
bar :: !Int
}

tupleSection = (1, ) <$> Just 2

{-# OPTIONS_GHC addOne #-}
addOne :: Int -> Int
addOne x = x + 1
10 changes: 10 additions & 0 deletions plugins/hls-pragmas-plugin/test/testdata/OptionsGhcAfterDecl.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
data Something = Something {
foo :: !String,
bar :: !Int
}

tupleSection = (1, ) <$> Just 2

{-# OPTIONS_GHC addOne #-}
addOne :: Int -> Int
addOne x = x + 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{-# LANGUAGE TypeApplications #-} {-| haddock -}

module PragmaFollowedByBlockHaddock where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{-# LANGUAGE TypeApplications #-} -- line comment
{-# LANGUAGE TupleSections #-}

module PragmaThenLineComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{-# LANGUAGE TypeApplications #-} -- line comment

module PragmaThenLineComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TupleSections #-}
-- | line haddock

module PragmaThenLineHaddock where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{-# LANGUAGE TypeApplications #-} -- | line haddock

module PragmaThenLineHaddock where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TupleSections #-}
-- | line haddock
-- line comment

module PragmaThenLineHaddockNewlineLineComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{-# LANGUAGE TypeApplications #-} -- | line haddock
-- line comment

module PragmaThenLineHaddockNewlineLineComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TupleSections #-}
{- multi
line
block
comment
-}

module PragmaThenSingleLineBlockComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{-# LANGUAGE TypeApplications #-} {- multi
line
block
comment
-}

module PragmaThenSingleLineBlockComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TupleSections #-}
{-| multi
line
block
haddock
-}

module PragmaThenMultiLineBlockHaddock where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{-# LANGUAGE TypeApplications #-} {-| multi
line
block
haddock
-}

module PragmaThenMultiLineBlockHaddock where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{-# LANGUAGE TypeApplications #-} {- single line block comment -}
{-# LANGUAGE TupleSections #-}

module PragmaThenSingleLineBlockComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{-# LANGUAGE TypeApplications #-} {- single line block comment -}

module PragmaThenSingleLineBlockComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TupleSections #-}
{-| single line block haddock -}

module PragmaThenSingleLineBlockHaddock where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{-# LANGUAGE TypeApplications #-} {-| single line block haddock -}

module PragmaThenSingleLineBlockHaddock where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TupleSections #-}
{-| single line block haddock -} {- single line block comment -}

module PragmaThenSingleLineBlockHaddockSingleLineBlockComment where

a = (1,)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{-# LANGUAGE TypeApplications #-} {-| single line block haddock -} {- single line block comment -}

module PragmaThenSingleLineBlockHaddockSingleLineBlockComment where

a = (1,)

0 comments on commit 083f542

Please sign in to comment.