|
1 | | -namespace JetBrains.ReSharper.Plugins.FSharp.Shim.FileSystem |
2 | | - |
3 | | -open System |
4 | | -open System.IO |
5 | | -open System.Collections.Concurrent |
6 | | -open System.Runtime.InteropServices |
7 | | -open System.Text |
8 | | -open JetBrains.Application.changes |
9 | | -open JetBrains.DataFlow |
10 | | -open JetBrains.Diagnostics |
11 | | -open JetBrains.DocumentManagers |
12 | | -open JetBrains.DocumentManagers.impl |
13 | | -open JetBrains.DocumentModel |
14 | | -open JetBrains.Lifetimes |
15 | | -open JetBrains.ProjectModel |
16 | | -open JetBrains.ReSharper.Plugins.FSharp |
17 | | -open JetBrains.ReSharper.Resources.Shell |
18 | | - |
19 | | -type FSharpSource = |
20 | | - | Exists of Source: byte[] * Timestamp: DateTime |
21 | | - | NotExists |
22 | | - |
23 | | - member x.ToRdFSharpSource() = |
24 | | - match x with |
25 | | - | Exists(source, timestamp) -> RdFSharpSource(Encoding.UTF8.GetString(source), timestamp) |
26 | | - | _ -> RdFSharpSource("NotExists", DateTime.MinValue) |
27 | | - |
28 | | -// Potentially drop this cache if transparent compiler is used? |
29 | | -// Might not be used if transparent compiler |
30 | | - |
31 | | -[<SolutionComponent>] |
32 | | -type FSharpSourceCache(lifetime: Lifetime, solution: ISolution, changeManager, documentManager: DocumentManager, |
33 | | - solutionDocumentChangeProvider: SolutionDocumentChangeProvider, fileExtensions: IProjectFileExtensions, |
34 | | - logger: ILogger) = |
35 | | - inherit FileSystemShimChangeProvider(Lifetime.Define(lifetime).Lifetime, changeManager) |
36 | | - |
37 | | - let fileUpdated = new Signal<VirtualFileSystemPath>("FSharpSourceCache.fileUpdated") |
38 | | - |
39 | | - let [<Literal>] RemoveFileChangeType = |
40 | | - ProjectModelChangeType.REMOVED ||| ProjectModelChangeType.MOVED_OUT |
41 | | - |
42 | | - let [<Literal>] UpdateFileChangeType = |
43 | | - ProjectModelChangeType.EXTERNAL_CHANGE ||| ProjectModelChangeType.ADDED ||| ProjectModelChangeType.MOVED_IN |
44 | | - |
45 | | - let files = ConcurrentDictionary<VirtualFileSystemPath, FSharpSource>() |
46 | | - |
47 | | - let getText (document: IDocument) = |
48 | | - Encoding.UTF8.GetBytes(document.GetText()) |
49 | | - |
50 | | - let tryAddSource (path: VirtualFileSystemPath) = |
51 | | - let mutable source = None |
52 | | - ReadLockCookie.TryExecute(fun _ -> |
53 | | - match files.TryGetValue(path) with |
54 | | - | true, value -> source <- Some value |
55 | | - | _ -> |
56 | | - |
57 | | - let document = documentManager.GetOrCreateDocument(path) |
58 | | - if isNull document then () else |
59 | | - |
60 | | - logger.Trace("Add: tryAddSource: {0}", path) |
61 | | - source <- |
62 | | - if path.ExistsFile then |
63 | | - Some(Exists(getText document, File.GetLastWriteTimeUtc(path.FullPath))) |
64 | | - else |
65 | | - Some(NotExists) |
66 | | - |
67 | | - files[path] <- source.Value |
68 | | - ) |> ignore |
69 | | - |
70 | | - source |
71 | | - |
72 | | - let isApplicable (path: VirtualFileSystemPath) = |
73 | | - // todo: support FCS fake paths like `startup`, prevent going to FS to check existence, etc. |
74 | | - not path.IsEmpty && path.IsAbsolute && fileExtensions.GetFileType(path).Is<FSharpProjectFileType>() |
75 | | - |
76 | | - let applyChange (projectFile: IProjectFile) (document: IDocument) changeSource = |
77 | | - let path = projectFile.Location |
78 | | - if not (isApplicable path) then () else |
79 | | - |
80 | | - let text = getText document |
81 | | - |
82 | | - let mutable fsSource = Unchecked.defaultof<_> |
83 | | - match files.TryGetValue(path, &fsSource), fsSource with |
84 | | - | true, Exists(source, _) when source = text -> () |
85 | | - | _ -> |
86 | | - |
87 | | - logger.Trace("Add: {0} change: {1}", changeSource, path) |
88 | | - files[path] <- Exists(text, DateTime.UtcNow) |
89 | | - fileUpdated.Fire(path) |
90 | | - |
91 | | - member x.FileUpdated = fileUpdated |
92 | | - |
93 | | - member x.TryGetSource(path: VirtualFileSystemPath, [<Out>] source: byref<FSharpSource>) = |
94 | | - match files.TryGetValue(path) with |
95 | | - | true, value -> source <- value; true |
96 | | - | _ -> |
97 | | - |
98 | | - match tryAddSource path with |
99 | | - | Some v -> source <- v; true |
100 | | - | _ -> false |
101 | | - |
102 | | - override this.ReadFile(path, useMemoryMappedFile, shouldShadowCopy) = |
103 | | - if not (isApplicable path) then base.ReadFile(path, useMemoryMappedFile, shouldShadowCopy) else |
104 | | - |
105 | | - match this.TryGetSource(path) with |
106 | | - | true, Exists(source, _) -> new MemoryStream(source) :> _ |
107 | | - | true, NotExists -> failwithf $"Reading not existing file: {path}" |
108 | | - | _ -> |
109 | | - |
110 | | - logger.Trace("Miss: FileStreamReadShim miss: {0}", path) |
111 | | - base.ReadFile(path, useMemoryMappedFile, shouldShadowCopy) |
112 | | - |
113 | | - override x.GetLastWriteTime(path) = |
114 | | - if not (isApplicable path) then base.GetLastWriteTime(path) else |
115 | | - |
116 | | - match x.TryGetSource(path) with |
117 | | - | true, Exists(_, timestamp) -> timestamp |
118 | | - | true, NotExists -> FileNotFoundException($"GetLastWriteTime: NotExists: {path}") |> raise |
119 | | - | _ -> |
120 | | - |
121 | | - logger.Trace("GetLastWriteTime: miss: {0}", path) |
122 | | - base.GetLastWriteTime(path) |
123 | | - |
124 | | - override x.ExistsFile(path) = |
125 | | - if not (isApplicable path) then base.ExistsFile(path) else |
126 | | - |
127 | | - match files.TryGetValue(path) with |
128 | | - | true, Exists _ -> true |
129 | | - | true, NotExists -> false |
130 | | - | _ -> |
131 | | - |
132 | | - match tryAddSource path with |
133 | | - | Some (Exists _) -> true |
134 | | - | Some NotExists -> false |
135 | | - | _ -> |
136 | | - |
137 | | - logger.Trace("Miss: Exists: {0}", path) |
138 | | - base.ExistsFile(path) |
139 | | - |
140 | | - member x.ProcessDocumentChange(change: DocumentChange) = |
141 | | - let projectFile = |
142 | | - match change with |
143 | | - | :? ProjectFileDocumentChange as change -> change.ProjectFile |
144 | | - | :? ProjectFileDocumentCopyChange as change -> change.ProjectFile |
145 | | - | _ -> null |
146 | | - |
147 | | - if isNotNull projectFile && projectFile.LanguageType.Is<FSharpProjectFileType>() then |
148 | | - applyChange projectFile change.Document "Document" |
149 | | - |
150 | | - member x.ProcessProjectModelChange(change: ProjectModelChange) = |
151 | | - if isNull change then () else |
152 | | - |
153 | | - let visitor = |
154 | | - { new RecursiveProjectModelChangeDeltaVisitor() with |
155 | | - override v.VisitItemDelta(change) = |
156 | | - base.VisitItemDelta(change) |
157 | | - |
158 | | - if change.ContainsChangeType(RemoveFileChangeType) then |
159 | | - files.TryRemove(change.OldLocation) |> ignore |
160 | | - |
161 | | - if change.ContainsChangeType(UpdateFileChangeType) then |
162 | | - let projectFile = change.ProjectItem.As<IProjectFile>() |
163 | | - if isValid projectFile && projectFile.LanguageType.Is<FSharpProjectFileType>() then |
164 | | - let document = projectFile.GetDocument() |
165 | | - if isNotNull document then |
166 | | - applyChange projectFile document "Project model" } |
167 | | - |
168 | | - visitor.VisitDelta(change) |
169 | | - |
170 | | - override x.Execute(changeEventArgs: ChangeEventArgs) = |
171 | | - let changeMap = changeEventArgs.ChangeMap |
172 | | - x.ProcessDocumentChange(changeMap.GetChange<DocumentChange>(solutionDocumentChangeProvider)) |
173 | | - x.ProcessProjectModelChange(changeMap.GetChange<ProjectModelChange>(solution)) |
174 | | - |
175 | | - member x.GetRdFSharpSource(fileName: string): RdFSharpSource = |
176 | | - let path = VirtualFileSystemPath.TryParse(fileName, InteractionContext.SolutionContext) |
177 | | - let mutable fsSource = Unchecked.defaultof<FSharpSource> |
178 | | - if x.TryGetSource(path, &fsSource) then fsSource.ToRdFSharpSource() else null |
| 1 | +namespace JetBrains.ReSharper.Plugins.FSharp.Shim.FileSystem |
| 2 | + |
| 3 | +open System |
| 4 | +open System.IO |
| 5 | +open System.Collections.Concurrent |
| 6 | +open System.Runtime.InteropServices |
| 7 | +open System.Text |
| 8 | +open JetBrains.Application.changes |
| 9 | +open JetBrains.DataFlow |
| 10 | +open JetBrains.Diagnostics |
| 11 | +open JetBrains.DocumentManagers |
| 12 | +open JetBrains.DocumentManagers.impl |
| 13 | +open JetBrains.DocumentModel |
| 14 | +open JetBrains.Lifetimes |
| 15 | +open JetBrains.ProjectModel |
| 16 | +open JetBrains.ReSharper.Plugins.FSharp |
| 17 | +open JetBrains.ReSharper.Resources.Shell |
| 18 | + |
| 19 | +type FSharpSource = |
| 20 | + | Exists of Source: byte[] * Timestamp: DateTime |
| 21 | + | NotExists |
| 22 | + |
| 23 | + member x.ToRdFSharpSource() = |
| 24 | + match x with |
| 25 | + | Exists(source, timestamp) -> RdFSharpSource(Encoding.UTF8.GetString(source), timestamp) |
| 26 | + | _ -> RdFSharpSource("NotExists", DateTime.MinValue) |
| 27 | + |
| 28 | +// Potentially drop this cache if transparent compiler is used? |
| 29 | +// Might not be used if transparent compiler |
| 30 | + |
| 31 | +[<SolutionComponent>] |
| 32 | +type FSharpSourceCache(lifetime: Lifetime, solution: ISolution, changeManager, documentManager: DocumentManager, |
| 33 | + solutionDocumentChangeProvider: SolutionDocumentChangeProvider, fileExtensions: IProjectFileExtensions, |
| 34 | + logger: ILogger) = |
| 35 | + inherit FileSystemShimChangeProvider(Lifetime.Define(lifetime).Lifetime, changeManager) |
| 36 | + |
| 37 | + let fileUpdated = new Signal<VirtualFileSystemPath>("FSharpSourceCache.fileUpdated") |
| 38 | + |
| 39 | + let [<Literal>] RemoveFileChangeType = |
| 40 | + ProjectModelChangeType.REMOVED ||| ProjectModelChangeType.MOVED_OUT |
| 41 | + |
| 42 | + let [<Literal>] UpdateFileChangeType = |
| 43 | + ProjectModelChangeType.EXTERNAL_CHANGE ||| ProjectModelChangeType.ADDED ||| ProjectModelChangeType.MOVED_IN |
| 44 | + |
| 45 | + let files = ConcurrentDictionary<VirtualFileSystemPath, FSharpSource>() |
| 46 | + |
| 47 | + let getText (document: IDocument) = |
| 48 | + Encoding.UTF8.GetBytes(document.GetText()) |
| 49 | + |
| 50 | + let tryAddSource (path: VirtualFileSystemPath) = |
| 51 | + let mutable source = None |
| 52 | + ReadLockCookie.TryExecute(fun _ -> |
| 53 | + match files.TryGetValue(path) with |
| 54 | + | true, value -> source <- Some value |
| 55 | + | _ -> |
| 56 | + |
| 57 | + let document = documentManager.GetOrCreateDocument(path) |
| 58 | + if isNull document then () else |
| 59 | + |
| 60 | + logger.Trace("Add: tryAddSource: {0}", path) |
| 61 | + source <- |
| 62 | + if path.ExistsFile then |
| 63 | + Some(Exists(getText document, File.GetLastWriteTimeUtc(path.FullPath))) |
| 64 | + else |
| 65 | + Some(NotExists) |
| 66 | + |
| 67 | + files[path] <- source.Value |
| 68 | + ) |> ignore |
| 69 | + |
| 70 | + source |
| 71 | + |
| 72 | + let isApplicable (path: VirtualFileSystemPath) = |
| 73 | + // todo: support FCS fake paths like `startup`, prevent going to FS to check existence, etc. |
| 74 | + not path.IsEmpty && path.IsAbsolute && fileExtensions.GetFileType(path).Is<FSharpProjectFileType>() |
| 75 | + |
| 76 | + let applyChange (projectFile: IProjectFile) (document: IDocument) changeSource = |
| 77 | + let path = projectFile.Location |
| 78 | + if not (isApplicable path) then () else |
| 79 | + |
| 80 | + let text = getText document |
| 81 | + |
| 82 | + let mutable fsSource = Unchecked.defaultof<_> |
| 83 | + match files.TryGetValue(path, &fsSource), fsSource with |
| 84 | + | true, Exists(source, _) when source = text -> () |
| 85 | + | _ -> |
| 86 | + |
| 87 | + logger.Trace("Add: {0} change: {1}", changeSource, path) |
| 88 | + files[path] <- Exists(text, DateTime.UtcNow) |
| 89 | + fileUpdated.Fire(path) |
| 90 | + |
| 91 | + member x.FileUpdated = fileUpdated |
| 92 | + |
| 93 | + member x.TryGetSource(path: VirtualFileSystemPath, [<Out>] source: byref<FSharpSource>) = |
| 94 | + match files.TryGetValue(path) with |
| 95 | + | true, value -> source <- value; true |
| 96 | + | _ -> |
| 97 | + |
| 98 | + match tryAddSource path with |
| 99 | + | Some v -> source <- v; true |
| 100 | + | _ -> false |
| 101 | + |
| 102 | + override this.ReadFile(path, useMemoryMappedFile, shouldShadowCopy) = |
| 103 | + if not (isApplicable path) then base.ReadFile(path, useMemoryMappedFile, shouldShadowCopy) else |
| 104 | + |
| 105 | + match this.TryGetSource(path) with |
| 106 | + | true, Exists(source, _) -> new MemoryStream(source) :> _ |
| 107 | + | true, NotExists -> failwithf $"Reading not existing file: {path}" |
| 108 | + | _ -> |
| 109 | + |
| 110 | + logger.Trace("Miss: FileStreamReadShim miss: {0}", path) |
| 111 | + base.ReadFile(path, useMemoryMappedFile, shouldShadowCopy) |
| 112 | + |
| 113 | + override x.GetLastWriteTime(path) = |
| 114 | + if not (isApplicable path) then base.GetLastWriteTime(path) else |
| 115 | + |
| 116 | + match x.TryGetSource(path) with |
| 117 | + | true, Exists(_, timestamp) -> timestamp |
| 118 | + | true, NotExists -> FileNotFoundException($"GetLastWriteTime: NotExists: {path}") |> raise |
| 119 | + | _ -> |
| 120 | + |
| 121 | + logger.Trace("GetLastWriteTime: miss: {0}", path) |
| 122 | + base.GetLastWriteTime(path) |
| 123 | + |
| 124 | + override x.ExistsFile(path) = |
| 125 | + if not (isApplicable path) then base.ExistsFile(path) else |
| 126 | + |
| 127 | + match files.TryGetValue(path) with |
| 128 | + | true, Exists _ -> true |
| 129 | + | true, NotExists -> false |
| 130 | + | _ -> |
| 131 | + |
| 132 | + match tryAddSource path with |
| 133 | + | Some (Exists _) -> true |
| 134 | + | Some NotExists -> false |
| 135 | + | _ -> |
| 136 | + |
| 137 | + logger.Trace("Miss: Exists: {0}", path) |
| 138 | + base.ExistsFile(path) |
| 139 | + |
| 140 | + member x.ProcessDocumentChange(change: DocumentChange) = |
| 141 | + let projectFile = |
| 142 | + match change with |
| 143 | + | :? ProjectFileDocumentChange as change -> change.ProjectFile |
| 144 | + | :? ProjectFileDocumentCopyChange as change -> change.ProjectFile |
| 145 | + | _ -> null |
| 146 | + |
| 147 | + if isNotNull projectFile && projectFile.LanguageType.Is<FSharpProjectFileType>() then |
| 148 | + applyChange projectFile change.Document "Document" |
| 149 | + |
| 150 | + member x.ProcessProjectModelChange(change: ProjectModelChange) = |
| 151 | + if isNull change then () else |
| 152 | + |
| 153 | + let visitor = |
| 154 | + { new RecursiveProjectModelChangeDeltaVisitor() with |
| 155 | + override v.VisitItemDelta(change) = |
| 156 | + base.VisitItemDelta(change) |
| 157 | + |
| 158 | + if change.ContainsChangeType(RemoveFileChangeType) then |
| 159 | + files.TryRemove(change.OldLocation) |> ignore |
| 160 | + |
| 161 | + if change.ContainsChangeType(UpdateFileChangeType) then |
| 162 | + let projectFile = change.ProjectItem.As<IProjectFile>() |
| 163 | + if isValid projectFile && projectFile.LanguageType.Is<FSharpProjectFileType>() then |
| 164 | + let document = projectFile.GetDocument() |
| 165 | + if isNotNull document then |
| 166 | + applyChange projectFile document "Project model" } |
| 167 | + |
| 168 | + visitor.VisitDelta(change) |
| 169 | + |
| 170 | + override x.Execute(changeEventArgs: ChangeEventArgs) = |
| 171 | + let changeMap = changeEventArgs.ChangeMap |
| 172 | + x.ProcessDocumentChange(changeMap.GetChange<DocumentChange>(solutionDocumentChangeProvider)) |
| 173 | + x.ProcessProjectModelChange(changeMap.GetChange<ProjectModelChange>(solution)) |
| 174 | + |
| 175 | + member x.GetRdFSharpSource(fileName: string): RdFSharpSource = |
| 176 | + let path = VirtualFileSystemPath.TryParse(fileName, InteractionContext.SolutionContext) |
| 177 | + let mutable fsSource = Unchecked.defaultof<FSharpSource> |
| 178 | + if x.TryGetSource(path, &fsSource) then fsSource.ToRdFSharpSource() else null |
0 commit comments