-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy pathMiscellaneousFilesProjectSystem.cs
111 lines (98 loc) · 4.29 KB
/
MiscellaneousFilesProjectSystem.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
using System;
using System.Collections.Generic;
using System.Composition;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using OmniSharp.FileSystem;
using OmniSharp.FileWatching;
using OmniSharp.Mef;
using OmniSharp.Models.WorkspaceInformation;
using OmniSharp.Services;
namespace OmniSharp.MiscellaneousFile
{
[ExtensionOrder(After = ProjectSystemNames.MSBuildProjectSystem)]
[ExtensionOrder(After = ProjectSystemNames.DotNetProjectSystem)]
[ExportProjectSystem(ProjectSystemNames.MiscellaneousFilesProjectSystem), Shared]
public class MiscellaneousFilesProjectSystem : IProjectSystem
{
private const string miscFileExtension = ".cs";
public string Key => ProjectSystemNames.MiscellaneousFilesProjectSystem;
public string Language => LanguageNames.CSharp;
IEnumerable<string> IProjectSystem.Extensions => new[] { miscFileExtension };
public bool EnabledByDefault { get; } = true;
private readonly OmniSharpWorkspace _workspace;
private readonly IFileSystemWatcher _fileSystemWatcher;
private readonly FileSystemHelper _fileSystemHelper;
private readonly List<IWaitableProjectSystem> _projectSystems;
private readonly ILogger _logger;
[ImportingConstructor]
public MiscellaneousFilesProjectSystem(OmniSharpWorkspace workspace, IFileSystemWatcher fileSystemWatcher, FileSystemHelper fileSystemHelper,
ILoggerFactory loggerFactory, [ImportMany] IEnumerable<Lazy<IProjectSystem, ProjectSystemMetadata>> projectSystems)
{
_workspace = workspace;
_fileSystemWatcher = fileSystemWatcher;
_fileSystemHelper = fileSystemHelper;
_logger = loggerFactory.CreateLogger<MiscellaneousFilesProjectSystem>();
_projectSystems = projectSystems
.Where(ps => ps.Metadata.Name == ProjectSystemNames.MSBuildProjectSystem ||
ps.Metadata.Name == ProjectSystemNames.DotNetProjectSystem)
.Select(ps => ps.Value)
.Cast<IWaitableProjectSystem>()
.ToList();
}
Task<object> IProjectSystem.GetProjectModelAsync(string filePath)
{
return Task.FromResult<object>(null);
}
Task<object> IProjectSystem.GetWorkspaceModelAsync(WorkspaceInformationRequest request)
{
return Task.FromResult<object>(null);
}
void IProjectSystem.Initalize(IConfiguration configuration)
{
var allFiles = _fileSystemHelper.GetFiles("**/*.cs");
foreach (var filePath in allFiles)
TryAddMiscellaneousFile(filePath);
_fileSystemWatcher.Watch(miscFileExtension, OnMiscellaneousFileChanged);
}
private async void TryAddMiscellaneousFile(string filePath)
{
//wait for the project systems to finish processing the updates
foreach (var projectSystem in _projectSystems)
{
await projectSystem.WaitForUpdatesAsync();
}
var absoluteFilePath = new FileInfo(filePath).FullName;
if (!File.Exists(absoluteFilePath))
return;
if (_workspace.TryAddMiscellaneousDocument(absoluteFilePath, Language) != null)
{
_logger.LogInformation($"Successfully added file '{absoluteFilePath}' to workspace");
}
}
private void OnMiscellaneousFileChanged(string filePath, FileChangeType changeType)
{
if (changeType == FileChangeType.Unspecified && File.Exists(filePath) ||
changeType == FileChangeType.Create)
{
TryAddMiscellaneousFile(filePath);
}
else if (changeType == FileChangeType.Unspecified && !File.Exists(filePath) ||
changeType == FileChangeType.Delete)
{
RemoveFromWorkspace(filePath);
}
}
private void RemoveFromWorkspace(string filePath)
{
if (_workspace.TryRemoveMiscellaneousDocument(filePath))
{
_logger.LogDebug($"Removed file '{filePath}' from the workspace.");
}
}
}
}