Beispiel #1
0
        void SetColumn()
        {
            LineSegment curLine = this.document.GetLine(this.Line);

            if (curLine == null)
            {
                return;
            }
            this.location.Column = curLine.GetLogicalColumn(editor, this.DesiredColumn);
            if (curLine.GetVisualColumn(editor, this.location.Column) < this.DesiredColumn)
            {
                this.location.Column = editor.GetNextVirtualColumn(Line, this.location.Column);
            }
            else
            {
                if (this.Column > curLine.EditableLength)
                {
                    this.location.Column = System.Math.Min(curLine.EditableLength, System.Math.Max(0, this.Column));
                    if (AllowCaretBehindLineEnd)
                    {
                        this.location.Column = editor.GetNextVirtualColumn(Line, this.location.Column);
                    }
                }
            }
        }
Beispiel #2
0
        void SetColumn()
        {
            LineSegment curLine = TextEditorData.Document.GetLine(this.Line);

            if (curLine == null)
            {
                return;
            }
            this.location.Column = curLine.GetLogicalColumn(TextEditorData, this.DesiredColumn);
            if (curLine.GetVisualColumn(TextEditorData, this.location.Column) < this.DesiredColumn)
            {
                this.location.Column = TextEditorData.GetNextVirtualColumn(Line, this.location.Column);
            }
            else
            {
                if (this.Column > curLine.EditableLength + 1)
                {
                    this.location.Column = System.Math.Min(curLine.EditableLength + 1, System.Math.Max(DocumentLocation.MinColumn, this.Column));
                    if (AllowCaretBehindLineEnd)
                    {
                        this.location.Column = TextEditorData.GetNextVirtualColumn(Line, this.location.Column);
                    }
                }
            }
        }
        public DocumentLocation LogicalToVisualLocation(DocumentLocation location)
        {
            int         line        = LogicalToVisualLine(location.Line);
            LineSegment lineSegment = this.GetLine(location.Line);
            int         column      = lineSegment != null?lineSegment.GetVisualColumn(this, location.Column) : location.Column;

            return(new DocumentLocation(line, column));
        }
Beispiel #4
0
        void SetDesiredColumn()
        {
            LineSegment curLine = this.document.GetLine(this.Line);

            if (curLine == null)
            {
                return;
            }

            if (!AllowCaretBehindLineEnd)
            {
                this.Column = System.Math.Min(curLine.EditableLength, System.Math.Max(0, this.Column));
            }
            this.DesiredColumn = curLine.GetVisualColumn(editor, this.Column);
        }
Beispiel #5
0
        void SetDesiredColumn()
        {
            LineSegment curLine = TextEditorData.Document.GetLine(this.Line);

            if (curLine == null)
            {
                return;
            }

            if (!AllowCaretBehindLineEnd)
            {
                this.Column = System.Math.Min(curLine.EditableLength + 1, System.Math.Max(DocumentLocation.MinColumn, this.Column));
            }
            this.DesiredColumn = curLine.GetVisualColumn(TextEditorData, this.Column);
        }
Beispiel #6
0
        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();
        }
