-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathDidChangeWatchedFilesHandler.cs
More file actions
123 lines (105 loc) · 4.26 KB
/
Copy pathDidChangeWatchedFilesHandler.cs
File metadata and controls
123 lines (105 loc) · 4.26 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.Configuration;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Workspace;
// Using an alias since this is conflicting with System.IO.FileSystemWatcher and ends up really finicky.
using OmniSharpFileSystemWatcher = OmniSharp.Extensions.LanguageServer.Protocol.Models.FileSystemWatcher;
namespace Microsoft.PowerShell.EditorServices.Handlers;
/// <summary>
/// Receives file change notifications from the client for any file in the workspace, including those
/// that are not considered opened by the client. This handler is used to allow us to scan the
/// workspace only once when the language server starts.
/// </summary>
internal class DidChangeWatchedFilesHandler : IDidChangeWatchedFilesHandler
{
private readonly WorkspaceService _workspaceService;
private readonly ConfigurationService _configurationService;
public DidChangeWatchedFilesHandler(
WorkspaceService workspaceService,
ConfigurationService configurationService)
{
_workspaceService = workspaceService;
_configurationService = configurationService;
}
public DidChangeWatchedFilesRegistrationOptions GetRegistrationOptions(
DidChangeWatchedFilesCapability capability,
ClientCapabilities clientCapabilities)
#pragma warning disable CS8601 // Possible null reference assignment (it's from the library).
=> new()
{
Watchers = new[]
{
new OmniSharpFileSystemWatcher()
{
GlobPattern = "**/*.{ps1,psm1}",
Kind = WatchKind.Create | WatchKind.Delete | WatchKind.Change,
},
},
};
#pragma warning restore CS8601 // Possible null reference assignment.
public Task<Unit> Handle(DidChangeWatchedFilesParams request, CancellationToken cancellationToken)
{
LanguageServerSettings currentSettings = _configurationService.CurrentSettings;
if (currentSettings.AnalyzeOpenDocumentsOnly)
{
return Task.FromResult(Unit.Value);
}
// Honor `search.exclude` settings in the watcher.
Matcher matcher = new();
matcher.AddExcludePatterns(_workspaceService.ExcludeFilesGlob);
foreach (FileEvent change in request.Changes)
{
string changePath = change.Uri.ToUri().IsFile
? change.Uri.GetFileSystemPath()
: change.Uri.ToUri().AbsolutePath;
if (matcher.Match(changePath).HasMatches)
{
continue;
}
if (!_workspaceService.TryGetFile(change.Uri, out ScriptFile scriptFile))
{
continue;
}
if (change.Type is FileChangeType.Created)
{
// We've already triggered adding the file to `OpenedFiles` via `TryGetFile`.
continue;
}
if (change.Type is FileChangeType.Deleted)
{
_workspaceService.CloseFile(scriptFile);
continue;
}
if (change.Type is FileChangeType.Changed)
{
// If the file is opened by the editor (rather than by us in the background), let
// DidChangeTextDocument handle changes.
if (scriptFile.IsOpen)
{
continue;
}
string fileContents;
try
{
fileContents = _workspaceService.ReadFileContents(change.Uri);
}
catch
{
continue;
}
scriptFile.SetFileContents(fileContents);
scriptFile.References.TagAsChanged();
}
}
return Task.FromResult(Unit.Value);
}
}