Skip to content

Commit

Permalink
Support fixed in SingleExpr (#2114)
Browse files Browse the repository at this point in the history
* feat: support `fixed` in SingleExpr

* fix: oh Fantomas!

* fix: move test to correct file

* feat: add test with comment

* fix: oh Fantomas!

* fix: long live spaces

* feat: support comment after `fixed`
  • Loading branch information
sergey-tihon authored Feb 22, 2022
1 parent ca4488a commit 17e6b45
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/Fantomas.Tests/SingleExprTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,31 @@ let ``comment after address of tokens`` () =
&& // comment
foobar
"""

[<Test>]
let ``parse `fixed` in SingleExpr, 2112`` () =
formatSourceString false """let x = fixed expr""" config
|> prepend newline
|> should
equal
"""
let x = fixed expr
"""

[<Test>]
let ``handle comment after `fixed` in SingleExpr, 2112`` () =
formatSourceString
false
"""
let a = fixed // comment
b
"""
config
|> prepend newline
|> should
equal
"""
let a =
fixed // comment
b
"""
6 changes: 4 additions & 2 deletions src/Fantomas/AstTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,11 @@ module private Ast =
mkNode SynExpr_DiscardAfterMissingQualificationAfterDot range
:: nodes
|> finalContinuation)
| SynExpr.Fixed (expr, range) ->
| SynExpr.Fixed (expr, StartRange 5 (fixedKeyword, range)) ->
visit expr (fun nodes ->
mkNode SynExpr_Fixed range :: nodes
[ yield mkNode SynExpr_Fixed range
yield mkNode SynExpr_Fixed_Fixed fixedKeyword
yield! nodes ]
|> finalContinuation)
| SynExpr.InterpolatedString (parts, _, range) ->
mkNode SynExpr_InterpolatedString range
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,7 @@ and genExpr astContext synExpr ctx =
genTriviaFor SynExpr_YieldOrReturnFrom_ReturnBang returnBangKeyword !- "return! "
| Do doKeyword -> genTriviaFor SynExpr_Do_Do doKeyword !- "do "
| DoBang doBangKeyword -> genTriviaFor SynExpr_DoBang_DoBang doBangKeyword !- "do! "
| Fixed fixedKeyword -> genTriviaFor SynExpr_Fixed_Fixed fixedKeyword !- "fixed "
+> autoIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)
| ConstExpr (c, r) -> genConst c r
| NullExpr -> !- "null"
Expand Down
2 changes: 2 additions & 0 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ type ExprKind =
| ReturnFrom of keyword: range
| Do of keyword: range
| DoBang of Keyword: range
| Fixed of keyword: range

let (|SingleExpr|_|) =
function
Expand All @@ -584,6 +585,7 @@ let (|SingleExpr|_|) =
Some(ReturnFrom returnBangKeyword, e)
| SynExpr.Do (e, StartRange 2 (doKeyword, _range)) -> Some(Do doKeyword, e)
| SynExpr.DoBang (e, StartRange 3 (doBangKeyword, _range)) -> Some(DoBang doBangKeyword, e)
| SynExpr.Fixed (e, StartRange 5 (fixedKeyword, _range)) -> Some(Fixed fixedKeyword, e)
| _ -> None

type TypedExprKind =
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas/TriviaTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ type FsAstType =
| SynExpr_FromParseError
| SynExpr_DiscardAfterMissingQualificationAfterDot
| SynExpr_Fixed
| SynExpr_Fixed_Fixed
| SynExpr_InterpolatedString
| SynExpr_IndexRange
| SynExpr_IndexFromEnd
Expand Down

0 comments on commit 17e6b45

Please sign in to comment.