Skip to content

Commit

Permalink
Print trivia of SynBinding prefix outside multiline check. Fixes #2130.…
Browse files Browse the repository at this point in the history
… (#2132)
  • Loading branch information
nojaf authored Mar 8, 2022
1 parent 1e98bc3 commit ae67b24
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Comment after match keyword is lost. [#1851](https://github.com/fsprojects/fantomas/issues/1851)
* Preserve in keyword on next line. [#1182](https://github.com/fsprojects/fantomas/issues/1182)
* Remove Ionide.KeepAChangelog.Tasks and Dotnet.ReproducibleBuilds as NuGet dependencies. [#2131](https://github.com/fsprojects/fantomas/pull/2131)
* Comment lost between attribute and member. [#2130](https://github.com/fsprojects/fantomas/issues/2130)

## [4.7.0] - 2022-03-04

Expand Down
28 changes: 28 additions & 0 deletions src/Fantomas.Tests/AttributeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,3 +1042,31 @@ module AsyncOptionCEExtensions =
type AsyncOptionBuilder with
member inline Source: string -> string
"""

[<Test>]
let ``comment between attribute and member, 2130`` () =
formatSourceString
false
"""
type StreamReaderExtensions =
[<Extension>]
// Extension method for StreamReader using Tasks to enable specifying timeout.
// Returns `None` if timeout occurs, otherwise `Some string`.
// Beware that since StreamReader.ReadLineAsync task can't be cancelled,
// it might still be running after recieving None!
static member inline Meh(streamReader: StreamReader, timeout: TimeSpan) =
()
"""
config
|> prepend newline
|> should
equal
"""
type StreamReaderExtensions =
[<Extension>]
// Extension method for StreamReader using Tasks to enable specifying timeout.
// Returns `None` if timeout occurs, otherwise `Some string`.
// Beware that since StreamReader.ReadLineAsync task can't be cancelled,
// it might still be running after recieving None!
static member inline Meh(streamReader: StreamReader, timeout: TimeSpan) = ()
"""
9 changes: 8 additions & 1 deletion src/Fantomas/AstTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,14 @@ module private Ast =

and visitSynValData (svd: SynValData) : TriviaNodeAssigner list =
match svd with
| SynValData (_, svi, _) -> visitSynValInfo svi
| SynValData (memberFlags, svi, _) ->
let flagNodes =
match memberFlags with
| Some { Trivia = trivia } -> mkNodeOption SynValData_Static trivia.StaticRange
| _ -> []

[ yield! flagNodes
yield! visitSynValInfo svi ]

and visitSynValSig (svs: SynValSig) : TriviaNodeAssigner list =
match svs with
Expand Down
55 changes: 23 additions & 32 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,9 @@ and genTypeParamPostfix astContext tds =
| _ -> sepNone

and genLetBinding astContext pref b =
let genPref = !-pref
let genPref letKeyword =
genTriviaForOption SynBinding_Let letKeyword !-pref

let isRecursiveLetOrUseFunction = (pref = "and ")

match b with
Expand All @@ -567,8 +569,7 @@ and genLetBinding astContext pref b =
isRecursiveLetOrUseFunction
px
ats
letKeyword
genPref
(genPref letKeyword)
ao
isInline
isMutable
Expand All @@ -587,8 +588,7 @@ and genLetBinding astContext pref b =
isRecursiveLetOrUseFunction
px
ats
letKeyword
genPref
(genPref letKeyword)
ao
isInline
isMutable
Expand All @@ -604,8 +604,7 @@ and genLetBinding astContext pref b =
isRecursiveLetOrUseFunction
px
ats
letKeyword
genPref
(genPref letKeyword)
ao
isInline
isMutable
Expand All @@ -632,8 +631,7 @@ and genLetBinding astContext pref b =
isRecursiveLetOrUseFunction
px
ats
letKeyword
genPref
(genPref letKeyword)
ao
isInline
isMutable
Expand Down Expand Up @@ -800,7 +798,7 @@ and genMemberBinding astContext b =
(onlyIfNot mf.IsInstance (!- "static ")
+> !- "member ")

genMemberBindingImpl astContext prefix b ats px ao isInline pat equalsRange e synValInfo
genMemberBindingImpl astContext prefix ats px ao isInline pat equalsRange e synValInfo
| _ ->
let ps = List.map snd ps

Expand All @@ -822,7 +820,7 @@ and genMemberBinding astContext b =
| MemberBinding (ats, px, ao, isInline, mf, p, equalsRange, e, synValInfo) ->
let prefix = genMemberFlags mf

genMemberBindingImpl astContext prefix b ats px ao isInline p equalsRange e synValInfo
genMemberBindingImpl astContext prefix ats px ao isInline p equalsRange e synValInfo

| ExplicitCtor (ats, px, ao, p, equalsRange, e, so) ->
let prefix =
Expand Down Expand Up @@ -865,7 +863,6 @@ and genMemberBinding astContext b =
and genMemberBindingImpl
(astContext: ASTContext)
(prefix: Context -> Context)
(b: SynBinding)
(ats: SynAttributes)
(px: PreXmlDoc)
(ao: SynAccess option)
Expand All @@ -883,7 +880,6 @@ and genMemberBindingImpl
false
px
ats
None
prefix
ao
isInline
Expand All @@ -897,15 +893,18 @@ and genMemberBindingImpl
equalsRange
e
| e, PatLongIdent (ao, s, _, ps, tpso) when (List.isNotEmpty ps) ->
genSynBindingFunction astContext true false px ats None prefix ao isInline false s p.Range ps tpso equalsRange e
genSynBindingFunction astContext true false px ats prefix ao isInline false s p.Range ps tpso equalsRange e
| TypedExpr (Typed, e, t), pat ->
genSynBindingValue astContext false px ats None prefix ao isInline false pat (Some t) equalsRange e
| _, pat -> genSynBindingValue astContext false px ats None prefix ao isInline false pat None equalsRange e
genSynBindingValue astContext false px ats prefix ao isInline false pat (Some t) equalsRange e
| _, pat -> genSynBindingValue astContext false px ats prefix ao isInline false pat None equalsRange e

and genMemberFlags (mf: SynMemberFlags) =
match mf.Trivia with
| { StaticRange = Some _s
MemberRange = Some _m } -> !- "static member "
| { StaticRange = Some s
MemberRange = Some _m } ->
genTriviaFor SynValData_Static s !- "static"
+> sepSpace
+> !- "member "
| { OverrideRange = Some _o } -> !- "override "
| { DefaultRange = Some _d } -> !- "default "
| { AbstractRange = Some _a
Expand Down Expand Up @@ -5029,7 +5028,6 @@ and genSynBindingFunction
(isRecursiveLetOrUseFunction: bool)
(px: PreXmlDoc)
(ats: SynAttributes)
(letKeyword: range option)
(pref: Context -> Context)
(ao: SynAccess option)
(isInline: bool)
Expand Down Expand Up @@ -5074,8 +5072,7 @@ and genSynBindingFunction
| _ -> sepSpace

let short =
genPref
+> afterLetKeyword
afterLetKeyword
+> genFunctionName
+> spaceBeforeParameters
+> col sepSpace parameters (genPatWithIdent astContext)
Expand All @@ -5088,8 +5085,7 @@ and genSynBindingFunction
genParenTupleWithIndentAndNewlines astContext lpr ps rpr pp.Range, true
| _ -> col sepNln parameters (genPatWithIdent astContext), false

(genPref
+> afterLetKeyword
(afterLetKeyword
+> sepSpace
+> genFunctionName
+> indent
Expand All @@ -5113,7 +5109,7 @@ and genSynBindingFunction

(genPreXmlDoc px
+> genAttrIsFirstChild
+> optSingle (enterNodeFor SynBinding_Let) letKeyword
+> genPref
+> leadingExpressionIsMultiline genSignature genExpr)
ctx

Expand All @@ -5123,7 +5119,6 @@ and genSynBindingFunctionWithReturnType
(isRecursiveLetOrUseFunction: bool)
(px: PreXmlDoc)
(ats: SynAttributes)
(letKeyword: range option)
(pref: Context -> Context)
(ao: SynAccess option)
(isInline: bool)
Expand Down Expand Up @@ -5180,8 +5175,7 @@ and genSynBindingFunctionWithReturnType
| _ -> sepSpace

let short =
genPref
+> afterLetKeyword
afterLetKeyword
+> sepSpace
+> genFunctionName
+> spaceBeforeParameters
Expand All @@ -5196,8 +5190,7 @@ and genSynBindingFunctionWithReturnType
genParenTupleWithIndentAndNewlines astContext lpr ps rpr pp.Range, true
| _ -> col sepNln parameters (genPatWithIdent astContext), false

(genPref
+> afterLetKeyword
(afterLetKeyword
+> sepSpace
+> genFunctionName
+> indent
Expand All @@ -5221,7 +5214,7 @@ and genSynBindingFunctionWithReturnType

(genPreXmlDoc px
+> genAttrIsFirstChild
+> optSingle (enterNodeFor SynBinding_Let) letKeyword
+> genPref
+> leadingExpressionIsMultiline genSignature genExpr)
ctx

Expand Down Expand Up @@ -5277,7 +5270,6 @@ and genSynBindingValue
(isRecursiveLetOrUseFunction: bool)
(px: PreXmlDoc)
(ats: SynAttributes)
(letKeyword: range option)
(pref: Context -> Context)
(ao: SynAccess option)
(isInline: bool)
Expand Down Expand Up @@ -5325,7 +5317,6 @@ and genSynBindingValue

genPreXmlDoc px
+> genAttrIsFirstChild
+> optSingle (enterNodeFor SynBinding_Let) letKeyword
+> genPref
+> (fun ctx ->
let prefix =
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas/TriviaTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ type FsAstType =
| SynType_Paren_OpeningParenthesis
| SynType_Paren_ClosingParenthesis
| SynValData_
| SynValData_Static
| SynValInfo_
| SynArgInfo_
| ParsedHashDirective_
Expand Down

0 comments on commit ae67b24

Please sign in to comment.