Skip to content

Commit 4617938

Browse files
committed
Merge branch 'pses-changes' of https://github.com/rjmholt/PSReadLine into pses-changes
2 parents b14107c + 37debd7 commit 4617938

29 files changed

+668
-264
lines changed

.vsts-ci/releaseBuild.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
name: PSReadLine-ModuleBuild-$(Build.BuildId)
2-
trigger:
3-
branches:
4-
include:
5-
- master
2+
trigger: none
3+
pr: none
64

75
variables:
86
DOTNET_CLI_TELEMETRY_OPTOUT: 1
@@ -20,7 +18,9 @@ stages:
2018
- stage: Build
2119
displayName: Build and Sign
2220
pool:
23-
name: Package ES CodeHub Lab E
21+
name: PowerShell1ES
22+
demands:
23+
- ImageOverride -equals MMS2019
2424
jobs:
2525
- job: build_windows
2626
displayName: Build PSReadLine
@@ -127,10 +127,14 @@ stages:
127127
displayName: Compliance
128128
dependsOn: Build
129129
pool:
130-
name: Package ES CodeHub Lab E
130+
name: PowerShell1ES
131+
demands:
132+
- ImageOverride -equals MMS2019
131133
jobs:
132134
- job: Compliance_Job
133135
displayName: PSReadLine Compliance
136+
variables:
137+
- group: APIScan
134138
# APIScan can take a long time
135139
timeoutInMinutes: 240
136140

@@ -176,3 +180,4 @@ stages:
176180
softwareFolder: '$(Pipeline.Workspace)\PSReadLine'
177181
softwareName: 'PSReadLine'
178182
softwareVersion: '$(ModuleVersion)'
183+
connectionString: 'RunAs=App;AppId=$(APIScanClient);TenantId=$(APIScanTenant);AppKey=$(APIScanSecret)'

MockPSConsole/MockPSConsole.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</ItemGroup>
1919

2020
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
21-
<PackageReference Include="Microsoft.PowerShell.SDK" version="7.2.0-preview.6" />
21+
<PackageReference Include="Microsoft.PowerShell.SDK" version="7.2.0-preview.9" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

PSReadLine/BasicEditing.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static void CancelLine(ConsoleKeyInfo? key = null, object arg = null)
102102
/// </summary>
103103
public static void ForwardDeleteInput(ConsoleKeyInfo? key = null, object arg = null)
104104
{
105-
ForwardDeleteImpl(_singleton._buffer.Length);
105+
ForwardDeleteImpl(_singleton._buffer.Length, ForwardDeleteInput);
106106
}
107107

