Skip to content

Commit 187cb01

Browse files
authored
Remove Context.HasSource (#2890)
* Create context with HasSource true. * Correct interpolated string parts in ASTTransformer fallback.
1 parent bfb4afc commit 187cb01

File tree

9 files changed

+64
-29
lines changed

9 files changed

+64
-29
lines changed

src/Fantomas.Core.Tests/CodeFormatterTests.fs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,23 @@ let ``trivia is parsed for Oak`` () =
4747
|> fst
4848

4949
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)

src/Fantomas.Core.Tests/InterpolatedStringTests.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ let text = "foo"
7878
let s = $"%s{text} bar"
7979
"""
8080

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+
8196
[<Test>]
8297
let ``multiline expression in multiline string`` () =
8398
formatSourceString

src/Fantomas.Core/ASTTransformer.fs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,11 +1466,25 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr =
14661466
)
14671467
|> Expr.LibraryOnlyStaticOptimization
14681468
| SynExpr.InterpolatedString(parts, _, _) ->
1469+
let lastIndex = parts.Length - 1
1470+
14691471
let parts =
14701472
parts
1471-
|> List.map (function
1473+
|> List.mapi (fun idx part ->
1474+
match part with
14721475
| 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
14741488
| SynInterpolatedStringPart.FillExpr(fillExpr, qualifiers) ->
14751489
let m =
14761490
match qualifiers with

src/Fantomas.Core/CodeFormatter.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ type CodeFormatter =
8080

8181
static member FormatOakAsync(oak: Oak) : Async<string> =
8282
async {
83-
let context = Context.Context.Create false FormatConfig.Default
83+
let context = Context.Context.Create FormatConfig.Default
8484
let result = context |> CodePrinter.genFile oak |> Context.dump false
8585
return result.Code
8686
}
8787

8888
static member FormatOakAsync(oak: Oak, config: FormatConfig) : Async<string> =
8989
async {
90-
let context = Context.Context.Create false config
90+
let context = Context.Context.Create config
9191
let result = context |> CodePrinter.genFile oak |> Context.dump false
9292
return result.Code
9393
}

src/Fantomas.Core/CodeFormatterImpl.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ let formatAST
5757
(config: FormatConfig)
5858
(cursor: pos option)
5959
: FormatResult =
60-
let context = Context.Context.Create sourceText.IsSome config
60+
let context = Context.Context.Create config
6161

6262
let oak =
6363
match sourceText with

src/Fantomas.Core/CodePrinter.fs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,8 +1524,7 @@ let genExpr (e: Expr) =
15241524
|> fun ctx -> { ctx with Config = currentConfig }
15251525
|> atCurrentColumnIndent
15261526

1527-
onlyIfCtx (fun ctx -> not ctx.HasSource) (!- "$\"")
1528-
+> col sepNone node.Parts (fun part ->
1527+
col sepNone node.Parts (fun part ->
15291528
match part with
15301529
| Choice1Of2 stringNode -> genSingleTextNode stringNode
15311530
| Choice2Of2 fillNode ->
@@ -1534,11 +1533,7 @@ let genExpr (e: Expr) =
15341533
genInterpolatedFillExpr fillNode.Expr
15351534
+> optSingle (fun format -> sepColonFixed +> genSingleTextNode format) fillNode.Ident
15361535

1537-
if not ctx.HasSource then
1538-
(!- "{" +> genFill +> !- "}") ctx
1539-
else
1540-
genFill ctx)
1541-
+> onlyIfCtx (fun ctx -> not ctx.HasSource) (!- "\"")
1536+
genFill ctx)
15421537
|> genNode node
15431538
| Expr.IndexRangeWildcard node -> genSingleTextNode node
15441539
| Expr.TripleNumberIndexRange node ->

src/Fantomas.Core/Context.fs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,19 @@ module WriterEvents =
181181
[<System.Diagnostics.DebuggerDisplay("\"{Dump()}\"")>]
182182
type Context =
183183
{ Config: FormatConfig
184-
HasSource: bool
185184
WriterModel: WriterModel
186185
WriterEvents: Queue<WriterEvent>
187186
FormattedCursor: pos option }
188187

189188
/// Initialize with a string writer and use space as delimiter
190189
static member Default =
191190
{ Config = FormatConfig.Default
192-
HasSource = false
193191
WriterModel = WriterModel.init
194192
WriterEvents = Queue.empty
195193
FormattedCursor = None }
196194

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 }
201197

202198
member x.WithDummy(writerCommands, ?keepPageWidth) =
203199
let keepPageWidth = keepPageWidth |> Option.defaultValue false

src/Fantomas.Core/Context.fsi

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,14 @@ type WriterModel =
5353

5454
[<System.Diagnostics.DebuggerDisplay("\"{Dump()}\"")>]
5555
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 }
6560

6661
/// Initialize with a string writer and use space as delimiter
6762
static member Default: Context
68-
static member Create: hasSource: bool -> config: FormatConfig -> Context
63+
static member Create: config: FormatConfig -> Context
6964
member WithDummy: writerCommands: Queue<WriterEvent> * ?keepPageWidth: bool -> Context
7065
member WithShortExpression: maxWidth: int * ?startColumn: int -> Context
7166
member Column: int

src/Fantomas.Core/Selection.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ let formatSelection
404404
MaxLineLength = maxLineLength }
405405

406406
let formattedSelection =
407-
let context = Context.Context.Create true selectionConfig
407+
let context = Context.Context.Create selectionConfig
408408

409409
match tree with
410410
| TreeForSelection.Unsupported ->

0 commit comments

Comments
 (0)