Exemple #1
0
 public ClosedSnapshotSourceText(
     ITextBufferCloneService?textBufferCloneService,
     ITextImage textImage,
     Encoding?encoding
     ) : base(textBufferCloneService, textImage, encoding, container: null)
 {
 }
Exemple #2
0
            private static bool AreSameReiteratedVersion(ITextImage oldImage, ITextImage newImage)
            {
                var oldSnapshot = TryFindEditorSnapshot(oldImage);
                var newSnapshot = TryFindEditorSnapshot(newImage);

                return(oldSnapshot != null && newSnapshot != null && oldSnapshot.Version.ReiteratedVersionNumber == newSnapshot.Version.ReiteratedVersionNumber);
            }
        public TextImageLine(ITextImage image, int lineNumber, Span extent, int lineBreakLength)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            if ((lineNumber < 0) || (lineNumber >= image.LineCount))
            {
                throw new ArgumentOutOfRangeException(nameof(lineNumber));
            }

            if (extent.End > image.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(extent));
            }

            if ((lineBreakLength < 0) || (lineBreakLength > 2) || (extent.End + lineBreakLength > image.Length))
            {
                throw new ArgumentOutOfRangeException(nameof(lineBreakLength));
            }

            this.Image           = image;
            this.LineNumber      = lineNumber;
            this.Extent          = extent;
            this.LineBreakLength = lineBreakLength;
        }
Exemple #4
0
 public ITextBuffer CreateTextBuffer(ITextImage image, IContentType contentType)
 {
     if (image == null)
     {
         throw new ArgumentNullException(nameof(image));
     }
     return(CreateTextBuffer(image.GetText(), contentType));
 }
            public SnapshotSourceText(ITextImage textImage, Encoding encodingOpt, TextBufferContainer containerOpt)
            {
                Contract.ThrowIfNull(textImage);

                this.TextImage = textImage;
                _encodingOpt   = encodingOpt;
                _containerOpt  = containerOpt;
            }
Exemple #6
0
            private SnapshotSourceText(ITextSnapshot editorSnapshot, TextBufferContainer container)
            {
                Contract.ThrowIfNull(editorSnapshot);

                this.TextImage = RecordReverseMapAndGetImage(editorSnapshot);
                _encodingOpt   = editorSnapshot.TextBuffer.GetEncodingOrUTF8();
                _containerOpt  = container;
            }
            private SnapshotSourceText(ITextSnapshot editorSnapshot, Encoding encodingOpt)
            {
                Contract.ThrowIfNull(editorSnapshot);

                this.TextImage     = TextBufferMapper.RecordTextSnapshotAndGetImage(editorSnapshot);
                _containerOpt      = TextBufferContainer.From(editorSnapshot.TextBuffer);
                _reiteratedVersion = editorSnapshot.Version.ReiteratedVersionNumber;
                _encodingOpt       = encodingOpt;
            }
Exemple #8
0
            public SnapshotSourceText(ITextBufferCloneService?textBufferCloneService, ITextImage textImage, Encoding?encoding, TextBufferContainer?container)
            {
                Contract.ThrowIfNull(textImage);

                _textBufferCloneService = textBufferCloneService;
                this.TextImage          = textImage;
                _encoding  = encoding;
                _container = container;
            }
            public SnapshotSourceText(ITextBufferCloneService?textBufferCloneServiceOpt, ITextImage textImage, Encoding?encodingOpt, TextBufferContainer?containerOpt)
            {
                Contract.ThrowIfNull(textImage);

                _textBufferCloneServiceOpt = textBufferCloneServiceOpt;
                this.TextImage             = textImage;
                _encodingOpt  = encodingOpt;
                _containerOpt = containerOpt;
            }
Exemple #10
0
        protected BaseSnapshot(ITextVersion2 version, StringRebuilder content)
        {
            this.version        = version;
            this.Content        = content;
            this.cachingContent = CachingTextImage.Create(this.Content, version.ImageVersion);

            // we must extract the content type here, because the content type of the text buffer may change later.
            this.contentType = version.TextBuffer.ContentType;
        }
Exemple #11
0
            private static ITextSnapshot TryFindEditorSnapshot(ITextImage textImage)
            {
                if (!s_textImageToEditorSnapshotMap.TryGetValue(textImage, out var weakReference) ||
                    !weakReference.TryGetTarget(out var editorSnapshot))
                {
                    return(null);
                }

                return(editorSnapshot);
            }
Exemple #12
0
            public static ITextSnapshot TryFindEditorSnapshot(ITextImage textImage)
            {
                Contract.ThrowIfNull(textImage);
                if (!s_roslynToEditorSnapshotMap.TryGetValue(textImage, out var weakReference) ||
                    !weakReference.TryGetTarget(out var editorSnapshot))
                {
                    return(null);
                }

                return(editorSnapshot);
            }
