Example #1
0
        public void InsertRange(int start, IList <string> lines)
        {
            // If the set of lines is empty we can exit now
            if ((null == lines) || (lines.Count == 0))
            {
                hasMerged = true;
                return;
            }
            if (start < 0)
            {
                throw new ArgumentOutOfRangeException("start");
            }
            int insertLine  = start;
            int insertIndex = 0;
            // Verify that the insertion point is inside the buffer.
            int totalLines = LineCount;

            if (insertLine > totalLines)
            {
                insertLine = totalLines;
            }
            // Create the text to add to the buffer.
            StringBuilder builder = new StringBuilder();

            if ((insertLine == totalLines) && (totalLines > 0))
            {
                insertLine = totalLines - 1;
                ErrorHandler.ThrowOnFailure(textBuffer.GetLengthOfLine(insertLine, out insertIndex));
                builder.AppendLine();
            }
            foreach (string line in lines)
            {
                builder.AppendLine(line);
            }
            // Lock the buffer before changing its content.
            ErrorHandler.ThrowOnFailure(textBuffer.LockBuffer());
            try {
                // Get the text to insert and pin it so that we can pass it as pointer
                // to the buffer's functions.
                string   text   = builder.ToString();
                GCHandle handle = GCHandle.Alloc(text, GCHandleType.Pinned);
                try {
                    TextSpan[] span = new TextSpan[1];
                    ErrorHandler.ThrowOnFailure(textBuffer.ReplaceLines(insertLine, insertIndex, insertLine, insertIndex, handle.AddrOfPinnedObject(), text.Length, span));
                    hasMerged = true;
                } finally {
                    // Free the memory.
                    handle.Free();
                }
            } finally {
                // Make sure that the buffer is unlocked also in case of exception.
                textBuffer.UnlockBuffer();
            }
        }
        /// <summary>
        /// Flushes the pending data.
        /// </summary>
        public override void Flush()
        {
            // If there is no data in the buffer, then there is nothing to do.
            if (0 == usedBuffer)
            {
                // Make sure that the read-only region is correct and exit.
                ExtendReadOnlyRegion();
                return;
            }

            string text = null;

            // We have to use a StreamReader in order to work around problems with the
            // encoding of the data sent in, but in order to build the reader we need
            // a memory stream to read the data in the buffer.
            using (MemoryStream s = new MemoryStream(byteBuffer, 0, usedBuffer))
            {
                // Now we can build the reader from the memory stream.
                using (StreamReader reader = new StreamReader(s))
                {
                    // At the end we can get the text.
                    text = reader.ReadToEnd();
                }
            }
            // Now the buffer is empty.
            usedBuffer = 0;

            // The text is always added at the end of the buffer.
            int lastLine;
            int lastColumn;

            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(
                textLines.GetLastLineIndex(out lastLine, out lastColumn));

            // Lock the buffer before changing its content.
            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(
                textLines.LockBuffer());
            try
            {
                GCHandle handle = GCHandle.Alloc(text, GCHandleType.Pinned);
                try
                {
                    TextSpan[] span = new TextSpan[1];
                    Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(
                        textLines.ReplaceLines(lastLine, lastColumn, lastLine, lastColumn, handle.AddrOfPinnedObject(), text.Length, span));
                }
                finally
                {
                    handle.Free();
                }
                // Extend the read-only region of the buffer to include this text.
                ExtendReadOnlyRegion();
            }
            finally
            {
                Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(
                    textLines.UnlockBuffer());
            }
        }
Example #3
0
 public int LockBuffer()
 {
     return(_textBuffer.LockBuffer());
 }