Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"rollForward": false
},
"fantomas": {
"version": "6.3.9",
"version": "7.0.1",
"commands": [
"fantomas"
],
Expand Down
8 changes: 2 additions & 6 deletions docs/docs/end-users/Configuration.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,7 @@ Insert a newline before a computation expression that spans multiple lines

(*** hide ***)
printfn
$"# Default\n{toEditorConfigName (nameof FormatConfig.Default.NewlineBeforeMultilineComputationExpression)} = {FormatConfig.Default.NewlineBeforeMultilineComputationExpression
.ToString()
.ToLower()}"
$"# Default\n{toEditorConfigName (nameof FormatConfig.Default.NewlineBeforeMultilineComputationExpression)} = {FormatConfig.Default.NewlineBeforeMultilineComputationExpression.ToString().ToLower()}"
(*** include-output ***)

formatCode
Expand Down Expand Up @@ -1046,9 +1044,7 @@ Top level expressions will always follow the [2020 blank lines revision](https:/

(*** hide ***)
printfn
$"# Default\n{toEditorConfigName (nameof FormatConfig.Default.BlankLinesAroundNestedMultilineExpressions)} = {FormatConfig.Default.BlankLinesAroundNestedMultilineExpressions
.ToString()
.ToLower()}"
$"# Default\n{toEditorConfigName (nameof FormatConfig.Default.BlankLinesAroundNestedMultilineExpressions)} = {FormatConfig.Default.BlankLinesAroundNestedMultilineExpressions.ToString().ToLower()}"
(*** include-output ***)

formatCode
Expand Down
42 changes: 21 additions & 21 deletions src/Fantomas.Core.Tests/CodePrinterHelperFunctionsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ let ``+> will compose two functions`` () =
let ``partially application when composing function`` () =
// We can write the previous example in a more concise way.
// Because of partial application in F#: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/functions/#partial-application-of-arguments
let f = !- "f" // signature of f remains the same: Context -> Context
let g = !- " and g" // signature of g remains the same: Context -> Context
let f = !-"f" // signature of f remains the same: Context -> Context
let g = !-" and g" // signature of g remains the same: Context -> Context
let h = f +> g // signature of h remains the same: Context -> Context

// Conceptually, you should really read `+>` as and then.
Expand Down Expand Up @@ -88,7 +88,7 @@ let ``the Context module has a lot of helper functions`` () =

[<Test>]
let ``some helper function are clever like sepSpace`` () =
let f = !- "a" +> sepSpace +> sepSpace +> !- "b"
let f = !-"a" +> sepSpace +> sepSpace +> !-"b"

let contextBefore: Context = Context.Default
let contextAfter: Context = f contextBefore
Expand All @@ -102,7 +102,7 @@ let ``some helper function are clever like sepSpace`` () =

[<Test>]
let ``other helper function respect configuration settings`` () =
let f = !- "a" +> sepColon +> !- "b"
let f = !-"a" +> sepColon +> !-"b"
let defaultConfig: Context = Context.Default
// The `FormatConfig` is present in the `Context`.
let configWithSpaceBeforeTrue =
Expand All @@ -125,7 +125,7 @@ let ``traversing collections`` () =
let items = [ 2; 3; 4 ]
// The `col` function will traverse the collection and apply the first function between elements and the last function for each individual element.
let f (items: int seq) : Context -> Context =
col (!- " + ") items (fun (item: int) -> !- $"%i{item}")
col (!-" + ") items (fun (item: int) -> !- $"%i{item}")

// Note that there are some variants of `col` that can be used to process a collection in a different way.
// coli, colEx, ...
Expand All @@ -141,7 +141,7 @@ let ``newlines and indentation`` () =
// Indentation only kick in on the next line, this is something to be aware of.
// `sepNln` is a helper function that will add a newline.

let f = !- "first line" +> sepNln +> indent +> !- "second line"
let f = !-"first line" +> sepNln +> indent +> !-"second line"
// The dump function will respect the newline from the configuration.
// For this test we will set it to `EndOfLineStyle.LF`
let ctx =
Expand All @@ -154,14 +154,14 @@ let ``newlines and indentation`` () =
Assert.AreEqual("first line\nsecond line", code)
// There is no indentation because that would only kick in after the second line.

let g = !- "first line" +> indent +> sepNln +> !- "second line" +> unindent
let g = !-"first line" +> indent +> sepNln +> !-"second line" +> unindent
let indentedCode = g ctx |> dump
Assert.AreEqual("first line\n second line", indentedCode)

