Inheritance: ILineSplitter
        void EnsureBuild()
        {
            if (this.lines != null)
            {
                return;
            }
            var text  = src.Text;
            var nodes = new List <LineSegment> ();

            var delimiterType = UnicodeNewline.Unknown;
            int offset = 0, maxLength = 0, lineNumber = 0;

            while (true)
            {
                var delimiter = LineSplitter.NextDelimiter(text, offset);
                if (delimiter.IsInvalid)
                {
                    break;
                }
                int delimiterEndOffset = delimiter.Offset + delimiter.Length;
                var length             = delimiterEndOffset - offset;
                var newLine            = new LineSegment(this, lineNumber++, offset, length, delimiter.UnicodeNewline);
                nodes.Add(newLine);
                if (length > maxLength)
                {
                    maxLength = length;
                }
                if (offset > 0)
                {
                    LineEndingMismatch |= delimiterType != delimiter.UnicodeNewline;
                }
                else
                {
                    delimiterType = delimiter.UnicodeNewline;
                }
                offset = delimiterEndOffset;
            }
            var lastLine = new LineSegment(this, lineNumber++, offset, text.Length - offset, UnicodeNewline.Unknown);

            nodes.Add(lastLine);
            this.lines = nodes.ToArray();
        }
Beispiel #2
0
        public void Initalize(string text)
        {
            delimiters = new List <LineSplitter.Delimiter> ();

            int offset = 0;

            while (true)
            {
                var delimiter = LineSplitter.NextDelimiter(text, offset);
                if (delimiter.IsInvalid)
                {
                    break;
                }

                delimiters.Add(delimiter);

                offset = delimiter.EndOffset;
            }


            textLength = text.Length;
        }
Beispiel #3
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, (ClipboardReceivedFunc) delegate(Clipboard clp, SelectionData selectionData) {
                    if (selectionData.Length > 0)
                    {
                        byte[] selBytes = selectionData.Data;
                        var upperBound  = System.Math.Max(0, System.Math.Min(selBytes [1], selBytes.Length - 2));
                        byte[] copyData = new byte[upperBound];
                        Array.Copy(selBytes, 2, copyData, 0, copyData.Length);
                        var rawTextOffset = 1 + 1 + copyData.Length;
                        string text       = Encoding.UTF8.GetString(selBytes, rawTextOffset, selBytes.Length - rawTextOffset);
                        bool pasteBlock   = (selBytes [0] & 1) == 1;
                        bool pasteLine    = (selBytes [0] & 2) == 2;
                        if (pasteBlock)
                        {
                            using (var undo = data.OpenUndoGroup()) {
                                var version = data.Document.Version;
                                if (!preserveSelection)
                                {
                                    data.DeleteSelectedText(!data.IsSomethingSelected || data.MainSelection.SelectionMode != MonoDevelop.Ide.Editor.SelectionMode.Block);
                                }
                                int startLine = data.Caret.Line;
                                data.EnsureCaretIsNotVirtual();
                                insertionOffset = version.MoveOffsetTo(data.Document.Version, insertionOffset);

                                data.Caret.PreserveSelection = true;
                                var lines  = new List <string> ();
                                int offset = 0;
                                while (true)
                                {
                                    var delimiter = LineSplitter.NextDelimiter(text, offset);
                                    if (delimiter.IsInvalid)
                                    {
                                        break;
                                    }

                                    int delimiterEndOffset = delimiter.EndOffset;
                                    lines.Add(text.Substring(offset, delimiter.Offset - offset));
                                    offset = delimiterEndOffset;
                                }
                                if (offset < text.Length)
                                {
                                    lines.Add(text.Substring(offset, text.Length - offset));
                                }

                                int lineNr = data.Document.OffsetToLineNumber(insertionOffset);
                                int col    = insertionOffset - data.Document.GetLine(lineNr).Offset;
                                int visCol = data.Document.GetLine(lineNr).GetVisualColumn(data, col);
                                DocumentLine curLine;
                                int lineCol = col;
                                result      = 0;
                                for (int i = 0; i < lines.Count; i++)
                                {
                                    while (data.Document.LineCount <= lineNr + i)
                                    {
                                        data.Insert((int)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.Length + 1 < lineCol)
                                        {
                                            result += lineCol - curLine.Length;
                                            data.Insert(curLine.Offset + curLine.Length, new string (' ', lineCol - curLine.Length));
                                        }
                                        data.Insert(curLine.Offset + lineCol, lines [i]);
                                        result += lines [i].Length;
                                    }
                                    if (!preserveState)
                                    {
                                        data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length;
                                    }
                                }
                                if (!preserveState)
                                {
                                    data.ClearSelection();
                                }
                                data.FixVirtualIndentation(startLine);
                                data.Caret.PreserveSelection = false;
                            }
                        }
                        else if (pasteLine)
                        {
                            using (var undo = data.OpenUndoGroup()) {
                                if (!preserveSelection)
                                {
                                    data.DeleteSelectedText(!data.IsSomethingSelected || data.MainSelection.SelectionMode != MonoDevelop.Ide.Editor.SelectionMode.Block);
                                }
                                data.EnsureCaretIsNotVirtual();

                                data.Caret.PreserveSelection = true;
                                result = text.Length;
                                DocumentLine curLine = data.Document.GetLine(data.Caret.Line);
                                result = PastePlainText(data, curLine.Offset, text + data.EolMarker, preserveSelection, copyData);
                                if (!preserveState)
                                {
                                    data.ClearSelection();
                                }
                                data.Caret.PreserveSelection = false;
                                data.FixVirtualIndentation(curLine.LineNumber);
                            }
                        }
                        else
                        {
                            result = PastePlainText(data, insertionOffset, text, preserveSelection, copyData);
                        }
                    }
                });
                // we got MD_ATOM text - no need to request text. (otherwise buffer may get copied twice).
                return(result);
            }

            if (result < 0 && clipboard.WaitIsTextAvailable())
            {
                clipboard.RequestText(delegate(Clipboard clp, string text) {
                    if (string.IsNullOrEmpty(text))
                    {
                        return;
                    }
                    result = PastePlainText(data, insertionOffset, text, preserveSelection);
                });
            }

            return(result);
        }
 public void Initalize(string text)
 {
     delimiters = new List <LineSplitter.Delimiter> (LineSplitter.FindDelimiter(text));
     textLength = text.Length;
 }
