Skip to content

Commit 5406b2d

Browse files
committed
wip introduce ChooseCharSet and refactor code
1 parent 3ff4166 commit 5406b2d

File tree

19 files changed

+110
-67
lines changed

19 files changed

+110
-67
lines changed

dhall-lsp-server/src/Dhall/LSP/Backend/Formatting.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Dhall.LSP.Backend.Formatting (formatExpr, formatExprWithHeader) where
22

3-
import Data.Maybe (fromMaybe)
43
import Data.Text (Text)
54
import Dhall.Core (Expr)
65
import Dhall.Parser (Header (..))
@@ -12,21 +11,21 @@ import qualified Prettyprinter as Pretty
1211
import qualified Prettyprinter.Render.Text as Pretty
1312

1413
-- | Pretty-print the given Dhall expression.
15-
formatExpr :: Pretty.Pretty b => Maybe CharacterSet -> Expr Src b -> Text
14+
formatExpr :: Pretty.Pretty b => ChooseCharacterSet -> Expr Src b -> Text
1615
formatExpr chosenCharacterSet expr =
1716
Pretty.renderStrict
1817
. Dhall.Pretty.layout
1918
$ Dhall.Pretty.prettyCharacterSet charSet expr
2019
where
21-
charSet = fromMaybe (Dhall.Pretty.detectCharacterSet expr) chosenCharacterSet
20+
charSet = chooseCharsetOrUseDefault (Dhall.Pretty.detectCharacterSet expr) chosenCharacterSet
2221

2322
-- | Pretty-print the given Dhall expression, prepending the given a "header"
2423
-- (usually consisting of comments and whitespace).
25-
formatExprWithHeader :: Pretty.Pretty b => Maybe CharacterSet -> Expr Src b -> Header -> Text
24+
formatExprWithHeader :: Pretty.Pretty b => ChooseCharacterSet -> Expr Src b -> Header -> Text
2625
formatExprWithHeader chosenCharacterSet expr (Header header) = Pretty.renderStrict
2726
(Dhall.Pretty.layout doc)
2827
where
29-
charSet = fromMaybe (Dhall.Pretty.detectCharacterSet expr) chosenCharacterSet
28+
charSet = chooseCharsetOrUseDefault (Dhall.Pretty.detectCharacterSet expr) chosenCharacterSet
3029

3130
doc =
3231
Pretty.pretty header

dhall-lsp-server/src/Dhall/LSP/State.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ data Severity = Error
3939
-- ^ Log message, not displayed by default.
4040

4141
data ServerConfig = ServerConfig
42-
{ chosenCharacterSet :: Maybe CharacterSet
42+
{ chosenCharacterSet :: ChooseCharacterSet
4343
} deriving Show
4444

4545
instance Default ServerConfig where

dhall-openapi/openapi-to-dhall/Main.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import Dhall.Kubernetes.Types
3636
)
3737
import System.FilePath ((</>))
3838

39+
import Dhall.Pretty.Internal (ChooseCharacterSet(..))
40+
3941
import qualified Data.List as List
4042
import qualified Data.Map.Strict as Data.Map
4143
import qualified Data.Ord as Ord
@@ -74,7 +76,7 @@ writeDhall path expr = do
7476
putStrLn $ "Writing file '" <> path <> "'"
7577
Text.writeFile path $ pretty expr <> "\n"
7678

77-
let chosenCharacterSet = Nothing -- Infer from input
79+
let chosenCharacterSet = AutoInferCharSet
7880

7981
let censor = Dhall.Util.NoCensor
8082

dhall/src/.DS_Store

8 KB
Binary file not shown.

dhall/src/Dhall/.DS_Store

6 KB
Binary file not shown.

dhall/src/Dhall/DirectoryTree.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import qualified Dhall.Core as Core
5050
import qualified Dhall.Map as Map
5151
import qualified Dhall.Marshal.Decode as Decode
5252
import qualified Dhall.Pretty
53+
import Dhall.Pretty.Internal (ChooseCharacterSet(..))
5354
import qualified Dhall.TypeCheck as TypeCheck
5455
import qualified Dhall.Util as Util
5556
import qualified Prettyprinter as Pretty
@@ -252,8 +253,8 @@ decodeDirectoryTree expr = Exception.throwIO $ FilesystemError $ Core.denote exp
252253

253254
-- | The type of a fixpoint directory tree expression.
254255
directoryTreeType :: Expector (Expr Src Void)
255-
directoryTreeType = Pi Nothing "tree" (Const Type)
256-
<$> (Pi Nothing "make" <$> makeType <*> pure (App List (Var (V "tree" 0))))
256+
directoryTreeType = Pi AutoInferCharSet "tree" (Const Type)
257+
<$> (Pi AutoInferCharSet "make" <$> makeType <*> pure (App List (Var (V "tree" 0))))
257258

