예제 #1
0
        private void TextBox_KeyPressing(object sender, KeyPressEventArgs e)
        {
            var settings = CodeEditorExtension.Instance.Settings;

            _itemEnumerator.CanYieldItems = _autoCompleteMenu.Visible || settings.GetValue <bool>("AutoCompletion.ShowSuggestionsWhenTyping");

            if (_autoCompleteMenu != null && _autoCompleteMenu.Visible)
            {
                // auto complete when a key specified in the settings has been pressed.
                if ((settings.GetValue <bool>("AutoCompletion.AutoCompleteCommitOnSpaceBar") && e.KeyChar == ' ') ||
                    (settings.GetValue <bool>("AutoCompletion.AutoCompleteCommitOnTab") && e.KeyChar == '\t') ||
                    (settings.GetValue <string>("AutoCompletion.AutoCompleteCommitChars").Contains(e.KeyChar)))
                {
                    _autoCompleteMenu.OnSelecting();

                    var selectedItem = _autoCompleteMenu.Items.FocussedItem as CodeEditorAutoCompleteItem;
                    if (selectedItem != null)
                    {
                        // do not append space when surpressed by autocomplete item
                        if (e.KeyChar == ' ' && selectedItem.SurpressSpaceBar)
                        {
                            e.Handled = true;
                            return;
                        }

                        if (selectedItem is CodeEditorMethodAutoCompleteItem)
                        {
                            // do not append extra parantheses for methods when the paranthese key is pressed.
                            if (settings.GetValue <bool>("AutoCompletion.AutoCompleteMethodParantheses") && (selectedItem as CodeEditorMethodAutoCompleteItem).Parantheses[0] == e.KeyChar)
                            {
                                e.Handled = true;
                                return;
                            }
                        }
                    }
                }
            }

            if (_autoCompletionMap is ICodeBlockCompleter && this.TextBox.SelectionStart < this.TextBox.Text.Length)
            {
                var styles = this.TextBox.GetStylesOfChar(this.TextBox.Selection.Start);

                var    blockCompleter      = _autoCompletionMap as ICodeBlockCompleter;
                var    startIdentifiers    = blockCompleter.BlockIdentifiers.Keys;
                var    endIdentifiers      = blockCompleter.BlockIdentifiers.Values;
                string currentCharAsString = this.TextBox.Text[this.TextBox.SelectionStart].ToString();
                string nextCharAsString    = this.TextBox.Text.Length < this.TextBox.SelectionStart ? this.TextBox.Text[this.TextBox.SelectionStart + 1].ToString() : null;
                string keyCharAsString     = e.KeyChar.ToString();

                // do not handle ending blocks when they are already present
                if (endIdentifiers.Contains(currentCharAsString) && currentCharAsString == keyCharAsString)
                {
                    e.Handled = true;
                    this.TextBox.SelectionStart++;
                }
                // auto complete code blocks
                else if (settings.GetValue <bool>("AutoCompletion.AutoCompleteCodeBlocks") && !styles.Contains(this.TextBox.SyntaxHighlighter.CommentStyle) && !styles.Contains(this.TextBox.SyntaxHighlighter.StringStyle) && startIdentifiers.Contains(keyCharAsString))
                {
                    this.TextBox.InsertText(keyCharAsString + blockCompleter.BlockIdentifiers[keyCharAsString]);
                    this.TextBox.SelectionStart -= blockCompleter.BlockIdentifiers[keyCharAsString].Length;
                    e.Handled           = true;
                    _justCompletedBrace = true;
                }
            }
        }