Beispiel #5
0
        static int PasteFrom(TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState)
        {
            int result = -1;

            if (!data.CanEdit(data.Document.OffsetToLineNumber(insertionOffset)))
            {
                return(result);
            }
            if (Clipboard.ContainsData <byte[]>())
            {
                byte[] selBytes   = Clipboard.GetData <byte[]>();
                var    upperBound = System.Math.Max(0, System.Math.Min(selBytes[1], selBytes.Length - 2));
                byte[] copyData   = new byte[upperBound];
                Array.Copy(selBytes, 2, copyData, 0, copyData.Length);
                var    rawTextOffset = 1 + 1 + copyData.Length;
                string text          = Encoding.UTF8.GetString(selBytes, rawTextOffset, selBytes.Length - rawTextOffset);
                bool   pasteBlock    = (selBytes[0] & 1) == 1;
                bool   pasteLine     = (selBytes[0] & 2) == 2;
                if (pasteBlock)
                {
                    using (var undo = data.OpenUndoGroup())
                    {
                        var version = data.Document.Version;
                        if (!preserveSelection)
                        {
                            data.DeleteSelectedText(!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block);
                        }
                        int startLine = data.Caret.Line;
                        data.EnsureCaretIsNotVirtual();
                        insertionOffset = version.MoveOffsetTo(data.Document.Version, insertionOffset);

                        data.Caret.PreserveSelection = true;
                        var lines  = new List <string>();
                        int offset = 0;
                        while (true)
                        {
                            var delimiter = LineSplitter.NextDelimiter(text, offset);
                            if (delimiter.IsInvalid)
                            {
                                break;
                            }

                            int delimiterEndOffset = delimiter.EndOffset;
                            lines.Add(text.Substring(offset, delimiter.Offset - offset));
                            offset = delimiterEndOffset;
                        }
                        if (offset < text.Length)
                        {
                            lines.Add(text.Substring(offset, text.Length - offset));
                        }

                        int          lineNr = data.Document.OffsetToLineNumber(insertionOffset);
                        int          col    = insertionOffset - data.Document.GetLine(lineNr).Offset;
                        int          visCol = data.Document.GetLine(lineNr).GetVisualColumn(data, col);
                        DocumentLine curLine;
                        int          lineCol = col;
                        result = 0;
                        for (int i = 0; i < lines.Count; i++)
                        {
                            while (data.Document.LineCount <= lineNr + i)
                            {
                                data.Insert(data.Document.TextLength, Environment.NewLine);
                                result += Environment.NewLine.Length;
                            }
                            curLine = data.Document.GetLine(lineNr + i);
                            if (lines[i].Length > 0)
                            {
                                lineCol = curLine.GetLogicalColumn(data, visCol);
                                if (curLine.Length + 1 < lineCol)
                                {
                                    result += lineCol - curLine.Length;
                                    data.Insert(curLine.Offset + curLine.Length, new string(' ', lineCol - curLine.Length));
                                }
                                data.Insert(curLine.Offset + lineCol, lines[i]);
                                result += lines[i].Length;
                            }
                            if (!preserveState)
                            {
                                data.Caret.Offset = curLine.Offset + lineCol + lines[i].Length;
                            }
                        }
                        if (!preserveState)
                        {
                            data.ClearSelection();
                        }
                        data.FixVirtualIndentation(startLine);
                        data.Caret.PreserveSelection = false;
                    }
                }
                else if (pasteLine)
                {
                    using (var undo = data.OpenUndoGroup())
                    {
                        if (!preserveSelection)
                        {
                            data.DeleteSelectedText(!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block);
                        }
                        data.EnsureCaretIsNotVirtual();

                        data.Caret.PreserveSelection = true;
                        result = text.Length;
                        DocumentLine curLine = data.Document.GetLine(data.Caret.Line);

                        result = PastePlainText(data, curLine.Offset, text + data.EolMarker, preserveSelection, copyData);
                        if (!preserveState)
                        {
                            data.ClearSelection();
                        }
                        data.Caret.PreserveSelection = false;
                        data.FixVirtualIndentation(curLine.LineNumber);
                    }
                }
                else
                {
                    result = PastePlainText(data, insertionOffset, text, preserveSelection, copyData);
                }
            }
            else if (Clipboard.ContainsData(TransferDataType.Text))
            {
                var text = Clipboard.GetText();
                result = PastePlainText(data, insertionOffset, text, preserveState);
            }



            return(result);
        }