public ITextBuffer CreateTextBuffer(TextReader reader, IContentType contentType, long length, string traceId, bool throwOnInvalidCharacters) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } if (contentType == null) { throw new ArgumentNullException(nameof(contentType)); } if (length > int.MaxValue) { throw new InvalidOperationException(Strings.FileTooLarge); } int longestLineLength; StringRebuilder content = TextImageLoader.Load( reader, length, out var newlineState, out var leadingWhitespaceState, out longestLineLength, throwOnInvalidCharacters: throwOnInvalidCharacters); ITextBuffer buffer = Make(contentType, content, false); // Make the call to GetWhitespaceManager to add the manager to the properties. We don't need the return value here. var _ = _whitespaceManagerFactory.GetOrCreateWhitespaceManager(buffer, newlineState, leadingWhitespaceState); // Leave a sign about the longest line in the buffer. This is rather nasty, but for now // we don't want to pollute the API with this factoid buffer.Properties["LongestLineLength"] = longestLineLength; return(buffer); }
public ITextImage CreateTextImage(TextReader reader, long length) { bool hasConsistentLineEndings; int longestLineLength; return(CachingTextImage.Create(TextImageLoader.Load(reader, length, string.Empty, out hasConsistentLineEndings, out longestLineLength), null)); }
public ITextBuffer CreateTextBuffer(TextReader reader, IContentType contentType, long length, string traceId) { if (reader == null) { throw new ArgumentNullException("reader"); } if (contentType == null) { throw new ArgumentNullException("contentType"); } if (length > int.MaxValue) { throw new InvalidOperationException(Strings.FileTooLarge); } bool hasConsistentLineEndings; int longestLineLength; StringRebuilder content = TextImageLoader.Load(reader, length, traceId, out hasConsistentLineEndings, out longestLineLength); ITextBuffer buffer = Make(contentType, content, false); if (!hasConsistentLineEndings) { // leave a sign that line endings are inconsistent. This is rather nasty but for now // we don't want to pollute the API with this factoid buffer.Properties.AddProperty("InconsistentLineEndings", true); } // leave a similar sign about the longest line in the buffer. return(buffer); }
private void ReloadBufferFromStream(Stream stream, long fileSize, EditOptions options, Encoding encoding) { using (var streamReader = new EncodedStreamReader.NonStreamClosingStreamReader(stream, encoding, detectEncodingFromByteOrderMarks: false)) { TextBuffer concreteBuffer = _textBuffer as TextBuffer; if (concreteBuffer != null) { StringRebuilder newContent = TextImageLoader.Load( streamReader, fileSize, out var newlineState, out var leadingWhitespaceState, out var longestLineLength); if (!newlineState.HasConsistentLineEndings) { // leave a sign that line endings are inconsistent. This is rather nasty but for now // we don't want to pollute the API with this factoid. concreteBuffer.Properties["InconsistentLineEndings"] = true; } else { // this covers a really obscure case where on initial load the file had inconsistent line // endings, but the UI settings were such that it was ignored, and since then the file has // acquired consistent line endings and the UI settings have also changed. concreteBuffer.Properties.RemoveProperty("InconsistentLineEndings"); } // leave a similar sign about the longest line in the buffer. concreteBuffer.Properties["LongestLineLength"] = longestLineLength; concreteBuffer.ReloadContent(newContent, options, editTag: this); } else { // we may hit this path if somebody mocks the text buffer in a test. using (var edit = _textBuffer.CreateEdit(options, null, editTag: this)) { if (edit.Replace(new Span(0, edit.Snapshot.Length), streamReader.ReadToEnd())) { edit.Apply(); } else { edit.Cancel(); } } } } }
private void ReloadBufferFromStream(Stream stream, long fileSize, EditOptions options, Encoding encoding) { using (var streamReader = new EncodedStreamReader.NonStreamClosingStreamReader(stream, encoding, detectEncodingFromByteOrderMarks: false)) { TextBuffer concreteBuffer = _textBuffer as TextBuffer; if (concreteBuffer != null) { int longestLineLength; StringRebuilder newContent = TextImageLoader.Load(streamReader, fileSize, out var newlineState, out var leadingWhitespaceState, out longestLineLength); // Make the call to GetWhitespaceManager to add the manager to the properties. We don't need the return value here. _textDocumentFactoryService.WhitespaceManagerFactory.GetOrCreateWhitespaceManager(concreteBuffer, newlineState, leadingWhitespaceState); // Leave a sign about the longest line in the buffer. This is rather nasty, but for now // we don't want to pollute the API with this factoid concreteBuffer.Properties["LongestLineLength"] = longestLineLength; concreteBuffer.ReloadContent(newContent, options, editTag: this); } else { // we may hit this path if somebody mocks the text buffer in a test. using (var edit = _textBuffer.CreateEdit(options, null, editTag: this)) { if (edit.Replace(new Span(0, edit.Snapshot.Length), streamReader.ReadToEnd())) { edit.Apply(); } else { edit.Cancel(); } } } } }
public ITextImage CreateTextImage(TextReader reader, long length) { return(CachingTextImage.Create(TextImageLoader.Load(reader, length, out var _, out var _, out var _), null)); }
internal static StringRebuilder Load(TextReader reader, long fileSize, out NewlineState newlineState, out LeadingWhitespaceState leadingWhitespaceState, out int longestLineLength, int blockSize = 0, int minCompressedBlockSize = TextImageLoader.BlockSize, // Exposed for unit tests bool throwOnInvalidCharacters = false) { newlineState = new NewlineState(); leadingWhitespaceState = new LeadingWhitespaceState(); int currentLineLength = 0; longestLineLength = 0; char thresholdForInvalidCharacters = throwOnInvalidCharacters ? '\u0001' : '\0'; // Basically the only invalid character is \0, if we are looking for invalid characters. bool useCompressedStringRebuilders = (fileSize >= TextModelOptions.CompressedStorageFileSizeThreshold); if (blockSize == 0) { blockSize = useCompressedStringRebuilders ? TextModelOptions.CompressedStoragePageSize : TextImageLoader.BlockSize; } PageManager pageManager = null; char[] buffer; if (useCompressedStringRebuilders) { pageManager = new PageManager(); buffer = new char[blockSize]; } else { buffer = TextImageLoader.AcquireBuffer(blockSize); } StringRebuilder content = StringRebuilderForChars.Empty; try { bool nextCharIsStartOfLine = true; while (true) { int read = TextImageLoader.LoadNextBlock(reader, buffer); if (read == 0) { break; } var lineBreaks = TextImageLoader.ParseBlock( buffer, read, thresholdForInvalidCharacters, ref newlineState, ref leadingWhitespaceState, ref currentLineLength, ref longestLineLength, ref nextCharIsStartOfLine); char[] bufferForStringBuilder = buffer; if (read < (buffer.Length / 2)) { // We read far less characters than buffer so copy the contents to a new buffer and reuse the original buffer. bufferForStringBuilder = new char[read]; Array.Copy(buffer, bufferForStringBuilder, read); } else { // We're using most of buffer so allocate a new block for the next chunk. buffer = new char[blockSize]; } var newContent = (useCompressedStringRebuilders && (read > minCompressedBlockSize)) ? StringRebuilderForCompressedChars.Create(new Page(pageManager, bufferForStringBuilder, read), lineBreaks) : StringRebuilderForChars.Create(bufferForStringBuilder, read, lineBreaks); content = content.Insert(content.Length, newContent); } longestLineLength = Math.Max(longestLineLength, currentLineLength); } finally { if (!useCompressedStringRebuilders) { TextImageLoader.ReleaseBuffer(buffer); } } return(content); }
internal static StringRebuilder Load(TextReader reader, long fileSize, string id, out bool hasConsistentLineEndings, out int longestLineLength, int blockSize = 0, int minCompressedBlockSize = TextImageLoader.BlockSize) // Exposed for unit tests { LineEndingState lineEnding = LineEndingState.Unknown; int currentLineLength = 0; longestLineLength = 0; bool useCompressedStringRebuilders = (fileSize >= TextModelOptions.CompressedStorageFileSizeThreshold); if (blockSize == 0) { blockSize = useCompressedStringRebuilders ? TextModelOptions.CompressedStoragePageSize : TextImageLoader.BlockSize; } PageManager pageManager = null; char[] buffer; if (useCompressedStringRebuilders) { pageManager = new PageManager(); buffer = new char[blockSize]; } else { buffer = TextImageLoader.AcquireBuffer(blockSize); } StringRebuilder content = StringRebuilderForChars.Empty; try { while (true) { int read = TextImageLoader.LoadNextBlock(reader, buffer); if (read == 0) { break; } var lineBreaks = LineBreakManager.CreateLineBreakEditor(read); TextImageLoader.ParseBlock(buffer, read, lineBreaks, ref lineEnding, ref currentLineLength, ref longestLineLength); char[] bufferForStringBuilder = buffer; if (read < (buffer.Length / 2)) { // We read far less characters than buffer so copy the contents to a new buffer and reuse the original buffer. bufferForStringBuilder = new char[read]; Array.Copy(buffer, bufferForStringBuilder, read); } else { // We're using most of bufferForStringRebuilder so allocate a new block for the next chunk. buffer = new char[blockSize]; } var newContent = (useCompressedStringRebuilders && (read > minCompressedBlockSize)) ? StringRebuilderForCompressedChars.Create(new Page(pageManager, bufferForStringBuilder, read), lineBreaks) : StringRebuilderForChars.Create(bufferForStringBuilder, read, lineBreaks); content = content.Insert(content.Length, newContent); } longestLineLength = Math.Max(longestLineLength, currentLineLength); hasConsistentLineEndings = lineEnding != LineEndingState.Inconsistent; } finally { if (!useCompressedStringRebuilders) { TextImageLoader.ReleaseBuffer(buffer); } } return(content); }