108108
/// <summary>
@@ -111,15 +111,15 @@ public static void ForwardDeleteInput(ConsoleKeyInfo? key = null, object arg = n
111111
/// </summary>
112112
public static void ForwardDeleteLine(ConsoleKeyInfo? key = null, object arg = null)
113113
{
114-
ForwardDeleteImpl(GetEndOfLogicalLinePos(_singleton._current) + 1);
114+
ForwardDeleteImpl(GetEndOfLogicalLinePos(_singleton._current) + 1, ForwardDeleteLine);
115115
}
116116

117117
/// <summary>
118118
/// Deletes text from the cursor position to the specified end position
119119
/// but does not put the deleted text in the kill ring.
120120
/// </summary>
121121
/// <param name="endPosition">0-based offset to one character past the end of the text.</param>
122-
private static void ForwardDeleteImpl(int endPosition)
122+
private static void ForwardDeleteImpl(int endPosition, Action<ConsoleKeyInfo?, object> instigator)
123123
{
124124
var current = _singleton._current;
125125
var buffer = _singleton._buffer;
@@ -128,7 +128,15 @@ private static void ForwardDeleteImpl(int endPosition)
128128
{
129129
int length = endPosition - current;
130130
var str = buffer.ToString(current, length);
131-
_singleton.SaveEditItem(EditItemDelete.Create(str, current));
131+
132+
_singleton.SaveEditItem(
133+
EditItemDelete.Create(
134+
str,
135+
current,
136+
instigator,
137+
instigatorArg: null,
138+
!InViEditMode()));
139+
132140
buffer.Remove(current, length);
133141
_singleton.Render();
134142
}
@@ -152,13 +160,13 @@ public static void BackwardDeleteLine(ConsoleKeyInfo? key = null, object arg = n
152160
BackwardDeleteSubstring(position, BackwardDeleteLine);
153161
}
154162

155-
private static void BackwardDeleteSubstring(int position, Action<ConsoleKeyInfo?, object> instigator = null)
163+
private static void BackwardDeleteSubstring(int position, Action<ConsoleKeyInfo?, object> instigator)
156164
{
157165
if (_singleton._current > position)
158166
{
159167
var count = _singleton._current - position;
160-
161-
_singleton.RemoveTextToViRegister(position, count, instigator);
168+
169+
_singleton.RemoveTextToViRegister(position, count, instigator, arg: null, !InViEditMode());
162170
_singleton._current = position;
163171
_singleton.Render();
164172
}
@@ -184,7 +192,7 @@ public static void BackwardDeleteChar(ConsoleKeyInfo? key = null, object arg = n
184192

185193
int startDeleteIndex = _singleton._current - qty;
186194

187-
_singleton.RemoveTextToViRegister(startDeleteIndex, qty, BackwardDeleteChar, arg);
195+
_singleton.RemoveTextToViRegister(startDeleteIndex, qty, BackwardDeleteChar, arg, !InViEditMode());
188196
_singleton._current = startDeleteIndex;
189197
_singleton.Render();
190198
}
@@ -205,7 +213,7 @@ private void DeleteCharImpl(int qty, bool orExit)
205213
{
206214
qty = Math.Min(qty, _singleton._buffer.Length - _singleton._current);
207215

208-
RemoveTextToViRegister(_current, qty, DeleteChar, qty);
216+
RemoveTextToViRegister(_current, qty, DeleteChar, qty, !InViEditMode());
209217
if (_current >= _buffer.Length)
210218
{
211219
_current = Math.Max(0, _buffer.Length + ViEndOfLineFactor);

PSReadLine/History.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,24 @@ private bool WithHistoryFileMutexDo(int timeout, Action action)
313313
_historyFileMutex.ReleaseMutex();
314314
}
315315
}
316+
317+
// Consider it a failure if we timed out on the mutex.
318+
return false;
316319
}
317320
catch (AbandonedMutexException)
318321
{
319322
retryCount += 1;
323+
324+
// We acquired the mutex object that was abandoned by another powershell process.
325+
// Now, since we own it, we must release it before retry, otherwise, we will miss
326+
// a release and keep holding the mutex, in which case the 'WaitOne' calls from
327+
// all other powershell processes will time out.
328+
_historyFileMutex.ReleaseMutex();
320329
}
321330
} while (retryCount > 0 && retryCount < 3);
322331

323-
// No errors to report, so consider it a success even if we timed out on the mutex.
324-
return true;
332+
// If we reach here, that means we've done the retries but always got the 'AbandonedMutexException'.
333+
return false;
325334
}
326335

327336
private void WriteHistoryRange(int start, int end, bool overwritten)
@@ -427,18 +436,16 @@ private bool MaybeReadHistoryFile()
427436

428437
private void ReadHistoryFile()
429438
{
430-
WithHistoryFileMutexDo(1000, () =>
439+
if (File.Exists(Options.HistorySavePath))
431440
{
432-
if (!File.Exists(Options.HistorySavePath))
441+
WithHistoryFileMutexDo(1000, () =>
433442
{
434-
return;
435-
}
436-
437-
var historyLines = File.ReadAllLines(Options.HistorySavePath);
438-
UpdateHistoryFromFile(historyLines, fromDifferentSession: false, fromInitialRead: true);
439-
var fileInfo = new FileInfo(Options.HistorySavePath);
440-
_historyFileLastSavedSize = fileInfo.Length;
441-
});
443+
var historyLines = File.ReadAllLines(Options.HistorySavePath);
444+
UpdateHistoryFromFile(historyLines, fromDifferentSession: false, fromInitialRead: true);
445+
var fileInfo = new FileInfo(Options.HistorySavePath);
446+
_historyFileLastSavedSize = fileInfo.Length;
447+
});
448+
}
442449
}
443450

