Skip to content

Commit eb47061

Browse files
committed
Clean up LanguageServerSettings.cs
1 parent e2827a1 commit eb47061

File tree

1 file changed

+55
-78
lines changed

1 file changed

+55
-78
lines changed

src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs

Lines changed: 55 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,78 +15,61 @@ namespace Microsoft.PowerShell.EditorServices.Services.Configuration
1515
{
1616
internal class LanguageServerSettings
1717
{
18-
private readonly object updateLock = new object();
19-
20-
public bool EnableProfileLoading { get; set; } = false;
21-
18+
private readonly object updateLock = new();
19+
public bool EnableProfileLoading { get; set; }
2220
public bool PromptToUpdatePackageManagement { get; set; } = true;
23-
2421
public ScriptAnalysisSettings ScriptAnalysis { get; set; }
25-
2622
public CodeFormattingSettings CodeFormatting { get; set; }
27-
2823
public CodeFoldingSettings CodeFolding { get; set; }
29-
3024
public PesterSettings Pester { get; set; }
31-
3225
public string Cwd { get; set; }
3326

3427
public LanguageServerSettings()
3528
{
36-
this.ScriptAnalysis = new ScriptAnalysisSettings();
37-
this.CodeFormatting = new CodeFormattingSettings();
38-
this.CodeFolding = new CodeFoldingSettings();
39-
this.Pester = new PesterSettings();
29+
ScriptAnalysis = new ScriptAnalysisSettings();
30+
CodeFormatting = new CodeFormattingSettings();
31+
CodeFolding = new CodeFoldingSettings();
32+
Pester = new PesterSettings();
4033
}
4134

4235
public void Update(
4336
LanguageServerSettings settings,
4437
string workspaceRootPath,
4538
ILogger logger)
4639
{
47-
if (settings != null)
40+
if (settings is not null)
4841
{
4942
lock (updateLock)
5043
{
51-
this.EnableProfileLoading = settings.EnableProfileLoading;
52-
this.PromptToUpdatePackageManagement = settings.PromptToUpdatePackageManagement;
53-
this.ScriptAnalysis.Update(
54-
settings.ScriptAnalysis,
55-
workspaceRootPath,
56-
logger);
57-
this.CodeFormatting = new CodeFormattingSettings(settings.CodeFormatting);
58-
this.CodeFolding.Update(settings.CodeFolding, logger);
59-
this.Pester.Update(settings.Pester, logger);
60-
this.Cwd = settings.Cwd;
44+
EnableProfileLoading = settings.EnableProfileLoading;
45+
PromptToUpdatePackageManagement = settings.PromptToUpdatePackageManagement;
46+
ScriptAnalysis.Update(settings.ScriptAnalysis, workspaceRootPath, logger);
47+
CodeFormatting = new CodeFormattingSettings(settings.CodeFormatting);
48+
CodeFolding.Update(settings.CodeFolding, logger);
49+
Pester.Update(settings.Pester, logger);
50+
Cwd = settings.Cwd;
6151
}
6252
}
6353
}
6454
}
6555

6656
internal class ScriptAnalysisSettings
6757
{
68-
private readonly object updateLock = new object();
69-
58+
private readonly object updateLock = new();
7059
public bool? Enable { get; set; }
71-
7260
public string SettingsPath { get; set; }
73-
74-
public ScriptAnalysisSettings()
75-
{
76-
this.Enable = true;
77-
}
61+
public ScriptAnalysisSettings() { Enable = true; }
7862

7963
public void Update(
8064
ScriptAnalysisSettings settings,
8165
string workspaceRootPath,
8266
ILogger logger)
8367
{
84-
if (settings != null)
68+
if (settings is not null)
8569
{
86-
lock(updateLock)
70+
lock (updateLock)
8771
{
88-
this.Enable = settings.Enable;
89-
72+
Enable = settings.Enable;
9073
string settingsPath = settings.SettingsPath;
9174

9275
try
@@ -105,29 +88,22 @@ public void Update(
10588
// In this case we should just log an error and let
10689
// the specified settings path go through even though
10790
// it will fail to load.
108-
logger.LogError(
109-
"Could not resolve Script Analyzer settings path due to null or empty workspaceRootPath.");
91+
logger.LogError( "Could not resolve Script Analyzer settings path due to null or empty workspaceRootPath.");
11092
}
11193
else
11294
{
11395
settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath));
11496
}
11597
}
11698

117-
this.SettingsPath = settingsPath;
99+
SettingsPath = settingsPath;
118100
logger.LogTrace($"Using Script Analyzer settings path - '{settingsPath ?? ""}'.");
119101
}
120-
catch (Exception ex) when (
121-
ex is NotSupportedException ||
122-
ex is PathTooLongException ||
123-
ex is SecurityException)
102+
catch (Exception ex) when (ex is NotSupportedException or PathTooLongException or SecurityException)
124103
{
125104
// Invalid chars in path like ${env:HOME} can cause Path.GetFullPath() to throw, catch such errors here
126-
logger.LogException(
127-
$"Invalid Script Analyzer settings path - '{settingsPath}'.",
128-
ex);
129-
130-
this.SettingsPath = null;
105+
logger.LogException($"Invalid Script Analyzer settings path - '{settingsPath}'.", ex);
106+
SettingsPath = null;
131107
}
132108
}
133109
}
@@ -192,22 +168,20 @@ internal class CodeFormattingSettings
192168
/// <summary>
193169
/// Default constructor.
194170
/// </summary>>
195-
public CodeFormattingSettings()
196-
{
197-
}
171+
public CodeFormattingSettings() { }
198172

