InsertChar() public method

Inserts a single character at the caret position
public InsertChar ( char ch ) : void
ch char
return void
示例#1
0
        /// <summary>
        /// Execute the Inser Closing Element action
        /// </summary>
        /// <param name="textArea">The text area in which to perform the
        /// action</param>
        public override void Execute(TextArea textArea)
        {
            IDocument doc = textArea.Document;
            int endTag = -1, open = -1, close = -1, offset = textArea.Caret.Offset;
            char c;

            if(offset > 0 && doc.GetCharAt(offset - 1) == '>')
                offset--;

            // Find end of the element
            for(int i = offset; i < doc.TextLength; i++)
            {
                c = doc.GetCharAt(i);

                if(c == '<' || c== '>')
                {
                    if(c == '>')
                    {
                        endTag = close = i;

                        // Find start of tag
                        for(int j = close - 1; j >= 0; j--)
                        {
                            c = doc.GetCharAt(j);

                            if(c == '<' || c == '>')
                            {
                                if(c == '<')
                                    open = j;

                                break;
                            }

                            // On whitespace, assume we just moved past
                            // an attribute.
                            if(Char.IsWhiteSpace(c))
                                endTag = j;
                        }
                    }

                    break;
                }
            }

            // Execute the default action if we couldn't find an element
            // or it's a comment.
            if(open < 0 || close < 0 || doc.GetText(open + 1, 3) == "!--")
            {
                if(defaultAction != null)
                    defaultAction.Execute(textArea);
                else
                    textArea.InsertChar('\t');
            }
            else
            {
                open++;
                string tag = doc.GetText(open, endTag - open);

                close++;
                doc.Insert(close, "</" + tag + ">");
                textArea.Caret.Position = doc.OffsetToPosition(close);
            }
        }