/// <summary> /// Called when the text in the TextBox changes. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void EditorTextChanged(object sender, EventArgs e) { Text = _editor.Text; if (_popup != null) { string inputText = Text; if (inputText.ToLowerInvariant().StartsWith("new")) { inputText = inputText.Substring(3).Trim(); } IntellisenseEntry targetNode = await IntellisenseData.SearchNodesAsync(AddScopedNamespaces(inputText)) ?? IntellisenseData; if (targetNode.Children.Count == 0) { DestroyPopup(); } else { _popup.DataContext = targetNode.Children; } } var textChanged = TextChanged; if (textChanged != null) { textChanged.Invoke(this, EventArgs.Empty); } }
/// <summary> /// This is called just before a new key is added to the text box. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void EditorPreviewKeyDown(object sender, KeyEventArgs e) { bool isControlKeyDown = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control; // Move up/down the list if (e.Key == Key.Up || e.Key == Key.Down) { if (_popup == null || !_popup.IsOpen) { return; } _popup.SelectedIndex = (e.Key == Key.Up) ? _popup.SelectedIndex - 1 : _popup.SelectedIndex + 1; e.Handled = true; } else if (e.Key == Key.PageUp) { if (_popup == null || !_popup.IsOpen) { return; } _popup.SelectedIndex = _popup.SelectedIndex - 5; e.Handled = true; } else if (e.Key == Key.PageDown) { if (_popup == null || !_popup.IsOpen) { return; } _popup.SelectedIndex = _popup.SelectedIndex + 5; e.Handled = true; } else if (e.Key == Key.Home || e.Key == Key.End) { if (_popup == null || !_popup.IsOpen) { return; } _popup.SelectedIndex = (e.Key == Key.Home) ? 0 : _popup.ItemCount - 1; e.Handled = true; } // Close the popup else if (e.Key == Key.Escape) { DestroyPopup(); _startText = string.Empty; e.Handled = true; } // CTRL+Space or "." else if ((e.Key == Key.Decimal || e.Key == Key.OemPeriod) || (e.Key == Key.Space && isControlKeyDown)) { if (_popup == null || !_popup.IsOpen) { string rawText = _editor.Text; int pos = Math.Max(Math.Max( rawText.LastIndexOf(".", StringComparison.Ordinal), rawText.LastIndexOf("(", StringComparison.Ordinal)), rawText.LastIndexOf(" ", StringComparison.Ordinal)); _startText = (pos == -1) ? rawText : rawText.Substring(0, pos + 1); string inputText = rawText; if (inputText.ToLowerInvariant().StartsWith("new ")) { inputText = inputText.Substring(4); } if (e.Key == Key.Decimal || e.Key == Key.OemPeriod) { inputText = inputText + "."; } IntellisenseEntry intellisenseList = await IntellisenseData.SearchNodesAsync(AddScopedNamespaces(inputText)) ?? IntellisenseData; CreatePopup(intellisenseList.Children); e.Handled = (e.Key == Key.Space && isControlKeyDown); } } else if (e.Key == Key.Return || e.Key == Key.Enter || e.Key == Key.Space || e.Key == Key.Tab) { if (_popup == null || !_popup.IsOpen) { return; } IntellisenseEntry selectedItem = _popup.SelectedItem; if (selectedItem != null) { CommitIntellisenseNode(selectedItem); e.Handled = true; } } }