Skip to content

Commit

Permalink
Restore correct indent after update record expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Sep 27, 2021
1 parent 2fdf476 commit d35c175
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
73 changes: 73 additions & 0 deletions src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,3 +1826,76 @@ let ``anonymous update record, indent_size 3`` () =
// meh
someValue |}
"""

[<Test>]
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
"""
9 changes: 5 additions & 4 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 0 additions & 9 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d35c175

Please sign in to comment.