Skip to content

Commit

Permalink
Use findFirstNodeAfterLine to assign Directive trivia. Fixes fsprojec…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Apr 4, 2021
1 parent 21b45bd commit 7adf48f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
48 changes: 48 additions & 0 deletions src/Fantomas.Tests/CompilerDirectivesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2451,3 +2451,51 @@ let inputFileFlagsFsiBase (_tcConfigB: TcConfigBuilder) =
List.empty<CompilerOption>
#endif
"""

[<Test>]
let ``define before opening bracket of array, 1597`` () =
formatSourceString
false
"""
let Environment = { new IEnvironment with
member _.IsWindows() =
InteropServices.RuntimeInformation.IsOSPlatform(InteropServices.OSPlatform.Windows)
member _.GetScriptArgs() =
#if INTERACTIVE
fsi.CommandLineArgs
|> Array.skip 1
#else
[||]
#endif
member _.GetEnvironmentVariable(varName) =
System.Environment.GetEnvironmentVariable(varName)
member _.SetEnvironmentVariable(varName, value) =
System.Environment.SetEnvironmentVariable(varName, value)
}
"""
config
|> prepend newline
|> should
equal
"""
let Environment =
{ new IEnvironment with
member _.IsWindows() =
InteropServices.RuntimeInformation.IsOSPlatform(InteropServices.OSPlatform.Windows)
member _.GetScriptArgs() =
#if INTERACTIVE
fsi.CommandLineArgs |> Array.skip 1
#else
[||]
#endif
member _.GetEnvironmentVariable(varName) =
System.Environment.GetEnvironmentVariable(varName)
member _.SetEnvironmentVariable(varName, value) =
System.Environment.SetEnvironmentVariable(varName, value) }
"""
20 changes: 9 additions & 11 deletions src/Fantomas/Trivia.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,18 @@ let inline private isNotMemberKeyword (node: TriviaNodeAssigner) =

let private findFirstNodeAfterLine
(nodes: TriviaNodeAssigner list)
lineNumber
hasAnonModulesAndOpenStatements
(lineNumber: int)
(hasAnonModulesAndOpenStatements: bool)
: TriviaNodeAssigner option =
nodes
|> List.tryFind
(fun tn ->
if tn.Range.StartLine > lineNumber
&& isNotMemberKeyword tn then
not (
hasAnonModulesAndOpenStatements
&& mainNodeIs SynModuleOrNamespace_AnonModule tn
)
else
false)
tn.Range.StartLine > lineNumber
&& isNotMemberKeyword tn
&& not (
hasAnonModulesAndOpenStatements
&& mainNodeIs SynModuleOrNamespace_AnonModule tn
))

let private findLastNodeOnLine (nodes: TriviaNodeAssigner list) lineNumber : TriviaNodeAssigner option =
nodes
Expand Down Expand Up @@ -433,7 +431,7 @@ let private addTriviaToTriviaNode
match triviaBetweenAttributeAndParentBinding triviaNodes range.StartLine with
| Some _ as node -> updateTriviaNode (fun tn -> tn.ContentAfter.Add(directive)) triviaNodes node
| _ ->
match findNodeAfterLineAndColumn triviaNodes range.EndLine range.EndColumn with
match findFirstNodeAfterLine triviaNodes range.StartLine hasAnonModulesAndOpenStatements with
| Some _ as node -> updateTriviaNode (fun tn -> tn.ContentBefore.Add(directive)) triviaNodes node
| None ->
let findNode nodes =
Expand Down

0 comments on commit 7adf48f

Please sign in to comment.