-
Notifications
You must be signed in to change notification settings - Fork 417
/
Copy pathOmniSharpWorkspace.cs
591 lines (496 loc) · 23 KB
/
OmniSharpWorkspace.cs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.Logging;
using OmniSharp.FileSystem;
using OmniSharp.FileWatching;
using OmniSharp.Roslyn;
using OmniSharp.Roslyn.EditorConfig;
using OmniSharp.Roslyn.Utilities;
using OmniSharp.Utilities;
namespace OmniSharp
{
[Export, Shared]
public class OmniSharpWorkspace : Workspace
{
public bool Initialized
{
get { return isInitialized; }
set
{
if (isInitialized == value) return;
isInitialized = value;
OnInitialized(isInitialized);
}
}
public event Action<bool> OnInitialized = delegate { };
public bool EditorConfigEnabled { get; set; }
public BufferManager BufferManager { get; private set; }
private readonly ILogger<OmniSharpWorkspace> _logger;
private readonly ConcurrentBag<Func<string, Task>> _waitForProjectModelReadyHandlers = new ConcurrentBag<Func<string, Task>>();
private readonly ConcurrentDictionary<string, ProjectInfo> miscDocumentsProjectInfos = new ConcurrentDictionary<string, ProjectInfo>();
private readonly ConcurrentDictionary<ProjectId, Predicate<string>> documentInclusionRulesPerProject = new ConcurrentDictionary<ProjectId, Predicate<string>>();
private bool isInitialized;
[ImportingConstructor]
public OmniSharpWorkspace(HostServicesAggregator aggregator, ILoggerFactory loggerFactory, IFileSystemWatcher fileSystemWatcher)
: base(aggregator.CreateHostServices(), "Custom")
{
BufferManager = new BufferManager(this, loggerFactory, fileSystemWatcher);
_logger = loggerFactory.CreateLogger<OmniSharpWorkspace>();
fileSystemWatcher.WatchDirectories(OnDirectoryRemoved);
}
public override bool CanOpenDocuments => true;
private void OnDirectoryRemoved(string path, FileChangeType changeType)
{
if(changeType == FileChangeType.DirectoryDelete)
{
var docs = CurrentSolution.Projects.SelectMany(x => x.Documents)
.Where(x => x.FilePath.StartsWith(path + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase));
foreach(var doc in docs)
{
OnDocumentRemoved(doc.Id);
}
}
}
public void AddWaitForProjectModelReadyHandler(Func<string, Task> handler)
{
_waitForProjectModelReadyHandlers.Add(handler);
}
public override void OpenDocument(DocumentId documentId, bool activate = true)
{
var doc = this.CurrentSolution.GetDocument(documentId);
if (doc != null)
{
var text = doc.GetTextAsync(CancellationToken.None).WaitAndGetResult(CancellationToken.None);
this.OnDocumentOpened(documentId, text.Container, activate);
}
}
public override void CloseDocument(DocumentId documentId)
{
var doc = this.CurrentSolution.GetDocument(documentId);
if (doc != null)
{
var text = doc.GetTextAsync(CancellationToken.None).WaitAndGetResult(CancellationToken.None);
var version = doc.GetTextVersionAsync(CancellationToken.None).WaitAndGetResult(CancellationToken.None);
var loader = TextLoader.From(TextAndVersion.Create(text, version, doc.FilePath));
this.OnDocumentClosed(documentId, loader);
}
}
public void AddProject(ProjectInfo projectInfo)
{
OnProjectAdded(projectInfo);
}
public void AddDocumentInclusionRuleForProject(ProjectId projectId, Predicate<string> documentPathFilter)
{
documentInclusionRulesPerProject[projectId] = documentPathFilter;
}
public void AddProjectReference(ProjectId projectId, ProjectReference projectReference)
{
OnProjectReferenceAdded(projectId, projectReference);
}
public void RemoveProjectReference(ProjectId projectId, ProjectReference projectReference)
{
OnProjectReferenceRemoved(projectId, projectReference);
}
public void AddMetadataReference(ProjectId projectId, MetadataReference metadataReference)
{
OnMetadataReferenceAdded(projectId, metadataReference);
}
public void RemoveMetadataReference(ProjectId projectId, MetadataReference metadataReference)
{
OnMetadataReferenceRemoved(projectId, metadataReference);
}
public DocumentId TryAddMiscellaneousDocument(string filePath, TextLoader loader, string language)
{
if (GetDocument(filePath) != null)
return null; //if the workspace already knows about this document then it is not a miscellaneous document
var projectInfo = miscDocumentsProjectInfos.GetOrAdd(language, (lang) => CreateMiscFilesProject(lang));
var documentId = AddDocument(projectInfo.Id, filePath, loader);
_logger.LogInformation($"Miscellaneous file: {filePath} added to workspace");
if (!EditorConfigEnabled)
{
return documentId;
}
var analyzerConfigFiles = projectInfo.AnalyzerConfigDocuments.Select(document => document.FilePath);
var newAnalyzerConfigFiles = EditorConfigFinder
.GetEditorConfigPaths(filePath)
.Except(analyzerConfigFiles);
foreach (var analyzerConfigFile in newAnalyzerConfigFiles)
{
AddAnalyzerConfigDocument(projectInfo.Id, analyzerConfigFile);
}
return documentId;
}
public DocumentId TryAddMiscellaneousDocument(string filePath, string language)
{
return TryAddMiscellaneousDocument(filePath, new OmniSharpTextLoader(filePath), language);
}
public bool TryRemoveMiscellaneousDocument(string filePath)
{
var documentId = GetDocumentId(filePath);
if (documentId == null || !IsMiscellaneousDocument(documentId))
return false;
RemoveDocument(documentId);
_logger.LogDebug($"Miscellaneous file: {filePath} removed from workspace");
return true;
}
public void TryPromoteMiscellaneousDocumentsToProject(Project project)
{
if (project == null)
{
throw new ArgumentNullException(nameof(project));
}
var miscProjectInfos = miscDocumentsProjectInfos.Values.ToArray();
for (var i = 0; i < miscProjectInfos.Length; i++)
{
var miscProject = CurrentSolution.GetProject(miscProjectInfos[i].Id);
var documents = miscProject.Documents.ToArray();
for (var j = 0; j < documents.Length; j++)
{
var document = documents[j];
if (FileBelongsToProject(document.FilePath, project))
{
var textLoader = new DelegatingTextLoader(document);
var documentId = DocumentId.CreateNewId(project.Id);
var documentInfo = DocumentInfo.Create(
documentId,
document.FilePath,
filePath: document.FilePath,
loader: textLoader);
// This transitively will remove the document from the misc project.
AddDocument(documentInfo);
}
}
}
}
public void UpdateDiagnosticOptionsForProject(ProjectId projectId, ImmutableDictionary<string, ReportDiagnostic> rules)
{
var project = this.CurrentSolution.GetProject(projectId);
OnCompilationOptionsChanged(projectId, project.CompilationOptions.WithSpecificDiagnosticOptions(rules));
}
public void UpdateCompilationOptionsForProject(ProjectId projectId, CompilationOptions options)
{
OnCompilationOptionsChanged(projectId, options);
}
private ProjectInfo CreateMiscFilesProject(string language)
{
var projectInfo = ProjectInfo.Create(
id: ProjectId.CreateNewId(),
version: VersionStamp.Create(),
name: $"{Configuration.OmniSharpMiscProjectName}.csproj",
metadataReferences: DefaultMetadataReferenceHelper.GetDefaultMetadataReferenceLocations()
.Select(loc => MetadataReference.CreateFromFile(loc)),
assemblyName: Configuration.OmniSharpMiscProjectName,
language: language);
AddProject(projectInfo);
return projectInfo;
}
public void AddDocument(DocumentInfo documentInfo)
{
// if the file has already been added as a misc file,
// because of a possible race condition between the updates of the project systems,
// remove the misc file and add the document as required
TryRemoveMiscellaneousDocument(documentInfo.FilePath);
OnDocumentAdded(documentInfo);
}
public DocumentId AddDocument(ProjectId projectId, string filePath, SourceCodeKind sourceCodeKind = SourceCodeKind.Regular)
{
var project = this.CurrentSolution.GetProject(projectId);
return AddDocument(project, filePath, sourceCodeKind);
}
public DocumentId AddDocument(ProjectId projectId, string filePath, TextLoader loader, SourceCodeKind sourceCodeKind = SourceCodeKind.Regular)
{
var documentId = DocumentId.CreateNewId(projectId);
var project = this.CurrentSolution.GetProject(projectId);
return AddDocument(documentId, project, filePath, loader, sourceCodeKind);
}
public DocumentId AddDocument(Project project, string filePath, SourceCodeKind sourceCodeKind = SourceCodeKind.Regular)
{
var documentId = DocumentId.CreateNewId(project.Id);
return AddDocument(documentId, project, filePath, sourceCodeKind);
}
public DocumentId AddDocument(DocumentId documentId, Project project, string filePath, SourceCodeKind sourceCodeKind = SourceCodeKind.Regular)
{
return AddDocument(documentId, project, filePath, new OmniSharpTextLoader(filePath), sourceCodeKind);
}
internal DocumentId AddDocument(DocumentId documentId, ProjectId projectId, string filePath, TextLoader loader, SourceCodeKind sourceCodeKind = SourceCodeKind.Regular)
{
var project = this.CurrentSolution.GetProject(projectId);
return AddDocument(documentId, project, filePath, loader, sourceCodeKind);
}
internal DocumentId AddDocument(DocumentId documentId, Project project, string filePath, TextLoader loader, SourceCodeKind sourceCodeKind = SourceCodeKind.Regular)
{
var basePath = Path.GetDirectoryName(project.FilePath);
var fullPath = Path.GetDirectoryName(filePath);
IEnumerable<string> folders = null;
// folder computation is best effort. in case of exceptions, we back out because it's not essential for core features
try
{
// find the relative path from project file to our document
var relativeDocumentPath = FileSystemHelper.GetRelativePath(fullPath, basePath);
// only set document's folders if
// 1. relative path was computed
// 2. path is not pointing any level up
if (relativeDocumentPath != null && !relativeDocumentPath.StartsWith(".."))
{
folders = relativeDocumentPath?.Split(new[] { Path.DirectorySeparatorChar });
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, $"An error occurred when computing a relative path from {basePath} to {fullPath}. Document at {filePath} will be processed without folder structure.");
}
var documentInfo = DocumentInfo.Create(documentId, Path.GetFileName(filePath), folders: folders, filePath: filePath, loader: loader, sourceCodeKind: sourceCodeKind);
AddDocument(documentInfo);
return documentId;
}
public void RemoveDocument(DocumentId documentId)
{
OnDocumentRemoved(documentId);
}
public void RemoveProject(ProjectId projectId)
{
OnProjectRemoved(projectId);
}
public void SetCompilationOptions(ProjectId projectId, CompilationOptions options)
{
OnCompilationOptionsChanged(projectId, options);
}
public void SetParseOptions(ProjectId projectId, ParseOptions parseOptions)
{
OnParseOptionsChanged(projectId, parseOptions);
}
public void OnDocumentChanged(DocumentId documentId, SourceText text)
{
OnDocumentTextChanged(documentId, text, PreservationMode.PreserveIdentity);
}
public DocumentId GetDocumentId(string filePath)
{
var documentIds = CurrentSolution.GetDocumentIdsWithFilePath(filePath);
return documentIds.FirstOrDefault();
}
public IEnumerable<Document> GetDocuments(string filePath)
{
return CurrentSolution
.GetDocumentIdsWithFilePath(filePath)
.Select(id => CurrentSolution.GetDocument(id));
}
public Document GetDocument(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath)) return null;
var documentId = GetDocumentId(filePath);
if (documentId == null)
{
return null;
}
return CurrentSolution.GetDocument(documentId);
}
public async Task<IEnumerable<Document>> GetDocumentsFromFullProjectModelAsync(string filePath)
{
await OnWaitForProjectModelReadyAsync(filePath);
return GetDocuments(filePath);
}
public async Task<Document> GetDocumentFromFullProjectModelAsync(string filePath)
{
await OnWaitForProjectModelReadyAsync(filePath);
return GetDocument(filePath);
}
public override bool CanApplyChange(ApplyChangesKind feature)
{
return true;
}
internal bool FileBelongsToProject(string fileName, Project project)
{
if (string.IsNullOrWhiteSpace(project.FilePath) ||
string.IsNullOrWhiteSpace(fileName))
{
return false;
}
// File path needs to be checked against any rules defined by the specific project system. (e.g. MSBuild default excluded folders)
if (documentInclusionRulesPerProject.TryGetValue(project.Id, out Predicate<string> documentInclusionFilter))
{
return documentInclusionFilter(fileName);
}
// if no custom rule set for this ProjectId, fallback to simple directory heuristic.
var fileDirectory = new FileInfo(fileName).Directory;
var projectPath = project.FilePath;
var projectDirectory = new FileInfo(projectPath).Directory.FullName;
var otherProjectDirectories = CurrentSolution.Projects
.Where(p => p != project && !string.IsNullOrWhiteSpace(p.FilePath))
.Select(p => new FileInfo(p.FilePath).Directory.FullName)
.ToImmutableArray();
while (fileDirectory != null)
{
if (string.Equals(fileDirectory.FullName, projectDirectory, StringComparison.OrdinalIgnoreCase))
{
return true;
}
// if any project is closer to the file, file should belong to that project.
if (otherProjectDirectories.Contains(fileDirectory.FullName, StringComparer.OrdinalIgnoreCase))
{
return false;
}
fileDirectory = fileDirectory.Parent;
}
return false;
}
protected override void ApplyDocumentRemoved(DocumentId documentId)
{
var document = this.CurrentSolution.GetDocument(documentId);
if (document != null)
{
DeleteDocumentFile(document.Id, document.FilePath);
this.OnDocumentRemoved(documentId);
}
}
private void DeleteDocumentFile(DocumentId id, string fullPath)
{
try
{
File.Delete(fullPath);
}
catch (IOException e)
{
LogDeletionException(e, fullPath);
}
catch (NotSupportedException e)
{
LogDeletionException(e, fullPath);
}
catch (UnauthorizedAccessException e)
{
LogDeletionException(e, fullPath);
}
}
private void LogDeletionException(Exception e, string filePath)
{
_logger.LogError(e, $"Error deleting file {filePath}");
}
protected override void ApplyDocumentAdded(DocumentInfo info, SourceText text)
{
var fullPath = info.FilePath;
this.OnDocumentAdded(info);
if (text != null)
{
this.SaveDocumentText(info.Id, fullPath, text, text.Encoding ?? Encoding.UTF8);
}
}
private void SaveDocumentText(DocumentId id, string fullPath, SourceText newText, Encoding encoding)
{
try
{
var dir = Path.GetDirectoryName(fullPath);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
using (var writer = new StreamWriter(fullPath, append: false, encoding: encoding))
{
newText.Write(writer);
}
}
catch (IOException e)
{
_logger.LogError(e, $"Error saving document {fullPath}");
}
}
public bool IsCapableOfSemanticDiagnostics(Document document)
{
return !IsMiscellaneousDocument(document.Id);
}
private bool IsMiscellaneousDocument(DocumentId documentId)
{
return miscDocumentsProjectInfos.Where(p => p.Value.Id == documentId.ProjectId).Any();
}
private class DelegatingTextLoader : TextLoader
{
private readonly Document _fromDocument;
public DelegatingTextLoader(Document fromDocument)
{
_fromDocument = fromDocument ?? throw new ArgumentNullException(nameof(fromDocument));
}
public override async Task<TextAndVersion> LoadTextAndVersionAsync(
Workspace workspace,
DocumentId documentId,
CancellationToken cancellationToken)
{
var sourceText = await _fromDocument.GetTextAsync();
var version = await _fromDocument.GetTextVersionAsync();
var textAndVersion = TextAndVersion.Create(sourceText, version);
return textAndVersion;
}
}
private Task OnWaitForProjectModelReadyAsync(string filePath)
{
return Task.WhenAll(_waitForProjectModelReadyHandlers.Select(h => h(filePath)));
}
public void SetAnalyzerReferences(ProjectId id, ImmutableArray<AnalyzerFileReference> analyzerReferences)
{
var project = this.CurrentSolution.GetProject(id);
var refsToAdd = analyzerReferences.Where(newRef => project.AnalyzerReferences.All(oldRef => oldRef.Display != newRef.Display));
var refsToRemove = project.AnalyzerReferences.Where(newRef => analyzerReferences.All(oldRef => oldRef.Display != newRef.Display));
foreach (var toAdd in refsToAdd)
{
_logger.LogInformation($"Adding analyzer reference: {toAdd.FullPath}");
base.OnAnalyzerReferenceAdded(id, toAdd);
}
foreach (var toRemove in refsToRemove)
{
_logger.LogInformation($"Removing analyzer reference: {toRemove.FullPath}");
base.OnAnalyzerReferenceRemoved(id, toRemove);
}
}
public void AddAdditionalDocument(ProjectId projectId, string filePath)
{
var documentId = DocumentId.CreateNewId(projectId);
var loader = new OmniSharpTextLoader(filePath);
var documentInfo = DocumentInfo.Create(documentId, Path.GetFileName(filePath), filePath: filePath, loader: loader);
OnAdditionalDocumentAdded(documentInfo);
}
public void AddAnalyzerConfigDocument(ProjectId projectId, string filePath)
{
var documentId = DocumentId.CreateNewId(projectId);
var loader = new OmniSharpTextLoader(filePath);
var documentInfo = DocumentInfo.Create(documentId, Path.GetFileName(filePath), filePath: filePath, loader: loader);
OnAnalyzerConfigDocumentAdded(documentInfo);
}
public void ReloadAnalyzerConfigDocument(DocumentId documentId, string filePath)
{
var loader = new OmniSharpTextLoader(filePath);
OnAnalyzerConfigDocumentTextLoaderChanged(documentId, loader);
}
public void RemoveAdditionalDocument(DocumentId documentId)
{
OnAdditionalDocumentRemoved(documentId);
}
public void RemoveAnalyzerConfigDocument(DocumentId documentId)
{
OnAnalyzerConfigDocumentRemoved(documentId);
}
protected override void ApplyProjectChanges(ProjectChanges projectChanges)
{
// since Roslyn currently doesn't handle DefaultNamespace changes via ApplyProjectChanges
// and OnDefaultNamespaceChanged is internal, we use reflection for now
if (projectChanges.NewProject.DefaultNamespace != projectChanges.OldProject.DefaultNamespace)
{
var onDefaultNamespaceChanged = this.GetType().GetMethod("OnDefaultNamespaceChanged", BindingFlags.Instance | BindingFlags.NonPublic);
if (onDefaultNamespaceChanged != null)
{
onDefaultNamespaceChanged.Invoke(this, new object[] { projectChanges.ProjectId, projectChanges.NewProject.DefaultNamespace });
}
}
base.ApplyProjectChanges(projectChanges);
}
}
}