Skip to content

Commit 4748a87

Browse files
committed
Add NumericUnderscores extension when needed
1 parent f5af2d3 commit 4748a87

File tree

11 files changed

+70
-24
lines changed

11 files changed

+70
-24
lines changed

plugins/hls-alternate-number-format-plugin/src/Ide/Plugin/AlternateNumberFormat.hs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Development.IDE.Spans.Pragmas (NextPragmaInfo,
2323
import GHC.Generics (Generic)
2424
import Ide.Logger as Logger
2525
import Ide.Plugin.Conversion (AlternateFormat,
26-
ExtensionNeeded (NeedsExtension, NoExtension),
26+
ExtensionNeeded (..),
2727
alternateFormat)
2828
import Ide.Plugin.Error
2929
import Ide.Plugin.Literals
@@ -93,7 +93,7 @@ codeActionHandler state pId (CodeActionParams _ _ docId currRange _) = do
9393
pure $ InL actions
9494
where
9595
mkCodeAction :: NormalizedFilePath -> Literal -> [GhcExtension] -> NextPragmaInfo -> AlternateFormat -> Command |? CodeAction
96-
mkCodeAction nfp lit enabled npi af@(alt, ext) = InR CodeAction {
96+
mkCodeAction nfp lit enabled npi af@(alt, ExtensionNeeded exts) = InR CodeAction {
9797
_title = mkCodeActionTitle lit af enabled
9898
, _kind = Just $ CodeActionKind_Custom "quickfix.literals.style"
9999
, _diagnostics = Nothing
@@ -104,28 +104,29 @@ codeActionHandler state pId (CodeActionParams _ _ docId currRange _) = do
104104
, _data_ = Nothing
105105
}
106106
where
107-
edits = [TextEdit (realSrcSpanToRange $ getSrcSpan lit) alt] <> pragmaEdit
108-
pragmaEdit = case ext of
109-
NeedsExtension ext' -> [insertNewPragma npi ext' | needsExtension ext' enabled]
110-
NoExtension -> []
107+
edits = [TextEdit (realSrcSpanToRange $ getSrcSpan lit) alt] <> pragmaEdit exts
108+
pragmaEdit ext = case ext of
109+
ext': exts -> [insertNewPragma npi ext' | needsExtension enabled ext'] <> pragmaEdit exts
110+
[] -> []
111111

112112
mkWorkspaceEdit :: NormalizedFilePath -> [TextEdit] -> WorkspaceEdit
113113
mkWorkspaceEdit nfp edits = WorkspaceEdit changes Nothing Nothing
114114
where
115115
changes = Just $ Map.singleton (filePathToUri $ fromNormalizedFilePath nfp) edits
116116

117117
mkCodeActionTitle :: Literal -> AlternateFormat -> [GhcExtension] -> Text
118-
mkCodeActionTitle lit (alt, ext) ghcExts
119-
| (NeedsExtension ext') <- ext
120-
, needsExtension ext' ghcExts = title <> " (needs extension: " <> T.pack (show ext') <> ")"
121-
| otherwise = title
118+
mkCodeActionTitle lit (alt, ExtensionNeeded exts) ghcExts
119+
| null necessaryExtensions = title
120+
| otherwise = title <> " (needs extensions: " <> formattedExtensions <> ")"
122121
where
122+
formattedExtensions = T.intercalate ", " $ map (T.pack . show) necessaryExtensions
123+
necessaryExtensions = filter (needsExtension ghcExts) exts
123124
title = "Convert " <> getSrcText lit <> " into " <> alt
124125

125126

126127
-- | Checks whether the extension given is already enabled
127-
needsExtension :: Extension -> [GhcExtension] -> Bool
128-
needsExtension ext ghcExts = ext `notElem` map unExt ghcExts
128+
needsExtension :: [GhcExtension] -> Extension -> Bool
129+
needsExtension ghcExts ext = ext `notElem` map unExt ghcExts
129130

130131
requestLiterals :: MonadIO m => PluginId -> IdeState -> NormalizedFilePath -> ExceptT PluginError m CollectLiteralsResult
131132
requestLiterals (PluginId pId) state =

plugins/hls-alternate-number-format-plugin/src/Ide/Plugin/Conversion.hs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
{-# LANGUAGE CPP #-}
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE DerivingStrategies #-}
23
module Ide.Plugin.Conversion (
34
alternateFormat
45
, toOctal
@@ -54,8 +55,8 @@ data FracFormatType = FracDecimalFormat
5455

5556
instance NFData FracFormatType
5657

57-
data ExtensionNeeded = NoExtension
58-
| NeedsExtension Extension
58+
newtype ExtensionNeeded = ExtensionNeeded [Extension]
59+
deriving newtype (Semigroup, Monoid)
5960

6061
type AlternateFormat = (Text, ExtensionNeeded)
6162

@@ -76,19 +77,24 @@ data UnderscoreFormatType
7677
| UseUnderscores Int
7778
deriving (Show, Eq)
7879

80+
underscoreExtensions :: UnderscoreFormatType -> ExtensionNeeded
81+
underscoreExtensions = \case
82+
NoUnderscores -> mempty
83+
UseUnderscores _ -> ExtensionNeeded [NumericUnderscores]
84+
7985
alternateIntFormat :: Integer -> IntFormatType -> UnderscoreFormatType -> AlternateFormat
8086
alternateIntFormat val formatType underscoreFormat = case formatType of
81-
IntDecimalFormat -> (T.pack $ toDecimal underscoreFormat val , NoExtension)
82-
HexFormat -> (T.pack $ toHex underscoreFormat val , NoExtension)
83-
OctalFormat -> (T.pack $ toOctal underscoreFormat val , NoExtension)
84-
BinaryFormat -> (T.pack $ toBinary underscoreFormat val , NeedsExtension BinaryLiterals)
85-
NumDecimalFormat -> (T.pack $ toFloatExpDecimal underscoreFormat (fromInteger @Double val) , NeedsExtension NumDecimals)
87+
IntDecimalFormat -> (T.pack $ toDecimal underscoreFormat val , underscoreExtensions underscoreFormat)
88+
HexFormat -> (T.pack $ toHex underscoreFormat val , underscoreExtensions underscoreFormat)
89+
OctalFormat -> (T.pack $ toOctal underscoreFormat val , underscoreExtensions underscoreFormat)
90+
BinaryFormat -> (T.pack $ toBinary underscoreFormat val , underscoreExtensions underscoreFormat <> ExtensionNeeded [BinaryLiterals])
91+
NumDecimalFormat -> (T.pack $ toFloatExpDecimal underscoreFormat (fromInteger @Double val) , underscoreExtensions underscoreFormat <> ExtensionNeeded [NumDecimals])
8692

8793
alternateFracFormat :: Rational -> FracFormatType -> UnderscoreFormatType -> AlternateFormat
8894
alternateFracFormat val formatType underscoreFormat = case formatType of
89-
FracDecimalFormat -> (T.pack $ toFloatDecimal underscoreFormat (fromRational @Double val), NoExtension)
90-
ExponentFormat -> (T.pack $ toFloatExpDecimal underscoreFormat (fromRational @Double val), NoExtension)
91-
HexFloatFormat -> (T.pack $ toHexFloat underscoreFormat (fromRational @Double val), NeedsExtension HexFloatLiterals)
95+
FracDecimalFormat -> (T.pack $ toFloatDecimal underscoreFormat (fromRational @Double val), mempty)
96+
ExponentFormat -> (T.pack $ toFloatExpDecimal underscoreFormat (fromRational @Double val), mempty)
97+
HexFloatFormat -> (T.pack $ toHexFloat underscoreFormat (fromRational @Double val), underscoreExtensions underscoreFormat <> ExtensionNeeded [HexFloatLiterals])
9298

9399
intFormats :: Map.Map IntFormatType [UnderscoreFormatType]
94100
intFormats = Map.fromList $ map (\t -> (t, intFormatUnderscore t)) enumerate

plugins/hls-alternate-number-format-plugin/test/Main.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ test = testGroup "alternateNumberFormat" [
2525
codeActionHex 0 "TIntDtoH" 3 13
2626
, codeActionOctal 0 "TIntDtoO" 3 13
2727
, codeActionBinary 0 "TIntDtoB" 4 13
28+
, codeActionBinary 6 "TIntDtoBU0toU4MultiplePragma" 4 13
2829
, codeActionNumDecimal 0 "TIntDtoND" 5 13
30+
, codeActionDecimal 2 "TIntDtoDU0toU3" 4 13
2931
, codeActionFracExp 0 "TFracDtoE" 3 13
32+
, codeActionFracExp 3 "TFracDtoEU0toU3" 3 13
3033
, codeActionFloatHex 0 "TFracDtoHF" 4 13
34+
, codeActionFloatHex 6 "TFracDtoHFU0toU2" 4 13
3135
, codeActionDecimal 0 "TIntHtoD" 3 13
3236
, codeActionDecimal 0 "TFracHFtoD" 4 13
3337
, codeActionDecimal 3 "TFracDtoDU0toU3" 3 13
@@ -117,7 +121,7 @@ pointRange
117121
convertPrefix, intoInfix, maybeExtension, hexRegex, hexFloatRegex, binaryRegex, octalRegex, numDecimalRegex, decimalRegex :: Text
118122
convertPrefix = "Convert (" <> T.intercalate "|" [Conversion.hexRegex, Conversion.hexFloatRegex, Conversion.binaryRegex, Conversion.octalRegex, Conversion.numDecimalRegex, Conversion.decimalRegex] <> ")"
119123
intoInfix = " into "
120-
maybeExtension = "( \\(needs extension: .*)?"
124+
maybeExtension = "( \\(needs extensions: .*)?"
121125
hexRegex = intoInfix <> Conversion.hexRegex <> maybeExtension
122126
hexFloatRegex = intoInfix <> Conversion.hexFloatRegex <> maybeExtension
123127
binaryRegex = intoInfix <> Conversion.binaryRegex <> maybeExtension
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TFracDtoE where
2+
3+
convertMe = 1.234_567_890_123e2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TFracDtoE where
2+
3+
convertMe = 1.234567890123e2
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE HexFloatLiterals #-}
3+
{-# LANGUAGE NumericUnderscores #-}
4+
module TFracDtoHF where
5+
6+
convertMe = 0x1.ee_cc_cc_cc_cc_cc_dp6
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
module TFracDtoHF where
3+
4+
convertMe = 123.7
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE BinaryLiterals #-}
3+
{-# LANGUAGE NumericUnderscores #-}
4+
module TIntDtoB where
5+
6+
convertMe = 0b111_0101_1011_1100_1101_0001_0101
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
module TIntDtoB where
3+
4+
convertMe = 123456789
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
{-# LANGUAGE NumericUnderscores #-}
3+
module TIntDtoB where
4+
5+
convertMe = 12_345_678

0 commit comments

Comments
 (0)