Skip to content

Commit cf0032b

Browse files
committed
Multiple version fixes
1 parent 06f9a1e commit cf0032b

File tree

6 files changed

+55
-9
lines changed

6 files changed

+55
-9
lines changed

src/PowerShellEditorServices/Language/CompletionResults.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,10 @@ private static CompletionType ConvertCompletionResultType(
275275
case CompletionResultType.Type:
276276
return CompletionType.Type;
277277

278+
#if !PowerShellv3
278279
case CompletionResultType.Keyword:
279280
return CompletionType.Keyword;
281+
#endif
280282

281283
case CompletionResultType.ProviderContainer:
282284
case CompletionResultType.ProviderItem:

src/PowerShellEditorServices/Language/FindSymbolVisitor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ namespace Microsoft.PowerShell.EditorServices
1010
/// <summary>
1111
/// The visitor used to find the the symbol at a specfic location in the AST
1212
/// </summary>
13+
#if PowerShellv5
1314
internal class FindSymbolVisitor : AstVisitor2
15+
#else
16+
internal class FindSymbolVisitor : AstVisitor
17+
#endif
1418
{
1519
private int lineNumber;
1620
private int columnNumber;

src/PowerShellEditorServices/Language/FindSymbolsVisitor.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ namespace Microsoft.PowerShell.EditorServices
1111
/// <summary>
1212
/// The visitor used to find all the symbols (function and class defs) in the AST.
1313
/// </summary>
14+
#if PowerShellv5
1415
internal class FindSymbolsVisitor : AstVisitor2
16+
#else
17+
internal class FindSymbolsVisitor : AstVisitor
18+
#endif
1519
{
1620
public List<SymbolReference> SymbolReferences { get; private set; }
1721

@@ -69,9 +73,11 @@ public override AstVisitAction VisitVariableExpression(VariableExpressionAst var
6973
return AstVisitAction.Continue;
7074
}
7175

76+
#if PowerShell5
7277
public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinitionAst configurationDefinitionAst)
7378
{
74-
IScriptExtent nameExtent = new ScriptExtent() {
79+
IScriptExtent nameExtent = new ScriptExtent()
80+
{
7581
Text = configurationDefinitionAst.InstanceName.Extent.Text,
7682
StartLineNumber = configurationDefinitionAst.Extent.StartLineNumber,
7783
EndLineNumber = configurationDefinitionAst.Extent.EndLineNumber,
@@ -86,6 +92,8 @@ public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinit
8692

8793
return AstVisitAction.Continue;
8894
}
95+
#endif
96+
8997

9098
private bool IsAssignedAtScriptScope(VariableExpressionAst variableExpressionAst)
9199
{

src/PowerShellEditorServices/PowerShellEditorServices.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
<FileAlignment>512</FileAlignment>
1414
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
1515
<RestorePackages>true</RestorePackages>
16+
<DefineConstants Condition=" '$(PowerShellVersion)' == 'v3'">PowerShellv3</DefineConstants>
17+
<DefineConstants Condition=" '$(PowerShellVersion)' == 'v4'">PowerShellv4</DefineConstants>
18+
<DefineConstants Condition=" '$(PowerShellVersion)' == ''">PowerShellv5</DefineConstants>
1619
</PropertyGroup>
1720
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1821
<DebugSymbols>true</DebugSymbols>
1922
<DebugType>full</DebugType>
2023
<Optimize>false</Optimize>
2124
<OutputPath>bin\Debug\</OutputPath>
22-
<DefineConstants>DEBUG;TRACE</DefineConstants>
25+
<DefineConstants>TRACE;DEBUG;$(DefineConstants)</DefineConstants>
2326
<ErrorReport>prompt</ErrorReport>
2427
<WarningLevel>4</WarningLevel>
2528
<DocumentationFile>bin\Debug\Microsoft.PowerShell.EditorServices.XML</DocumentationFile>
@@ -28,7 +31,7 @@
2831
<DebugType>pdbonly</DebugType>
2932
<Optimize>true</Optimize>
3033
<OutputPath>bin\Release\</OutputPath>
31-
<DefineConstants>TRACE</DefineConstants>
34+
<DefineConstants>TRACE;$(DefineConstants)</DefineConstants>
3235
<ErrorReport>prompt</ErrorReport>
3336
<WarningLevel>4</WarningLevel>
3437
<WarningsAsErrors>1591,1573,1572</WarningsAsErrors>

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Microsoft.PowerShell.EditorServices
1717
{
18+
using System.Collections;
1819
using System.Management.Automation;
1920
using System.Management.Automation.Host;
2021
using System.Management.Automation.Runspaces;
@@ -68,6 +69,14 @@ public PowerShellContextState SessionState
6869
private set;
6970
}
7071

72+
/// <summary>
73+
/// PowerShell Version of the current runspace.
74+
/// </summary>
75+
public Version PowerShellVersion
76+
{
77+
get; private set;
78+
}
79+
7180
#endregion
7281

7382
#region Constructors
@@ -109,7 +118,7 @@ private void Initialize(Runspace initialRunspace)
109118

110119
this.initialRunspace = initialRunspace;
111120
this.currentRunspace = initialRunspace;
112-
this.currentRunspace.Debugger.SetDebugMode(DebugModes.LocalScript | DebugModes.RemoteScript);
121+
113122
this.currentRunspace.Debugger.BreakpointUpdated += OnBreakpointUpdated;
114123
this.currentRunspace.Debugger.DebuggerStop += OnDebuggerStop;
115124

@@ -120,6 +129,21 @@ private void Initialize(Runspace initialRunspace)
120129
// TODO: Should this be configurable?
121130
this.SetExecutionPolicy(ExecutionPolicy.RemoteSigned);
122131

132+
var psVersionTable = this.currentRunspace.SessionStateProxy.GetVariable("PSVersionTable") as Hashtable;
133+
if (psVersionTable != null)
134+
{
135+
Version version;
136+
Version.TryParse(psVersionTable["PSVersion"] as string, out version);
137+
PowerShellVersion = version;
138+
}
139+
140+
#if !PowerShellv3
141+
if (PowerShellVersion > new Version(3,0))
142+
{
143+
this.currentRunspace.Debugger.SetDebugMode(DebugModes.LocalScript | DebugModes.RemoteScript);
144+
}
145+
#endif
146+
123147
this.SessionState = PowerShellContextState.Ready;
124148
}
125149

@@ -370,7 +394,12 @@ internal void BreakExecution()
370394
{
371395
Logger.Write(LogLevel.Verbose, "Debugger break requested...");
372396

373-
this.currentRunspace.Debugger.SetDebuggerStepMode(true);
397+
#if PowerShellv5
398+
if (PowerShellVersion >= new Version(5, 0))
399+
{
400+
this.currentRunspace.Debugger.SetDebuggerStepMode(true);
401+
}
402+
#endif
374403
}
375404

376405
internal void ResumeDebugger(DebuggerResumeAction resumeAction)
@@ -568,7 +597,7 @@ private static string GetStringForPSCommand(PSCommand psCommand)
568597

569598
return stringBuilder.ToString();
570599
}
571-
600+
572601
private void SetExecutionPolicy(ExecutionPolicy desiredExecutionPolicy)
573602
{
574603
var currentPolicy = ExecutionPolicy.Undefined;

test/PowerShellEditorServices.Test/Language/LanguageServiceTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,15 @@ public void CompilesWithPowerShellVersion(string version)
319319

320320
try
321321
{
322-
Compile(projectVersion);
322+
Compile(projectVersion, version);
323323
}
324324
finally
325325
{
326326
File.Delete(projectVersion);
327327
}
328328
}
329329

330-
private void Compile(string project)
330+
private void Compile(string project, string version)
331331
{
332332
string msbuild;
333333
using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0"))
@@ -340,7 +340,7 @@ private void Compile(string project)
340340

341341
var p = new Process();
342342
p.StartInfo.FileName = msbuild;
343-
p.StartInfo.Arguments = string.Format(@" {0} /p:Configuration=Debug /t:Build /fileLogger /flp1:logfile=errors.txt;errorsonly /p:SolutionDir={1} /p:SolutionName=PowerShellEditorServices", project, fi.Directory.Parent.Parent.FullName);
343+
p.StartInfo.Arguments = string.Format(@" {0} /p:Configuration=Debug /t:Build /fileLogger /flp1:logfile=errors.txt;errorsonly /p:SolutionDir={1} /p:SolutionName=PowerShellEditorServices /p:DefineConstants=PowerShellv{2}", project, fi.Directory.Parent.Parent.FullName, version);
344344
p.StartInfo.UseShellExecute = false;
345345
p.StartInfo.CreateNoWindow = true;
346346
p.Start();

0 commit comments

Comments
 (0)