Skip to content

Commit cfffd0a

Browse files
committed
WIP - next step: update 'GenerateRender'
1 parent ebfe4ce commit cfffd0a

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

PSReadLine/BasicEditing.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,33 @@ public static void SelfInsert(ConsoleKeyInfo? key = null, object arg = null)
3131
{
3232
if (count <= 0)
3333
return;
34-
if (count > 1)
35-
{
36-
var toInsert = new string(keyChar, count);
37-
if (_singleton._visualSelectionCommandCount > 0)
38-
{
39-
_singleton.GetRegion(out var start, out var length);
40-
Replace(start, length, toInsert);
41-
}
42-
else
43-
{
44-
Insert(toInsert);
45-
}
46-
return;
47-
}
34+
}
35+
else
36+
{
37+
count = 1;
4838
}
4939

50-
if (_singleton._visualSelectionCommandCount > 0)
40+
try
5141
{
52-
_singleton.GetRegion(out var start, out var length);
53-
Replace(start, length, new string(keyChar, 1));
42+
_singleton._showSuggestion = true;
43+
44+
if (_singleton._visualSelectionCommandCount > 0)
45+
{
46+
_singleton.GetRegion(out var start, out var length);
47+
Replace(start, length, new string(keyChar, count));
48+
}
49+
else if (count > 1)
50+
{
51+
Insert(new string(keyChar, count));
52+
}
53+
else
54+
{
55+
Insert(keyChar);
56+
}
5457
}
55-
else
58+
finally
5659
{
57-
Insert(keyChar);
60+
_singleton._showSuggestion = false;
5861
}
5962
}
6063

PSReadLine/DummySuggestion.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Pseudo
5+
{
6+
internal class DummySuggestion
7+
{
8+
private static readonly Dictionary<string, string> s_suggestions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
9+
internal static string GetCommandLineSuggestion(string text)
10+
{
11+
if (s_suggestions.TryGetValue(text, out string suggestion))
12+
{
13+
return suggestion;
14+
}
15+
16+
return null;
17+
}
18+
}
19+
}

PSReadLine/ReadLine.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public partial class PSConsoleReadLine : IPSConsoleReadLineMockableMethods
6363

6464
private readonly StringBuilder _buffer;
6565
private readonly StringBuilder _statusBuffer;
66+
private string _suggestionText;
67+
private bool _showSuggestion;
6668
private bool _statusIsErrorMessage;
6769
private string _statusLinePrompt;
6870
private List<EditItem> _edits;

PSReadLine/Render.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ private string ParseInput()
9191
return text;
9292
}
9393

94+
private string GetSuggestion(string text)
95+
{
96+
if (!_showSuggestion || LineIsMultiLine())
97+
{
98+
_suggestionText = null;
99+
}
100+
else
101+
{
102+
bool reuseSuggestionBuffer =
103+
_suggestionText != null &&
104+
_suggestionText.Length >= text.Length &&
105+
_suggestionText.StartsWith(text, StringComparison.OrdinalIgnoreCase);
106+
107+
if (!reuseSuggestionBuffer)
108+
{
109+
_suggestionText = Pseudo.DummySuggestion.GetCommandLineSuggestion(text);
110+
}
111+
}
112+
113+
return _suggestionText == null ? null : _suggestionText.Substring(text.Length);
114+
}
115+
94116
private void ClearStatusMessage(bool render)
95117
{
96118
_statusBuffer.Clear();
@@ -156,6 +178,7 @@ private void ForceRender()
156178
private int GenerateRender(string defaultColor)
157179
{
158180
var text = ParseInput();
181+
var suggestion = GetSuggestion(text);
159182

160183
string color = defaultColor;
161184
string activeColor = string.Empty;

0 commit comments

Comments
 (0)