Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor with defines #2774

Merged
merged 7 commits into from
Mar 19, 2023
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
11 changes: 11 additions & 0 deletions src/Fantomas.Core.Tests/CursorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ let a =
"""
(3, 7)
|> assertCursor (1, 11)

[<Test>]
let ``cursor inside a node between defines`` () =
formatWithCursor
"""
#if FOO
()
#endif
"""
(3, 4)
|> assertCursor (2, 0)
2 changes: 1 addition & 1 deletion src/Fantomas.Core.Tests/DefinesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let private getDefines (v: string) =
| ParsedInput.SigFile(ParsedSigFileInput(trivia = { ConditionalDirectives = directives })) -> directives

getDefineCombination hashDirectives
|> List.collect id
|> List.collect (fun (DefineCombination(defines)) -> defines)
|> List.distinct
|> List.sort

Expand Down
1 change: 1 addition & 0 deletions src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
<Compile Include="ChainTests.fs" />
<Compile Include="TryWithTests.fs" />
<Compile Include="CursorTests.fs" />
<Compile Include="MultipleDefineCombinationsTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas.Core\Fantomas.Core.fsproj" />
Expand Down
194 changes: 194 additions & 0 deletions src/Fantomas.Core.Tests/MultipleDefineCombinationsTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
module Fantomas.Core.Tests.MultipleDefineCombinationsTests

open NUnit.Framework
open Fantomas.Core
open Fantomas.Core.Tests.TestHelper

let private mergeAndCompare (aDefines, aCode) (bDefines, bCode) expected =
let result =
MultipleDefineCombinations.mergeMultipleFormatResults
{ config with
EndOfLine = EndOfLineStyle.LF }
[ DefineCombination(aDefines),
{ Code = String.normalizeNewLine aCode
Cursor = None }
DefineCombination(bDefines),
{ Code = String.normalizeNewLine bCode
Cursor = None } ]

let normalizedExpected = String.normalizeNewLine expected
normalizedExpected == result.Code

[<Test>]
let ``merging of source code that starts with a hash`` () =
let a =
"""#if NOT_DEFINED
printfn \"meh\"
#else

#endif
"""

let b =
"""#if NOT_DEFINED

#else
printfn \"foo\"
#endif
"""

"""#if NOT_DEFINED
printfn \"meh\"
#else
printfn \"foo\"
#endif
"""
|> mergeAndCompare ([], a) ([ "NOT_DEFINED" ], b)

[<Test>]
let ``merging of defines content work when source code starts with a newline`` () =
let a =
"""
[<Literal>]
let private assemblyConfig() =
#if TRACE

#else
let x = "x"
#endif
x
"""

let b =
"""
[<Literal>]
let private assemblyConfig() =
#if TRACE
let x = ""
#else

#endif
x
"""

"""
[<Literal>]
let private assemblyConfig() =
#if TRACE
let x = ""
#else
let x = "x"
#endif
x
"""
|> mergeAndCompare ([], a) ([ "TRACE" ], b)

[<Test>]
let ``only split on control structure keyword`` () =
let a =
"""
#if INTERACTIVE
#else
#load "../FSharpx.TypeProviders/SetupTesting.fsx"

SetupTesting.generateSetupScript __SOURCE_DIRECTORY__

#load "__setup__.fsx"
#endif
"""

let b =
"""
#if INTERACTIVE
#else



#endif
"""

"""
#if INTERACTIVE
#else
#load "../FSharpx.TypeProviders/SetupTesting.fsx"

SetupTesting.generateSetupScript __SOURCE_DIRECTORY__

#load "__setup__.fsx"
#endif
"""
|> mergeAndCompare ([], a) ([ "INTERACTIVE" ], b)

// This test illustrates the goal of MultipleDefineCombinations
// All three results will be merged in one go.
[<Test>]
let ``triple merge`` () =
let result =
MultipleDefineCombinations.mergeMultipleFormatResults
{ config with
EndOfLine = EndOfLineStyle.LF }
[ DefineCombination([]),
{ Code =
String.normalizeNewLine
"""
let v =
#if A

#else
#if B

#else
'C'
#endif
#endif
"""
Cursor = None }
DefineCombination([ "A" ]),
{ Code =
String.normalizeNewLine
"""
let v =
#if A
'A'
#else
#if B

