-
Notifications
You must be signed in to change notification settings - Fork 301
/
Printer.fs
95 lines (74 loc) · 2.81 KB
/
Printer.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// fsharplint:disable InterfaceNames
module Fable.Transforms.Printer
open System
open Fable
open Fable.AST
type Writer =
inherit IDisposable
abstract AddSourceMapping:
srcLine: int * srcCol: int * genLine: int * genCol: int * file: string option * displayName: string option ->
unit
abstract MakeImportPath: string -> string
abstract Write: string -> Async<unit>
abstract AddLog: msg: string * severity: Fable.Severity * ?range: SourceLocation -> unit
type Printer =
abstract Line: int
abstract Column: int
abstract PushIndentation: unit -> unit
abstract PopIndentation: unit -> unit
abstract Print: string * ?loc: SourceLocation -> unit
abstract PrintNewLine: unit -> unit
abstract AddLocation: SourceLocation option -> unit
abstract MakeImportPath: string -> string
abstract AddLog: msg: string * severity: Fable.Severity * ?range: SourceLocation -> unit
// TODO: Line comments
type PrinterImpl(writer: Writer, ?indent: string) =
let indentSpaces = defaultArg indent " "
let builder = Text.StringBuilder()
let mutable indent = 0
let mutable line = 1
let mutable column = 0
let addLoc (loc: SourceLocation option) =
match loc with
| None -> ()
| Some loc ->
writer.AddSourceMapping(
srcLine = loc.start.line,
srcCol = loc.start.column,
genLine = line,
genCol = column,
file = loc.File,
displayName = loc.DisplayName
)
member _.Flush() : Async<unit> =
async {
if builder.Length > 0 then
do! writer.Write(builder.ToString())
builder.Clear() |> ignore
}
interface IDisposable with
member _.Dispose() = writer.Dispose()
interface Printer with
member _.Line = line
member _.Column = column
member _.PrintNewLine() =
builder.AppendLine() |> ignore
line <- line + 1
column <- 0
member _.PushIndentation() = indent <- indent + 1
member _.PopIndentation() =
if indent > 0 then
indent <- indent - 1
member _.AddLocation(loc) = addLoc loc
member _.Print(str: string, ?loc) =
if not (String.IsNullOrEmpty(str)) then
addLoc loc
if column = 0 then
let indent = String.replicate indent indentSpaces
builder.Append(indent) |> ignore
column <- indent.Length
builder.Append(str) |> ignore
column <- column + str.Length
member _.MakeImportPath(path) = writer.MakeImportPath(path)
member _.AddLog(msg, severity, ?range) =
writer.AddLog(msg, severity, ?range = range)