Skip to content

Commit a003902

Browse files
authored
Merge pull request PowerShell#406 from PowerShell/rkeithhill/fix-auto-variables
Fix issue w/Auto variables empty in script scope.
2 parents c0270c8 + 2b7d4d8 commit a003902

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/PowerShellEditorServices/Debugging/DebugService.cs

+23-21
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,25 @@ private async Task<VariableContainerDetails> FetchVariableContainer(
712712

713713
private bool AddToAutoVariables(PSObject psvariable, string scope)
714714
{
715+
if ((scope == VariableContainerDetails.GlobalScopeName) ||
716+
(scope == VariableContainerDetails.ScriptScopeName))
717+
{
718+
// We don't A) have a good way of distinguishing built-in from user created variables
719+
// and B) globalScopeVariables.Children.ContainsKey() doesn't work for built-in variables
720+
// stored in a child variable container within the globals variable container.
721+
return false;
722+
}
723+
715724
string variableName = psvariable.Properties["Name"].Value as string;
716725
object variableValue = psvariable.Properties["Value"].Value;
717726

727+
// Don't put any variables created by PSES in the Auto variable container.
728+
if (variableName.StartsWith(PsesGlobalVariableNamePrefix) ||
729+
variableName.Equals("PSDebugContext"))
730+
{
731+
return false;
732+
}
733+
718734
ScopedItemOptions variableScope = ScopedItemOptions.None;
719735
PSPropertyInfo optionsProperty = psvariable.Properties["Options"];
720736
if (string.Equals(optionsProperty.TypeNameOfValue, "System.String"))
@@ -733,20 +749,8 @@ private bool AddToAutoVariables(PSObject psvariable, string scope)
733749
variableScope = (ScopedItemOptions)optionsProperty.Value;
734750
}
735751

736-
if ((scope == VariableContainerDetails.GlobalScopeName) ||
737-
(scope == VariableContainerDetails.ScriptScopeName))
738-
{
739-
// We don't A) have a good way of distinguishing built-in from user created variables
740-
// and B) globalScopeVariables.Children.ContainsKey() doesn't work for built-in variables
741-
// stored in a child variable container within the globals variable container.
742-
return false;
743-
}
744-
745-
var constantAllScope = ScopedItemOptions.AllScope | ScopedItemOptions.Constant;
746-
var readonlyAllScope = ScopedItemOptions.AllScope | ScopedItemOptions.ReadOnly;
747-
748752
// Some local variables, if they exist, should be displayed by default
749-
if (psvariable.TypeNames.Any(typeName => typeName.EndsWith("LocalVariable")))
753+
if (psvariable.TypeNames[0].EndsWith("LocalVariable"))
750754
{
751755
if (variableName.Equals("_"))
752756
{
@@ -760,13 +764,16 @@ private bool AddToAutoVariables(PSObject psvariable, string scope)
760764

761765
return false;
762766
}
763-
else if (!psvariable.TypeNames.Any(typeName => typeName.EndsWith("PSVariable")))
767+
else if (!psvariable.TypeNames[0].EndsWith(nameof(PSVariable)))
764768
{
765769
return false;
766770
}
767771

768-
if (((variableScope | constantAllScope) == constantAllScope) ||
769-
((variableScope | readonlyAllScope) == readonlyAllScope))
772+
var constantAllScope = ScopedItemOptions.AllScope | ScopedItemOptions.Constant;
773+
var readonlyAllScope = ScopedItemOptions.AllScope | ScopedItemOptions.ReadOnly;
774+
775+
if (((variableScope & constantAllScope) == constantAllScope) ||
776+
((variableScope & readonlyAllScope) == readonlyAllScope))
770777
{
771778
string prefixedVariableName = VariableDetails.DollarPrefix + variableName;
772779
if (this.globalScopeVariables.Children.ContainsKey(prefixedVariableName))
@@ -775,11 +782,6 @@ private bool AddToAutoVariables(PSObject psvariable, string scope)
775782
}
776783
}
777784

778-
if (variableValue != null && variableValue.GetType().Name.EndsWith(nameof(PSDebugContext)))
779-
{
780-
return false;
781-
}
782-
783785
return true;
784786
}
785787

0 commit comments

Comments
 (0)