Browse Source

Fix underscore token parsing

pull/165/head
Ionite 1 year ago
parent
commit
ffb7fc451f
No known key found for this signature in database
  1. 13
      StabilityMatrix.Avalonia/Behaviors/TextEditorCompletionBehavior.cs
  2. 5
      StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionList.cs
  3. 28
      StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionWindow.axaml.cs

13
StabilityMatrix.Avalonia/Behaviors/TextEditorCompletionBehavior.cs

@ -123,7 +123,7 @@ public class TextEditorCompletionBehavior : Behavior<TextEditor>
{ {
if (completionWindow is null) return; if (completionWindow is null) return;
Dispatcher.UIThread.Post(() => /*Dispatcher.UIThread.Post(() =>
{ {
// When completion window is open, parse and update token offsets // When completion window is open, parse and update token offsets
if (GetCaretToken(textEditor) is not { } tokenSegment) if (GetCaretToken(textEditor) is not { } tokenSegment)
@ -134,7 +134,7 @@ public class TextEditorCompletionBehavior : Behavior<TextEditor>
completionWindow.StartOffset = tokenSegment.Offset; completionWindow.StartOffset = tokenSegment.Offset;
completionWindow.EndOffset = tokenSegment.EndOffset; completionWindow.EndOffset = tokenSegment.EndOffset;
}); });*/
/*if (e.Text?.Length > 0) { /*if (e.Text?.Length > 0) {
if (!char.IsLetterOrDigit(e.Text[0])) { if (!char.IsLetterOrDigit(e.Text[0])) {
@ -147,6 +147,11 @@ public class TextEditorCompletionBehavior : Behavior<TextEditor>
// We still want to insert the character that was typed. // We still want to insert the character that was typed.
} }
private static bool IsCompletionChar(char c)
{
return char.IsLetterOrDigit(c) || c == '_' || c == '-';
}
/// <summary> /// <summary>
/// Gets a segment of the token the caret is currently in. /// Gets a segment of the token the caret is currently in.
/// </summary> /// </summary>
@ -157,13 +162,13 @@ public class TextEditorCompletionBehavior : Behavior<TextEditor>
// Search for the start and end of a token // Search for the start and end of a token
// A token is defined as either alphanumeric chars or a space // A token is defined as either alphanumeric chars or a space
var start = caret; var start = caret;
while (start > 0 && char.IsLetterOrDigit(textEditor.Document.GetCharAt(start - 1))) while (start > 0 && IsCompletionChar(textEditor.Document.GetCharAt(start - 1)))
{ {
start--; start--;
} }
var end = caret; var end = caret;
while (end < textEditor.Document.TextLength && char.IsLetterOrDigit(textEditor.Document.GetCharAt(end))) while (end < textEditor.Document.TextLength && IsCompletionChar(textEditor.Document.GetCharAt(end)))
{ {
end++; end++;
} }

5
StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionList.cs

@ -122,7 +122,7 @@ public class CompletionList : TemplatedControl
} }
/// <summary> /// <summary>
/// Gets or sets the array of keys that are supposed to request insertation of the completion /// Gets or sets the array of keys that request insertion of the completion
/// </summary> /// </summary>
public Key[] CompletionAcceptKeys { get; set; } public Key[] CompletionAcceptKeys { get; set; }
@ -187,7 +187,8 @@ public class CompletionList : TemplatedControl
_listBox.SelectIndex(_listBox.ItemCount - 1); _listBox.SelectIndex(_listBox.ItemCount - 1);
break; break;
default: default:
if (CompletionAcceptKeys.Contains(e.Key) && CurrentList.Count > 0) // Check insertion keys
if (CompletionAcceptKeys.Contains(e.Key) && CurrentList?.Count > 0)
{ {
e.Handled = true; e.Handled = true;
RequestInsertion(e); RequestInsertion(e);

28
StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionWindow.axaml.cs

@ -43,6 +43,11 @@ public class CompletionWindow : CompletionWindowBase
private PopupWithCustomPosition? _toolTip; private PopupWithCustomPosition? _toolTip;
private ContentControl? _toolTipContent; private ContentControl? _toolTipContent;
/// <summary>
/// Max number of items in the completion list.
/// </summary>
public int MaxListLength { get; set; } = 40;
/// <summary> /// <summary>
/// Gets the completion list used in this completion window. /// Gets the completion list used in this completion window.
/// </summary> /// </summary>
@ -254,22 +259,41 @@ public class CompletionWindow : CompletionWindowBase
Debug.WriteLine("CaretPositionChanged newText: " + newText); Debug.WriteLine("CaretPositionChanged newText: " + newText);
// CompletionList.SelectItem(newText); // CompletionList.SelectItem(newText);
Dispatcher.UIThread.Post(() => UpdateQuery(newText)); Dispatcher.UIThread.Post(() => UpdateQuery(newText));
// UpdateQuery(newText);
IsVisible = CompletionList.ListBox!.ItemCount != 0; IsVisible = CompletionList.ListBox!.ItemCount != 0;
} }
} }
} }
private string? lastSearchTerm;
private int lastCompletionLength;
/// <summary> /// <summary>
/// Update the completion window's current search term. /// Update the completion window's current search term.
/// </summary> /// </summary>
public void UpdateQuery(string searchTerm) public void UpdateQuery(string searchTerm)
{ {
var results = completionProvider.GetCompletions(searchTerm, 30, true); // Fast path if the search term starts with the last search term
// and the last completion count was less than the max list length
// (such we won't get new results by searching again)
if (lastSearchTerm is not null
&& searchTerm.StartsWith(lastSearchTerm)
&& lastCompletionLength < MaxListLength)
{
CompletionList.SelectItem(searchTerm);
lastSearchTerm = searchTerm;
return;
}
var results = completionProvider.GetCompletions(searchTerm, MaxListLength, true);
CompletionList.CompletionData.Clear(); CompletionList.CompletionData.Clear();
CompletionList.CompletionData.AddRange(results); CompletionList.CompletionData.AddRange(results);
CompletionList.SelectItem(searchTerm, true); CompletionList.SelectItem(searchTerm, true);
lastSearchTerm = searchTerm;
lastCompletionLength = CompletionList.CompletionData.Count;
} }
} }

Loading…
Cancel
Save