handle key inputs better

This commit is contained in:
Matt Parker
2025-10-28 21:04:04 +10:00
parent f4df002358
commit 542a3a33a6
3 changed files with 47 additions and 47 deletions

View File

@@ -60,13 +60,19 @@ public partial class SharpIdeCodeEdit : CodeEdit
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"_", "<", ".", "#" "_", "<", ".", "#"
]; ];
private readonly List<string> _additionalCodeCompletionPrefixes =
[
//"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
//"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"(", ",", "=", "\t"
];
public override void _Ready() public override void _Ready()
{ {
// _filter_code_completion_candidates_impl uses these prefixes to determine where the completions menu is allowed to show. // _filter_code_completion_candidates_impl uses these prefixes to determine where the completions menu is allowed to show.
// It is quite annoying as we cannot override it via _FilterCodeCompletionCandidates, as we would lose the filtering as well. // It is quite annoying as we cannot override it via _FilterCodeCompletionCandidates, as we would lose the filtering as well.
// Currently, it is not possible to show completions on a new line at col 0 // Currently, it is not possible to show completions on a new line at col 0
CodeCompletionPrefixes = [.._codeCompletionTriggers, "(", ",", "=", "\t"]; CodeCompletionPrefixes = [.._codeCompletionTriggers, .._additionalCodeCompletionPrefixes];
SyntaxHighlighter = _syntaxHighlighter; SyntaxHighlighter = _syntaxHighlighter;
_popupMenu = GetNode<PopupMenu>("CodeFixesMenu"); _popupMenu = GetNode<PopupMenu>("CodeFixesMenu");
_popupMenu.IdPressed += OnCodeFixSelected; _popupMenu.IdPressed += OnCodeFixSelected;
@@ -335,16 +341,46 @@ public partial class SharpIdeCodeEdit : CodeEdit
// return base._FilterCodeCompletionCandidates(candidates); // return base._FilterCodeCompletionCandidates(candidates);
// } // }
// public override void _GuiInput(InputEvent @event) public override void _GuiInput(InputEvent @event)
// { {
// if (@event.IsActionPressed("ui_text_completion_query")) if (@event is InputEventKey { Pressed: true } keyEvent)
// { {
// GD.Print("Entering CompletionQueryBuiltin _GuiInput"); var codeCompletionSelectedIndex = GetCodeCompletionSelectedIndex();
// AcceptEvent(); var isCodeCompletionPopupOpen = codeCompletionSelectedIndex is not -1;
// //GetViewport().SetInputAsHandled(); if (keyEvent is { Keycode: Key.Backspace, CtrlPressed: false })
// Callable.From(() => RequestCodeCompletion(true)).CallDeferred(); {
// }
// } }
if (keyEvent is { Keycode: Key.Delete, CtrlPressed: false })
{
}
else if (keyEvent.Unicode != 0)
{
var unicodeString = char.ConvertFromUtf32((int)keyEvent.Unicode);
if (isCodeCompletionPopupOpen && unicodeString is " ")
{
Callable.From(() => CancelCodeCompletion()).CallDeferred();
}
else if (isCodeCompletionPopupOpen is false && _codeCompletionTriggers.Contains(unicodeString, StringComparer.OrdinalIgnoreCase))
{
void OnAction()
{
TextChanged -= OnAction;
Callable.From(() => RequestCodeCompletion(true)).CallDeferred();
}
TextChanged += OnAction; // We need to wait for the text to actually change before requesting completions
}
}
}
// else if (@event.IsActionPressed("ui_text_completion_query"))
// {
// GD.Print("Entering CompletionQueryBuiltin _GuiInput");
// AcceptEvent();
// //GetViewport().SetInputAsHandled();
// Callable.From(() => RequestCodeCompletion(true)).CallDeferred();
// }
}
public override void _UnhandledKeyInput(InputEvent @event) public override void _UnhandledKeyInput(InputEvent @event)
{ {

View File

@@ -1,35 +0,0 @@
using Godot;
namespace SharpIDE.Godot.Features.CodeEditor;
public partial class SharpIdeCodeEdit
{
// public override void _Backspace(int caretIndex)
// {
// //base._Backspace(caretIndex);
// var caretLine = GetCaretLine(caretIndex);
// var caretCol = GetCaretColumn(caretIndex);
// RemoveText(caretLine, caretCol - 1, caretLine, caretCol);
// }
public override void _HandleUnicodeInput(int unicodeChar, int caretIndex)
{
StartAction(EditAction.Typing);
string charStr = char.ConvertFromUtf32(unicodeChar);
InsertTextAtCaret(charStr, caretIndex);
var codeCompletionSelectedIndex = GetCodeCompletionSelectedIndex();
var isCodeCompletionPopupOpen = codeCompletionSelectedIndex is not -1;
if (isCodeCompletionPopupOpen && charStr == " ")
{
//CancelCodeCompletion();
Callable.From(() => CancelCodeCompletion()).CallDeferred();
}
else if (isCodeCompletionPopupOpen is false && _codeCompletionTriggers.Contains(charStr, StringComparer.OrdinalIgnoreCase))
{
// This is hopes and prayers that OnTextChanged has finished updating the document in the workspace...
//Callable.From(() => EmitSignalCodeCompletionRequested()).CallDeferred();
Callable.From(() => RequestCodeCompletion(true)).CallDeferred();
}
EndAction();
}