Skip to content

Commit d390bd8

Browse files
committed
Treat all relative paths for scriptAnalysisSettingsPath as workspace relative. You can also use an absolute path or string empty for the built-in defaults.
1 parent a21652a commit d390bd8

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.PowerShell.EditorServices.Protocol.LanguageServer;
77
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
88
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel;
9+
using Microsoft.PowerShell.EditorServices.Session;
910
using Microsoft.PowerShell.EditorServices.Utility;
1011
using System;
1112
using System.Collections.Generic;
@@ -23,18 +24,27 @@ public class LanguageServer : LanguageServerBase
2324
{
2425
private static CancellationTokenSource existingRequestCancellation;
2526

27+
private bool profilesLoaded;
2628
private EditorSession editorSession;
2729
private OutputDebouncer outputDebouncer;
2830
private LanguageServerSettings currentSettings = new LanguageServerSettings();
2931

30-
public LanguageServer() : this(new StdioServerChannel())
32+
/// <param name="hostDetails">
33+
/// Provides details about the host application.
34+
/// </param>
35+
public LanguageServer(HostDetails hostDetails)
36+
: this(hostDetails, new StdioServerChannel())
3137
{
3238
}
3339

34-
public LanguageServer(ChannelBase serverChannel) : base(serverChannel)
40+
/// <param name="hostDetails">
41+
/// Provides details about the host application.
42+
/// </param>
43+
public LanguageServer(HostDetails hostDetails, ChannelBase serverChannel)
44+
: base(serverChannel)
3545
{
3646
this.editorSession = new EditorSession();
37-
this.editorSession.StartSession();
47+
this.editorSession.StartSession(hostDetails);
3848
this.editorSession.ConsoleService.OutputWritten += this.powerShellContext_OutputWritten;
3949

4050
// Always send console prompts through the UI in the language service
@@ -58,7 +68,7 @@ protected override void Initialize()
5868
this.SetEventHandler(DidOpenTextDocumentNotification.Type, this.HandleDidOpenTextDocumentNotification);
5969
this.SetEventHandler(DidCloseTextDocumentNotification.Type, this.HandleDidCloseTextDocumentNotification);
6070
this.SetEventHandler(DidChangeTextDocumentNotification.Type, this.HandleDidChangeTextDocumentNotification);
61-
this.SetEventHandler(DidChangeConfigurationNotification<SettingsWrapper>.Type, this.HandleDidChangeConfigurationNotification);
71+
this.SetEventHandler(DidChangeConfigurationNotification<LanguageServerSettingsWrapper>.Type, this.HandleDidChangeConfigurationNotification);
6272

6373
this.SetRequestHandler(DefinitionRequest.Type, this.HandleDefinitionRequest);
6474
this.SetRequestHandler(ReferencesRequest.Type, this.HandleReferencesRequest);
@@ -286,20 +296,29 @@ protected Task HandleDidChangeTextDocumentNotification(
286296
}
287297

288298
protected async Task HandleDidChangeConfigurationNotification(
289-
DidChangeConfigurationParams<SettingsWrapper> configChangeParams,
299+
DidChangeConfigurationParams<LanguageServerSettingsWrapper> configChangeParams,
290300
EventContext eventContext)
291301
{
292-
bool oldScriptAnalysisEnabled =
302+
bool oldLoadProfiles = this.currentSettings.EnableProfileLoading;
303+
bool oldScriptAnalysisEnabled =
293304
this.currentSettings.ScriptAnalysis.Enable.HasValue;
294305
string oldScriptAnalysisSettingsPath =
295306
this.currentSettings.ScriptAnalysis.SettingsPath;
296307

297308
this.currentSettings.Update(
298-
configChangeParams.Settings.Powershell);
309+
configChangeParams.Settings.Powershell,
310+
this.editorSession.Workspace.WorkspacePath);
311+
312+
if (!this.profilesLoaded &&
313+
this.currentSettings.EnableProfileLoading &&
314+
oldLoadProfiles != this.currentSettings.EnableProfileLoading)
315+
{
316+
await this.editorSession.PowerShellContext.LoadHostProfiles();
317+
this.profilesLoaded = true;
318+
}
299319

300-
string newSettingsPath = this.currentSettings.ScriptAnalysis.SettingsPath;
301-
302320
// If there is a new settings file path, restart the analyzer with the new settigs.
321+
string newSettingsPath = this.currentSettings.ScriptAnalysis.SettingsPath;
303322
if (!(oldScriptAnalysisSettingsPath?.Equals(newSettingsPath, StringComparison.OrdinalIgnoreCase) ?? false))
304323
{
305324
this.editorSession.RestartAnalysisService(newSettingsPath);
@@ -1000,7 +1019,6 @@ private static CompletionItem CreateCompletionItem(
10001019
{
10011020
string detailString = null;
10021021
string documentationString = null;
1003-
string labelString = completionDetails.ListItemText;
10041022

10051023
if ((completionDetails.CompletionType == CompletionType.Variable) ||
10061024
(completionDetails.CompletionType == CompletionType.ParameterName))
@@ -1013,11 +1031,6 @@ private static CompletionItem CreateCompletionItem(
10131031
{
10141032
detailString = matches[0].Groups[1].Value;
10151033
}
1016-
1017-
// PowerShell returns ListItemText for parameters & variables that is not prefixed
1018-
// and it needs to be or the completion will not appear for these CompletionTypes.
1019-
string prefix = (completionDetails.CompletionType == CompletionType.Variable) ? "$" : "-";
1020-
labelString = prefix + completionDetails.ListItemText;
10211034
}
10221035
else if ((completionDetails.CompletionType == CompletionType.Method) ||
10231036
(completionDetails.CompletionType == CompletionType.Property))
@@ -1054,13 +1067,13 @@ private static CompletionItem CreateCompletionItem(
10541067
// completion list. Technically we don't need the ListItemText at all but it may come
10551068
// in handy during debug.
10561069
var sortText = (completionDetails.CompletionType == CompletionType.ParameterName)
1057-
? string.Format("{0:D3}{1}", sortIndex, completionDetails.ListItemText)
1058-
: null;
1070+
? $"{sortIndex:D3}{completionDetails.ListItemText}"
1071+
: null;
10591072

10601073
return new CompletionItem
10611074
{
10621075
InsertText = completionDetails.CompletionText,
1063-
Label = labelString,
1076+
Label = completionDetails.ListItemText,
10641077
Kind = MapCompletionKind(completionDetails.CompletionType),
10651078
Detail = detailString,
10661079
Documentation = documentationString,

src/PowerShellEditorServices.Protocol/Server/LanguageServerSettings.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using System.IO;
7+
using Microsoft.PowerShell.EditorServices.Utility;
8+
69
namespace Microsoft.PowerShell.EditorServices.Protocol.Server
710
{
811
public class LanguageServerSettings
912
{
13+
public bool EnableProfileLoading { get; set; }
14+
1015
public ScriptAnalysisSettings ScriptAnalysis { get; set; }
1116

1217
public LanguageServerSettings()
1318
{
1419
this.ScriptAnalysis = new ScriptAnalysisSettings();
1520
}
1621

17-
public void Update(LanguageServerSettings settings)
22+
public void Update(LanguageServerSettings settings, string workspaceRootPath)
1823
{
1924
if (settings != null)
2025
{
21-
this.ScriptAnalysis.Update(settings.ScriptAnalysis);
26+
this.EnableProfileLoading = settings.EnableProfileLoading;
27+
this.ScriptAnalysis.Update(settings.ScriptAnalysis, workspaceRootPath);
2228
}
2329
}
2430
}
@@ -34,17 +40,31 @@ public ScriptAnalysisSettings()
3440
this.Enable = true;
3541
}
3642

37-
public void Update(ScriptAnalysisSettings settings)
43+
public void Update(ScriptAnalysisSettings settings, string workspaceRootPath)
3844
{
3945
if (settings != null)
4046
{
47+
Validate.IsNotNullOrEmptyString(nameof(workspaceRootPath), workspaceRootPath);
48+
4149
this.Enable = settings.Enable;
42-
this.SettingsPath = settings.SettingsPath;
50+
51+
string settingsPath = settings.SettingsPath;
52+
53+
if (string.IsNullOrWhiteSpace(settingsPath))
54+
{
55+
settingsPath = null;
56+
}
57+
else if (!Path.IsPathRooted(settingsPath))
58+
{
59+
settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath));
60+
}
61+
62+
this.SettingsPath = settingsPath;
4363
}
4464
}
4565
}
4666

47-
public class SettingsWrapper
67+
public class LanguageServerSettingsWrapper
4868
{
4969
// NOTE: This property is capitalized as 'Powershell' because the
5070
// mode name sent from the client is written as 'powershell' and

0 commit comments

Comments
 (0)