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

Fix some block comments in Rational #2949

Merged
merged 6 commits into from
Aug 29, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed
* Update FCS to 'Included get,set in range of AutoProperty', commit d508186f510681d1261291474e3f9f25485999a8
* Update FCS to 'Add a Paren case to SynRationalConst', commit 97a5b6584b54707e3e8541fe758e1aa22132a8fe

### Added
* Initial support for `SynExpr.DotLambda` [#2920](https://github.com/fsprojects/fantomas/pull/2920) (See [dotnet/fsharp#13907](https://github.com/dotnet/fsharp/pull/13907))
Expand All @@ -12,6 +13,9 @@
### Fixed
* Comment no longer attached to autoproperty. [#2948](https://github.com/fsprojects/fantomas/issues/2948)
* `begin`/`end` are replaced with parens. [#2368](https://github.com/fsprojects/fantomas/issues/2368)
* Block comment in Rational between lparen and numerator is moved before lparen. [#2930](https://github.com/fsprojects/fantomas/issues/2930)
* Block comment in Rational between / and denominator is moved before /. [#2932](https://github.com/fsprojects/fantomas/issues/2932)
* Block comment in Rational between denominator and rparen is moved behind rparen. [#2933](https://github.com/fsprojects/fantomas/issues/2933)

## 6.1.3 - 2023-08-25

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Some common use cases include:

<!-- Versions -->
<PropertyGroup>
<FCSCommitHash>d508186f510681d1261291474e3f9f25485999a8</FCSCommitHash>
<FCSCommitHash>97a5b6584b54707e3e8541fe758e1aa22132a8fe</FCSCommitHash>
<StreamJsonRpcVersion>2.8.28</StreamJsonRpcVersion>
<FSharpCoreVersion>6.0.1</FSharpCoreVersion>
</PropertyGroup>
Expand Down
45 changes: 45 additions & 0 deletions src/Fantomas.Core.Tests/SynConstTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -846,3 +846,48 @@ let ``block comment between ^ and exponent in SynMeasure.Power is lost, 2936`` (
"""
234<m^ (* foo *) 2>
"""

[<Test>]
let ``block comment in Rational between lparen and numerator is moved before lparen, 2930`` () =
formatSourceString
false
"""
234<kg^((* foo *)2/3)>
"""
config
|> prepend newline
|> should
equal
"""
234<kg^( (* foo *) 2/3)>
"""

[<Test>]
let ``block comment in Rational between / and denominator is moved before /, 2932`` () =
formatSourceString
false
"""
234<kg^(2/(* foo *)3)>
"""
config
|> prepend newline
|> should
equal
"""
234<kg^(2/ (* foo *) 3)>
"""

[<Test>]
let ``block comment in Rational between denominator and rparen is moved behind rparen, 2933`` () =
formatSourceString
false
"""
234<kg^(2/3(* foo *))>
"""
config
|> prepend newline
|> should
equal
"""
234<kg^(2/3 (* foo *) )>
"""
27 changes: 25 additions & 2 deletions src/Fantomas.Core/ASTTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2024,16 +2024,39 @@ let mkSynRationalConst (creationAide: CreationAide) rc =
| SynRationalConst.Integer(i, range) ->
stn (creationAide.TextFromSource (fun () -> string i) range) range
|> RationalConstNode.Integer
| SynRationalConst.Rational(numerator, numeratorRange, denominator, denominatorRange, range) ->

| SynRationalConst.Paren(SynRationalConst.Rational(numerator,
numeratorRange,
divRange,
denominator,
denominatorRange,
_),
range) ->
let openingParen =
let r =
withEnd (Position.mkPos range.Start.Line (range.StartRange.StartColumn + 1)) range.StartRange

stn "(" r

let n =
stn (creationAide.TextFromSource (fun () -> string numerator) numeratorRange) numeratorRange

let div = stn "/" divRange

let d =
stn (creationAide.TextFromSource (fun () -> string denominator) denominatorRange) denominatorRange

RationalConstNode.Rational(RationalNode(n, d, range))
let closingParen =
let r =
withStart (Position.mkPos range.End.Line (range.End.Column - 1)) range.EndRange

stn ")" r

RationalConstNode.Rational(RationalNode(openingParen, n, div, d, closingParen, range))
| SynRationalConst.Paren(innerRc, _) -> visit innerRc
| SynRationalConst.Negate(innerRc, range) ->
RationalConstNode.Negate(NegateRationalNode(stn "-" range.StartRange, visit innerRc, range))
| SynRationalConst.Rational _ -> failwith "SynRationalConst.Rational not wrapped in SynRationalConst.Paren"

visit rc

Expand Down
6 changes: 3 additions & 3 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ let genRational (rat: RationalConstNode) =
| RationalConstNode.Integer i -> genSingleTextNode i
| RationalConstNode.Negate negate -> genSingleTextNode negate.Minus +> genRational negate.Rational
| RationalConstNode.Rational rationalNode ->
genSingleTextNode (SingleTextNode("(", Fantomas.FCS.Text.Range.Zero))
genSingleTextNode rationalNode.OpeningParen
+> genSingleTextNode rationalNode.Numerator
+> genSingleTextNode (SingleTextNode("/", Fantomas.FCS.Text.Range.Zero))
+> genSingleTextNode rationalNode.DivOp
+> genSingleTextNode rationalNode.Denominator
+> genSingleTextNode (SingleTextNode(")", Fantomas.FCS.Text.Range.Zero))
+> genSingleTextNode rationalNode.ClosingParen
|> genNode rationalNode

let genAttributesCore (ats: AttributeNode list) =
Expand Down
20 changes: 18 additions & 2 deletions src/Fantomas.Core/SyntaxOak.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2834,13 +2834,29 @@ type MeasureParenNode(openingParen: SingleTextNode, measure: Measure, closingPar
member val Measure = measure
member val ClosingParen = closingParen

type RationalNode(numerator: SingleTextNode, denominator: SingleTextNode, range: range) =
type RationalNode
(
openingParen: SingleTextNode,
numerator: SingleTextNode,
divOp: SingleTextNode,
denominator: SingleTextNode,
closingParen: SingleTextNode,
range: range
) =
inherit NodeBase(range)

override val Children: Node array = [| yield numerator; yield denominator |]
override val Children: Node array =
[| yield openingParen
yield numerator
yield divOp
yield denominator
yield closingParen |]

member val OpeningParen = openingParen
member val Numerator = numerator
member val DivOp = divOp
member val Denominator = denominator
member val ClosingParen = closingParen

type NegateRationalNode(minus: SingleTextNode, rationalConst: RationalConstNode, range: range) =
inherit NodeBase(range)
Expand Down