Skip to content

Fix test failure for AlternateNumberFormat #2752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ jobs:
name: Test hls-module-name-plugin test suite
run: cabal test hls-module-name-plugin --test-options="$TEST_OPTS" || cabal test hls-module-name-plugin --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test hls-module-name-plugin --test-options="$TEST_OPTS"

- if: matrix.test && matrix.ghc != '9.2.1'
- if: matrix.test
name: Test hls-alternate-number-format-plugin test suite
run: cabal test hls-alternate-number-format-plugin --test-options="$TEST_OPTS" || cabal test hls-alternate-number-format-plugin --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test hls-alternate-number-format-plugin --test-options="$TEST_OPTS"

Expand Down
4 changes: 4 additions & 0 deletions plugins/hls-alternate-number-format-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ To generate suggestions, the plugin leverages the `Numeric` package which provid

### 1.0.1.1
- Buildable with GHC 9.2

### 1.0.2.0
- Test Suite upgraded for 9.2 semantics (GHC2021)
- Fix SYB parsing with GHC 9.2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.4
name: hls-alternate-number-format-plugin
version: 1.0.1.1
version: 1.0.2.0
synopsis: Provide Alternate Number Formats plugin for Haskell Language Server
description:
Please see the README on GitHub at <https://github.com/haskell/haskell-language-server#readme>
Expand All @@ -21,6 +21,7 @@ library
exposed-modules: Ide.Plugin.AlternateNumberFormat, Ide.Plugin.Conversion
other-modules: Ide.Plugin.Literals
hs-source-dirs: src
ghc-options: -Wall
build-depends:
aeson
, base >=4.12 && < 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import Development.IDE.Types.Logger as Logger
import GHC.Generics (Generic)
import Ide.Plugin.Conversion (FormatType, alternateFormat,
toFormatTypes)
import Ide.Plugin.Literals (Literal (..), collectLiterals,
getSrcSpan, getSrcText)
import Ide.Plugin.Literals
import Ide.PluginUtils (handleMaybe, handleMaybeM,
response)
import Ide.Types
Expand Down Expand Up @@ -126,7 +125,6 @@ requestLiterals state = handleMaybeM "Error: Could not Collect Literals"
. runAction "AlternateNumberFormat.CollectLiterals" state
. use CollectLiterals


