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

Collect newlines between signature module declaration consistently. #1534

Merged
merged 1 commit into from
Mar 25, 2021
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
76 changes: 76 additions & 0 deletions src/Fantomas.Tests/SignatureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,16 @@ module Foo =

type t = int
val lmao: unit -> bool

module Foo2 =
module Bar =
type t = bool

val lol: unit -> bool

type t = int

val lmao: unit -> bool
"""
config
|> prepend newline
Expand All @@ -969,6 +979,14 @@ module Foo =
module Example

module Foo =
module Bar =
type t = bool
val lol : unit -> bool

type t = int
val lmao : unit -> bool

module Foo2 =
module Bar =
type t = bool

Expand Down Expand Up @@ -1222,3 +1240,61 @@ val create :
and_another_to_make_the_line_long_enough: unit ->
unit
"""

[<Test>]
let ``print trivia before exception`` () =
formatSourceString
true
"""
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

/// The configuration of the compiler (TcConfig and TcConfigBuilder)
module internal FSharp.Compiler.CompilerConfig

open System
open Internal.Utilities
open Internal.Utilities.Library
open FSharp.Compiler
open FSharp.Compiler.AbstractIL
open FSharp.Compiler.AbstractIL.IL
open FSharp.Compiler.AbstractIL.ILBinaryReader
open FSharp.Compiler.AbstractIL.ILPdbWriter
open FSharp.Compiler.DependencyManager
open FSharp.Compiler.Diagnostics
open FSharp.Compiler.ErrorLogger
open FSharp.Compiler.Features
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Text
exception FileNameNotResolved of string (*description of searched locations*) * string * range (*filename*)

exception LoadedSourceNotFoundIgnoring of string * range (*filename*)
"""
config
|> prepend newline
|> should
equal
"""
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

/// The configuration of the compiler (TcConfig and TcConfigBuilder)
module internal FSharp.Compiler.CompilerConfig

open System
open Internal.Utilities
open Internal.Utilities.Library
open FSharp.Compiler
open FSharp.Compiler.AbstractIL
open FSharp.Compiler.AbstractIL.IL
open FSharp.Compiler.AbstractIL.ILBinaryReader
open FSharp.Compiler.AbstractIL.ILPdbWriter
open FSharp.Compiler.DependencyManager
open FSharp.Compiler.Diagnostics
open FSharp.Compiler.ErrorLogger
open FSharp.Compiler.Features
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Text

exception FileNameNotResolved of string (*description of searched locations*) * string * range (*filename*)

exception LoadedSourceNotFoundIgnoring of string * range (*filename*)
"""
97 changes: 23 additions & 74 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -286,86 +286,34 @@ and genModuleDeclList astContext e =

collectItems e |> colWithNlnWhenItemIsMultiline

and genSigModuleDeclList astContext node =
match node with
| [ x ] -> genSigModuleDecl astContext x

| SigOpenL (xs, ys) ->
let sepXsAndYs =
match List.tryHead ys with
| Some _ -> sepNln
| None -> rep 2 sepNln
and genSigModuleDeclList astContext (e: SynModuleSigDecl list) =
let rec collectItems (e: SynModuleSigDecl list) : ColMultilineItem list =
match e with
| [] -> []
| SigOpenL (xs, ys) ->
let expr =
col sepNln xs (genSigModuleDecl astContext)

fun ctx ->
match ys with
| [] -> col sepNln xs (genSigModuleDecl astContext) ctx
| _ ->
(col sepNln xs (genSigModuleDecl astContext)
+> sepXsAndYs
+> genSigModuleDeclList astContext ys)
ctx
let r = List.head xs |> fun mdl -> mdl.Range
// SynModuleSigDecl.Open cannot have attributes
let sepNln =
sepNlnConsideringTriviaContentBeforeForMainNode SynModuleSigDecl_Open r

| SigHashDirectiveL (xs, ys) ->
match ys with
| [] -> col sepNone xs (genSigModuleDecl astContext)
| _ ->
col sepNone xs (genSigModuleDecl astContext)
+> sepNln
+> genSigModuleDeclList astContext ys

| SigModuleAbbrevL (xs, ys)
| SigValL (xs, ys) ->
match ys with
| [] -> col sepNln xs (genSigModuleDecl astContext)
| _ ->
let sepXsYs =
match List.tryHead ys with
| Some _ -> sepNln
| None -> rep 2 sepNln

col sepNln xs (genSigModuleDecl astContext)
+> sepXsYs
+> genSigModuleDeclList astContext ys

| SigMultilineModuleDeclL (xs, ys) ->
match ys with
| [] ->
colEx
(fun (x: SynModuleSigDecl) ->
let attrs =
getRangesFromAttributesFromSynModuleSigDeclaration x
ColMultilineItem(expr, sepNln, r)
:: collectItems ys
| s :: rest ->
let attrs =
getRangesFromAttributesFromSynModuleSigDeclaration s

sepNln
+> sepNlnConsideringTriviaContentBeforeWithAttributesFor
(synModuleSigDeclToFsAstType x)
x.Range
attrs)
xs
(genSigModuleDecl astContext)
| _ ->
let sepXsYs =
match List.tryHead ys with
| Some y ->
sepNln
+> sepNlnConsideringTriviaContentBeforeForMainNode (synModuleSigDeclToFsAstType y) y.Range
| None -> rep 2 sepNln
let sepNln =
sepNlnConsideringTriviaContentBeforeWithAttributesFor (synModuleSigDeclToFsAstType s) s.Range attrs

colEx
(fun (x: SynModuleSigDecl) ->
let attrs =
getRangesFromAttributesFromSynModuleSigDeclaration x
let expr = genSigModuleDecl astContext s

sepNln
+> sepNlnConsideringTriviaContentBeforeWithAttributesFor
(synModuleSigDeclToFsAstType x)
x.Range
attrs)
xs
(genSigModuleDecl astContext)
+> sepXsYs
+> genSigModuleDeclList astContext ys
ColMultilineItem(expr, sepNln, s.Range)
:: (collectItems rest)

| _ -> sepNone
collectItems e |> colWithNlnWhenItemIsMultiline

and genModuleDecl astContext (node: SynModuleDecl) =
match node with
Expand Down Expand Up @@ -515,6 +463,7 @@ and genSigModuleDecl astContext node =
| SynModuleSigDecl.Open (SynOpenDeclTarget.ModuleOrNamespace _, _) ->
genTriviaFor SynModuleSigDecl_Open node.Range
| SynModuleSigDecl.Open (SynOpenDeclTarget.Type _, _) -> genTriviaFor SynModuleSigDecl_OpenType node.Range
| SynModuleSigDecl.Exception _ -> genTriviaFor SynModuleSigDecl_Exception node.Range
| _ -> id)

and genAccess (Access s) = !-s
Expand Down
14 changes: 0 additions & 14 deletions src/Fantomas/SourceTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,6 @@ let rec (|SigValL|_|) =
| SigVal _ as x :: ys -> Some([ x ], ys)
| _ -> None

let (|SigMultilineModuleDecl|_|) =
function
| SigHashDirective _
| SigModuleAbbrev _
| SigVal _
| SigOpen _ -> None
| md -> Some md

let rec (|SigMultilineModuleDeclL|_|) =
function
| SigMultilineModuleDecl x :: SigMultilineModuleDeclL (xs, ys) -> Some(x :: xs, ys)
| SigMultilineModuleDecl x :: ys -> Some([ x ], ys)
| _ -> None

/// Gather PropertyGetSet in one printing call.
/// Assume that PropertySet comes right after PropertyGet.
let (|PropertyWithGetSet|_|) =
Expand Down