ProcessKeyEvent() public method

public ProcessKeyEvent ( char ch ) : bool
ch char
return bool
Example #1
0
        /// <summary>
        /// Return true to handle the keypress, return false to let the text area handle the keypress
        /// </summary>
        bool TextAreaKeyEventHandler(char key)
        {
            if (m_code_complete != null)
            {
                // If completion window is open and wants to handle the key, don't let the text area
                // handle it
                if (m_code_complete.ProcessKeyEvent(key))
                {
                    return(true);
                }
            }

            ICSharpCode.TextEditor.Gui.CompletionWindow.ICompletionDataProvider completionDataProvider = CodeComplete.GetCompletionProvider(this, key);
            if (completionDataProvider == null && key == '(')
            {
                ICSharpCode.TextEditor.Document.LineSegment line = this.Document.GetLineSegment(ActiveTextAreaControl.Caret.Line);
                if (null == line)
                {
                    return(false);
                }

                List <ICSharpCode.TextEditor.Document.TextWord> words = line.Words;
                if (words == null || words.Count < 1)
                {
                    return(false);
                }

                ICSharpCode.TextEditor.Document.TextWord lastWord = words[words.Count - 1];
                if (lastWord.Type != ICSharpCode.TextEditor.Document.TextWordType.Word)
                {
                    return(false);
                }
                // Todo: Remove hard coded colors
                if (lastWord.Color == System.Drawing.Color.Green || lastWord.Color == System.Drawing.Color.Red)
                {
                    return(false);
                }

                // make sure first real word on this line is not "def"
                for (int i = 0; i < words.Count; i++)
                {
                    if (words[i].Type != ICSharpCode.TextEditor.Document.TextWordType.Word)
                    {
                        continue;
                    }
                    if (words[i].Word.Equals("def", StringComparison.Ordinal))
                    {
                        return(false);
                    }
                    break;
                }

                char c = lastWord.Word[0];
                if (char.IsLetter(c))
                {
                    // Build up a python script to execute
                    int           line_number = this.ActiveTextAreaControl.TextArea.Caret.Line;
                    StringBuilder script      = new StringBuilder();
                    for (int i = 0; i < line_number; i++)
                    {
                        line = this.ActiveTextAreaControl.TextArea.Document.GetLineSegment(i);
                        if (line != null && line.Words.Count > 2)
                        {
                            string firstword = line.Words[0].Word;
                            if (firstword.Equals("import", StringComparison.Ordinal) || firstword.Equals("from"))
                            {
                                script.AppendLine(this.ActiveTextAreaControl.TextArea.Document.GetText(line));
                            }
                        }
                    }

                    ICSharpCode.TextEditor.Document.LineSegment lastLine = this.ActiveTextAreaControl.TextArea.Document.GetLineSegment(line_number);
                    if (null == lastLine)
                    {
                        return(false);
                    }

                    //walk backward through the line until we hit something that is NOT a word or period
                    string evaluation = "";
                    for (int i = lastLine.Words.Count - 1; i >= 0; i--)
                    {
                        ICSharpCode.TextEditor.Document.TextWord word = lastLine.Words[i];
                        if (word.Type != ICSharpCode.TextEditor.Document.TextWordType.Word)
                        {
                            break;
                        }
                        c = word.Word[0];
                        if (c != '.' && !char.IsLetter(c))
                        {
                            break;
                        }
                        evaluation = evaluation.Insert(0, word.Word);
                    }

                    if (evaluation != "if" && evaluation != "while")
                    {
                        RhinoDLR_Python.Intellisense isense = m_intellisense;
                        if (isense == null)
                        {
                            isense = CodeComplete.Intellisense;
                        }
                        string rc = isense.EvaluateHelp(script.ToString(), evaluation);
                        if (!string.IsNullOrEmpty(rc) && null != m_help_callback)
                        {
                            m_help_callback(rc);
                        }
                    }
                }
            }
            else if (completionDataProvider != null)
            {
                ScriptEditor.Model.ScriptDocument doc = ScriptEditor.Model.Documents.DocumentFromId(DocumentId);
                string path = "none";
                if (doc != null && !string.IsNullOrEmpty(doc.FullPath))
                {
                    path = doc.FullPath;
                }

                m_code_complete = ICSharpCode.TextEditor.Gui.CompletionWindow.CodeCompletionWindow.ShowCompletionWindow(
                    m_mainform,             // The parent window for the completion window
                    this,                   // The text editor to show the window for
                    path,                   // Filename - will be passed back to the provider
                    completionDataProvider, // Provider to get the list of possible completions
                    key                     // Key pressed - will be passed to the provider
                    );
                if (m_code_complete != null)
                {
                    // ShowCompletionWindow can return null when the provider returns an empty list
                    m_code_complete.Closed += new EventHandler(CloseCodeCompletionWindow);
                }
            }
            return(false);
        }