// Using `indent` typically goes together with and `unindent` call.
// This is a very common pattern in CodePrinter, so the use of `indentSepNlnUnindent` is encouraged.
// Forgetting to `unindent` can be a nasty bug in Fantomas.
let h = !- "first line" +> indentSepNlnUnindent (!- "second line")
let h = !-"first line" +> indentSepNlnUnindent (!-"second line")
let indentedCtx = h ctx
let indentedCode = dump indentedCtx
Assert.AreEqual("first line\n second line", indentedCode)
Expand All @@ -176,8 +176,8 @@ let ``newlines and indentation`` () =
let ``trying multiple code paths`` () =
// Sometimes we want to try and fit everything in a single line.
// And have a fallback behavior when that is not possible.
let short = !- "This fits on a single line"
let long = !- "This fits on" +> sepNln +> !- "two lines"
let short = !-"This fits on a single line"
let long = !-"This fits on" +> sepNln +> !-"two lines"
// `expressionFitsOnRestOfLine` will try the first expression and if it doesn't fit, it will try the second expression.
// All the events of the first expression will be remove from the context when it needs to fallback to the second expression.
let f = expressionFitsOnRestOfLine short long
Expand Down Expand Up @@ -242,7 +242,7 @@ let a =
let genExpr (expr: Expr) =
match expr with
| Expr.Ident identNode -> !-identNode.Text
| _ -> !- "error"
| _ -> !-"error"

let f (genExpr: Expr -> Context -> Context) (tree: Oak) : Context -> Context =
match tree.ModulesOrNamespaces.[0].Declarations.[0] with
Expand All @@ -254,8 +254,8 @@ let a =
| Choice1Of2 functionNameNode ->
match functionNameNode.Content with
| [ IdentifierOrDot.Ident node ] -> !-node.Text
| _ -> !- "error"
| Choice2Of2 _ -> !- "error"
| _ -> !-"error"
| Choice2Of2 _ -> !-"error"

let genEq = !-bindingNode.Equals.Text

Expand All @@ -267,7 +267,7 @@ let a =
// Try to add a space and print the expression.
// If the expression is multiline add indent, newline, print the expression and unindent.
+> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr bindingNode.Expr)
| _ -> !- "error"
| _ -> !-"error"

let ctx =
{ Context.Default with
Expand Down Expand Up @@ -302,10 +302,10 @@ let a =
// If found we check the content and try to print the comment text followed by a newline
match triviaNode.Content with
| CommentOnSingleLine comment -> !-comment +> sepNln
| _ -> !- "error"
| _ -> !-"error"

firstComment +> !-identNode.Text
| _ -> !- "error"
| _ -> !-"error"

let codeWithTriviaPrinting = f genExprWithTrivia tree ctx |> dump
Assert.AreEqual("let a =\n // code comment\n b", codeWithTriviaPrinting)
Expand Down Expand Up @@ -379,7 +379,7 @@ let b = 2

// print trivia before BindingNode
enterNode node
+> !- "let"
+> !-"let"
+> sepSpace
+> !-name.Text
+> sepEq
Expand All @@ -393,7 +393,7 @@ let b = 2
+> sepNln
+> genBinding b

| _ -> !- "error"
| _ -> !-"error"

let ctx =
{ Context.Default with
Expand Down Expand Up @@ -438,7 +438,7 @@ let b = 2

// print trivia before BindingNode
enterNode node
+> !- "let"
+> !-"let"
+> sepSpace
+> !-name.Text
+> sepEq
Expand All @@ -456,7 +456,7 @@ let b = 2
+> sepNlnUnlessBindingHasTrivia b
+> genBinding b

| _ -> !- "error"
| _ -> !-"error"

let finalCode = g tree ctx |> dump
Assert.AreEqual("let a = 1\n\nlet b = 2", finalCode)
Expand All @@ -467,7 +467,7 @@ let ``locking the indentation at a fixed column`` () =
// This is typically to produce valid F# due to the offset rules.
let f =
sepOpenT
+> atCurrentColumn (!- "first line" +> sepNln +> !- "second line")
+> atCurrentColumn (!-"first line" +> sepNln +> !-"second line")
+> sepCloseT

let ctxBefore =
Expand Down
18 changes: 9 additions & 9 deletions src/Fantomas.Core.Tests/ContextTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ let private dump ctx = (dump false ctx).Code

[<Test>]
let ``sepSpace should not add an additional space if the line ends with a space`` () =
let expr = !- "let a = " +> sepSpace
let expr = !-"let a = " +> sepSpace
let result = dump (expr Context.Default)
result |> should equal "let a ="

[<Test>]
let ``sepColon should not add a space when nothing proceeds it`` () =
let expr =
!- "let add a b"
!-"let add a b"
+> indent
+> sepNln
+> sepColon
+> !- "int ="
+> !-"int ="
+> indent
+> sepNln
+> !- "a + b"
+> !-"a + b"
+> unindent
+> unindent
+> sepNln
Expand All @@ -49,7 +49,7 @@ let add a b

[<Test>]
let ``sepColon should not add a space when space proceeds it`` () =
let expr = !- "let a " +> sepNone +> sepColon
let expr = !-"let a " +> sepNone +> sepColon

let config =
{ FormatConfig.Default with
Expand Down Expand Up @@ -87,15 +87,15 @@ Long comment
[<Test>]
let ``nested exceedsMultiline expression should bubble up to parent check`` () =
let expression =
!- "let a ="
!-"let a ="
+> autoIndentAndNlnIfExpressionExceedsPageWidth (
expressionFitsOnRestOfLine
// short expression, should cross the max page width of 50
(!- "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +> !- "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")
(!-"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +> !-"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")
// fallback expression
(!- "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
(!-"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
+> sepNln
+> !- "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
+> !-"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
+> sepNln)
)

Expand Down
Loading
Loading