Skip to content

Commit 9160c6b

Browse files
committed
Use the history as the suggestion source
1 parent 2d3af8b commit 9160c6b

File tree

7 files changed

+37
-38
lines changed

7 files changed

+37
-38
lines changed

PSReadLine/BasicEditing.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public static void CancelLine(ConsoleKeyInfo? key = null, object arg = null)
7979
{
8080
_singleton.ClearStatusMessage(false);
8181
_singleton._current = _singleton._buffer.Length;
82+
83+
using var _ = _singleton.ChangeSuggestionMode(showSuggestion: false);
8284
_singleton.ForceRender();
8385

8486
_singleton._console.Write("\x1b[91m^C\x1b[0m");

PSReadLine/Completion.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ private void CompleteImpl(bool menuSelect)
217217
ViInsertWithAppend();
218218
}
219219

220+
// Do not show suggestion text during tab completion.
221+
using var _ = ChangeSuggestionMode(showSuggestion: false);
222+
220223
var completions = GetCompletions();
221224
if (completions == null || completions.CompletionMatches.Count == 0)
222225
return;

PSReadLine/History.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,20 @@ private void HistorySearch(int direction)
679679
}
680680
}
681681

682+
private string GetHistorySuggestion(string text)
683+
{
684+
for (int index = _history.Count - 1; index > 0; index --)
685+
{
686+
var line = _history[index].CommandLine;
687+
if (line.Length > text.Length && !LineIsMultiLine(line) && line.StartsWith(text, Options.HistoryStringComparison))
688+
{
689+
return line;
690+
}
691+
}
692+
693+
return null;
694+
}
695+
682696
/// <summary>
683697
/// Move to the first item in the history.
684698
/// </summary>

PSReadLine/PublicAPI.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public static void AcceptSuggestion()
243243
{
244244
if (_singleton._suggestionText != null)
245245
{
246+
using var _ = _singleton.ChangeSuggestionMode(showSuggestion: false);
246247
Replace(0, _singleton._buffer.Length, _singleton._suggestionText);
247248
}
248249
}

PSReadLine/ReadLine.vi.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ internal static IDisposable UseViInsertModeTables()
522522

523523
private void ViIndicateCommandMode()
524524
{
525+
// Show suggestion in 'InsertMode' but not 'CommandMode'.
526+
_showSuggestion = false;
527+
525528
if (_options.ViModeIndicator == ViModeStyle.Cursor)
526529
{
527530
_console.CursorSize = _normalCursorSize < 50 ? 100 : 25;
@@ -541,6 +544,9 @@ private void ViIndicateCommandMode()
541544

542545
private void ViIndicateInsertMode()
543546
{
547+
// Show suggestion in 'InsertMode' but not 'CommandMode'.
548+
_showSuggestion = true;
549+
544550
if (_options.ViModeIndicator == ViModeStyle.Cursor)
545551
{
546552
_console.CursorSize = _normalCursorSize;

PSReadLine/Render.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private string GetSuggestion(string text)
110110
_lastRenderedTextHash = FNV1a32Hash.ComputeHash(text);
111111
if (textHash != _lastRenderedTextHash)
112112
{
113-
_suggestionText = DummySuggestion.GetCommandLineSuggestion(text);
113+
_suggestionText = GetHistorySuggestion(text);
114114
}
115115
}
116116
catch
@@ -1257,6 +1257,16 @@ private bool LineIsMultiLine()
12571257
return false;
12581258
}
12591259

1260+
private bool LineIsMultiLine(string text)
1261+
{
1262+
for (int i = 0; i < text.Length; i++)
1263+
{
1264+
if (text[i] == '\n')
1265+
return true;
1266+
}
1267+
return false;
1268+
}
1269+
12601270
private int GetStatusLineCount()
12611271
{
12621272
if (_statusLinePrompt == null)

PSReadLine/Suggestion.cs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,6 @@
33

44
namespace Microsoft.PowerShell
55
{
6-
internal class DummySuggestion
7-
{
8-
private static readonly List<string> s_suggestions;
9-
10-
static DummySuggestion()
11-
{
12-
s_suggestions = new List<string>()
13-
{
14-
"ildasm",
15-
"git",
16-
"git branch -r",
17-
"git checkout -b",
18-
"git checkout master",
19-
"git fetch --all -p",
20-
"git status",
21-
"git diff",
22-
"git diff --cached",
23-
"git add -u",
24-
"git add -A",
25-
"start",
26-
};
27-
}
28-
29-
internal static string GetCommandLineSuggestion(string text)
30-
{
31-
foreach (string candidate in s_suggestions)
32-
{
33-
if (candidate.Length > text.Length && candidate.StartsWith(text, StringComparison.OrdinalIgnoreCase))
34-
{
35-
return candidate;
36-
}
37-
}
38-
39-
return null;
40-
}
41-
}
42-
436
public partial class PSConsoleReadLine
447
{
458
private class SuggestionModeRestore : IDisposable

0 commit comments

Comments
 (0)