258259
-- | The type of make part of a fixpoint directory tree expression.
259260
makeType :: Expector (Expr Src Void)
@@ -265,7 +266,7 @@ makeType = Record . Map.fromList <$> sequenceA
265266
where
266267
makeConstructor :: Text -> Decoder b -> Expector (Text, RecordField Src Void)
267268
makeConstructor name dec = (name,) . Core.makeRecordField
268-
<$> (Pi Nothing "_" <$> expected dec <*> pure (Var (V "tree" 0)))
269+
<$> (Pi AutoInferCharSet "_" <$> expected dec <*> pure (Var (V "tree" 0)))
269270

270271
-- | Resolve a `User` to a numerical id.
271272
getUser :: User -> IO UserID

dhall/src/Dhall/Format.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ module Dhall.Format
1111

1212
import Data.Foldable (for_)
1313
import Data.List.NonEmpty (NonEmpty)
14-
import Data.Maybe (fromMaybe)
15-
import Dhall.Pretty (CharacterSet, annToAnsiStyle, detectCharacterSet)
14+
import Dhall.Pretty (annToAnsiStyle, detectCharacterSet)
1615
import Dhall.Util
1716
( Censor
1817
, CheckFailed (..)
@@ -34,10 +33,11 @@ import qualified System.AtomicWrite.Writer.LazyText as AtomicWrite.LazyText
3433
import qualified System.Console.ANSI
3534
import qualified System.FilePath
3635
import qualified System.IO
36+
import Dhall.Pretty.Internal (ChooseCharacterSet(..), chooseCharsetOrUseDefault)
3737

3838
-- | Arguments to the `format` subcommand
3939
data Format = Format
40-
{ chosenCharacterSet :: Maybe CharacterSet
40+
{ chosenCharacterSet :: ChooseCharacterSet
4141
, censor :: Censor
4242
, transitivity :: Transitivity
4343
, inputs :: NonEmpty Input
@@ -59,7 +59,7 @@ format (Format { inputs = inputs0, transitivity = transitivity0, ..}) =
5959
let status = Dhall.Import.emptyStatus directory
6060

6161
let layoutHeaderAndExpr (Header header, expr) =
62-
let characterSet = fromMaybe (detectCharacterSet expr) chosenCharacterSet
62+
let characterSet = chooseCharsetOrUseDefault (detectCharacterSet expr) chosenCharacterSet
6363
in
6464
Dhall.Pretty.layout
6565
( Pretty.pretty header

dhall/src/Dhall/Freeze.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ module Dhall.Freeze
3131

3232
import Data.Foldable (for_)
3333
import Data.List.NonEmpty (NonEmpty)
34-
import Data.Maybe (fromMaybe)
3534
import Dhall (EvaluateSettings)
36-
import Dhall.Pretty (CharacterSet, detectCharacterSet)
35+
import Dhall.Pretty (detectCharacterSet)
36+
import Dhall.Pretty.Internal (ChooseCharacterSet(..), chooseCharsetOrUseDefault)
37+
3738
import Dhall.Syntax
3839
( Expr (..)
3940
, Import (..)
@@ -128,7 +129,7 @@ freeze
128129
-> NonEmpty Input
129130
-> Scope
130131
-> Intent
131-
-> Maybe CharacterSet
132+
-> ChooseCharacterSet
132133
-> Censor
133134
-> IO ()
134135
freeze = freezeWithSettings Dhall.defaultEvaluateSettings
@@ -141,7 +142,7 @@ freezeWithManager
141142
-> NonEmpty Input
142143
-> Scope
143144
-> Intent
144-
-> Maybe CharacterSet
145+
-> ChooseCharacterSet
145146
-> Censor
146147
-> IO ()
147148
freezeWithManager newManager = freezeWithSettings (set Dhall.newManager newManager Dhall.defaultEvaluateSettings)
@@ -242,7 +243,7 @@ freezeWithSettings
242243
-> NonEmpty Input
243244
-> Scope
244245
-> Intent
245-
-> Maybe CharacterSet
246+
-> ChooseCharacterSet
246247
-> Censor
247248
-> IO ()
248249
freezeWithSettings settings outputMode transitivity0 inputs scope intent chosenCharacterSet censor =
@@ -270,7 +271,7 @@ freezeWithSettings settings outputMode transitivity0 inputs scope intent chosenC
270271

271272
(Header header, parsedExpression) <- Util.getExpressionAndHeaderFromStdinText censor inputName originalText
272273

273-
let characterSet = fromMaybe (detectCharacterSet parsedExpression) chosenCharacterSet
274+
let characterSet = chooseCharsetOrUseDefault (detectCharacterSet parsedExpression) chosenCharacterSet
274275

275276
case transitivity of
276277
Transitive ->

dhall/src/Dhall/Import/Headers.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Dhall.Import.Headers
1010
, toOriginHeaders
1111
) where
1212

13-
import Control.Applicative (Alternative (..), liftA2)
13+
import Control.Applicative (Alternative (..))
1414
import Control.Exception (SomeException)
1515
import Control.Monad.Catch (handle, throwM)
1616
import Data.Text (Text)

dhall/src/Dhall/Main.hs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import qualified Dhall.Lint
9999
import qualified Dhall.Map
100100
import qualified Dhall.Package
101101
import qualified Dhall.Pretty
102+
import Dhall.Pretty.Internal (ChooseCharacterSet(..), chooseCharsetOrUseDefault)
102103
import qualified Dhall.Repl
103104
import qualified Dhall.Schemas
104105
import qualified Dhall.Tags
@@ -122,7 +123,7 @@ data Options = Options
122123
{ mode :: Mode
123124
, explain :: Bool
124125
, plain :: Bool
125-
, chosenCharacterSet :: Maybe CharacterSet
126+
, chosenCharacterSet :: ChooseCharacterSet
126127
, censor :: Censor
127128
}
128129

@@ -221,16 +222,16 @@ parseOptions =
221222

222223
parseCharacterSet =
223224
Options.Applicative.flag'
224-
(Just Unicode)
225+
(Specify Unicode)
225226
( Options.Applicative.long "unicode"
226227
<> Options.Applicative.help "Format code using only Unicode syntax"
227228
)
228229
<|> Options.Applicative.flag'
229-
(Just ASCII)
230+
(Specify ASCII)
230231
( Options.Applicative.long "ascii"
231232
<> Options.Applicative.help "Format code using only ASCII syntax"
232233
)
233-
<|> pure Nothing
234+
<|> pure AutoInferCharSet
234235

235236
subcommand :: Group -> String -> String -> Parser a -> Parser a
236237
subcommand group name description parser =
@@ -634,7 +635,7 @@ command (Options {..}) = do
634635
let getExpressionAndCharacterSet file = do
635636
expr <- getExpression file
636637

637-
let characterSet = fromMaybe (detectCharacterSet expr) chosenCharacterSet
638+
let characterSet = chooseCharsetOrUseDefault (detectCharacterSet expr) chosenCharacterSet
638639

639640
return (expr, characterSet)
640641

@@ -833,7 +834,7 @@ command (Options {..}) = do
833834

834835
Repl ->
835836
Dhall.Repl.repl
836-
(fromMaybe Unicode chosenCharacterSet) -- Default to Unicode if no characterSet specified
837+
(chooseCharsetOrUseDefault Unicode chosenCharacterSet) -- Default to Unicode if no characterSet specified
837838
explain
838839

839840
Diff {..} -> do
@@ -908,7 +909,7 @@ command (Options {..}) = do
908909
(Header header, parsedExpression) <-
909910
Dhall.Util.getExpressionAndHeaderFromStdinText censor inputName originalText
910911

911-
let characterSet = fromMaybe (detectCharacterSet parsedExpression) chosenCharacterSet
912+
let characterSet = chooseCharsetOrUseDefault (detectCharacterSet parsedExpression) chosenCharacterSet
912913

913914
case transitivity of
914915
Transitive ->
@@ -994,7 +995,7 @@ command (Options {..}) = do
994995
else do
995996
let doc =
996997
Dhall.Pretty.prettyCharacterSet
997-
(fromMaybe Unicode chosenCharacterSet) -- default to Unicode
998+
(chooseCharsetOrUseDefault Unicode chosenCharacterSet) -- default to Unicode
998999
(Dhall.Core.renote expression :: Expr Src Import)
9991000

10001001
renderDoc System.IO.stdout doc
@@ -1060,10 +1061,16 @@ command (Options {..}) = do
10601061

10611062
Package {..} -> do
10621063
let options = appEndo
1063-
(maybe mempty (Endo . set Dhall.Package.characterSet) chosenCharacterSet
1064+
(maybe mempty (Endo . set Dhall.Package.characterSet) chosenCharacterSetAsMaybe
10641065
<> packageOptions
10651066
) Dhall.Package.defaultOptions
10661067
writePackage options packageFiles
1068+
where
1069+
chosenCharacterSetAsMaybe :: Maybe CharacterSet
1070+
chosenCharacterSetAsMaybe = case chosenCharacterSet of
1071+
AutoInferCharSet -> Nothing
1072+
Specify c -> Just c
1073+
10671074

10681075
-- | Entry point for the @dhall@ executable
10691076
main :: IO ()

0 commit comments

Comments
 (0)