199173
/// <summary>
200174
/// Copy constructor.
201175
/// </summary>
202176
/// <param name="codeFormattingSettings">An instance of type CodeFormattingSettings.</param>
203177
public CodeFormattingSettings(CodeFormattingSettings codeFormattingSettings)
204178
{
205-
if (codeFormattingSettings == null)
179+
if (codeFormattingSettings is null)
206180
{
207181
throw new ArgumentNullException(nameof(codeFormattingSettings));
208182
}
209183

210-
foreach (var prop in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
184+
foreach (PropertyInfo prop in GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
211185
{
212186
prop.SetValue(this, prop.GetValue(codeFormattingSettings));
213187
}
@@ -226,29 +200,28 @@ public CodeFormattingSettings(CodeFormattingSettings codeFormattingSettings)
226200
public bool WhitespaceBeforeOpenParen { get; set; }
227201
public bool WhitespaceAroundOperator { get; set; }
228202
public bool WhitespaceAfterSeparator { get; set; }
229-
public bool WhitespaceBetweenParameters { get; set; }
203+
public bool WhitespaceBetweenParameters { get; set; }
230204
public bool WhitespaceInsideBrace { get; set; }
231205
public bool IgnoreOneLineBlock { get; set; }
232206
public bool AlignPropertyValuePairs { get; set; }
233207
public bool UseCorrectCasing { get; set; }
234208

235-
236209
/// <summary>
237210
/// Get the settings hashtable that will be consumed by PSScriptAnalyzer.
238211
/// </summary>
239212
/// <param name="tabSize">The tab size in the number spaces.</param>
240213
/// <param name="insertSpaces">If true, insert spaces otherwise insert tabs for indentation.</param>
241-
/// <returns></returns>
242214
public Hashtable GetPSSASettingsHashtable(
243215
int tabSize,
244216
bool insertSpaces,
245217
ILogger logger)
246218
{
247-
var settings = GetCustomPSSASettingsHashtable(tabSize, insertSpaces);
248-
var ruleSettings = (Hashtable)(settings["Rules"]);
249-
var closeBraceSettings = (Hashtable)ruleSettings["PSPlaceCloseBrace"];
250-
var openBraceSettings = (Hashtable)ruleSettings["PSPlaceOpenBrace"];
251-
switch(Preset)
219+
Hashtable settings = GetCustomPSSASettingsHashtable(tabSize, insertSpaces);
220+
Hashtable ruleSettings = settings["Rules"] as Hashtable;
221+
Hashtable closeBraceSettings = ruleSettings["PSPlaceCloseBrace"] as Hashtable;
222+
Hashtable openBraceSettings = ruleSettings["PSPlaceOpenBrace"] as Hashtable;
223+
224+
switch (Preset)
252225
{
253226
case CodeFormattingPreset.Allman:
254227
openBraceSettings["OnSameLine"] = false;
@@ -268,6 +241,7 @@ public Hashtable GetPSSASettingsHashtable(
268241
closeBraceSettings["NewLineAfter"] = true;
269242
break;
270243

244+
case CodeFormattingPreset.Custom:
271245
default:
272246
break;
273247
}
@@ -278,7 +252,7 @@ public Hashtable GetPSSASettingsHashtable(
278252

279253
private Hashtable GetCustomPSSASettingsHashtable(int tabSize, bool insertSpaces)
280254
{
281-
var ruleConfigurations = new Hashtable
255+
Hashtable ruleConfigurations = new()
282256
{
283257
{ "PSPlaceOpenBrace", new Hashtable {
284258
{ "Enable", true },
@@ -337,8 +311,7 @@ private Hashtable GetCustomPSSASettingsHashtable(int tabSize, bool insertSpaces)
337311
"PSAlignAssignmentStatement",
338312
"PSAvoidUsingDoubleQuotesForConstantString",
339313
}},
340-
{
341-
"Rules", ruleConfigurations
314+
{ "Rules", ruleConfigurations
342315
}
343316
};
344317
}
@@ -366,14 +339,17 @@ public void Update(
366339
CodeFoldingSettings settings,
367340
ILogger logger)
368341
{
369-
if (settings != null) {
370-
if (this.Enable != settings.Enable) {
371-
this.Enable = settings.Enable;
372-
logger.LogTrace(string.Format("Using Code Folding Enabled - {0}", this.Enable));
342+
if (settings is not null)
343+
{
344+
if (Enable != settings.Enable)
345+
{
346+
Enable = settings.Enable;
347+
logger.LogTrace(string.Format("Using Code Folding Enabled - {0}", Enable));
373348
}
374-
if (this.ShowLastLine != settings.ShowLastLine) {
375-
this.ShowLastLine = settings.ShowLastLine;
376-
logger.LogTrace(string.Format("Using Code Folding ShowLastLine - {0}", this.ShowLastLine));
349+
if (ShowLastLine != settings.ShowLastLine)
350+
{
351+
ShowLastLine = settings.ShowLastLine;
352+
logger.LogTrace(string.Format("Using Code Folding ShowLastLine - {0}", ShowLastLine));
377353
}
378354
}
379355
}
@@ -401,20 +377,21 @@ public void Update(
401377
PesterSettings settings,
402378
ILogger logger)
403379
{
404-
if (settings is null) {
380+
if (settings is null)
381+
{
405382
return;
406383
}
407384

408-
if (this.CodeLens != settings.CodeLens)
385+
if (CodeLens != settings.CodeLens)
409386
{
410-
this.CodeLens = settings.CodeLens;
411-
logger.LogTrace(string.Format("Using Pester Code Lens - {0}", this.CodeLens));
387+
CodeLens = settings.CodeLens;
388+
logger.LogTrace(string.Format("Using Pester Code Lens - {0}", CodeLens));
412389
}
413390

414-
if (this.UseLegacyCodeLens != settings.UseLegacyCodeLens)
391+
if (UseLegacyCodeLens != settings.UseLegacyCodeLens)
415392
{
416-
this.UseLegacyCodeLens = settings.UseLegacyCodeLens;
417-
logger.LogTrace(string.Format("Using Pester Legacy Code Lens - {0}", this.UseLegacyCodeLens));
393+
UseLegacyCodeLens = settings.UseLegacyCodeLens;
394+
logger.LogTrace(string.Format("Using Pester Legacy Code Lens - {0}", UseLegacyCodeLens));
418395
}
419396
}
420397
}

0 commit comments

Comments
 (0)