Skip to content

Commit de25fc0

Browse files
committed
Changed all references to languagemode to initialSessionState
1 parent c32d50f commit de25fc0

File tree

6 files changed

+30
-40
lines changed

6 files changed

+30
-40
lines changed

src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ private EditorServicesConfig CreateConfigObject()
349349
var profile = (PSObject)GetVariableValue("profile");
350350

351351
var hostInfo = new HostInfo(HostName, HostProfileId, HostVersion);
352+
var iss = Runspace.DefaultRunspace.InitialSessionState;
353+
iss.LanguageMode = Runspace.DefaultRunspace.SessionStateProxy.LanguageMode;
352354
var editorServicesConfig = new EditorServicesConfig(hostInfo, Host, SessionDetailsPath, bundledModulesPath, LogPath)
353355
{
354356
FeatureFlags = FeatureFlags,
@@ -357,7 +359,7 @@ private EditorServicesConfig CreateConfigObject()
357359
AdditionalModules = AdditionalModules,
358360
LanguageServiceTransport = GetLanguageServiceTransport(),
359361
DebugServiceTransport = GetDebugServiceTransport(),
360-
LanguageMode = Runspace.DefaultRunspace.SessionStateProxy.LanguageMode,
362+
InitialSessionState = iss,
361363
ProfilePaths = new ProfilePathConfig
362364
{
363365
AllUsersAllHosts = GetProfilePathFromProfileObject(profile, ProfileUserKind.AllUsers, ProfileHostKind.AllHosts),

src/PowerShellEditorServices.Hosting/Configuration/EditorServicesConfig.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Management.Automation;
66
using System.Management.Automation.Host;
7+
using System.Management.Automation.Runspaces;
78

89
namespace Microsoft.PowerShell.EditorServices.Hosting
910
{
@@ -111,10 +112,9 @@ public EditorServicesConfig(
111112
public ProfilePathConfig ProfilePaths { get; set; }
112113

113114
/// <summary>
114-
/// The language mode inherited from the orginal PowerShell process.
115-
/// This will be used when creating runspaces so that we honor the same language mode.
115+
/// The InitialSessionState to use when creating runspaces. LanguageMode can be set here.
116116
/// </summary>
117-
public PSLanguageMode LanguageMode { get; internal set; }
117+
public InitialSessionState InitialSessionState { get; internal set; }
118118

119119
public string StartupBanner { get; set; } = @"
120120

src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private HostStartupInfo CreateHostStartupInfo()
288288
profilePaths,
289289
_config.FeatureFlags,
290290
_config.AdditionalModules,
291-
_config.LanguageMode,
291+
_config.InitialSessionState,
292292
_config.LogPath,
293293
(int)_config.LogLevel,
294294
consoleReplEnabled: _config.ConsoleRepl != ConsoleReplKind.None,

src/PowerShellEditorServices/Hosting/HostStartupInfo.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Management.Automation;
77
using System.Management.Automation.Host;
8+
using System.Management.Automation.Runspaces;
89

910
namespace Microsoft.PowerShell.EditorServices.Hosting
1011
{
@@ -92,10 +93,10 @@ public sealed class HostStartupInfo
9293
public string LogPath { get; }
9394

9495
/// <summary>
95-
/// The language mode inherited from the orginal PowerShell process.
96-
/// This will be used when creating runspaces so that we honor the same language mode.
96+
/// The initialSessionState will be inherited from the orginal PowerShell process.
97+
/// This will be used when creating runspaces so that we honor the same initialSessionState including allowed modules, cmdlets and language mode.
9798
/// </summary>
98-
public PSLanguageMode LanguageMode { get; }
99+
public InitialSessionState InitialSessionState { get; }
99100

100101
/// <summary>
101102
/// The minimum log level of log events to be logged.
@@ -135,7 +136,7 @@ public sealed class HostStartupInfo
135136
/// <param name="currentUsersProfilePath">The path to the user specific profile.</param>
136137
/// <param name="featureFlags">Flags of features to enable.</param>
137138
/// <param name="additionalModules">Names or paths of additional modules to import.</param>
138-
/// <param name="languageMode">The language mode inherited from the orginal PowerShell process. This will be used when creating runspaces so that we honor the same language mode.</param>
139+
/// <param name="initialSessionState">The language mode inherited from the orginal PowerShell process. This will be used when creating runspaces so that we honor the same initialSessionState including allowed modules, cmdlets and language mode.</param>
139140
/// <param name="logPath">The path to log to.</param>
140141
/// <param name="logLevel">The minimum log event level.</param>
141142
/// <param name="consoleReplEnabled">Enable console if true.</param>
@@ -149,7 +150,7 @@ public HostStartupInfo(
149150
ProfilePathInfo profilePaths,
150151
IReadOnlyList<string> featureFlags,
151152
IReadOnlyList<string> additionalModules,
152-
PSLanguageMode languageMode,
153+
InitialSessionState initialSessionState,
153154
string logPath,
154155
int logLevel,
155156
bool consoleReplEnabled,
@@ -163,7 +164,7 @@ public HostStartupInfo(
163164
ProfilePaths = profilePaths;
164165
FeatureFlags = featureFlags ?? Array.Empty<string>();
165166
AdditionalModules = additionalModules ?? Array.Empty<string>();
166-
LanguageMode = languageMode;
167+
InitialSessionState = initialSessionState;
167168
LogPath = logPath;
168169
LogLevel = logLevel;
169170
ConsoleReplEnabled = consoleReplEnabled;

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public static PowerShellContextService Create(
219219

220220
EditorServicesPSHostUserInterface hostUserInterface =
221221
hostStartupInfo.ConsoleReplEnabled
222-
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, hostStartupInfo.PSHost, logger)
222+
? (EditorServicesPSHostUserInterface)new TerminalPSHostUserInterface(powerShellContext, hostStartupInfo.PSHost, logger)
223223
: new ProtocolPSHostUserInterface(languageServer, powerShellContext, logger);
224224

225225
EditorServicesPSHost psHost =
@@ -230,7 +230,7 @@ public static PowerShellContextService Create(
230230
logger);
231231

232232
logger.LogTrace("Creating initial PowerShell runspace");
233-
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost, hostStartupInfo.LanguageMode);
233+
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost, hostStartupInfo.InitialSessionState);
234234
powerShellContext.Initialize(hostStartupInfo.ProfilePaths, initialRunspace, true, hostUserInterface);
235235
powerShellContext.ImportCommandsModuleAsync();
236236

@@ -274,7 +274,7 @@ public static Runspace CreateRunspace(
274274
var psHost = new EditorServicesPSHost(powerShellContext, hostDetails, hostUserInterface, logger);
275275
powerShellContext.ConsoleWriter = hostUserInterface;
276276
powerShellContext.ConsoleReader = hostUserInterface;
277-
return CreateRunspace(psHost, hostDetails.LanguageMode);
277+
return CreateRunspace(psHost, hostDetails.InitialSessionState);
278278
}
279279

280280
/// <summary>
@@ -283,20 +283,8 @@ public static Runspace CreateRunspace(
283283
/// <param name="psHost">The PSHost that will be used for this Runspace.</param>
284284
/// <param name="languageMode">The language mode inherited from the orginal PowerShell process. This will be used when creating runspaces so that we honor the same language mode.</param>
285285
/// <returns></returns>
286-
public static Runspace CreateRunspace(PSHost psHost, PSLanguageMode languageMode)
286+
public static Runspace CreateRunspace(PSHost psHost, InitialSessionState initialSessionState)
287287
{
288-
InitialSessionState initialSessionState;
289-
if (Environment.GetEnvironmentVariable("PSES_TEST_USE_CREATE_DEFAULT") == "1") {
290-
initialSessionState = InitialSessionState.CreateDefault();
291-
} else {
292-
initialSessionState = InitialSessionState.CreateDefault2();
293-
}
294-
295-
// Create and initialize a new Runspace while honoring the LanguageMode of the original runspace
296-
// that started PowerShell Editor Services. This is because the PowerShell Integrated Console
297-
// should have the same LanguageMode of whatever is set by the system.
298-
initialSessionState.LanguageMode = languageMode;
299-
300288
// We set the process scope's execution policy (which is really the runspace's scope) to
301289
// Bypass so we can import our bundled modules. This is equivalent in scope to the CLI
302290
// argument `-Bypass`, which (for instance) the extension passes. Thus we emulate this
@@ -612,7 +600,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
612600
// cancelled prompt when it's called again.
613601
if (executionOptions.AddToHistory)
614602
{
615-
this.PromptContext.AddToHistory(executionOptions.InputString ?? psCommand.Commands[0].CommandText);
603+
this.PromptContext.AddToHistory(executionOptions.InputString ?? psCommand.Commands [0].CommandText);
616604
}
617605

618606
bool hadErrors = false;
@@ -666,7 +654,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
666654
// Instruct PowerShell to send output and errors to the host
667655
if (executionOptions.WriteOutputToHost)
668656
{
669-
psCommand.Commands[0].MergeMyResults(
657+
psCommand.Commands [0].MergeMyResults(
670658
PipelineResultTypes.Error,
671659
PipelineResultTypes.Output);
672660

@@ -701,7 +689,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
701689
if (executionOptions.WriteInputToHost)
702690
{
703691
this.WriteOutput(
704-
executionOptions.InputString ?? psCommand.Commands[0].CommandText,
692+
executionOptions.InputString ?? psCommand.Commands [0].CommandText,
705693
includeNewLine: true);
706694
}
707695

@@ -1014,7 +1002,7 @@ public Task<IEnumerable<object>> ExecuteScriptStringAsync(
10141002
Validate.IsNotNull(nameof(scriptString), scriptString);
10151003

10161004
PSCommand command = null;
1017-
if(CurrentRunspace.Runspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage)
1005+
if (CurrentRunspace.Runspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage)
10181006
{
10191007
try
10201008
{
@@ -1029,7 +1017,7 @@ public Task<IEnumerable<object>> ExecuteScriptStringAsync(
10291017
}
10301018

10311019
// fall back to old behavior
1032-
if(command == null)
1020+
if (command == null)
10331021
{
10341022
command = new PSCommand().AddScript(scriptString.Trim());
10351023
}
@@ -1729,7 +1717,7 @@ internal static string WildcardEscapePath(string path, bool escapeSpaces = false
17291717
var sb = new StringBuilder();
17301718
for (int i = 0; i < path.Length; i++)
17311719
{
1732-
char curr = path[i];
1720+
char curr = path [i];
17331721
switch (curr)
17341722
{
17351723
// Escape '[', ']', '?' and '*' with '`'
@@ -1783,11 +1771,11 @@ internal static string UnescapeWildcardEscapedPath(string wildcardEscapedPath)
17831771
for (int i = 0; i < wildcardEscapedPath.Length; i++)
17841772
{
17851773
// If we see a backtick perform a lookahead
1786-
char curr = wildcardEscapedPath[i];
1774+
char curr = wildcardEscapedPath [i];
17871775
if (curr == '`' && i + 1 < wildcardEscapedPath.Length)
17881776
{
17891777
// If the next char is an escapable one, don't add this backtick to the new string
1790-
char next = wildcardEscapedPath[i + 1];
1778+
char next = wildcardEscapedPath [i + 1];
17911779
switch (next)
17921780
{
17931781
case '[':
@@ -2178,14 +2166,14 @@ private void SetExecutionPolicy()
21782166
// set to expected values, so we must sift through those.
21792167

21802168
ExecutionPolicy policyToSet = ExecutionPolicy.Bypass;
2181-
var currentUserPolicy = (ExecutionPolicy)policies[policies.Count - 2].Members["ExecutionPolicy"].Value;
2169+
var currentUserPolicy = (ExecutionPolicy)policies [policies.Count - 2].Members ["ExecutionPolicy"].Value;
21822170
if (currentUserPolicy != ExecutionPolicy.Undefined)
21832171
{
21842172
policyToSet = currentUserPolicy;
21852173
}
21862174
else
21872175
{
2188-
var localMachinePolicy = (ExecutionPolicy)policies[policies.Count - 1].Members["ExecutionPolicy"].Value;
2176+
var localMachinePolicy = (ExecutionPolicy)policies [policies.Count - 1].Members ["ExecutionPolicy"].Value;
21892177
if (localMachinePolicy != ExecutionPolicy.Undefined)
21902178
{
21912179
policyToSet = localMachinePolicy;

test/PowerShellEditorServices.Test/PowerShellContextFactory.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Management.Automation;
8+
using System.Management.Automation.Runspaces;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.Extensions.Logging;
@@ -49,9 +50,7 @@ public static PowerShellContextService Create(ILogger logger)
4950
TestProfilePaths,
5051
new List<string>(),
5152
new List<string>(),
52-
// TODO: We want to replace this property with an entire initial session state,
53-
// which would then also control the process-scoped execution policy.
54-
PSLanguageMode.FullLanguage,
53+
InitialSessionState.CreateDefault(),
5554
null,
5655
0,
5756
consoleReplEnabled: false,

0 commit comments

Comments
 (0)