File tree Expand file tree Collapse file tree 9 files changed +64
-29
lines changed Expand file tree Collapse file tree 9 files changed +64
-29
lines changed Original file line number Diff line number Diff line change @@ -47,3 +47,23 @@ let ``trivia is parsed for Oak`` () =
47
47
|> fst
48
48
49
49
Assert.True( oak.ModulesOrNamespaces.[ 0 ]. HasContentAfter)
50
+
51
+ [<Test>]
52
+ let ``parsed oak can be formatted back to source`` () =
53
+ let source = " $\" gc{i}\" "
54
+
55
+ let oak =
56
+ CodeFormatter.ParseOakAsync( false , source)
57
+ |> Async.RunSynchronously
58
+ |> Array.head
59
+ |> fst
60
+
61
+ let formatted =
62
+ CodeFormatter.FormatOakAsync(
63
+ oak,
64
+ { FormatConfig.Default with
65
+ InsertFinalNewline = false }
66
+ )
67
+ |> Async.RunSynchronously
68
+
69
+ Assert.AreEqual( source, formatted)
Original file line number Diff line number Diff line change @@ -78,6 +78,21 @@ let text = "foo"
78
78
let s = $"%s {text} bar"
79
79
"""
80
80
81
+ [<Test>]
82
+ let ``interpolation from AST with multiple fillExprs`` () =
83
+ formatAST
84
+ false
85
+ """
86
+ $"%s {text} %i {bar} %f {meh}"
87
+ """
88
+ config
89
+ |> prepend newline
90
+ |> should
91
+ equal
92
+ """
93
+ $"%s {text} %i {bar} %f {meh}"
94
+ """
95
+
81
96
[<Test>]
82
97
let ``multiline expression in multiline string`` () =
83
98
formatSourceString
Original file line number Diff line number Diff line change @@ -1466,11 +1466,25 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr =
1466
1466
)
1467
1467
|> Expr.LibraryOnlyStaticOptimization
1468
1468
| SynExpr.InterpolatedString( parts, _, _) ->
1469
+ let lastIndex = parts.Length - 1
1470
+
1469
1471
let parts =
1470
1472
parts
1471
- |> List.map ( function
1473
+ |> List.mapi ( fun idx part ->
1474
+ match part with
1472
1475
| SynInterpolatedStringPart.String( v, r) ->
1473
- stn ( creationAide.TextFromSource ( fun () -> v) r) r |> Choice1Of2
1476
+ stn
1477
+ ( creationAide.TextFromSource
1478
+ ( fun () ->
1479
+ if idx = 0 && not ( v.StartsWith( " $" )) then
1480
+ $" $\" %s {v}{{"
1481
+ elif idx = lastIndex && not ( v.EndsWith( " \" " )) then
1482
+ $" }}%s {v}\" "
1483
+ else
1484
+ $" }}{v}{{" )
1485
+ r)
1486
+ r
1487
+ |> Choice1Of2
1474
1488
| SynInterpolatedStringPart.FillExpr( fillExpr, qualifiers) ->
1475
1489
let m =
1476
1490
match qualifiers with
Original file line number Diff line number Diff line change @@ -80,14 +80,14 @@ type CodeFormatter =
80
80
81
81
static member FormatOakAsync ( oak : Oak ) : Async < string > =
82
82
async {
83
- let context = Context.Context.Create false FormatConfig.Default
83
+ let context = Context.Context.Create FormatConfig.Default
84
84
let result = context |> CodePrinter.genFile oak |> Context.dump false
85
85
return result.Code
86
86
}
87
87
88
88
static member FormatOakAsync ( oak : Oak , config : FormatConfig ) : Async < string > =
89
89
async {
90
- let context = Context.Context.Create false config
90
+ let context = Context.Context.Create config
91
91
let result = context |> CodePrinter.genFile oak |> Context.dump false
92
92
return result.Code
93
93
}
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ let formatAST
57
57
( config : FormatConfig )
58
58
( cursor : pos option )
59
59
: FormatResult =
60
- let context = Context.Context.Create sourceText.IsSome config
60
+ let context = Context.Context.Create config
61
61
62
62
let oak =
63
63
match sourceText with
Original file line number Diff line number Diff line change @@ -1524,8 +1524,7 @@ let genExpr (e: Expr) =
1524
1524
|> fun ctx -> { ctx with Config = currentConfig }
1525
1525
|> atCurrentColumnIndent
1526
1526
1527
- onlyIfCtx ( fun ctx -> not ctx.HasSource) (!- " $\" " )
1528
- +> col sepNone node.Parts ( fun part ->
1527
+ col sepNone node.Parts ( fun part ->
1529
1528
match part with
1530
1529
| Choice1Of2 stringNode -> genSingleTextNode stringNode
1531
1530
| Choice2Of2 fillNode ->
@@ -1534,11 +1533,7 @@ let genExpr (e: Expr) =
1534
1533
genInterpolatedFillExpr fillNode.Expr
1535
1534
+> optSingle ( fun format -> sepColonFixed +> genSingleTextNode format) fillNode.Ident
1536
1535
1537
- if not ctx.HasSource then
1538
- (!- " {" +> genFill +> !- " }" ) ctx
1539
- else
1540
- genFill ctx)
1541
- +> onlyIfCtx ( fun ctx -> not ctx.HasSource) (!- " \" " )
1536
+ genFill ctx)
1542
1537
|> genNode node
1543
1538
| Expr.IndexRangeWildcard node -> genSingleTextNode node
1544
1539
| Expr.TripleNumberIndexRange node ->
Original file line number Diff line number Diff line change @@ -181,23 +181,19 @@ module WriterEvents =
181
181
[<System.Diagnostics.DebuggerDisplay( " \" {Dump()}\" " ) >]
182
182
type Context =
183
183
{ Config: FormatConfig
184
- HasSource: bool
185
184
WriterModel: WriterModel
186
185
WriterEvents: Queue < WriterEvent >
187
186
FormattedCursor: pos option }
188
187
189
188
/// Initialize with a string writer and use space as delimiter
190
189
static member Default =
191
190
{ Config = FormatConfig.Default
192
- HasSource = false
193
191
WriterModel = WriterModel.init
194
192
WriterEvents = Queue.empty
195
193
FormattedCursor = None }
196
194
197
- static member Create hasSource config : Context =
198
- { Context.Default with
199
- Config = config
200
- HasSource = hasSource }
195
+ static member Create config : Context =
196
+ { Context.Default with Config = config }
201
197
202
198
member x.WithDummy ( writerCommands , ? keepPageWidth ) =
203
199
let keepPageWidth = keepPageWidth |> Option.defaultValue false
Original file line number Diff line number Diff line change @@ -53,19 +53,14 @@ type WriterModel =
53
53
54
54
[<System.Diagnostics.DebuggerDisplay( " \" {Dump()}\" " ) >]
55
55
type Context =
56
- {
57
- Config: FormatConfig
58
- /// Indicates the presence of source code.
59
- /// This could be absent in the case we are formatting from AST.
60
- HasSource: bool
61
- WriterModel: WriterModel
62
- WriterEvents: Queue < WriterEvent >
63
- FormattedCursor: pos option
64
- }
56
+ { Config: FormatConfig
57
+ WriterModel: WriterModel
58
+ WriterEvents: Queue < WriterEvent >
59
+ FormattedCursor: pos option }
65
60
66
61
/// Initialize with a string writer and use space as delimiter
67
62
static member Default : Context
68
- static member Create : hasSource : bool -> config : FormatConfig -> Context
63
+ static member Create : config : FormatConfig -> Context
69
64
member WithDummy : writerCommands : Queue < WriterEvent > * ?keepPageWidth : bool -> Context
70
65
member WithShortExpression : maxWidth : int * ?startColumn : int -> Context
71
66
member Column : int
Original file line number Diff line number Diff line change @@ -404,7 +404,7 @@ let formatSelection
404
404
MaxLineLength = maxLineLength }
405
405
406
406
let formattedSelection =
407
- let context = Context.Context.Create true selectionConfig
407
+ let context = Context.Context.Create selectionConfig
408
408
409
409
match tree with
410
410
| TreeForSelection.Unsupported ->
You can’t perform that action at this time.
0 commit comments