Beispiel #7
0
        static int PasteFrom(Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState)
        {
            int result = -1;

            if (!data.CanEdit(data.Document.OffsetToLineNumber(insertionOffset)))
            {
                return(result);
            }
            if (clipboard.WaitIsTargetAvailable(CopyOperation.MD_ATOM))
            {
                clipboard.RequestContents(CopyOperation.MD_ATOM, delegate(Clipboard clp, SelectionData selectionData) {
                    if (selectionData.Length > 0)
                    {
                        byte[] selBytes = selectionData.Data;

                        string text     = System.Text.Encoding.UTF8.GetString(selBytes, 1, selBytes.Length - 1);
                        bool pasteBlock = (selBytes [0] & 1) == 1;
                        bool pasteLine  = (selBytes [0] & 2) == 2;
                        if (!pasteBlock && !pasteLine)
                        {
                            return;
                        }

                        data.Document.BeginAtomicUndo();
                        if (preserveSelection && data.IsSomethingSelected)
                        {
                            data.DeleteSelectedText();
                        }

                        data.Caret.PreserveSelection = true;
                        if (pasteBlock)
                        {
                            string[] lines = text.Split('\r');
                            int lineNr     = data.Document.OffsetToLineNumber(insertionOffset);
                            int col        = insertionOffset - data.Document.GetLine(lineNr).Offset;
                            int visCol     = data.Document.GetLine(lineNr).GetVisualColumn(data, col);
                            LineSegment curLine;
                            int lineCol = col;
                            result      = 0;
                            for (int i = 0; i < lines.Length; i++)
                            {
                                while (data.Document.LineCount <= lineNr + i)
                                {
                                    data.Insert(data.Document.Length, Environment.NewLine);
                                    result += Environment.NewLine.Length;
                                }
                                curLine = data.Document.GetLine(lineNr + i);
                                if (lines [i].Length > 0)
                                {
                                    lineCol = curLine.GetLogicalColumn(data, visCol);
                                    if (curLine.EditableLength + 1 < lineCol)
                                    {
                                        result += lineCol - curLine.EditableLength;
                                        data.Insert(curLine.Offset + curLine.EditableLength, new string (' ', lineCol - curLine.EditableLength));
                                    }
                                    data.Insert(curLine.Offset + lineCol, lines [i]);
                                    result += lines [i].Length;
                                }
                                if (!preserveState)
                                {
                                    data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length;
                                }
                            }
                        }
                        else if (pasteLine)
                        {
                            result = text.Length;
                            LineSegment curLine = data.Document.GetLine(data.Caret.Line);
                            data.Insert(curLine.Offset, text + data.EolMarker);
                            if (!preserveState)
                            {
                                data.Caret.Offset += text.Length + data.EolMarker.Length;
                            }
                        }

                        /*				data.MainSelection = new Selection (data.Document.OffsetToLocation (insertionOffset),
                         *                                  data.Caret.Location,
                         *                                  lines.Length > 1 ? SelectionMode.Block : SelectionMode.Normal);*/
                        if (!preserveState)
                        {
                            data.ClearSelection();
                        }
                        data.Caret.PreserveSelection = false;
                        data.Document.EndAtomicUndo();
                    }
                });
            }

            if (result < 0 && clipboard.WaitIsTextAvailable())
            {
                clipboard.RequestText(delegate(Clipboard clp, string text) {
                    if (string.IsNullOrEmpty(text))
                    {
                        return;
                    }
                    data.Document.BeginAtomicUndo();
                    int caretPos = data.Caret.Offset;
                    if (data.IsSomethingSelected && data.MainSelection.SelectionMode == SelectionMode.Block)
                    {
                        data.Caret.PreserveSelection = true;
                        data.DeleteSelectedText(false);
                        int textLength           = 0;
                        int minLine              = data.MainSelection.MinLine;
                        int maxLine              = data.MainSelection.MaxLine;
                        var visualInsertLocation = data.LogicalToVisualLocation(data.Caret.Location);
                        for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++)
                        {
                            LineSegment lineSegment = data.GetLine(lineNumber);
                            int insertOffset        = lineSegment.GetLogicalColumn(data, visualInsertLocation.Column) - 1;
                            if (lineSegment.EditableLength < insertOffset)
                            {
                                int visualLastColumn = lineSegment.GetVisualColumn(data, lineSegment.EditableLength + 1);
                                int charsToInsert    = visualInsertLocation.Column - visualLastColumn;
                                int spaceCount       = charsToInsert % data.Options.TabSize;
                                string textToInsert  = new string ('\t', (charsToInsert - spaceCount) / data.Options.TabSize) + new string (' ', spaceCount) + text;
                                insertOffset         = lineSegment.EditableLength;
                                data.Insert(lineSegment.Offset + insertOffset, textToInsert);
                                data.PasteText(lineSegment.Offset + insertOffset, textToInsert);
                            }
                            else
                            {
                                textLength = data.Insert(lineSegment.Offset + insertOffset, text);
                                data.PasteText(lineSegment.Offset + insertOffset, text);
                            }
                        }

                        data.Caret.Offset           += textLength;
                        data.MainSelection.Anchor    = new DocumentLocation(System.Math.Max(DocumentLocation.MinLine, data.Caret.Line == minLine ? maxLine : minLine), System.Math.Max(DocumentLocation.MinColumn, data.Caret.Column - textLength));
                        data.MainSelection.Lead      = new DocumentLocation(data.Caret.Line, data.Caret.Column);
                        data.Caret.PreserveSelection = false;
                        data.Document.CommitMultipleLineUpdate(data.MainSelection.MinLine, data.MainSelection.MaxLine);
                    }
                    else
                    {
                        ISegment selection = data.SelectionRange;
                        if (preserveSelection && data.IsSomethingSelected)
                        {
                            data.DeleteSelectedText();
                        }
                        data.Caret.PreserveSelection = true;
                        //int oldLine = data.Caret.Line;
                        int textLength = data.Insert(insertionOffset, text);
                        result         = textLength;

                        if (data.IsSomethingSelected && data.SelectionRange.Offset >= insertionOffset)
                        {
                            data.SelectionRange.Offset += textLength;
                        }
                        if (data.IsSomethingSelected && data.MainSelection.GetAnchorOffset(data) >= insertionOffset)
                        {
                            data.MainSelection.Anchor = data.Document.OffsetToLocation(data.MainSelection.GetAnchorOffset(data) + textLength);
                        }

                        data.Caret.PreserveSelection = false;
                        if (!preserveState)
                        {
                            data.Caret.Offset += textLength;
                        }
                        else
                        {
                            if (caretPos >= insertionOffset)
                            {
                                data.Caret.Offset += textLength;
                            }
                            if (selection != null)
                            {
                                int offset = selection.Offset;
                                if (offset >= insertionOffset)
                                {
                                    offset += textLength;
                                }
                                data.SelectionRange = new Segment(offset, selection.Length);
                            }
                        }
                        data.PasteText(insertionOffset, text);
                    }
                    data.Document.EndAtomicUndo();
                });
            }

            return(result);
        }