diff --git a/build.fsx b/build.fsx index df8576f89d..4726a68d60 100644 --- a/build.fsx +++ b/build.fsx @@ -57,14 +57,14 @@ pipeline "Build" { run ( cleanFolders [| "bin" - "src/Fantomas.FCS/bin" - "src/Fantomas.FCS/obj" - "src/Fantomas.Core/bin" - "src/Fantomas.Core/obj" - "src/Fantomas/bin" - "src/Fantomas/obj" - "src/Fantomas.Client/bin" - "src/Fantomas.Client/obj" |] + "src/Fantomas.FCS/bin/Release" + "src/Fantomas.FCS/obj/Release" + "src/Fantomas.Core/bin/Release" + "src/Fantomas.Core/obj/Release" + "src/Fantomas/bin/Release" + "src/Fantomas/obj/Release" + "src/Fantomas.Client/bin/Release" + "src/Fantomas.Client/obj/Release" |] ) } stage "CheckFormat" { run "dotnet fantomas src docs build.fsx --recurse --check" } diff --git a/docs/docs/end-users/Configuration.fsx b/docs/docs/end-users/Configuration.fsx index 837da6e679..7b90fe74fc 100644 --- a/docs/docs/end-users/Configuration.fsx +++ b/docs/docs/end-users/Configuration.fsx @@ -908,32 +908,6 @@ let singleList = ExperimentalElmish = true } (*** include-output ***) -(** - - -If being set, pretty printing is only done via ASTs. Compiler directives, inline comments and block comments will be ignored. -There are numerous situations when the information in the AST alone cannot restore the original code. - -**Please do not use this setting for formatting hand written code!** - -Valid use-case of this settings is code generation in projects like [FsAst](https://github.com/ionide/FsAst) and [Myriad](https://github.com/MoiraeSoftware/myriad). - -Default = false. -*) - -formatCode - """ - // some great comment - let add a b = - #if INTERACTIVE - 42 - #else - a + b - #endif - """ - { FormatConfig.Default with - StrictMode = true } -(*** include-output ***) (** diff --git a/docs/docs/end-users/UpgradeGuide.md b/docs/docs/end-users/UpgradeGuide.md index e7828a14b8..8847d0ca07 100644 --- a/docs/docs/end-users/UpgradeGuide.md +++ b/docs/docs/end-users/UpgradeGuide.md @@ -66,6 +66,7 @@ fsharp_experimental_stroustrup_style = true - `fsharp_multiline_block_brackets_on_same_column` and `fsharp_experimental_stroustrup_style` are replaced with `fsharp_multiline_bracket_style` - `experimental_stroustrup` for `fsharp_multiline_bracket_style` is now `stroustrup` - `fsharp_newline_before_multiline_computation_expression` was extracted from `fsharp_multiline_bracket_style = stroustrup` and now controls how computation expression behave. +- `fsharp_strict_mode` was removed and can no longer be used. ### console application - `-v` is now short for `--verbosity` instead of `--version` @@ -74,5 +75,6 @@ fsharp_experimental_stroustrup_style = true ### Miscellaneous - The public API of CodeFormatter no longer uses `FSharpOption<'T>`, instead overloads are now used. +- `StrictMode` was removed from `FormatConfig`, not passing the source text in the public API will have the same effect. diff --git a/src/Fantomas.Core.Tests/ExternTests.fs b/src/Fantomas.Core.Tests/ExternTests.fs index cb7f8d00d8..8d618d18fe 100644 --- a/src/Fantomas.Core.Tests/ExternTests.fs +++ b/src/Fantomas.Core.Tests/ExternTests.fs @@ -6,14 +6,14 @@ open Fantomas.Core.Tests.TestHelper [] let ``attribute above extern keyword, 562`` () = - formatSourceString + formatAST false """ module C = [] extern IntPtr f() """ - { config with StrictMode = true } + config |> prepend newline |> should equal diff --git a/src/Fantomas.Core.Tests/InterpolatedStringTests.fs b/src/Fantomas.Core.Tests/InterpolatedStringTests.fs index c5f333c88b..e3b20c67b1 100644 --- a/src/Fantomas.Core.Tests/InterpolatedStringTests.fs +++ b/src/Fantomas.Core.Tests/InterpolatedStringTests.fs @@ -63,13 +63,13 @@ let s = $\"\"\"%s{text} bar\"\"\" [] let ``interpolation in strict mode`` () = - formatSourceString + formatAST false """ let text = "foo" let s = $"%s{text} bar" """ - { config with StrictMode = true } + config |> prepend newline |> should equal diff --git a/src/Fantomas.Core.Tests/StringTests.fs b/src/Fantomas.Core.Tests/StringTests.fs index 38733e2569..f3297036fb 100644 --- a/src/Fantomas.Core.Tests/StringTests.fs +++ b/src/Fantomas.Core.Tests/StringTests.fs @@ -113,7 +113,7 @@ let g = '\n' [] let ``uncommon literals strict mode`` () = - formatSourceString + formatAST false """ let a = 0xFFy @@ -123,7 +123,7 @@ let e = 1.40e10f let f = 23.4M let g = '\n' """ - { config with StrictMode = true } + config |> prepend newline |> should equal @@ -219,14 +219,14 @@ let ``chars should be properly escaped`` () = [] let ``quotes should be escaped in strict mode`` () = - formatSourceString + formatAST false """ let formatter = // escape commas left in invalid entries sprintf "%i,\"%s\"" """ - { config with StrictMode = true } + config |> should equal """let formatter = sprintf "%i,\"%s\"" diff --git a/src/Fantomas.Core.Tests/SynConstTests.fs b/src/Fantomas.Core.Tests/SynConstTests.fs index 33bac8b930..f111fbd6a0 100644 --- a/src/Fantomas.Core.Tests/SynConstTests.fs +++ b/src/Fantomas.Core.Tests/SynConstTests.fs @@ -705,12 +705,12 @@ a:hover {color: #ecc;} [] let ``verbatim string in AST is preserved, 560`` () = - formatSourceString + formatAST false """ let s = @"\" """ - { config with StrictMode = true } + config |> prepend newline |> should equal diff --git a/src/Fantomas.Core.Tests/SynLongIdentTests.fs b/src/Fantomas.Core.Tests/SynLongIdentTests.fs index d4cfc3b519..7e0cb150fd 100644 --- a/src/Fantomas.Core.Tests/SynLongIdentTests.fs +++ b/src/Fantomas.Core.Tests/SynLongIdentTests.fs @@ -401,7 +401,6 @@ let ``backticks can be added from AST only scenarios`` () = tree, config = { config with - StrictMode = true InsertFinalNewline = false } ) |> Async.RunSynchronously diff --git a/src/Fantomas.Core.Tests/TestHelpers.fs b/src/Fantomas.Core.Tests/TestHelpers.fs index 562011c84c..4c65d65e2f 100644 --- a/src/Fantomas.Core.Tests/TestHelpers.fs +++ b/src/Fantomas.Core.Tests/TestHelpers.fs @@ -25,15 +25,24 @@ let private safeToIgnoreWarnings = let formatSourceString isFsiFile (s: string) config = async { - let! formatted = - if not config.StrictMode then - CodeFormatter.FormatDocumentAsync(isFsiFile, s, config) - else - let ast, _ = - Fantomas.FCS.Parse.parseFile isFsiFile (FSharp.Compiler.Text.SourceText.ofString s) [] + let! formatted = CodeFormatter.FormatDocumentAsync(isFsiFile, s, config) + let! isValid = CodeFormatter.IsValidFSharpCodeAsync(isFsiFile, formatted.Code) - CodeFormatter.FormatASTAsync(ast, config = config) + if not isValid then + failwithf $"The formatted result is not valid F# code or contains warnings\n%s{formatted.Code}" + return formatted.Code.Replace("\r\n", "\n") + } + + |> Async.RunSynchronously + +/// The `source` will first be parsed to AST. +let formatAST isFsiFile (source: string) config = + async { + let ast, _ = + Fantomas.FCS.Parse.parseFile isFsiFile (FSharp.Compiler.Text.SourceText.ofString source) [] + + let! formatted = CodeFormatter.FormatASTAsync(ast, config = config) let! isValid = CodeFormatter.IsValidFSharpCodeAsync(isFsiFile, formatted.Code) if not isValid then @@ -41,7 +50,6 @@ let formatSourceString isFsiFile (s: string) config = return formatted.Code.Replace("\r\n", "\n") } - |> Async.RunSynchronously let formatSourceStringWithDefines defines (s: string) config = @@ -83,11 +91,6 @@ let equal x = equal x let inline prepend s content = s + content - -let formatConfig = - { FormatConfig.Default with - StrictMode = true } - let (==) actual expected = Assert.AreEqual(expected, actual) let fail () = Assert.Fail() let pass () = Assert.Pass() diff --git a/src/Fantomas.Core.Tests/UnionTests.fs b/src/Fantomas.Core.Tests/UnionTests.fs index c3d2c99be5..9315d9e66d 100644 --- a/src/Fantomas.Core.Tests/UnionTests.fs +++ b/src/Fantomas.Core.Tests/UnionTests.fs @@ -203,7 +203,7 @@ let main argv = [] let ``enums conversion with strict mode`` () = - formatSourceString + formatAST false """ type uColor = @@ -211,7 +211,7 @@ type uColor = | Green = 1u | Blue = 2u let col3 = Microsoft.FSharp.Core.LanguagePrimitives.EnumOfValue(2u)""" - { config with StrictMode = true } + config |> prepend newline |> should equal diff --git a/src/Fantomas.Core/CodeFormatter.fs b/src/Fantomas.Core/CodeFormatter.fs index 39d58e7042..69d1648da6 100644 --- a/src/Fantomas.Core/CodeFormatter.fs +++ b/src/Fantomas.Core/CodeFormatter.fs @@ -66,14 +66,14 @@ type CodeFormatter = static member FormatOakAsync(oak: Oak) : Async = async { - let context = Context.Context.Create FormatConfig.Default + let context = Context.Context.Create false FormatConfig.Default let result = context |> CodePrinter.genFile oak |> Context.dump false return result.Code } static member FormatOakAsync(oak: Oak, config: FormatConfig) : Async = async { - let context = Context.Context.Create config + let context = Context.Context.Create false config let result = context |> CodePrinter.genFile oak |> Context.dump false return result.Code } diff --git a/src/Fantomas.Core/CodeFormatterImpl.fs b/src/Fantomas.Core/CodeFormatterImpl.fs index 1ddaf29829..85edd8cd41 100644 --- a/src/Fantomas.Core/CodeFormatterImpl.fs +++ b/src/Fantomas.Core/CodeFormatterImpl.fs @@ -57,7 +57,7 @@ let formatAST (config: FormatConfig) (cursor: pos option) : FormatResult = - let context = Context.Context.Create config + let context = Context.Context.Create sourceText.IsSome config let oak = match sourceText with diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 0e2eb0d0ed..1e9711bf94 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -1510,7 +1510,7 @@ let genExpr (e: Expr) = |> fun ctx -> { ctx with Config = currentConfig } |> atCurrentColumnIndent - onlyIfCtx (fun ctx -> ctx.Config.StrictMode) (!- "$\"") + onlyIfCtx (fun ctx -> not ctx.HasSource) (!- "$\"") +> col sepNone node.Parts (fun part -> match part with | Choice1Of2 stringNode -> genSingleTextNode stringNode @@ -1520,11 +1520,11 @@ let genExpr (e: Expr) = genInterpolatedFillExpr fillNode.Expr +> optSingle (fun format -> sepColonFixed +> genSingleTextNode format) fillNode.Ident - if ctx.Config.StrictMode then + if not ctx.HasSource then (!- "{" +> genFill +> !- "}") ctx else genFill ctx) - +> onlyIfCtx (fun ctx -> ctx.Config.StrictMode) (!- "\"") + +> onlyIfCtx (fun ctx -> not ctx.HasSource) (!- "\"") |> genNode node | Expr.IndexRangeWildcard node -> genSingleTextNode node | Expr.TripleNumberIndexRange node -> diff --git a/src/Fantomas.Core/Context.fs b/src/Fantomas.Core/Context.fs index 7513f90606..c7876b4907 100644 --- a/src/Fantomas.Core/Context.fs +++ b/src/Fantomas.Core/Context.fs @@ -181,6 +181,7 @@ module WriterEvents = [] type Context = { Config: FormatConfig + HasSource: bool WriterModel: WriterModel WriterEvents: Queue FormattedCursor: pos option } @@ -188,12 +189,15 @@ type Context = /// Initialize with a string writer and use space as delimiter static member Default = { Config = FormatConfig.Default + HasSource = false WriterModel = WriterModel.init WriterEvents = Queue.empty FormattedCursor = None } - static member Create config : Context = - { Context.Default with Config = config } + static member Create hasSource config : Context = + { Context.Default with + Config = config + HasSource = hasSource } member x.WithDummy(writerCommands, ?keepPageWidth) = let keepPageWidth = keepPageWidth |> Option.defaultValue false diff --git a/src/Fantomas.Core/Context.fsi b/src/Fantomas.Core/Context.fsi index 80e4d4921e..d54c6938c6 100644 --- a/src/Fantomas.Core/Context.fsi +++ b/src/Fantomas.Core/Context.fsi @@ -53,14 +53,19 @@ type WriterModel = [] type Context = - { Config: FormatConfig - WriterModel: WriterModel - WriterEvents: Queue - FormattedCursor: pos option } + { + Config: FormatConfig + /// Indicates the presence of source code. + /// This could be absent in the case we are formatting from AST. + HasSource: bool + WriterModel: WriterModel + WriterEvents: Queue + FormattedCursor: pos option + } /// Initialize with a string writer and use space as delimiter static member Default: Context - static member Create: config: FormatConfig -> Context + static member Create: hasSource: bool -> config: FormatConfig -> Context member WithDummy: writerCommands: Queue * ?keepPageWidth: bool -> Context member WithShortExpression: maxWidth: int * ?startColumn: int -> Context member Column: int diff --git a/src/Fantomas.Core/FormatConfig.fs b/src/Fantomas.Core/FormatConfig.fs index 10d8ce2cb5..e2abad7247 100644 --- a/src/Fantomas.Core/FormatConfig.fs +++ b/src/Fantomas.Core/FormatConfig.fs @@ -227,12 +227,7 @@ type FormatConfig = [] [] - ExperimentalElmish: bool - - [] - [] - [] - StrictMode: bool } + ExperimentalElmish: bool } member x.IsStroustrupStyle = x.MultilineBracketStyle = Stroustrup @@ -273,5 +268,4 @@ type FormatConfig = MultilineBracketStyle = Cramped KeepMaxNumberOfBlankLines = 100 NewlineBeforeMultilineComputationExpression = true - ExperimentalElmish = false - StrictMode = false } + ExperimentalElmish = false } diff --git a/src/Fantomas.Core/Selection.fs b/src/Fantomas.Core/Selection.fs index c94d03ee67..1faca42e7a 100644 --- a/src/Fantomas.Core/Selection.fs +++ b/src/Fantomas.Core/Selection.fs @@ -402,7 +402,7 @@ let formatSelection MaxLineLength = maxLineLength } let formattedSelection = - let context = Context.Context.Create selectionConfig + let context = Context.Context.Create true selectionConfig match tree with | TreeForSelection.Unsupported ->