Skip to content

Commit

Permalink
Merge pull request #2925 from dawedawe/add_measure_trivia
Browse files Browse the repository at this point in the history
Adjust to optional lhs of SynMeasure.Divide
  • Loading branch information
dawedawe authored Jul 23, 2023
2 parents 41fe8d6 + 6cb2a6d commit 08a5245
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 20 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 6.1.2 - 2023-07-23

### Changed
* Update FCS to 'Add trivia information to SynConst.Measure', commit 7b5e12842d673b7daa467e0091378bf4acc95e4f

### Fixed
* Left out lhs in SynMeasure.Divide should not be restored as SynMeasure.One. [#2926](https://github.com/fsprojects/fantomas/issues/2926)
* Block comments in measure are lost or restored twice and in wrong place. [#2927](https://github.com/fsprojects/fantomas/issues/2927)

## 6.1.1 - 2023-06-29

### Changed
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>9c8b4192966e6554adc5dcc8973f2a23c8fa2722</FCSCommitHash>
<FCSCommitHash>7b5e12842d673b7daa467e0091378bf4acc95e4f</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 @@ -726,3 +726,48 @@ let ``single digit constant`` () =
{ config with
InsertFinalNewline = false }
|> should equal "1"

[<Test>]
let ``left out lhs in SynMeasure.Divide should not be restored as SynMeasure.One, 2926`` () =
formatSourceString
false
"""
234</kg>
"""
config
|> prepend newline
|> should
equal
"""
234< / kg>
"""

[<Test>]
let ``explicit SynMeasure.One in SynMeasure.Divide should be preserved`` () =
formatSourceString
false
"""
234<1/kg>
"""
config
|> prepend newline
|> should
equal
"""
234<1 / kg>
"""

[<Test>]
let ``block comments in measure are lost or restored twice and in wrong place, 2927`` () =
formatSourceString
false
"""
234<(* foo *)kg(* bar *)>
"""
config
|> prepend newline
|> should
equal
"""
234< (* foo *) kg (* bar *) >
"""
38 changes: 26 additions & 12 deletions src/Fantomas.Core/ASTTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,37 @@ let mkConstant (creationAide: CreationAide) c r : Constant =
$"\"{content}\"B"

stn (creationAide.TextFromSource fallback r) r |> Constant.FromText
| SynConst.Measure(c, numberRange, measure) ->
ConstantMeasureNode(mkConstant creationAide c numberRange, mkMeasure creationAide measure, r)
| SynConst.Measure(c, numberRange, measure, trivia) ->
let uOfMRange =
mkRange trivia.LessRange.FileName trivia.LessRange.Start trivia.GreaterRange.End

let unitOfMeasure =
UnitOfMeasureNode(
stn "<" trivia.LessRange,
mkMeasure creationAide measure,
stn ">" trivia.GreaterRange,
uOfMRange
)

ConstantMeasureNode(mkConstant creationAide c numberRange, unitOfMeasure, r)
|> Constant.Measure
| SynConst.SourceIdentifier(c, _, r) -> stn c r |> Constant.FromText

let mkMeasure (creationAide: CreationAide) (measure: SynMeasure) : Measure =
match measure with
| SynMeasure.Var(typar, _) -> mkSynTypar typar |> Measure.Single
| SynMeasure.Anon m -> stn "_" m |> Measure.Single
| SynMeasure.One -> stn "1" Range.Zero |> Measure.Single
| SynMeasure.One m -> stn "1" m |> Measure.Single
| SynMeasure.Product(m1, m2, m) ->
MeasureOperatorNode(mkMeasure creationAide m1, stn "*" Range.Zero, mkMeasure creationAide m2, m)
|> Measure.Operator
| SynMeasure.Divide(m1, m2, m) ->
MeasureOperatorNode(mkMeasure creationAide m1, stn "/" Range.Zero, mkMeasure creationAide m2, m)
|> Measure.Operator
let lhs = m1 |> Option.map (mkMeasure creationAide)

MeasureDivideNode(lhs, stn "/" Range.Zero, mkMeasure creationAide m2, m)
|> Measure.Divide
| SynMeasure.Power(ms, rat, m) ->
MeasurePowerNode(mkMeasure creationAide ms, stn (mkSynRationalConst rat) Range.Zero, m)
MeasurePowerNode(mkMeasure creationAide ms, stn (mkSynRationalConst creationAide rat) Range.Zero, m)
|> Measure.Power
| SynMeasure.Named(lid, _) -> mkLongIdent lid |> Measure.Multiple
| SynMeasure.Paren(measure, StartEndRange 1 (mOpen, m, mClose)) ->
Expand Down Expand Up @@ -1991,12 +2004,13 @@ let mkSynValTyparDecls (creationAide: CreationAide) (vt: SynValTyparDecls option
| None -> None
| Some(SynValTyparDecls(tds, _)) -> Option.map (mkSynTyparDecls creationAide) tds

let mkSynRationalConst rc =
let mkSynRationalConst (creationAide: CreationAide) rc =
let rec visit rc =
match rc with
| SynRationalConst.Integer i -> string i
| SynRationalConst.Rational(numerator, denominator, _) -> $"(%i{numerator}/%i{denominator})"
| SynRationalConst.Negate innerRc -> $"-{visit innerRc}"
| SynRationalConst.Integer(i, range) -> creationAide.TextFromSource (fun () -> string i) range
| SynRationalConst.Rational(numerator = numerator; denominator = denominator) ->
$"(%i{numerator}/%i{denominator})"
| SynRationalConst.Negate(rationalConst = innerRc) -> $"-{visit innerRc}"

visit rc

Expand Down Expand Up @@ -2097,7 +2111,7 @@ let mkType (creationAide: CreationAide) (t: SynType) : Type =
TypeHashConstraintNode(stn "#" mHash, mkType creationAide t, typeRange)
|> Type.HashConstraint
| SynType.MeasurePower(t, rc, _) ->
TypeMeasurePowerNode(mkType creationAide t, mkSynRationalConst rc, typeRange)
TypeMeasurePowerNode(mkType creationAide t, mkSynRationalConst creationAide rc, typeRange)
|> Type.MeasurePower
| SynType.StaticConstant(SynConst.String(null, kind, mString), r) ->
mkConstant creationAide (SynConst.String("null", kind, mString)) r
Expand Down Expand Up @@ -2656,7 +2670,7 @@ let mkMemberDefn (creationAide: CreationAide) (md: SynMemberDefn) =
memberDefn = SynBinding(
attributes = ats
xmlDoc = px
valData = SynValData(Some { MemberKind = SynMemberKind.Constructor }, _, ido)
valData = SynValData(Some { MemberKind = SynMemberKind.Constructor }, _, ido, _)
headPat = SynPat.LongIdent(
longDotId = SynLongIdent(id = [ newIdent ])
argPats = SynArgPats.Pats [ SynPat.Paren _ as pat ]
Expand Down
15 changes: 11 additions & 4 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ let genConstant (c: Constant) =
| Constant.Unit n -> genUnit n
| Constant.Measure n ->
(genConstant n.Constant |> genNode (Constant.Node n.Constant))
+> !- "<"
+> genMeasure n.Measure
+> !- ">"
+> genSingleTextNode n.Measure.LessThan
+> genMeasure n.Measure.Measure
+> genSingleTextNode n.Measure.GreaterThan
|> genNode n

let genMeasure (measure: Measure) =
Expand All @@ -222,8 +222,15 @@ let genMeasure (measure: Measure) =
+> sepSpace
+> genMeasure n.RightHandSide
|> genNode n
| Measure.Divide n ->
optSingle genMeasure n.LeftHandSide
+> sepSpace
+> genSingleTextNode n.Operator
+> sepSpace
+> genMeasure n.RightHandSide
|> genNode n
| Measure.Power n -> genMeasure n.Measure +> !- "^" +> genSingleTextNode n.Exponent |> genNode n
| Measure.Seq n -> col sepSpace n.Measures genMeasure
| Measure.Seq n -> col sepSpace n.Measures genMeasure |> genNode n
| Measure.Multiple n -> genIdentListNode n
| Measure.Paren n ->
genSingleTextNode n.OpeningParen
Expand Down
28 changes: 26 additions & 2 deletions src/Fantomas.Core/SyntaxOak.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2630,9 +2630,9 @@ type UnitNode(openingParen: SingleTextNode, closingParen: SingleTextNode, range)
member val OpeningParen = openingParen
member val ClosingParen = closingParen

type ConstantMeasureNode(constant: Constant, measure: Measure, range) =
type ConstantMeasureNode(constant: Constant, measure: UnitOfMeasureNode, range) =
inherit NodeBase(range)
override val Children: Node array = [| yield Constant.Node constant; yield Measure.Node measure |]
override val Children: Node array = [| yield Constant.Node constant; yield measure |]
member val Constant = constant
member val Measure = measure

Expand Down Expand Up @@ -2745,6 +2745,15 @@ type TypeConstraint =
| EnumOrDelegate n -> n
| WhereSelfConstrained t -> Type.Node t

type UnitOfMeasureNode(lessThan: SingleTextNode, measure: Measure, greaterThan: SingleTextNode, range) =
inherit NodeBase(range)

override val Children: Node array = [| yield lessThan; yield Measure.Node measure; yield greaterThan |]

member val LessThan = lessThan
member val Measure = measure
member val GreaterThan = greaterThan

type MeasureOperatorNode(lhs: Measure, operator: SingleTextNode, rhs: Measure, range) =
inherit NodeBase(range)

Expand All @@ -2754,6 +2763,19 @@ type MeasureOperatorNode(lhs: Measure, operator: SingleTextNode, rhs: Measure, r
member val Operator = operator
member val RightHandSide = rhs

type MeasureDivideNode(lhs: Measure option, operator: SingleTextNode, rhs: Measure, range) =
inherit NodeBase(range)

override val Children: Node array =
[| if Option.isSome lhs then
yield Measure.Node lhs.Value
yield operator
yield Measure.Node rhs |]

member val LeftHandSide = lhs
member val Operator = operator
member val RightHandSide = rhs

type MeasurePowerNode(measure: Measure, exponent: SingleTextNode, range) =
inherit NodeBase(range)
override val Children: Node array = [| yield Measure.Node measure; yield exponent |]
Expand All @@ -2778,6 +2800,7 @@ type MeasureParenNode(openingParen: SingleTextNode, measure: Measure, closingPar
type Measure =
| Single of SingleTextNode
| Operator of MeasureOperatorNode
| Divide of MeasureDivideNode
| Power of MeasurePowerNode
| Multiple of IdentListNode
| Seq of MeasureSequenceNode
Expand All @@ -2787,6 +2810,7 @@ type Measure =
match m with
| Single n -> n
| Operator n -> n
| Divide n -> n
| Power n -> n
| Multiple n -> n
| Seq n -> n
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.FCS/Parse.fs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ let EmptyParsedInput (filename, isLastCompiland) =
)

let createLexbuf langVersion sourceText =
UnicodeLexing.SourceTextAsLexbuf(true, LanguageVersion(langVersion), sourceText)
UnicodeLexing.SourceTextAsLexbuf(true, LanguageVersion(langVersion), None, sourceText)

let createLexerFunction (defines: string list) lexbuf (errorLogger: CapturingDiagnosticsLogger) =
let lightStatus = IndentationAwareSyntaxStatus(true, true)
Expand Down

0 comments on commit 08a5245

Please sign in to comment.