Skip to content

Commit aa1b557

Browse files
authored
Merge pull request #35 from DevMando/Fix/paste-detection-fast-typing
Fix paste detection triggering on fast typing
2 parents 1de4412 + 96bb844 commit aa1b557

1 file changed

Lines changed: 18 additions & 41 deletions

File tree

src/MandoCode/Services/CommandAutocomplete.cs

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public static string ReadLineWithAutocomplete()
5555
List<string> filteredCommands = new();
5656
List<string> filteredFiles = new();
5757
int atAnchorPos = -1;
58-
string? pastedFullText = null; // holds full content when paste is detected
5958

6059
while (true)
6160
{
@@ -103,7 +102,7 @@ public static string ReadLineWithAutocomplete()
103102
if (autocompleteMode != AutocompleteMode.None)
104103
ClearAutocompleteDisplay(ref cursorTop);
105104
Console.WriteLine();
106-
return pastedFullText ?? input.ToString();
105+
return input.ToString();
107106
}
108107

109108
case ConsoleKey.Tab:
@@ -177,24 +176,6 @@ public static string ReadLineWithAutocomplete()
177176
continue;
178177

179178
case ConsoleKey.Backspace:
180-
if (pastedFullText != null)
181-
{
182-
// Clear the entire paste — start fresh
183-
pastedFullText = null;
184-
input.Clear();
185-
Console.SetCursorPosition(cursorLeft, cursorTop);
186-
Console.Write(new string(' ', Math.Max(0, Console.WindowWidth - cursorLeft - 1)));
187-
Console.SetCursorPosition(cursorLeft, cursorTop);
188-
if (autocompleteMode != AutocompleteMode.None)
189-
{
190-
ClearAutocompleteDisplay(ref cursorTop);
191-
Console.SetCursorPosition(cursorLeft, cursorTop);
192-
}
193-
autocompleteMode = AutocompleteMode.None;
194-
atAnchorPos = -1;
195-
selectedIndex = 0;
196-
continue;
197-
}
198179
if (input.Length > 0)
199180
{
200181
input.Length--;
@@ -281,38 +262,34 @@ public static string ReadLineWithAutocomplete()
281262
// Detect paste: if more characters are immediately buffered, consume them all
282263
if (Console.KeyAvailable)
283264
{
284-
var pasteChars = new StringBuilder();
285-
pasteChars.Append(key.KeyChar);
265+
var bufferedChars = new List<char>();
266+
bufferedChars.Add(key.KeyChar);
286267

287268
while (Console.KeyAvailable)
288269
{
289270
var pk = Console.ReadKey(intercept: true);
290271
if (pk.Key == ConsoleKey.Enter || pk.KeyChar == '\n' || pk.KeyChar == '\r')
291-
pasteChars.Append(' '); // newlines → spaces
272+
bufferedChars.Add(' '); // newlines → spaces
292273
else if (!char.IsControl(pk.KeyChar))
293-
pasteChars.Append(pk.KeyChar);
274+
bufferedChars.Add(pk.KeyChar);
294275
}
295276

296-
// Store full content: any text typed before + pasted chars
297-
var beforePaste = input.ToString();
298-
pastedFullText = beforePaste + pasteChars.ToString();
299-
300-
// Update display to show summary
301-
input.Clear();
302-
input.Append($"[pasted {pastedFullText.Length} characters]");
277+
// Only treat as paste if 5+ chars arrived at once; otherwise it's fast typing
278+
// Append all buffered chars normally (whether fast typing or paste)
279+
foreach (var c in bufferedChars)
280+
{
281+
input.Append(c);
282+
Console.Write(c);
283+
}
303284

304-
// Close any autocomplete
285+
// Close any autocomplete if open
305286
if (autocompleteMode != AutocompleteMode.None)
287+
{
306288
ClearAutocompleteDisplay(ref cursorTop);
307-
autocompleteMode = AutocompleteMode.None;
308-
atAnchorPos = -1;
309-
selectedIndex = 0;
310-
311-
// Redraw input line with paste summary
312-
Console.SetCursorPosition(cursorLeft, cursorTop);
313-
Console.Write(new string(' ', Math.Max(0, Console.WindowWidth - cursorLeft - 1)));
314-
Console.SetCursorPosition(cursorLeft, cursorTop);
315-
Console.Write(input.ToString());
289+
autocompleteMode = AutocompleteMode.None;
290+
atAnchorPos = -1;
291+
selectedIndex = 0;
292+
}
316293
continue;
317294
}
318295

0 commit comments

Comments
 (0)