Exemple #13
0
            private static IReadOnlyList <TextChangeRange> GetChangeRanges(
                ITextImage snapshot1,
                ITextImage snapshot2,
                bool forward
                )
            {
                var oldSnapshot = forward ? snapshot1 : snapshot2;
                var newSnapshot = forward ? snapshot2 : snapshot1;

                INormalizedTextChangeCollection?changes = null;

                for (
                    var oldVersion = oldSnapshot.Version;
                    oldVersion != newSnapshot.Version;
                    oldVersion = oldVersion.Next
                    )
                {
                    if (oldVersion.Changes.Count != 0)
                    {
                        if (changes != null)
                        {
                            // Oops - more than one "textual" change between these snapshots, bail and try to find smallest changes span
                            Logger.Log(
                                FunctionId.Workspace_SourceText_GetChangeRanges,
                                s_textLog,
                                snapshot1.Version.VersionNumber,
                                snapshot2.Version.VersionNumber
                                );

                            return(new[]
                            {
                                GetChangeRanges(oldSnapshot.Version, newSnapshot.Version, forward)
                            });
                        }
                        else
                        {
                            changes = oldVersion.Changes;
                        }
                    }
                }

                if (changes == null)
                {
                    return(ImmutableArray.Create <TextChangeRange>());
                }
                else
                {
                    return(ImmutableArray.CreateRange(
                               changes.Select(
                                   forward ? s_forwardTextChangeRange : s_backwardTextChangeRange
                                   )
                               ));
                }
            }
Exemple #14
0
        public ITextBuffer CreateTextBuffer(ITextImage image, IContentType contentType)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }
            if (contentType == null)
            {
                throw new ArgumentNullException(nameof(contentType));
            }

            StringRebuilder content = StringRebuilder.Create(image);

            return(Make(contentType, content, false));
        }
Exemple #15
0
 public ChangedSourceText(
     ITextBufferCloneService?textBufferCloneService,
     SnapshotSourceText baseText,
     ITextImage baseSnapshot,
     ITextImage currentSnapshot
     )
     : base(
         textBufferCloneService,
         currentSnapshot,
         baseText.Encoding,
         container: null
         )
 {
     _baseText     = baseText;
     _baseSnapshot = baseSnapshot;
 }
        public static StringRebuilder Create(ITextImage image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            var cti = image as CachingTextImage;

            if (cti != null)
            {
                return(cti.Builder);
            }

            // This shouldn't happen but as a fallback, create a new string rebuilder from the text of the provided image.
            return(StringRebuilder.Create(image.GetText(0, image.Length)));
        }
Exemple #17
0
 private IReadOnlyList <TextChangeRange> GetChangeRanges(ITextImage oldImage, int oldTextLength, ITextImage newImage)
 {
     if (oldImage == null ||
         newImage == null ||
         oldImage.Version.Identifier != newImage.Version.Identifier)
     {
         // Claim its all changed
         Logger.Log(FunctionId.Workspace_SourceText_GetChangeRanges, "Invalid Snapshots");
         return(ImmutableArray.Create(new TextChangeRange(new TextSpan(0, oldTextLength), this.Length)));
     }
     else if (AreSameReiteratedVersion(oldImage, newImage))
     {
         // content of two snapshot must be same even if versions are different
         return(TextChangeRange.NoChanges);
     }
     else
     {
         return(GetChangeRanges(oldImage, newImage, forward: oldImage.Version.VersionNumber <= newImage.Version.VersionNumber));
     }
 }
Exemple #18
0
 public ClosedSnapshotSourceText(ITextBufferCloneService textBufferCloneServiceOpt, ITextImage textImage, Encoding encodingOpt)
     : base(textBufferCloneServiceOpt, textImage, encodingOpt, containerOpt: null)
 {
 }
Exemple #19
0
 /// <summary>
 /// Create a new <see cref="ITextImage"/> that is a clone of a subspan of this <see cref="ITextImage"/>.
 /// </summary>
 /// <remarks>
 /// The new <see cref="ITextImage"/> will not inherit the version or version history of this <see cref="ITextImage"/>.</remarks>
 public static ITextImage GetSubText(this ITextImage image, int startIndex, int length)
 {
     return(image.GetSubText(new Span(startIndex, length)));
 }
