-
Notifications
You must be signed in to change notification settings - Fork 156
/
State.fs
294 lines (232 loc) · 10.2 KB
/
State.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
namespace FsAutoComplete
open System
open System.Collections.Concurrent
open System.Threading
open FSharp.Compiler.Text
open Ionide.ProjInfo.ProjectSystem
open FSharp.UMX
open System.Diagnostics
open FSharp.Compiler.EditorServices
open FSharp.Compiler.Syntax
open FSharp.Compiler.CodeAnalysis
open FsToolkit.ErrorHandling
[<AutoOpen>]
module ProjInfoExtensions =
let private internalGetCSharpReferenceInfo =
fun (r: FSharpReferencedProject) ->
let rCase, fields =
FSharp.Reflection.FSharpValue.GetUnionFields(
r,
typeof<FSharpReferencedProject>,
System.Reflection.BindingFlags.Public
||| System.Reflection.BindingFlags.NonPublic
||| System.Reflection.BindingFlags.Instance
)
if rCase.Name = "PEReference" then
let path: string = fields[0] :?> _
let getStamp: unit -> DateTime = fields[1] :?> _
let reader = fields[2]
Some(path, getStamp, reader)
else
None
let private internalGetProjectOptions =
fun (r: FSharpReferencedProject) ->
let rCase, fields =
FSharp.Reflection.FSharpValue.GetUnionFields(
r,
typeof<FSharpReferencedProject>,
System.Reflection.BindingFlags.Public
||| System.Reflection.BindingFlags.NonPublic
||| System.Reflection.BindingFlags.Instance
)
if rCase.Name = "FSharpReference" then
let projOptions: FSharpProjectOptions = rCase.GetFields().[1].GetValue(box r) :?> _
Some projOptions
else
None
type FSharpReferencedProject with
member x.ProjectFilePath =
let rCase, fields =
FSharp.Reflection.FSharpValue.GetUnionFields(
x,
typeof<FSharpReferencedProject>,
System.Reflection.BindingFlags.Public
||| System.Reflection.BindingFlags.NonPublic
||| System.Reflection.BindingFlags.Instance
)
if rCase.Name = "FSharpReference" then
(fields[1] :?> FSharpProjectOptions).ProjectFileName |> Some
else
None
type FSharpProjectOptions with
member x.OutputDll =
x.OtherOptions |> Array.find (fun o -> o.StartsWith("-o:")) |> (fun s -> s[3..])
member x.SourceFilesThatThisFileDependsOn(file: string<LocalPath>) =
let untagged = UMX.untag file
match Array.tryFindIndex ((=) untagged) x.SourceFiles with
| None -> [||]
| Some 0 -> [||] // at the start, so no possible dependents
| Some index -> x.SourceFiles[0..index]
member x.SourceFilesThatDependOnFile(file: string<LocalPath>) =
let untagged = UMX.untag file
match Array.tryFindIndex ((=) untagged) x.SourceFiles with
| None -> [||]
| Some index when index < x.SourceFiles.Length -> x.SourceFiles[index + 1 ..]
| Some index -> [||] // at the end, so return empty list
type ProjectController with
/// returns all projects that depend on this one, transitively
member x.GetDependentProjectsOfProjects(ps: FSharpProjectOptions list) : list<FSharpProjectOptions> =
let projectSnapshot = x.ProjectOptions |> Seq.map snd
let allDependents = System.Collections.Generic.HashSet<FSharpProjectOptions>()
let currentPass = ResizeArray()
currentPass.AddRange(ps |> List.map (fun p -> p.ProjectFileName))
let mutable continueAlong = true
while continueAlong do
let dependents =
projectSnapshot
|> Seq.filter (fun p ->
p.ReferencedProjects
|> Seq.exists (fun r ->
match r.ProjectFilePath with
| None -> false
| Some p -> currentPass.Contains(p)))
if Seq.isEmpty dependents then
continueAlong <- false
currentPass.Clear()
else
for d in dependents do
allDependents.Add d |> ignore<bool>
currentPass.Clear()
currentPass.AddRange(dependents |> Seq.map (fun p -> p.ProjectFileName))
Seq.toList allDependents
/// crawls the project set and returns projects that contain a given file
member x.ProjectsThatContainFile(file: string<LocalPath>) =
let untagged = UMX.untag file
x.ProjectOptions
|> Seq.choose (fun (_, p) ->
if p.SourceFiles |> Array.contains untagged then
Some p
else
None)
|> Seq.distinct
|> Seq.toList
type DeclName = string
type CompletionNamespaceInsert =
{ Namespace: string
Position: Position
Scope: ScopeKind }
[<DebuggerDisplay("{DebugString}")>]
type State =
{ Files: ConcurrentDictionary<string<LocalPath>, VolatileFile>
LastCheckedVersion: ConcurrentDictionary<string<LocalPath>, int>
ProjectController: ProjectController
HelpText: ConcurrentDictionary<DeclName, ToolTipText>
Declarations: ConcurrentDictionary<DeclName, DeclarationListItem * Position * string<LocalPath>>
CompletionNamespaceInsert: ConcurrentDictionary<DeclName, CompletionNamespaceInsert>
mutable CurrentAST: ParsedInput option
NavigationDeclarations: ConcurrentDictionary<string<LocalPath>, NavigationTopLevelDeclaration[]>
CancellationTokens: ConcurrentDictionary<string<LocalPath>, CancellationTokenSource list>
ScriptProjectOptions: ConcurrentDictionary<string<LocalPath>, int * FSharpProjectOptions>
mutable ColorizationOutput: bool
WorkspaceStateDirectory: System.IO.DirectoryInfo }
member x.DebugString =
$"{x.Files.Count} Files, {x.ProjectController.ProjectOptions |> Seq.length} Projects"
static member Initial toolsPath workspaceStateDir workspaceLoaderFactory =
{ Files = ConcurrentDictionary()
LastCheckedVersion = ConcurrentDictionary()
ProjectController = new ProjectController(toolsPath, workspaceLoaderFactory)
HelpText = ConcurrentDictionary()
Declarations = ConcurrentDictionary()
CurrentAST = None
CompletionNamespaceInsert = ConcurrentDictionary()
CancellationTokens = ConcurrentDictionary()
NavigationDeclarations = ConcurrentDictionary()
ScriptProjectOptions = ConcurrentDictionary()
ColorizationOutput = false
WorkspaceStateDirectory = workspaceStateDir }
member x.RefreshCheckerOptions(file: string<LocalPath>, text: NamedText) : FSharpProjectOptions option =
x.ProjectController.GetProjectOptions(UMX.untag file)
|> Option.map (fun opts ->
x.Files.[file] <-
{ Lines = text
Touched = DateTime.Now
Version = None }
opts)
member x.GetProjectOptions(file: string<LocalPath>) : FSharpProjectOptions option =
x.ProjectController.GetProjectOptions(UMX.untag file)
member x.GetProjectOptions'(file: string<LocalPath>) : FSharpProjectOptions =
(x.ProjectController.GetProjectOptions(UMX.untag file)).Value
member x.RemoveProjectOptions(file: string<LocalPath>) : unit =
x.ProjectController.RemoveProjectOptions(UMX.untag file)
member x.FSharpProjectOptions = x.ProjectController.ProjectOptions
member x.TryGetFileVersion(file: string<LocalPath>) : int option =
x.Files.TryFind file |> Option.bind (fun f -> f.Version)
member x.TryGetLastCheckedVersion(file: string<LocalPath>) : int option = x.LastCheckedVersion.TryFind file
member x.SetFileVersion (file: string<LocalPath>) (version: int) =
x.Files.TryFind file
|> Option.iter (fun n ->
let fileState = { n with Version = Some version }
x.Files.[file] <- fileState)
member x.SetLastCheckedVersion (file: string<LocalPath>) (version: int) = x.LastCheckedVersion.[file] <- version
member x.AddFileTextAndCheckerOptions(file: string<LocalPath>, text: NamedText, opts, version) =
let fileState =
{ Lines = text
Touched = DateTime.Now
Version = version }
x.Files.[file] <- fileState
x.ProjectController.SetProjectOptions(UMX.untag file, opts)
member x.AddFileText(file: string<LocalPath>, text: NamedText, version) =
let fileState =
{ Lines = text
Touched = DateTime.Now
Version = version }
x.Files.[file] <- fileState
member x.AddCancellationToken(file: string<LocalPath>, token: CancellationTokenSource) =
x.CancellationTokens.AddOrUpdate(file, [ token ], (fun _ lst -> token :: lst))
|> ignore
member x.GetCancellationTokens(file: string<LocalPath>) =
let lst = x.CancellationTokens.GetOrAdd(file, (fun _ -> []))
x.CancellationTokens.TryRemove(file) |> ignore
lst
static member private FileWithoutProjectOptions(file: string<LocalPath>) =
let opts = [| yield sprintf "-r:%s" Environment.fsharpCore; yield "--noframework" |]
{ ProjectId = Some((UMX.untag file) + ".fsproj")
ProjectFileName = (UMX.untag file) + ".fsproj"
SourceFiles = [| (UMX.untag file) |]
OtherOptions = opts // "--noframework"
ReferencedProjects = [||]
IsIncompleteTypeCheckEnvironment = true
UseScriptResolutionRules = false
LoadTime = DateTime.Now
UnresolvedReferences = None
OriginalLoadReferences = []
Stamp = None }
member x.TryGetFileCheckerOptionsWithLines
(file: string<LocalPath>)
: ResultOrString<FSharpProjectOptions * NamedText> =
match x.Files.TryFind(file) with
| None -> ResultOrString.Error(sprintf "File '%s' not parsed" (UMX.untag file))
| Some (volFile) ->
match x.ProjectController.GetProjectOptions((UMX.untag file)) with
| None -> Ok(State.FileWithoutProjectOptions(file), volFile.Lines)
| Some opts -> Ok(opts, volFile.Lines)
member x.TryGetFileCheckerOptionsWithSource
(file: string<LocalPath>)
: ResultOrString<FSharpProjectOptions * NamedText> =
match x.TryGetFileCheckerOptionsWithLines(file) with
| ResultOrString.Error x -> ResultOrString.Error x
| Ok (opts, lines) -> Ok(opts, lines)
member x.TryGetFileSource(file: string<LocalPath>) : ResultOrString<NamedText> =
match x.Files.TryFind(file) with
| None -> ResultOrString.Error(sprintf "File '%s' not parsed" (UMX.untag file))
| Some f -> Ok f.Lines
member x.TryGetFileCheckerOptionsWithLinesAndLineStr
(
file: string<LocalPath>,
pos: Position
) : ResultOrString<FSharpProjectOptions * NamedText * LineStr> =
result {
let! (opts, text) = x.TryGetFileCheckerOptionsWithLines(file)
let! line = text.GetLine pos |> Result.ofOption (fun _ -> "Position is out of range")
return (opts, text, line)
}