logIO :: (MonadIO m, Show a) => IdeState -> a -> m ()
logIO state = liftIO . Logger.logDebug (ideLogger state) . T.pack . show

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ViewPatterns #-}
module Ide.Plugin.Literals (
collectLiterals
, Literal(..)
Expand All @@ -13,7 +15,6 @@ import Data.Maybe (maybeToList)
import Data.Text (Text)
import qualified Data.Text as T
import Development.IDE.GHC.Compat hiding (getSrcSpan)
import Development.IDE.GHC.Util (unsafePrintSDoc)
import Development.IDE.Graph.Classes (NFData (rnf))
import qualified GHC.Generics as GHC
import Generics.SYB (Data, Typeable, everything,
Expand Down Expand Up @@ -48,25 +49,36 @@ getSrcSpan = \case
collectLiterals :: (Data ast, Typeable ast) => ast -> [Literal]
collectLiterals = everything (<>) (maybeToList . (const Nothing `extQ` getLiteral `extQ` getPattern))


-- | Translate from HsLit and HsOverLit Types to our Literal Type
getLiteral :: GenLocated SrcSpan (HsExpr GhcPs) -> Maybe Literal
getLiteral (L (UnhelpfulSpan _) _) = Nothing
getLiteral (L (RealSrcSpan sSpan _ ) expr) = case expr of
getLiteral :: (LHsExpr GhcPs) -> Maybe Literal
getLiteral (L (locA -> (RealSrcSpan sSpan _)) expr) = case expr of
HsLit _ lit -> fromLit lit sSpan
HsOverLit _ overLit -> fromOverLit overLit sSpan
_ -> Nothing
getLiteral _ = Nothing



-- GHC 8.8 typedefs LPat = Pat
#if __GLASGOW_HASKELL__ == 808
type LocPat a = GenLocated SrcSpan (Pat a)
#else
type LocPat a = LPat a
#endif

-- | Destructure Patterns to unwrap any Literals
getPattern :: GenLocated SrcSpan (Pat GhcPs) -> Maybe Literal
getPattern (L (UnhelpfulSpan _) _) = Nothing
getPattern (L (RealSrcSpan patSpan _) pat) = case pat of
getPattern :: (LocPat GhcPs) -> Maybe Literal
getPattern (L (locA -> (RealSrcSpan patSpan _)) pat) = case pat of
LitPat _ lit -> case lit of
HsInt _ val -> fromIntegralLit patSpan val
HsRat _ val _ -> fromFractionalLit patSpan val
_ -> Nothing
-- a located HsOverLit is (GenLocated SrcSpan HsOverLit) NOT (GenLocated SrcSpanAnn' a HsOverLit)
NPat _ (L (RealSrcSpan sSpan _) overLit) _ _ -> fromOverLit overLit sSpan
NPlusKPat _ _ (L (RealSrcSpan sSpan _) overLit1) _ _ _ -> fromOverLit overLit1 sSpan
_ -> Nothing
getPattern _ = Nothing

fromLit :: HsLit p -> RealSrcSpan -> Maybe Literal
fromLit lit sSpan = case lit of
Expand All @@ -91,30 +103,3 @@ fromSourceText :: SourceText -> Maybe Text
fromSourceText = \case
SourceText s -> Just $ T.pack s
NoSourceText -> Nothing

-- mostly for debugging purposes
literalToString :: HsLit p -> String
literalToString = \case
HsChar _ c -> "Char: " <> show c
HsCharPrim _ c -> "CharPrim: " <> show c
HsString _ fs -> "String: " <> show fs
HsStringPrim _ bs -> "StringPrim: " <> show bs
HsInt _ il -> "Int: " <> show il
HsIntPrim _ n -> "IntPrim: " <> show n
HsWordPrim _ n -> "WordPrim: " <> show n
HsInt64Prim _ n -> "Int64Prim: " <> show n
HsWord64Prim _ n -> "Word64Prim: " <> show n
HsInteger _ n ty -> "Integer: " <> show n <> " Type: " <> tyToLiteral ty
HsRat _ fl ty -> "Rat: " <> show fl <> " Type: " <> tyToLiteral ty
HsFloatPrim _ fl -> "FloatPrim: " <> show fl
HsDoublePrim _ fl -> "DoublePrim: " <> show fl
_ -> "XHsLit"
where
tyToLiteral :: Type -> String
tyToLiteral = unsafePrintSDoc . ppr

overLitToString :: OverLitVal -> String
overLitToString = \case
HsIntegral int -> case int of { IL{il_value} -> "IntegralOverLit: " <> show il_value}
HsFractional frac -> case frac of { fl -> "RationalOverLit: " <> show (rationalFromFractionalLit fl)}
HsIsString _ str -> "HIsString: " <> show str
31 changes: 6 additions & 25 deletions plugins/hls-alternate-number-format-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ main = defaultTestRunner test
alternateNumberFormatPlugin :: PluginDescriptor IdeState
alternateNumberFormatPlugin = AlternateNumberFormat.descriptor mempty "alternateNumberFormat"


-- NOTE: For whatever reason, this plugin does not play nice with creating Code Actions on time.
-- As a result tests will mostly pass if `import Prelude` is added at the top. We (mostly fendor) surmise this has something
-- to do with how
Expand All @@ -37,36 +36,18 @@ test = testGroup "alternateNumberFormat" [
, codeActionFloatHex "TFracDtoHF" 4 13
, codeActionDecimal "TIntHtoD" 3 13
, codeActionDecimal "TFracHFtoD" 4 13
, codeActionProperties "TFindLiteralIntPattern" [(3, 25), (4,25)] $ \actions -> do
, codeActionProperties "TFindLiteralIntPattern" [(4, 25), (5,25)] $ \actions -> do
liftIO $ length actions @?= 4
, codeActionProperties "TFindLiteralIntCase" [(3, 29)] $ \actions -> do
, codeActionProperties "TFindLiteralIntCase" [(4, 29)] $ \actions -> do
liftIO $ length actions @?= 2
, codeActionProperties "TFindLiteralIntCase2" [(4, 21)] $ \actions -> do
, codeActionProperties "TFindLiteralIntCase2" [(5, 21)] $ \actions -> do
liftIO $ length actions @?= 2
, codeActionProperties "TFindLiteralDoReturn" [(5, 10)] $ \actions -> do
, codeActionProperties "TFindLiteralDoReturn" [(6, 10)] $ \actions -> do
liftIO $ length actions @?= 2
, codeActionProperties "TFindLiteralDoLet" [(5, 13), (6, 13)] $ \actions -> do
, codeActionProperties "TFindLiteralDoLet" [(6, 13), (7, 13)] $ \actions -> do
liftIO $ length actions @?= 4
, codeActionProperties "TFindLiteralList" [(3, 28)] $ \actions -> do
liftIO $ length actions @?= 2
, codeActionProperties "TExpectNoBinaryFormat" [(3, 12)] $ \actions -> do
liftIO $ length actions @?= 2
liftIO $ actions `doesNotContain` binaryRegex @? "Contains binary codeAction"
, codeActionProperties "TExpectBinaryFormat" [(4, 10)] $ \actions -> do
liftIO $ length actions @?= 3
liftIO $ actions `contains` binaryRegex @? "Does not contain binary codeAction"
, codeActionProperties "TExpectNoHexFloatFormat" [(3, 14)] $ \actions -> do
liftIO $ length actions @?= 1
liftIO $ actions `doesNotContain` hexFloatRegex @? "Contains hex float codeAction"
, codeActionProperties "TExpectHexFloatFormat" [(4, 12)] $ \actions -> do
liftIO $ length actions @?= 2
liftIO $ actions `contains` hexFloatRegex @? "Does not contain hex float codeAction"
, codeActionProperties "TExpectNoNumDecimalFormat" [(3, 16)] $ \actions -> do
, codeActionProperties "TFindLiteralList" [(4, 28)] $ \actions -> do
liftIO $ length actions @?= 2
liftIO $ actions `doesNotContain` numDecimalRegex @? "Contains numDecimal codeAction"
, codeActionProperties "TExpectNumDecimalFormat" [(4, 14)] $ \actions -> do
liftIO $ length actions @?= 5
liftIO $ actions `contains` numDecimalRegex @? "Contains numDecimal codeAction"
, conversions
]

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE NoBinaryLiterals #-}
module TFindLiteralDoLet where

doLet :: IO ()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE NoBinaryLiterals #-}
module TFindLiteralDoReturn where

doReturn :: IO Integer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE NoBinaryLiterals #-}
module TFindLiteralIntCase where

caseExpression x = case x + 34 of
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE NoBinaryLiterals #-}
module TFindLiteralIntCase where

caseExpression x = case x of
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE NoBinaryLiterals #-}
module TFindLiteralIntPattern where

patternMatchingFunction 1 = "one"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE NoBinaryLiterals #-}
module TFindLiteralList where

listTest = [reverse $ show 57]