Skip to content

Commit 11d5e50

Browse files
authored
Handle 'PSObject' correctly so that script block handlers works for 'AddToHistoryHandler' (#1068)
1 parent 394bacc commit 11d5e50

File tree

2 files changed

+300
-129
lines changed

2 files changed

+300
-129
lines changed

PSReadLine/History.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,32 @@ private AddToHistoryOption GetAddToHistoryOption(string line)
137137
}
138138

139139
object value = Options.AddToHistoryHandler(line);
140+
if (value is PSObject psObj)
141+
{
142+
value = psObj.BaseObject;
143+
}
144+
145+
if (value is bool boolValue)
146+
{
147+
return boolValue ? AddToHistoryOption.MemoryAndFile : AddToHistoryOption.SkipAdding;
148+
}
140149

141-
if (LanguagePrimitives.TryConvertTo(value, out AddToHistoryOption enumValue))
150+
if (value is AddToHistoryOption enumValue)
142151
{
143152
return enumValue;
144153
}
145154

146-
if (value is bool boolValue && !boolValue)
155+
if (value is string strValue && Enum.TryParse(strValue, out enumValue))
147156
{
148-
return AddToHistoryOption.SkipAdding;
157+
return enumValue;
158+
}
159+
160+
// 'TryConvertTo' incurs exception handling when the value cannot be converted to the target type.
161+
// It's expensive, especially when we need to process lots of history items from file during the
162+
// initialization. So do the conversion as the last resort.
163+
if (LanguagePrimitives.TryConvertTo(value, out enumValue))
164+
{
165+
return enumValue;
149166
}
150167
}
151168

0 commit comments

Comments
 (0)