ICSharpCode.TextEditor.TextArea.InsertChar C# (CSharp) Method

InsertChar() public method

Inserts a single character at the caret position
public InsertChar ( char ch ) : void
ch char
return void
		public void InsertChar(char ch)
		{
			bool updating = motherTextEditorControl.IsInUpdate;
			if (!updating) {
				BeginUpdate();
			}
			
			// filter out forgein whitespace chars and replace them with standard space (ASCII 32)
			if (Char.IsWhiteSpace(ch) && ch != '\t' && ch != '\n') {
				ch = ' ';
			}
			
			Document.UndoStack.StartUndoGroup();
			if (Document.TextEditorProperties.DocumentSelectionMode == DocumentSelectionMode.Normal &&
			    SelectionManager.SelectionCollection.Count > 0) {
				Caret.Position = SelectionManager.SelectionCollection[0].StartPosition;
				SelectionManager.RemoveSelectedText();
			}
			LineSegment caretLine = Document.GetLineSegment(Caret.Line);
			int offset = Caret.Offset;
			// use desired column for generated whitespaces
			int dc = Caret.Column;
			if (caretLine.Length < dc && ch != '\n') {
				Document.Insert(offset, GenerateWhitespaceString(dc - caretLine.Length) + ch);
			} else {
				Document.Insert(offset, ch.ToString());
			}
			Document.UndoStack.EndUndoGroup();
			++Caret.Column;
			
			if (!updating) {
				EndUpdate();
				UpdateLineToEnd(Caret.Line, Caret.Column);
			}
			
			// I prefer to set NOT the standard column, if you type something
//			++Caret.DesiredColumn;
		}
		

Usage Example

示例#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);
            }
        }