-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathConfigurationHandler.cs
214 lines (188 loc) · 9.42 KB
/
ConfigurationHandler.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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Logging;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.Configuration;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using OmniSharp.Extensions.LanguageServer.Protocol.Window;
using OmniSharp.Extensions.LanguageServer.Protocol.Workspace;
namespace Microsoft.PowerShell.EditorServices.Handlers
{
internal class PsesConfigurationHandler : IDidChangeConfigurationHandler
{
private readonly ILogger _logger;
private readonly WorkspaceService _workspaceService;
private readonly ConfigurationService _configurationService;
private readonly PowerShellContextService _powerShellContextService;
private readonly ILanguageServerFacade _languageServer;
private DidChangeConfigurationCapability _capability;
private bool _profilesLoaded;
private bool _consoleReplStarted;
private bool _cwdSet;
public PsesConfigurationHandler(
ILoggerFactory factory,
WorkspaceService workspaceService,
AnalysisService analysisService,
ConfigurationService configurationService,
PowerShellContextService powerShellContextService,
ILanguageServerFacade languageServer)
{
_logger = factory.CreateLogger<PsesConfigurationHandler>();
_workspaceService = workspaceService;
_configurationService = configurationService;
_powerShellContextService = powerShellContextService;
_languageServer = languageServer;
ConfigurationUpdated += analysisService.OnConfigurationUpdated;
}
public object GetRegistrationOptions()
{
return null;
}
public async Task<Unit> Handle(DidChangeConfigurationParams request, CancellationToken cancellationToken)
{
LanguageServerSettingsWrapper incomingSettings = request.Settings.ToObject<LanguageServerSettingsWrapper>();
if(incomingSettings == null)
{
return await Unit.Task.ConfigureAwait(false);
}
SendFeatureChangesTelemetry(incomingSettings);
bool oldLoadProfiles = _configurationService.CurrentSettings.EnableProfileLoading;
bool oldScriptAnalysisEnabled =
_configurationService.CurrentSettings.ScriptAnalysis.Enable ?? false;
string oldScriptAnalysisSettingsPath =
_configurationService.CurrentSettings.ScriptAnalysis?.SettingsPath;
_configurationService.CurrentSettings.Update(
incomingSettings.Powershell,
_workspaceService.WorkspacePath,
_logger);
if (!this._cwdSet)
{
if (!string.IsNullOrEmpty(_configurationService.CurrentSettings.Cwd)
&& Directory.Exists(_configurationService.CurrentSettings.Cwd))
{
await _powerShellContextService.SetWorkingDirectoryAsync(
_configurationService.CurrentSettings.Cwd,
isPathAlreadyEscaped: false).ConfigureAwait(false);
} else if (_workspaceService.WorkspacePath != null
&& Directory.Exists(_workspaceService.WorkspacePath))
{
await _powerShellContextService.SetWorkingDirectoryAsync(
_workspaceService.WorkspacePath,
isPathAlreadyEscaped: false).ConfigureAwait(false);
}
this._cwdSet = true;
}
if (!this._profilesLoaded &&
_configurationService.CurrentSettings.EnableProfileLoading &&
oldLoadProfiles != _configurationService.CurrentSettings.EnableProfileLoading)
{
await _powerShellContextService.LoadHostProfilesAsync().ConfigureAwait(false);
this._profilesLoaded = true;
}
// Wait until after profiles are loaded (or not, if that's the
// case) before starting the interactive console.
if (!this._consoleReplStarted)
{
// Start the interactive terminal
_powerShellContextService.ConsoleReader.StartCommandLoop();
this._consoleReplStarted = true;
}
// Run any events subscribed to configuration updates
ConfigurationUpdated(this, _configurationService.CurrentSettings);
// Convert the editor file glob patterns into an array for the Workspace
// Both the files.exclude and search.exclude hash tables look like (glob-text, is-enabled):
// "files.exclude" : {
// "Makefile": true,
// "*.html": true,
// "build/*": true
// }
var excludeFilePatterns = new List<string>();
if (incomingSettings.Files?.Exclude != null)
{
foreach(KeyValuePair<string, bool> patternEntry in incomingSettings.Files.Exclude)
{
if (patternEntry.Value) { excludeFilePatterns.Add(patternEntry.Key); }
}
}
if (incomingSettings.Search?.Exclude != null)
{
foreach(KeyValuePair<string, bool> patternEntry in incomingSettings.Search.Exclude)
{
if (patternEntry.Value && !excludeFilePatterns.Contains(patternEntry.Key)) { excludeFilePatterns.Add(patternEntry.Key); }
}
}
_workspaceService.ExcludeFilesGlob = excludeFilePatterns;
// Convert the editor file search options to Workspace properties
if (incomingSettings.Search?.FollowSymlinks != null)
{
_workspaceService.FollowSymlinks = incomingSettings.Search.FollowSymlinks;
}
return await Unit.Task.ConfigureAwait(false);
}
private void SendFeatureChangesTelemetry(LanguageServerSettingsWrapper incomingSettings)
{
var configChanges = new Dictionary<string, bool>();
// Send telemetry if the user opted-out of ScriptAnalysis
if (incomingSettings.Powershell.ScriptAnalysis.Enable == false &&
_configurationService.CurrentSettings.ScriptAnalysis.Enable != incomingSettings.Powershell.ScriptAnalysis.Enable)
{
configChanges["ScriptAnalysis"] = incomingSettings.Powershell.ScriptAnalysis.Enable ?? false;
}
// Send telemetry if the user opted-out of CodeFolding
if (!incomingSettings.Powershell.CodeFolding.Enable &&
_configurationService.CurrentSettings.CodeFolding.Enable != incomingSettings.Powershell.CodeFolding.Enable)
{
configChanges["CodeFolding"] = incomingSettings.Powershell.CodeFolding.Enable;
}
// Send telemetry if the user opted-out of the prompt to update PackageManagement
if (!incomingSettings.Powershell.PromptToUpdatePackageManagement &&
_configurationService.CurrentSettings.PromptToUpdatePackageManagement != incomingSettings.Powershell.PromptToUpdatePackageManagement)
{
configChanges["PromptToUpdatePackageManagement"] = incomingSettings.Powershell.PromptToUpdatePackageManagement;
}
// Send telemetry if the user opted-out of Profile loading
if (!incomingSettings.Powershell.EnableProfileLoading &&
_configurationService.CurrentSettings.EnableProfileLoading != incomingSettings.Powershell.EnableProfileLoading)
{
configChanges["ProfileLoading"] = incomingSettings.Powershell.EnableProfileLoading;
}
// Send telemetry if the user opted-in to Pester 5+ CodeLens
if (!incomingSettings.Powershell.Pester.UseLegacyCodeLens &&
_configurationService.CurrentSettings.Pester.UseLegacyCodeLens != incomingSettings.Powershell.Pester.UseLegacyCodeLens)
{
// From our perspective we want to see how many people are opting in to this so we flip the value
configChanges["Pester5CodeLens"] = !incomingSettings.Powershell.Pester.UseLegacyCodeLens;
}
// No need to send any telemetry since nothing changed
if (configChanges.Count == 0)
{
return;
}
_languageServer.Window.SendTelemetryEvent(new TelemetryEventParams
{
Data = new PsesTelemetryEvent
{
EventName = "NonDefaultPsesFeatureConfiguration",
Data = JObject.FromObject(configChanges)
}
});
}
public void SetCapability(DidChangeConfigurationCapability capability)
{
_capability = capability;
}
public event EventHandler<LanguageServerSettings> ConfigurationUpdated;
}
}