コード例 #1
0
ファイル: EditMode.cs プロジェクト: yayanyang/monodevelop
        protected void InsertCharacter(uint unicodeKey)
        {
            if (!textEditorData.CanEdit(Data.Caret.Line))
            {
                return;
            }

            HideMouseCursor();
            using (var undo = Document.OpenUndoGroup()) {
                textEditorData.DeleteSelectedText(textEditorData.IsSomethingSelected ? textEditorData.MainSelection.SelectionMode != SelectionMode.Block : true);

                char ch = (char)unicodeKey;
                if (!char.IsControl(ch) && textEditorData.CanEdit(Caret.Line))
                {
                    LineSegment line = Document.GetLine(Caret.Line);
                    if (Caret.IsInInsertMode || Caret.Column >= line.EditableLength + 1)
                    {
                        string text = Caret.Column > line.EditableLength + 1 ? textEditorData.GetVirtualSpaces(Caret.Line, Caret.Column) + ch.ToString() : ch.ToString();
                        if (textEditorData.IsSomethingSelected && textEditorData.MainSelection.SelectionMode == SelectionMode.Block)
                        {
                            int length = 0;
                            var visualInsertLocation = editor.LogicalToVisualLocation(Caret.Location);
                            for (int lineNumber = textEditorData.MainSelection.MinLine; lineNumber <= textEditorData.MainSelection.MaxLine; lineNumber++)
                            {
                                LineSegment lineSegment  = textEditorData.GetLine(lineNumber);
                                int         insertOffset = lineSegment.GetLogicalColumn(textEditorData, visualInsertLocation.Column) - 1;
                                string      textToInsert;
                                if (lineSegment.EditableLength < insertOffset)
                                {
                                    int visualLastColumn = lineSegment.GetVisualColumn(textEditorData, lineSegment.EditableLength + 1);
                                    int charsToInsert    = visualInsertLocation.Column - visualLastColumn;
                                    int spaceCount       = charsToInsert % editor.Options.TabSize;
                                    textToInsert = new string ('\t', (charsToInsert - spaceCount) / editor.Options.TabSize) + new string (' ', spaceCount) + text;
                                    insertOffset = lineSegment.EditableLength;
                                }
                                else
                                {
                                    textToInsert = text;
                                }
                                length = textEditorData.Insert(lineSegment.Offset + insertOffset, textToInsert);
                            }
                            Caret.PreserveSelection = true;
                            Caret.Column           += length - 1;

                            textEditorData.MainSelection.Lead   = new DocumentLocation(textEditorData.MainSelection.Lead.Line, Caret.Column + 1);
                            textEditorData.MainSelection.Anchor = new DocumentLocation(textEditorData.MainSelection.Anchor.Line, Caret.Column + 1);
                            Document.CommitMultipleLineUpdate(textEditorData.MainSelection.MinLine, textEditorData.MainSelection.MaxLine);
                        }
                        else
                        {
                            int length = textEditorData.Insert(Caret.Offset, text);
                            Caret.Column += length - 1;
                        }
                    }
                    else
                    {
                        int length = textEditorData.Replace(Caret.Offset, 1, ch.ToString());
                        if (length > 1)
                        {
                            Caret.Offset += length - 1;
                        }
                    }
                    // That causes unnecessary redraws:
                    //				bool autoScroll = Caret.AutoScrollToCaret;
                    Caret.Column++;
                    if (Caret.PreserveSelection)
                    {
                        Caret.PreserveSelection = false;
                    }
                    //				Caret.AutoScrollToCaret = autoScroll;
                    //				if (autoScroll)
                    //					Editor.ScrollToCaret ();
                    //				Document.RequestUpdate (new LineUpdate (Caret.Line));
                    //				Document.CommitDocumentUpdate ();
                }
            }
            Document.OptimizeTypedUndo();
        }