From ba7cb4cce9bb4fbc705b0215678ae8e1728745cc Mon Sep 17 00:00:00 2001 From: jindraivanek Date: Sat, 7 May 2022 13:23:33 +0200 Subject: [PATCH 1/8] new KeepMaxBlankLines config --- docs/Documentation.md | 26 +++++++++++++++++++++++++- src/Fantomas.Core/FormatConfig.fs | 5 +++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/Documentation.md b/docs/Documentation.md index 75dc8f4545..78d7b50c67 100644 --- a/docs/Documentation.md +++ b/docs/Documentation.md @@ -147,6 +147,7 @@ fsharp_disable_elmish_syntax=false fsharp_keep_indent_in_branch=false fsharp_blank_lines_around_nested_multiline_expressions=true fsharp_bar_before_discriminated_union_declaration=false +fsharp_keep_max_blank_lines=100 fsharp_strict_mode=false ``` @@ -1167,7 +1168,7 @@ let main argv = ### fsharp_blank_lines_around_nested_multiline_expressions Surround **nested** multi-line expressions with blank lines. -Existing blank lines are always preserved (via trivia). +Existing blank lines are always preserved (via trivia), with exception when [fsharp_keep_max_blank_lines](#fsharp_keep_max_blank_lines) is used. Top level expressions will always follow the [2020 blank lines revision](https://github.com/fsprojects/fantomas/blob/master/docs/FormattingConventions.md#2020-revision) principle. Default = true. @@ -1221,6 +1222,29 @@ type MyDU = Short of int type MyDU = | Short of int ``` +### fsharp_keep_max_blank_lines + +Set maximal number of consecutive blank lines to keep from original source. It doesn't change number of new blank lines generated by Fantomas. +Default=100 + +`defaultConfig` + +```fsharp +open Foo + + +let x = 42 +``` + +`{ defaultConfig with KeepMaxBlankLines = 1 }` + +```fsharp +open Foo + +let x = 42 +``` + + ### fsharp_strict_mode If being set, pretty printing is only done via ASTs. Compiler directives, inline comments and block comments will be ignored. diff --git a/src/Fantomas.Core/FormatConfig.fs b/src/Fantomas.Core/FormatConfig.fs index 4af8ae3652..2ed1fa5fb1 100644 --- a/src/Fantomas.Core/FormatConfig.fs +++ b/src/Fantomas.Core/FormatConfig.fs @@ -221,6 +221,10 @@ type FormatConfig = [] [] Ragnarok: bool + + [] + [] + KeepMaxBlankLines: Num [] [] @@ -268,4 +272,5 @@ type FormatConfig = BlankLinesAroundNestedMultilineExpressions = true BarBeforeDiscriminatedUnionDeclaration = false Ragnarok = false + KeepMaxBlankLines = 100 StrictMode = false } From cfb0321b90d2229fa961c4b08b0febc12d9b3b7d Mon Sep 17 00:00:00 2001 From: jindraivanek Date: Sat, 7 May 2022 13:24:35 +0200 Subject: [PATCH 2/8] KeepMaxBlankLines implementation --- src/Fantomas.Core.Tests/TriviaTests.fs | 5 +++-- src/Fantomas.Core/Context.fs | 2 +- src/Fantomas.Core/Trivia.fs | 16 +++++++++++----- src/Fantomas.Core/Utils.fs | 7 +++++++ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Fantomas.Core.Tests/TriviaTests.fs b/src/Fantomas.Core.Tests/TriviaTests.fs index a461d79e46..52b68cf416 100644 --- a/src/Fantomas.Core.Tests/TriviaTests.fs +++ b/src/Fantomas.Core.Tests/TriviaTests.fs @@ -5,11 +5,12 @@ open Fantomas.Core open Fantomas.Core.SourceParser open Fantomas.Core.Tests.TestHelper open Fantomas.Core.TriviaTypes +open Fantomas.Core.FormatConfig let private toTrivia source = let sourceText = CodeFormatterImpl.getSourceText source let ast, _ = Fantomas.FCS.Parse.parseFile false sourceText [] - Trivia.collectTrivia sourceText ast + Trivia.collectTrivia FormatConfig.Default sourceText ast let private toTriviaWithDefines source = let sourceText = CodeFormatterImpl.getSourceText source @@ -23,7 +24,7 @@ let private toTriviaWithDefines source = let defineCombinations = Defines.getDefineCombination hashDirectives defineCombinations - |> List.map (fun dc -> dc, Trivia.collectTrivia sourceText ast) + |> List.map (fun dc -> dc, Trivia.collectTrivia FormatConfig.Default sourceText ast) |> Map.ofList [] diff --git a/src/Fantomas.Core/Context.fs b/src/Fantomas.Core/Context.fs index 8440b5799b..4e7a569b64 100644 --- a/src/Fantomas.Core/Context.fs +++ b/src/Fantomas.Core/Context.fs @@ -200,7 +200,7 @@ type internal Context = static member Create config (source: ISourceText option) (defineCombination: DefineCombination) (ast: ParsedInput) = let trivia, sourceText = match source with - | Some source when not config.StrictMode -> Trivia.collectTrivia source ast, Some source + | Some source when not config.StrictMode -> Trivia.collectTrivia config source ast, Some source | _ -> [], None let triviaByNodes = diff --git a/src/Fantomas.Core/Trivia.fs b/src/Fantomas.Core/Trivia.fs index 1957459319..47a38542f6 100644 --- a/src/Fantomas.Core/Trivia.fs +++ b/src/Fantomas.Core/Trivia.fs @@ -8,6 +8,7 @@ open Fantomas.Core.ISourceTextExtensions open Fantomas.Core.SourceParser open Fantomas.Core.AstTransformer open Fantomas.Core.TriviaTypes +open Fantomas.Core.FormatConfig let inline isMainNodeFor nodeType (node: TriviaNodeAssigner) = nodeType = node.Type @@ -148,7 +149,7 @@ let private addAllTriviaAsContentAfter (trivia: Trivia list) (singleNode: Trivia ContentAfter = contentAfter } |> List.singleton -let private addTriviaToTriviaNode (startOfSourceCode: int) (triviaNodes: TriviaNodeAssigner list) trivia = +let private addTriviaToTriviaNode (config: FormatConfig) (startOfSourceCode: int) (triviaNodes: TriviaNodeAssigner list) trivia = match trivia with | { Item = Comment (LineCommentOnSingleLine _ as comment) Range = range } -> @@ -207,16 +208,21 @@ let private addTriviaToTriviaNode (startOfSourceCode: int) (triviaNodes: TriviaN // Newlines are only relevant if they occur after the first line of source code | { Item = Newline; Range = range } when (range.StartLine > startOfSourceCode) -> + let canAddNewline triviaContent = + let numberOfNewlinesAtEndOfContent triviaContent = + ResizeArray.revSeq triviaContent |> Seq.takeWhile ((=) Newline) |> Seq.length + numberOfNewlinesAtEndOfContent triviaContent < config.KeepMaxBlankLines + let nodeAfterLine = findFirstNodeAfterLine triviaNodes range.StartLine match nodeAfterLine with | Some _ -> nodeAfterLine - |> updateTriviaNode (fun tn -> tn.ContentBefore.Add(Newline)) triviaNodes + |> updateTriviaNode (fun tn -> if canAddNewline tn.ContentBefore then tn.ContentBefore.Add(Newline)) triviaNodes | None -> // try and find a node above findNodeBeforeLineFromStart triviaNodes range.StartLine - |> updateTriviaNode (fun tn -> tn.ContentAfter.Add(Newline)) triviaNodes + |> updateTriviaNode (fun tn -> if canAddNewline tn.ContentAfter then tn.ContentAfter.Add(Newline)) triviaNodes | { Item = Directive dc as directive Range = range } -> @@ -348,7 +354,7 @@ let private collectTriviaFromBlankLines 3. Merge trivias with triviaNodes 4. genTrivia should use ranges to identify what extra content should be added from what triviaNode *) -let collectTrivia (source: ISourceText) (ast: ParsedInput) : TriviaNode list = +let collectTrivia (config: FormatConfig) (source: ISourceText) (ast: ParsedInput) : TriviaNode list = let triviaNodesFromAST, directives, codeComments = match ast with | ParsedInput.ImplFile (ParsedImplFileInput (hds, mns, directives, codeComments)) -> @@ -377,5 +383,5 @@ let collectTrivia (source: ISourceText) (ast: ParsedInput) : TriviaNode list = match ast, triviaNodes with | EmptyFile _, h :: _ -> addAllTriviaAsContentAfter trivia h | _ -> - List.fold (addTriviaToTriviaNode startOfSourceCode) triviaNodes trivia + List.fold (addTriviaToTriviaNode config startOfSourceCode) triviaNodes trivia |> transformNonEmptyNodes diff --git a/src/Fantomas.Core/Utils.fs b/src/Fantomas.Core/Utils.fs index 65de659722..71e5842067 100644 --- a/src/Fantomas.Core/Utils.fs +++ b/src/Fantomas.Core/Utils.fs @@ -154,6 +154,13 @@ module List = go 0 [] xs +module ResizeArray = + let revSeq (xs: ResizeArray<_>) = + let count = xs.Count + seq { + for i = count - 1 downto 0 do yield xs[i] + } + module Map = let tryFindOrDefault (defaultValue: 'g) (key: 't) (map: Map<'t, 'g>) = match Map.tryFind key map with From 8c21e5d8b732051d94d433aa403685fe4e1d849d Mon Sep 17 00:00:00 2001 From: jindraivanek Date: Sat, 7 May 2022 13:25:23 +0200 Subject: [PATCH 3/8] tests --- .../Fantomas.Core.Tests.fsproj | 1 + .../KeepMaxEmptyLinesTests.fs | 98 +++++++++++++++++++ src/Fantomas.Core.Tests/TestHelpers.fs | 7 +- 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs diff --git a/src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj b/src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj index a19cb6e16f..dcf9f4b107 100644 --- a/src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj +++ b/src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj @@ -91,6 +91,7 @@ + diff --git a/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs b/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs new file mode 100644 index 0000000000..37ed691b15 --- /dev/null +++ b/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs @@ -0,0 +1,98 @@ +module Fantomas.Core.Tests.KeepMaxEmptyLines + +open NUnit.Framework +open FsUnit +open Fantomas.Core.Tests.TestHelper +open Fantomas.Core.FormatConfig + +let config x = { config with KeepMaxBlankLines = x } + +[] +let ``reduce 2 empty lines to 1`` () = + checkFormat (config 1) + """ +open Foo + + +let x = 42 +""" + """ +open Foo + +let x = 42 +""" + +[] +let ``reduce 3 empty lines to 2`` () = + checkFormat (config 2) + """ +open Foo + + + +let x = 42 +""" + """ +open Foo + + +let x = 42 +""" + +[] +let ``reduce 3 empty lines to 1`` () = + checkFormat (config 1) + """ +open Foo + + + +let x = 42 +""" + """ +open Foo + +let x = 42 +""" + + +[] +let ``only generated empty lines`` () = + checkFormat (config 0) + """ +open Foo + +open Goo + +module M1 = + + let x = 42 +module M2 = let y = 42 +""" + """ +open Foo +open Goo + +module M1 = + let x = 42 + +module M2 = + let y = 42 +""" + +[] +let ``dont reduce empty lines in string`` () = + checkFormat (config 1) + " +let x = \"\"\" + + +\"\"\" +" + " +let x = + \"\"\" + + +\"\"\" +" diff --git a/src/Fantomas.Core.Tests/TestHelpers.fs b/src/Fantomas.Core.Tests/TestHelpers.fs index b245177224..8a4bf99a16 100644 --- a/src/Fantomas.Core.Tests/TestHelpers.fs +++ b/src/Fantomas.Core.Tests/TestHelpers.fs @@ -133,10 +133,13 @@ let tryFormatAST ast sourceCode config = let formatConfig = { FormatConfig.Default with StrictMode = true } -let shouldNotChangeAfterFormat source = +let checkFormat config source expected = formatSourceString false source config |> prepend newline - |> should equal source + |> should equal expected + +let shouldNotChangeAfterFormat source = + checkFormat config source source let (==) actual expected = Assert.AreEqual(expected, actual) let fail () = Assert.Fail() From d2ec00fd4f45ef802dcbdc1965936b6df4aeb245 Mon Sep 17 00:00:00 2001 From: jindraivanek Date: Sat, 7 May 2022 14:53:40 +0200 Subject: [PATCH 4/8] format --- .../KeepMaxEmptyLinesTests.fs | 15 +++++++---- src/Fantomas.Core.Tests/TestHelpers.fs | 5 ++-- src/Fantomas.Core/FormatConfig.fs | 2 +- src/Fantomas.Core/Trivia.fs | 26 +++++++++++++++---- src/Fantomas.Core/Utils.fs | 4 ++- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs b/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs index 37ed691b15..30424fadbb 100644 --- a/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs +++ b/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs @@ -9,7 +9,8 @@ let config x = { config with KeepMaxBlankLines = x } [] let ``reduce 2 empty lines to 1`` () = - checkFormat (config 1) + checkFormat + (config 1) """ open Foo @@ -24,7 +25,8 @@ let x = 42 [] let ``reduce 3 empty lines to 2`` () = - checkFormat (config 2) + checkFormat + (config 2) """ open Foo @@ -41,7 +43,8 @@ let x = 42 [] let ``reduce 3 empty lines to 1`` () = - checkFormat (config 1) + checkFormat + (config 1) """ open Foo @@ -58,7 +61,8 @@ let x = 42 [] let ``only generated empty lines`` () = - checkFormat (config 0) + checkFormat + (config 0) """ open Foo @@ -82,7 +86,8 @@ module M2 = [] let ``dont reduce empty lines in string`` () = - checkFormat (config 1) + checkFormat + (config 1) " let x = \"\"\" diff --git a/src/Fantomas.Core.Tests/TestHelpers.fs b/src/Fantomas.Core.Tests/TestHelpers.fs index 8a4bf99a16..526bb7f12e 100644 --- a/src/Fantomas.Core.Tests/TestHelpers.fs +++ b/src/Fantomas.Core.Tests/TestHelpers.fs @@ -137,9 +137,8 @@ let checkFormat config source expected = formatSourceString false source config |> prepend newline |> should equal expected - -let shouldNotChangeAfterFormat source = - checkFormat config source source + +let shouldNotChangeAfterFormat source = checkFormat config source source let (==) actual expected = Assert.AreEqual(expected, actual) let fail () = Assert.Fail() diff --git a/src/Fantomas.Core/FormatConfig.fs b/src/Fantomas.Core/FormatConfig.fs index 2ed1fa5fb1..da5cb9a46f 100644 --- a/src/Fantomas.Core/FormatConfig.fs +++ b/src/Fantomas.Core/FormatConfig.fs @@ -221,7 +221,7 @@ type FormatConfig = [] [] Ragnarok: bool - + [] [] KeepMaxBlankLines: Num diff --git a/src/Fantomas.Core/Trivia.fs b/src/Fantomas.Core/Trivia.fs index 47a38542f6..339e11bd9d 100644 --- a/src/Fantomas.Core/Trivia.fs +++ b/src/Fantomas.Core/Trivia.fs @@ -149,7 +149,12 @@ let private addAllTriviaAsContentAfter (trivia: Trivia list) (singleNode: Trivia ContentAfter = contentAfter } |> List.singleton -let private addTriviaToTriviaNode (config: FormatConfig) (startOfSourceCode: int) (triviaNodes: TriviaNodeAssigner list) trivia = +let private addTriviaToTriviaNode + (config: FormatConfig) + (startOfSourceCode: int) + (triviaNodes: TriviaNodeAssigner list) + trivia + = match trivia with | { Item = Comment (LineCommentOnSingleLine _ as comment) Range = range } -> @@ -210,19 +215,30 @@ let private addTriviaToTriviaNode (config: FormatConfig) (startOfSourceCode: int | { Item = Newline; Range = range } when (range.StartLine > startOfSourceCode) -> let canAddNewline triviaContent = let numberOfNewlinesAtEndOfContent triviaContent = - ResizeArray.revSeq triviaContent |> Seq.takeWhile ((=) Newline) |> Seq.length + ResizeArray.revSeq triviaContent + |> Seq.takeWhile ((=) Newline) + |> Seq.length + numberOfNewlinesAtEndOfContent triviaContent < config.KeepMaxBlankLines - + let nodeAfterLine = findFirstNodeAfterLine triviaNodes range.StartLine match nodeAfterLine with | Some _ -> nodeAfterLine - |> updateTriviaNode (fun tn -> if canAddNewline tn.ContentBefore then tn.ContentBefore.Add(Newline)) triviaNodes + |> updateTriviaNode + (fun tn -> + if canAddNewline tn.ContentBefore then + tn.ContentBefore.Add(Newline)) + triviaNodes | None -> // try and find a node above findNodeBeforeLineFromStart triviaNodes range.StartLine - |> updateTriviaNode (fun tn -> if canAddNewline tn.ContentAfter then tn.ContentAfter.Add(Newline)) triviaNodes + |> updateTriviaNode + (fun tn -> + if canAddNewline tn.ContentAfter then + tn.ContentAfter.Add(Newline)) + triviaNodes | { Item = Directive dc as directive Range = range } -> diff --git a/src/Fantomas.Core/Utils.fs b/src/Fantomas.Core/Utils.fs index 71e5842067..e90e17f9b7 100644 --- a/src/Fantomas.Core/Utils.fs +++ b/src/Fantomas.Core/Utils.fs @@ -157,8 +157,10 @@ module List = module ResizeArray = let revSeq (xs: ResizeArray<_>) = let count = xs.Count + seq { - for i = count - 1 downto 0 do yield xs[i] + for i = count - 1 downto 0 do + yield xs[i] } module Map = From 31b4a3d029d5d0c6366d181e9cb5b7401e2efdce Mon Sep 17 00:00:00 2001 From: jindraivanek Date: Wed, 11 May 2022 21:43:19 +0200 Subject: [PATCH 5/8] implement in collectTriviaFromBlankLines --- src/Fantomas.Core/Trivia.fs | 45 ++++++++++++------------------------- src/Fantomas.Core/Utils.fs | 18 +++++++-------- 2 files changed, 23 insertions(+), 40 deletions(-) diff --git a/src/Fantomas.Core/Trivia.fs b/src/Fantomas.Core/Trivia.fs index 97a16066ab..8ecad6bc73 100644 --- a/src/Fantomas.Core/Trivia.fs +++ b/src/Fantomas.Core/Trivia.fs @@ -149,12 +149,7 @@ let private addAllTriviaAsContentAfter (trivia: Trivia list) (singleNode: Trivia ContentAfter = contentAfter } |> List.singleton -let private addTriviaToTriviaNode - (config: FormatConfig) - (startOfSourceCode: int) - (triviaNodes: TriviaNodeAssigner list) - trivia - = +let private addTriviaToTriviaNode (startOfSourceCode: int) (triviaNodes: TriviaNodeAssigner list) trivia = match trivia with | { Item = Comment (LineCommentOnSingleLine _ as comment) Range = range } -> @@ -213,32 +208,16 @@ let private addTriviaToTriviaNode // Newlines are only relevant if they occur after the first line of source code | { Item = Newline; Range = range } when (range.StartLine > startOfSourceCode) -> - let canAddNewline triviaContent = - let numberOfNewlinesAtEndOfContent triviaContent = - ResizeArray.revSeq triviaContent - |> Seq.takeWhile ((=) Newline) - |> Seq.length - - numberOfNewlinesAtEndOfContent triviaContent < config.KeepMaxBlankLines - let nodeAfterLine = findFirstNodeAfterLine triviaNodes range.StartLine match nodeAfterLine with | Some _ -> nodeAfterLine - |> updateTriviaNode - (fun tn -> - if canAddNewline tn.ContentBefore then - tn.ContentBefore.Add(Newline)) - triviaNodes + |> updateTriviaNode (fun tn -> tn.ContentBefore.Add(Newline)) triviaNodes | None -> // try and find a node above findNodeBeforeLineFromStart triviaNodes range.StartLine - |> updateTriviaNode - (fun tn -> - if canAddNewline tn.ContentAfter then - tn.ContentAfter.Add(Newline)) - triviaNodes + |> updateTriviaNode (fun tn -> tn.ContentAfter.Add(Newline)) triviaNodes | { Item = Directive dc as directive Range = range } -> @@ -311,6 +290,7 @@ let internal collectTriviaFromCodeComments (source: ISourceText) (codeComments: { Item = item; Range = r }) let internal collectTriviaFromBlankLines + (config: FormatConfig) (source: ISourceText) (triviaNodes: TriviaNodeAssigner list) (codeComments: CommentTrivia list) @@ -348,21 +328,24 @@ let internal collectTriviaFromBlankLines let max = source.GetLineCount() - 1 - [ 0..max ] - |> List.choose (fun idx -> + (0, [ 0..max ]) + ||> List.chooseState (fun count idx -> if ignoreLines.Contains(idx + 1) then - None + 0, None else let line = source.GetLineString(idx) if String.isNotNullOrWhitespace line then - None + 0, None else let range = let p = Position.mkPos (idx + 1) 0 Range.mkFileIndexRange fileIndex p p - Some { Item = Newline; Range = range }) + if count < config.KeepMaxBlankLines then + (count + 1), Some { Item = Newline; Range = range } + else + count, None) (* 1. Collect TriviaNode from tokens and AST @@ -385,7 +368,7 @@ let collectTrivia (config: FormatConfig) (source: ISourceText) (ast: ParsedInput let trivia = [ yield! collectTriviaFromDirectives source directives yield! collectTriviaFromCodeComments source codeComments - yield! collectTriviaFromBlankLines source triviaNodes codeComments ] + yield! collectTriviaFromBlankLines config source triviaNodes codeComments ] |> List.sortBy (fun n -> n.Range.Start.Line, n.Range.Start.Column) let startOfSourceCode = 1 @@ -399,5 +382,5 @@ let collectTrivia (config: FormatConfig) (source: ISourceText) (ast: ParsedInput match ast, triviaNodes with | EmptyFile _, h :: _ -> addAllTriviaAsContentAfter trivia h | _ -> - List.fold (addTriviaToTriviaNode config startOfSourceCode) triviaNodes trivia + List.fold (addTriviaToTriviaNode startOfSourceCode) triviaNodes trivia |> transformNonEmptyNodes diff --git a/src/Fantomas.Core/Utils.fs b/src/Fantomas.Core/Utils.fs index 386f0c0cb4..01cf431fcf 100644 --- a/src/Fantomas.Core/Utils.fs +++ b/src/Fantomas.Core/Utils.fs @@ -135,6 +135,15 @@ module List = s <- s' r) + let chooseState f state l = + let mutable s = state + + l + |> List.choose (fun x -> + let s', r = f s x + s <- s' + r) + let isNotEmpty l = (List.isEmpty >> not) l let moreThanOne = @@ -154,15 +163,6 @@ module List = go 0 [] xs -module ResizeArray = - let revSeq (xs: ResizeArray<_>) = - let count = xs.Count - - seq { - for i = count - 1 downto 0 do - yield xs[i] - } - module Map = let tryFindOrDefault (defaultValue: 'g) (key: 't) (map: Map<'t, 'g>) = match Map.tryFind key map with From 84eb80e69143235fb2962cd3c0b5e63b790aa38a Mon Sep 17 00:00:00 2001 From: jindraivanek Date: Wed, 11 May 2022 21:49:40 +0200 Subject: [PATCH 6/8] rename option to KeepMaxNumberOfBlankLines --- docs/Documentation.md | 8 ++++---- src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs | 2 +- src/Fantomas.Core/FormatConfig.fs | 4 ++-- src/Fantomas.Core/Trivia.fs | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/Documentation.md b/docs/Documentation.md index 78d7b50c67..ddfa51aad4 100644 --- a/docs/Documentation.md +++ b/docs/Documentation.md @@ -147,7 +147,7 @@ fsharp_disable_elmish_syntax=false fsharp_keep_indent_in_branch=false fsharp_blank_lines_around_nested_multiline_expressions=true fsharp_bar_before_discriminated_union_declaration=false -fsharp_keep_max_blank_lines=100 +fsharp_keep_max_number_of_blank_lines=100 fsharp_strict_mode=false ``` @@ -1168,7 +1168,7 @@ let main argv = ### fsharp_blank_lines_around_nested_multiline_expressions Surround **nested** multi-line expressions with blank lines. -Existing blank lines are always preserved (via trivia), with exception when [fsharp_keep_max_blank_lines](#fsharp_keep_max_blank_lines) is used. +Existing blank lines are always preserved (via trivia), with exception when [fsharp_keep_max_number_of_blank_lines](#fsharp_keep_max_number_of_blank_lines) is used. Top level expressions will always follow the [2020 blank lines revision](https://github.com/fsprojects/fantomas/blob/master/docs/FormattingConventions.md#2020-revision) principle. Default = true. @@ -1222,7 +1222,7 @@ type MyDU = Short of int type MyDU = | Short of int ``` -### fsharp_keep_max_blank_lines +### fsharp_keep_max_number_of_blank_lines Set maximal number of consecutive blank lines to keep from original source. It doesn't change number of new blank lines generated by Fantomas. Default=100 @@ -1236,7 +1236,7 @@ open Foo let x = 42 ``` -`{ defaultConfig with KeepMaxBlankLines = 1 }` +`{ defaultConfig with KeepMaxNumberOfBlankLines = 1 }` ```fsharp open Foo diff --git a/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs b/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs index 30424fadbb..087e90f585 100644 --- a/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs +++ b/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs @@ -5,7 +5,7 @@ open FsUnit open Fantomas.Core.Tests.TestHelper open Fantomas.Core.FormatConfig -let config x = { config with KeepMaxBlankLines = x } +let config x = { config with KeepMaxNumberOfBlankLines = x } [] let ``reduce 2 empty lines to 1`` () = diff --git a/src/Fantomas.Core/FormatConfig.fs b/src/Fantomas.Core/FormatConfig.fs index da5cb9a46f..2e96cdfdbd 100644 --- a/src/Fantomas.Core/FormatConfig.fs +++ b/src/Fantomas.Core/FormatConfig.fs @@ -224,7 +224,7 @@ type FormatConfig = [] [] - KeepMaxBlankLines: Num + KeepMaxNumberOfBlankLines: Num [] [] @@ -272,5 +272,5 @@ type FormatConfig = BlankLinesAroundNestedMultilineExpressions = true BarBeforeDiscriminatedUnionDeclaration = false Ragnarok = false - KeepMaxBlankLines = 100 + KeepMaxNumberOfBlankLines = 100 StrictMode = false } diff --git a/src/Fantomas.Core/Trivia.fs b/src/Fantomas.Core/Trivia.fs index 8ecad6bc73..d0092f7f9c 100644 --- a/src/Fantomas.Core/Trivia.fs +++ b/src/Fantomas.Core/Trivia.fs @@ -342,7 +342,7 @@ let internal collectTriviaFromBlankLines let p = Position.mkPos (idx + 1) 0 Range.mkFileIndexRange fileIndex p p - if count < config.KeepMaxBlankLines then + if count < config.KeepMaxNumberOfBlankLines then (count + 1), Some { Item = Newline; Range = range } else count, None) From 4156fc1336dad4878ce7b4ffbfc568bce744693a Mon Sep 17 00:00:00 2001 From: jindraivanek Date: Wed, 11 May 2022 22:04:31 +0200 Subject: [PATCH 7/8] move checkFormat to KeepMaxEmptyLinesTests --- src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs | 7 ++++++- src/Fantomas.Core.Tests/TestHelpers.fs | 6 ++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs b/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs index 087e90f585..f046972888 100644 --- a/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs +++ b/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs @@ -1,10 +1,15 @@ -module Fantomas.Core.Tests.KeepMaxEmptyLines +module Fantomas.Core.Tests.KeepMaxEmptyLinesTests open NUnit.Framework open FsUnit open Fantomas.Core.Tests.TestHelper open Fantomas.Core.FormatConfig +let checkFormat config source expected = + formatSourceString false source config + |> prepend newline + |> should equal expected + let config x = { config with KeepMaxNumberOfBlankLines = x } [] diff --git a/src/Fantomas.Core.Tests/TestHelpers.fs b/src/Fantomas.Core.Tests/TestHelpers.fs index a73d429272..daeffc3f57 100644 --- a/src/Fantomas.Core.Tests/TestHelpers.fs +++ b/src/Fantomas.Core.Tests/TestHelpers.fs @@ -133,12 +133,10 @@ let tryFormatAST ast sourceCode config = let formatConfig = { FormatConfig.Default with StrictMode = true } -let checkFormat config source expected = +let shouldNotChangeAfterFormat source = formatSourceString false source config |> prepend newline - |> should equal expected - -let shouldNotChangeAfterFormat source = checkFormat config source source + |> should equal source let (==) actual expected = Assert.AreEqual(expected, actual) let fail () = Assert.Fail() From fd3468356be368fa40485e1e6baa1f47d118633d Mon Sep 17 00:00:00 2001 From: jindraivanek Date: Wed, 11 May 2022 22:05:44 +0200 Subject: [PATCH 8/8] format --- src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs b/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs index f046972888..eb64fd1558 100644 --- a/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs +++ b/src/Fantomas.Core.Tests/KeepMaxEmptyLinesTests.fs @@ -10,7 +10,8 @@ let checkFormat config source expected = |> prepend newline |> should equal expected -let config x = { config with KeepMaxNumberOfBlankLines = x } +let config x = + { config with KeepMaxNumberOfBlankLines = x } [] let ``reduce 2 empty lines to 1`` () =