Exemple #20
0
 /// <summary>
 /// Writes the contents of the image.
 /// </summary>
 /// <param name="writer">The <see cref="System.IO.TextWriter"/>to use.</param>
 /// <exception cref="ArgumentNullException"><paramref name="writer"/> is null.</exception>
 public static void Write(this ITextImage image, System.IO.TextWriter writer)
 {
     image.Write(writer, new Span(0, image.Length));
 }
 public ITextBuffer Clone(ITextImage textImage)
 => _textBufferFactoryService.CreateTextBuffer(textImage, _unknownContentType);
Exemple #22
0
 private ITextBuffer Clone(ITextImage textImage, IContentType contentType) =>
 _textBufferFactoryService.CreateTextBuffer(textImage, contentType);
Exemple #23
0
 public ITextBuffer CloneWithUnknownContentType(ITextImage textImage) =>
 Clone(textImage, _unknownContentType);
 public ClosedSnapshotSourceText(ITextImage textImage, Encoding encodingOpt)
     : base(textImage, encodingOpt, containerOpt: null)
 {
 }
Exemple #25
0
        public static uint[] CreateLineOffsets(ITextImage textImage)
        {
            var  buffer        = Cache.GetReadBuffer();
            var  builder       = Cache.GetOffsetBuilder();
            int  pos           = 0;
            int  endPos        = textImage.Length;
            bool lastCharWasCR = false;
            int  linePos       = pos;
            int  lineLen       = 0;

            while (pos < endPos)
            {
                int bufLen = buffer.Length;
                if (bufLen > endPos - pos)
                {
                    bufLen = endPos - pos;
                }
                textImage.CopyTo(pos, buffer, 0, bufLen);
                pos += bufLen;
                int bufPos = 0;

                if (lastCharWasCR)
                {
                    var c = buffer[0];
                    if (c == '\n')
                    {
                        builder.Add((uint)((2 << LINEBREAK_SHIFT) | linePos));
                        linePos += lineLen + 2;
                        bufPos++;
                    }
                    else
                    {
                        builder.Add((uint)((1 << LINEBREAK_SHIFT) | linePos));
                        linePos += lineLen + 1;
                    }
                    lineLen       = 0;
                    lastCharWasCR = false;
                }

                for (; bufPos < bufLen;)
                {
                    int  lineBreakSize;
                    char c = buffer[bufPos++];
                    if (c != '\r' && c != '\n' && c != '\u0085' && c != '\u2028' && c != '\u2029')
                    {
                        lineLen++;
                        continue;
                    }
                    if (c == '\r')
                    {
                        if (bufPos == bufLen)
                        {
                            lastCharWasCR = true;
                            break;
                        }
                        if (buffer[bufPos] == '\n')
                        {
                            lineBreakSize = 2;
                            bufPos++;
                        }
                        else
                        {
                            lineBreakSize = 1;
                        }
                    }
                    else
                    {
                        lineBreakSize = 1;
                    }
                    builder.Add((uint)((lineBreakSize << LINEBREAK_SHIFT) | linePos));
                    linePos      += lineLen + lineBreakSize;
                    lineLen       = 0;
                    lastCharWasCR = false;
                }
            }
            Debug.Assert(pos == endPos);
            if (lastCharWasCR)
            {
                const int lineBreakSize = 1;
                builder.Add((uint)((lineBreakSize << LINEBREAK_SHIFT) | linePos));
                linePos += lineLen + lineBreakSize;
                lineLen  = 0;
            }
            {
                const int lineBreakSize = 0;
                builder.Add((uint)((lineBreakSize << LINEBREAK_SHIFT) | linePos));
                linePos += lineLen + lineBreakSize;
            }
            Debug.Assert(linePos == endPos);

            Cache.FreeReadBuffer(buffer);
            Debug.Assert(builder.Count > 0);
            return(Cache.FreeOffsetBuilder(builder));
        }
 public ITextBuffer CloneWithUnknownContentType(ITextImage textImage) => throw new NotImplementedException();
Exemple #27
0
 /// <summary>
 /// Gets all the text in the image.
 /// </summary>
 /// <returns>A non-null string.</returns>
 /// <remarks>Caveat emptor. Calling GetText() on a 100MB <see cref="ITextImage"/> will give you exactly what you asked for, which
 /// probably isn't what you wanted.</remarks>
 public static string GetText(this ITextImage image)
 {
     return(image.GetText(new Span(0, image.Length)));
 }
Exemple #28
0
 public ITextBuffer Clone(ITextImage textImage) => throw new NotImplementedException();
 public ChangedSourceText(SnapshotSourceText baseText, ITextImage baseSnapshot, ITextImage currentSnapshot)
     : base(currentSnapshot, baseText.Encoding, containerOpt: null)
 {
     _baseText     = baseText;
     _baseSnapshot = baseSnapshot;
 }