From d35c175ecdae528dc98d0727b769a5956b04dce9 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 24 Sep 2021 21:58:33 +0200 Subject: [PATCH] Restore correct indent after update record expression. --- src/Fantomas.Tests/RecordTests.fs | 73 +++++++++++++++++++++++++++++++ src/Fantomas/CodePrinter.fs | 9 ++-- src/Fantomas/Context.fs | 9 ---- 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/src/Fantomas.Tests/RecordTests.fs b/src/Fantomas.Tests/RecordTests.fs index 1e3449c361..16b22e71b4 100644 --- a/src/Fantomas.Tests/RecordTests.fs +++ b/src/Fantomas.Tests/RecordTests.fs @@ -1826,3 +1826,76 @@ let ``anonymous update record, indent_size 3`` () = // meh someValue |} """ + +[] +let ``restore correct indent after update record expression`` () = + formatSourceString + false + """ +let parse (checker: FSharpChecker) (parsingOptions: FSharpParsingOptions) { FileName = fileName; Source = source } = + let allDefineOptions, defineHashTokens = TokenParser.getDefines source + + allDefineOptions + |> List.map (fun conditionalCompilationDefines -> + async { + let parsingOptionsWithDefines = + { parsingOptions with + ConditionalCompilationDefines = conditionalCompilationDefines + SourceFiles = Array.map safeFileName parsingOptions.SourceFiles } + // Run the first phase (untyped parsing) of the compiler + let sourceText = + FSharp.Compiler.Text.SourceText.ofString source + + let! untypedRes = checker.ParseFile(fileName, sourceText, parsingOptionsWithDefines) + + if untypedRes.ParseHadErrors then + let errors = + untypedRes.Diagnostics + |> Array.filter (fun e -> e.Severity = FSharpDiagnosticSeverity.Error) + + if not <| Array.isEmpty errors then + raise + <| FormatException( + sprintf "Parsing failed with errors: %A\nAnd options: %A" errors parsingOptionsWithDefines + ) + + return (untypedRes.ParseTree, conditionalCompilationDefines, defineHashTokens) + }) + |> Async.Parallel +""" + config + |> prepend newline + |> should + equal + """ +let parse (checker: FSharpChecker) (parsingOptions: FSharpParsingOptions) { FileName = fileName; Source = source } = + let allDefineOptions, defineHashTokens = TokenParser.getDefines source + + allDefineOptions + |> List.map (fun conditionalCompilationDefines -> + async { + let parsingOptionsWithDefines = + { parsingOptions with + ConditionalCompilationDefines = conditionalCompilationDefines + SourceFiles = Array.map safeFileName parsingOptions.SourceFiles } + // Run the first phase (untyped parsing) of the compiler + let sourceText = + FSharp.Compiler.Text.SourceText.ofString source + + let! untypedRes = checker.ParseFile(fileName, sourceText, parsingOptionsWithDefines) + + if untypedRes.ParseHadErrors then + let errors = + untypedRes.Diagnostics + |> Array.filter (fun e -> e.Severity = FSharpDiagnosticSeverity.Error) + + if not <| Array.isEmpty errors then + raise + <| FormatException( + sprintf "Parsing failed with errors: %A\nAnd options: %A" errors parsingOptionsWithDefines + ) + + return (untypedRes.ParseTree, conditionalCompilationDefines, defineHashTokens) + }) + |> Async.Parallel +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 80f568a7fb..58498cea13 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -2591,7 +2591,7 @@ and genExpr astContext synExpr ctx = genExpr astContext optExpr +> genSynStaticOptimizationConstraint astContext constraints +> sepEq - +> sepSpaceOrNlnIfExpressionExceedsPageWidth (genExpr astContext e) + +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e) | UnsupportedExpr r -> raise @@ -3009,10 +3009,11 @@ and genMultilineRecordInstance tokN synExpr.Range LBRACE sepOpenS +> genExpr astContext e +> !- " with" - +> ifIndentLesserThan 3 (indent +> indent) indent - +> sepSpaceOrNlnIfExpressionExceedsPageWidth fieldsExpr + +> ifIndentLesserThan + 3 + (sepSpaceOrDoubleIndentAndNlnIfExpressionExceedsPageWidth fieldsExpr) + (sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth fieldsExpr) +> tokN synExpr.Range RBRACE sepCloseS - +> ifIndentLesserThan 3 (unindent +> unindent) unindent expr ctx diff --git a/src/Fantomas/Context.fs b/src/Fantomas/Context.fs index cae67aac20..99bcf333c0 100644 --- a/src/Fantomas/Context.fs +++ b/src/Fantomas/Context.fs @@ -1018,15 +1018,6 @@ let internal sepSpaceWhenOrIndentAndNlnIfExpressionExceedsPageWidth (addSpace: C expr ctx -let internal sepSpaceOrNlnIfExpressionExceedsPageWidth expr (ctx: Context) = - expressionExceedsPageWidth - sepSpace - sepNone // before and after for short expressions - sepNln - unindent // before and after for long expressions - expr - ctx - let internal autoNlnIfExpressionExceedsPageWidth expr (ctx: Context) = expressionExceedsPageWidth sepNone