444451
void UpdateHistoryFromFile(IEnumerable<string> historyLines, bool fromDifferentSession, bool fromInitialRead)

PSReadLine/Keys.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void AppendPart(string str)
215215
// Keys I'm not familiar with, presumably we'd want to ignore.
216216
case ConsoleKey.Clear: case ConsoleKey.Pause: case ConsoleKey.Select: case ConsoleKey.Print:
217217
case ConsoleKey.Execute: case ConsoleKey.Help: case ConsoleKey.Sleep: case ConsoleKey.Process:
218-
case ConsoleKey.Packet: case ConsoleKey.Attention: case ConsoleKey.CrSel: case ConsoleKey.ExSel:
218+
case ConsoleKey.Attention: case ConsoleKey.CrSel: case ConsoleKey.ExSel:
219219
case ConsoleKey.EraseEndOfFile: case ConsoleKey.Play: case ConsoleKey.Zoom: case ConsoleKey.NoName:
220220
case ConsoleKey.Pa1:
221221

PSReadLine/Movement.vi.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ public static void ViEndOfPreviousGlob(ConsoleKeyInfo? key = null, object arg =
159159
/// Move the cursor to the end of the current logical line.
160160
/// </summary>
161161
public static void MoveToEndOfLine(ConsoleKeyInfo? key = null, object arg = null)
162-
{
163-
var eol = GetEndOfLogicalLinePos(_singleton._current);
164-
if (eol != _singleton._current)
165-
{
166-
_singleton.MoveCursor(eol);
167-
}
168-
_singleton._moveToEndOfLineCommandCount++;
169-
_singleton._moveToLineDesiredColumn = int.MaxValue;
162+
{
163+
var eol = GetEndOfLogicalLinePos(_singleton._current);
164+
if (eol != _singleton._current)
165+
{
166+
_singleton.MoveCursor(eol);
167+
}
168+
_singleton._moveToEndOfLineCommandCount++;
169+
_singleton._moveToLineDesiredColumn = int.MaxValue;
170170
}
171171

172172
/// <summary>
@@ -189,7 +189,7 @@ public static void NextWordEnd(ConsoleKeyInfo? key = null, object arg = null)
189189
public static void GotoColumn(ConsoleKeyInfo? key = null, object arg = null)
190190
{
191191
int col = arg as int? ?? -1;
192-
if (col < 0)
192+
if (col < 0)
193193
{
194194
Ding();
195195
return;
@@ -210,11 +210,11 @@ public static void GotoColumn(ConsoleKeyInfo? key = null, object arg = null)
210210
/// Move the cursor to the first non-blank character in the line.
211211
/// </summary>
212212
public static void GotoFirstNonBlankOfLine(ConsoleKeyInfo? key = null, object arg = null)
213-
{
214-
var newCurrent = GetFirstNonBlankOfLogicalLinePos(_singleton._current);
215-
if (newCurrent != _singleton._current)
216-
{
217-
_singleton.MoveCursor(newCurrent);
213+
{
214+
var newCurrent = GetFirstNonBlankOfLogicalLinePos(_singleton._current);
215+
if (newCurrent != _singleton._current)
216+
{
217+
_singleton.MoveCursor(newCurrent);
218218
}
219219
}
220220

@@ -234,6 +234,11 @@ public static void ViGotoBrace(ConsoleKeyInfo? key = null, object arg = null)
234234

235235
private int ViFindBrace(int i)
236236
{
237+
if (_buffer.Length == 0)
238+
{
239+
return i;
240+
}
241+
237242
switch (_buffer[i])
238243
{
239244
case '{':

PSReadLine/PSReadLine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</ItemGroup>
2323

2424
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
25-
<PackageReference Include="System.Management.Automation" Version="7.2.0-preview.6" />
25+
<PackageReference Include="System.Management.Automation" Version="7.2.0-preview.9" />
2626
</ItemGroup>
2727

2828
<ItemGroup>

0 commit comments

Comments
 (0)