private void Editor_LostFocus(object sender, Avalonia.Interactivity.RoutedEventArgs e) { intellisenseJobRunner.InvokeAsync(() => { CloseIntellisense(); }); completionAssistant.Close(); }
public async Task OnKeyUp(KeyEventArgs e) { var isVisible = intellisenseControl.IsVisible; var caretIndex = editor.CaretIndex; var caretChar = '\0'; var behindCaretChar = '\0'; var behindBehindCaretChar = '\0'; await Dispatcher.UIThread.InvokeTaskAsync(() => { if (caretIndex > 1 && caretIndex < editor.TextDocument.TextLength) { behindBehindCaretChar = editor.TextDocument.GetCharAt(caretIndex - 2); } if (caretIndex > 0 && caretIndex < editor.TextDocument.TextLength) { caretChar = editor.TextDocument.GetCharAt(caretIndex); } if (caretIndex > 0 && caretIndex <= editor.TextDocument.TextLength) { behindCaretChar = editor.TextDocument.GetCharAt(caretIndex - 1); } }); await CompletionAssistantOnKeyUp(behindCaretChar, behindBehindCaretChar); if (e.Key == Key.Escape && e.Modifiers == InputModifiers.None) { await Dispatcher.UIThread.InvokeTaskAsync(() => { if (completionAssistant.IsVisible) { completionAssistant.Close(); } else if (intellisenseControl.IsVisible) { Close(); } }); } if (IsIntellisenseKey(e)) { if (caretIndex <= intellisenseStartedAt) { await Dispatcher.UIThread.InvokeTaskAsync(() => { Close(); }); return; } if (IsIntellisenseResetKey(e)) { isVisible = false; // We dont actually want to hide, so use backing field. //currentFilter = string.Empty; } await Dispatcher.UIThread.InvokeTaskAsync(async() => { await CompleteOnKeyUp(); }); IEnumerable <CompletionDataViewModel> filteredResults = null; if (!intellisenseControl.IsVisible && (IsIntellisenseOpenKey(e) || IsIntellisenseResetKey(e))) { var caret = new TextLocation(); var behindStartChar = '\0'; if (behindCaretChar == ':' && behindBehindCaretChar == ':') { intellisenseStartedAt = caretIndex; intellisenseEndsAt = intellisenseStartedAt; intellisenseOpenedAt = intellisenseStartedAt; } else if (behindCaretChar == '>' || behindBehindCaretChar == ':' || behindBehindCaretChar == '>') { intellisenseStartedAt = caretIndex - 1; intellisenseEndsAt = intellisenseStartedAt; intellisenseOpenedAt = intellisenseStartedAt; } else { await Dispatcher.UIThread.InvokeTaskAsync( () => { if (caretIndex > 1) { intellisenseOpenedAt = caretIndex - 1; intellisenseStartedAt = TextUtilities.GetNextCaretPosition(editor.TextDocument, caretIndex, TextUtilities.LogicalDirection.Backward, TextUtilities.CaretPositioningMode.WordStart); } else { intellisenseOpenedAt = 1; intellisenseStartedAt = 1; } behindStartChar = editor.TextDocument.GetCharAt(intellisenseStartedAt - 1); if ((behindStartChar.IsWhiteSpace() || !behindStartChar.IsSymbolChar()) && (caretChar.IsWhiteSpace() || !caretChar.IsSymbolChar())) { intellisenseEndsAt = intellisenseStartedAt; } else { intellisenseEndsAt = TextUtilities.GetNextCaretPosition(editor.TextDocument, caretIndex, TextUtilities.LogicalDirection.Forward, TextUtilities.CaretPositioningMode.WordBorder) - 1; } }); } if (IsIntellisenseResetKey(e)) { intellisenseStartedAt++; intellisenseOpenedAt++; } await Dispatcher.UIThread.InvokeTaskAsync(() => { currentFilter = editor.TextDocument.GetText(intellisenseStartedAt, caretIndex - intellisenseStartedAt); caret = GetTextLocation(intellisenseStartedAt); }); var codeCompletionResults = await intellisenseControl.DoCompletionRequestAsync(caret.Line, caret.Column); unfilteredCompletions.Clear(); if (codeCompletionResults != null) { foreach (var result in codeCompletionResults.Completions) { if (result.Suggestion.ToLower().Contains(currentFilter.ToLower())) { CompletionDataViewModel currentCompletion = null; currentCompletion = unfilteredCompletions.BinarySearch(c => c.Title, result.Suggestion); if (currentCompletion == null) { unfilteredCompletions.Add(CompletionDataViewModel.Create(result)); } else { currentCompletion.Overloads++; } } } } filteredResults = unfilteredCompletions; } else { await Dispatcher.UIThread.InvokeTaskAsync( () => { if (intellisenseStartedAt != -1 && caretIndex > intellisenseStartedAt) { currentFilter = editor.TextDocument.GetText(intellisenseStartedAt, caretIndex - intellisenseStartedAt); } else { currentFilter = string.Empty; } }); filteredResults = unfilteredCompletions.Where(c => c.Title.ToLower().Contains(currentFilter.ToLower())); } CompletionDataViewModel suggestion = null; if (currentFilter != string.Empty) { IEnumerable <CompletionDataViewModel> newSelectedCompletions = null; newSelectedCompletions = filteredResults.Where(s => s.Title.StartsWith(currentFilter)); // try find exact match case sensitive if (newSelectedCompletions.Count() == 0) { newSelectedCompletions = filteredResults.Where(s => s.Title.ToLower().StartsWith(currentFilter.ToLower())); // try find non-case sensitve match } if (newSelectedCompletions.Count() == 0) { suggestion = noSelectedCompletion; } else { var newSelectedCompletion = newSelectedCompletions.FirstOrDefault(); suggestion = newSelectedCompletion; } } else { suggestion = noSelectedCompletion; } if (filteredResults?.Count() > 0) { if (filteredResults?.Count() == 1 && filteredResults.First().Title == currentFilter) { await Dispatcher.UIThread.InvokeTaskAsync(() => { Close(); }); } else { var list = filteredResults.ToList(); await Dispatcher.UIThread.InvokeTaskAsync(() => { intellisenseControl.IsVisible = true; }); await Dispatcher.UIThread.InvokeTaskAsync(() => { intellisenseControl.CompletionData = list; intellisenseControl.SelectedCompletion = null; intellisenseControl.SelectedCompletion = suggestion; }); } } else { await Dispatcher.UIThread.InvokeTaskAsync(() => { Close(); }); } } else if (IsAllowedNonFilterModificationKey(e)) { // do nothing } else { if (intellisenseControl.IsVisible && IsCompletionKey(e)) { e.Handled = true; } await Dispatcher.UIThread.InvokeTaskAsync(() => { Close(); }); } }
private void Editor_LostFocus(object sender, Avalonia.Interactivity.RoutedEventArgs e) { CloseIntellisense(); completionAssistant?.Close(); }
public void OnKeyDown(KeyEventArgs e, int caretIndex, int line, int column) { if (e.Source == editor.TextArea) { capturedOnKeyDown = e.Key; if (!_hidden) { switch (capturedOnKeyDown) { case Key.Down: { var index = intellisenseControl.CompletionData.IndexOf(intellisenseControl.SelectedCompletion); if (index < intellisenseControl.CompletionData.Count - 1) { intellisenseControl.SelectedCompletion = intellisenseControl.CompletionData[index + 1]; } e.Handled = true; } break; case Key.Up: { var index = intellisenseControl.CompletionData.IndexOf(intellisenseControl.SelectedCompletion); if (index > 0) { intellisenseControl.SelectedCompletion = intellisenseControl.CompletionData[index - 1]; } e.Handled = true; } break; case Key.Back: if (caretIndex - 1 >= intellisenseStartedAt) { UpdateFilter(caretIndex - 1); } break; case Key.Enter: DoComplete(false); e.Handled = true; break; } } if (completionAssistant.IsVisible) { if (!e.Handled) { switch (e.Key) { case Key.Down: { completionAssistant.IncrementSignatureIndex(); e.Handled = true; } break; case Key.Up: { completionAssistant.DecrementSignatureIndex(); e.Handled = true; } break; } } } if (e.Key == Key.Escape) { if (completionAssistant.IsVisible) { completionAssistant.Close(); } else if (!_hidden) { CloseIntellisense(); } } } }
private async Task UpdateActiveParameterAndVisibility(bool canTrigger = false) { if (completionAssistant.IsVisible) { if (completionAssistant.CurrentSignatureHelp != null) { var indexStack = new Stack <int>(); int index = 0; int level = -1; int offset = completionAssistant.Stack.Last().Offset; SignatureHelp currentHelpStack = null; while (offset < editor.CaretOffset) { var curChar = '\0'; curChar = editor.Document.GetCharAt(offset++); switch (curChar) { case ',': index++; break; case '(': level++; if ((level + 1) > completionAssistant.Stack.Count) { await PushToSignatureHelp("", offset); } currentHelpStack = completionAssistant.Stack[completionAssistant.Stack.Count - level - 1]; if (level >= 0) { indexStack.Push(index); index = 0; } break; case ')': if (indexStack.Count > 0) { index = indexStack.Pop(); } level--; if (level >= 0) { currentHelpStack = completionAssistant.Stack[completionAssistant.Stack.Count - level - 1]; } else { currentHelpStack = null; } break; } } if (currentHelpStack != null) { _onSetSignatureHelpPosition(currentHelpStack.Offset); completionAssistant.SelectStack(currentHelpStack); completionAssistant.SetParameterIndex(index); } else { completionAssistant.Close(); } } } else if (canTrigger) { var currentChar = editor.Document.GetCharAt(editor.CaretOffset - 1); if ((currentChar == '(' || (currentChar == ',') && (completionAssistant.CurrentSignatureHelp == null || completionAssistant.CurrentSignatureHelp.Offset != editor.CaretOffset))) { string currentWord = string.Empty; char behindBehindCaretChar = '\0'; behindBehindCaretChar = editor.Document.GetCharAt(editor.CaretOffset - 2); if (behindBehindCaretChar.IsWhiteSpace() && behindBehindCaretChar != '\0') { currentWord = editor.Document.GetPreviousWordAtIndex(editor.CaretOffset - 1); } else { currentWord = editor.Document.GetWordAtIndex(editor.CaretOffset - 1); } await PushToSignatureHelp(currentWord, editor.CaretOffset); } else if (currentChar == ')') { completionAssistant.PopMethod(); } } }
private void Editor_LostFocus(object sender, EventArgs e) { CloseIntellisense(); completionAssistant?.Close(); }