#else

#endif
#endif
"""
Cursor = None }
DefineCombination([ "B" ]),
{ Code =
String.normalizeNewLine
"""
let v =
#if A

#else
#if B
'B'
#else

#endif
#endif
"""
Cursor = None } ]

let expected =
String.normalizeNewLine
"""
let v =
#if A
'A'
#else
#if B
'B'
#else
'C'
#endif
#endif
"""

expected == result.Code
13 changes: 6 additions & 7 deletions src/Fantomas.Core.Tests/TestHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,21 @@ let formatSourceStringWithDefines defines (s: string) config =
let! asts = CodeFormatterImpl.parse false source

let ast =
Array.filter (fun (_, d: DefineCombination) -> List.sort d = List.sort defines) asts
Array.filter (fun (_, DefineCombination(d)) -> List.sort d = List.sort defines) asts
|> Array.head
|> fst

return CodeFormatterImpl.formatAST ast (Some source) config None
}
|> Async.RunSynchronously

let defines = DefineCombination(defines)

// merge with itself to make #if go on beginning of line
let _, fragments =
String.splitInFragments config.EndOfLine.NewLineString [ defines, result.Code ]
|> List.head
let mergedFormatResult =
MultipleDefineCombinations.mergeMultipleFormatResults config [ (defines, result); (defines, result) ]

String.merge fragments fragments
|> String.concat config.EndOfLine.NewLineString
|> String.normalizeNewLine
String.normalizeNewLine mergedFormatResult.Code

let isValidFSharpCode isFsiFile s =
CodeFormatter.IsValidFSharpCodeAsync(isFsiFile, s) |> Async.RunSynchronously
Expand Down
116 changes: 0 additions & 116 deletions src/Fantomas.Core.Tests/UtilsTests.fs
Original file line number Diff line number Diff line change
@@ -1,125 +1,9 @@
module Fantomas.Core.Tests.UtilsTests

open System
open NUnit.Framework
open Fantomas.Core
open Fantomas.Core.Tests.TestHelper
open FsCheck

let private mergeAndCompare a b expected =
let result =
let getFragments code =
String.splitInFragments config.EndOfLine.NewLineString [ code ]
|> List.head
|> snd

String.merge (getFragments a) (getFragments b)
|> String.concat Environment.NewLine
|> String.normalizeNewLine

let normalizedExpected = String.normalizeNewLine expected
normalizedExpected == result

[<Test>]
let ``merging of source code that starts with a hash`` () =
let a =
"""#if NOT_DEFINED
printfn \"meh\"
#else

#endif
"""

let b =
"""#if NOT_DEFINED

#else
printfn \"foo\"
#endif
"""

"""#if NOT_DEFINED
printfn \"meh\"
#else
printfn \"foo\"
#endif
"""
|> mergeAndCompare ([], a) ([ "NOT_DEFINED" ], b)

[<Test>]
let ``merging of defines content work when source code starts with a newline`` () =
let a =
"""
[<Literal>]
let private assemblyConfig() =
#if TRACE

#else
let x = "x"
#endif
x
"""

let b =
"""
[<Literal>]
let private assemblyConfig() =
#if TRACE
let x = ""
#else

#endif
x
"""

"""
[<Literal>]
let private assemblyConfig() =
#if TRACE
let x = ""
#else
let x = "x"
#endif
x
"""
|> mergeAndCompare ([], a) ([ "TRACE" ], b)

[<Test>]
let ``only split on control structure keyword`` () =
let a =
"""
#if INTERACTIVE
#else
#load "../FSharpx.TypeProviders/SetupTesting.fsx"

SetupTesting.generateSetupScript __SOURCE_DIRECTORY__

#load "__setup__.fsx"
#endif
"""

let b =
"""
#if INTERACTIVE
#else



#endif
"""

"""
#if INTERACTIVE
#else
#load "../FSharpx.TypeProviders/SetupTesting.fsx"

SetupTesting.generateSetupScript __SOURCE_DIRECTORY__

#load "__setup__.fsx"
#endif
"""
|> mergeAndCompare ([], a) ([ "INTERACTIVE" ], b)

[<Test>]
let ``when input is empty`` () =
let property (p: bool) : bool =
Expand Down
Loading