forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestHelpers.fs
94 lines (77 loc) · 3.98 KB
/
TestHelpers.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
module Fantomas.Tests.TestHelper
open FsUnit
open System
open Fantomas.FormatConfig
open Fantomas
open Microsoft.FSharp.Compiler.SourceCodeServices
let config = FormatConfig.Default
let newline = "\n"
let argsDotNET451 =
[|"--noframework"; "--debug-"; "--optimize-"; "--tailcalls-";
// Some constants are used in unit tests
"--define:DEBUG"; "--define:TRACE"; "--define:SILVERLIGHT";
@"-r:C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.1.0\FSharp.Core.dll";
@"-r:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\mscorlib.dll";
@"-r:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.dll";
@"-r:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.Core.dll";
@"-r:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.Drawing.dll";
@"-r:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.Numerics.dll";
@"-r:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.Windows.Forms.dll"|]
let projectOptions =
fun fileName ->
{ ProjectFileName = @"C:\Project.fsproj"
ProjectFileNames = [| fileName |]
OtherOptions = argsDotNET451
ReferencedProjects = Array.empty
IsIncompleteTypeCheckEnvironment = false
UseScriptResolutionRules = true
LoadTime = DateTime.UtcNow
UnresolvedReferences = None }
let sharedChecker = lazy(FSharpChecker.Create())
let formatSourceString isFsiFile (s : string) config =
// On Linux/Mac this will exercise different line endings
let s = s.Replace("\r\n", Environment.NewLine)
let fileName = if isFsiFile then "/src.fsi" else "/src.fsx"
CodeFormatter.FormatDocumentAsync(fileName, s, config, projectOptions fileName, sharedChecker.Value)
|> Async.RunSynchronously
|> fun s -> s.Replace("\r\n", "\n")
let formatSelectionFromString isFsiFile r (s : string) config =
let s = s.Replace("\r\n", Environment.NewLine)
let fileName = if isFsiFile then "/tmp.fsi" else "/tmp.fsx"
CodeFormatter.FormatSelectionInDocumentAsync(fileName, r, s, config, projectOptions fileName, sharedChecker.Value)
|> Async.RunSynchronously
|> fun s -> s.Replace("\r\n", "\n")
let formatSelectionOnly isFsiFile r (s : string) config =
let s = s.Replace("\r\n", Environment.NewLine)
let fileName = if isFsiFile then "/tmp.fsi" else "/tmp.fsx"
CodeFormatter.FormatSelectionAsync(fileName, r, s, config, projectOptions fileName, sharedChecker.Value)
|> Async.RunSynchronously
|> fun s -> s.Replace("\r\n", "\n")
let formatAroundCursor isFsiFile p (s : string) config =
let s = s.Replace("\r\n", Environment.NewLine)
let fileName = if isFsiFile then "/tmp.fsi" else "/tmp.fsx"
CodeFormatter.FormatAroundCursorAsync(fileName, p, s, config, projectOptions fileName, sharedChecker.Value)
|> Async.RunSynchronously
|> fun s -> s.Replace("\r\n", "\n")
let isValidFSharpCode isFsiFile s =
let fileName = if isFsiFile then "/tmp.fsi" else "/tmp.fsx"
CodeFormatter.IsValidFSharpCodeAsync(fileName, s, projectOptions fileName, sharedChecker.Value)
|> Async.RunSynchronously
let parse isFsiFile s =
let fileName = if isFsiFile then "/tmp.fsi" else "/tmp.fsx"
CodeFormatter.ParseAsync(fileName, s, projectOptions fileName, sharedChecker.Value)
|> Async.RunSynchronously
let formatAST a s c =
CodeFormatter.FormatAST(a, "/tmp.fsx",s, c)
let makeRange l1 c1 l2 c2 =
CodeFormatter.MakeRange("/tmp.fsx", l1, c1, l2, c2)
let makePos l1 c1 =
CodeFormatter.MakePos(l1, c1)
let equal x =
let x =
match box x with
| :? String as s -> s.Replace("\r\n", "\n") |> box
| x -> x
equal x
let inline prepend s content = s + content
let inline append s content = content + s