From 444a029343b715eeba44040cbdfb077558de568a Mon Sep 17 00:00:00 2001 From: Todd Grunke Date: Fri, 17 May 2024 11:00:11 -0700 Subject: [PATCH] Fix up a couple places that create a buffer or sourcetext from strings (#73537) For SourceText->ITextBuffer, use ITextBufferCloneService.Clone For ITextSnapshot -> SourceText, use ITextSnaphshot.AsText extension method --- .../Core.Wpf/Preview/PreviewFactoryService.cs | 3 +++ .../Core/Preview/AbstractPreviewFactoryService.cs | 7 ++++--- src/VisualStudio/Core/Def/Preview/FileChange.cs | 11 ++++------- .../Impl/Options/AbstractOptionPreviewViewModel.cs | 6 +++--- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/EditorFeatures/Core.Wpf/Preview/PreviewFactoryService.cs b/src/EditorFeatures/Core.Wpf/Preview/PreviewFactoryService.cs index aebd395175110..de16129008ca8 100644 --- a/src/EditorFeatures/Core.Wpf/Preview/PreviewFactoryService.cs +++ b/src/EditorFeatures/Core.Wpf/Preview/PreviewFactoryService.cs @@ -11,6 +11,7 @@ using Microsoft.CodeAnalysis.Editor.Shared.Utilities; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Options; +using Microsoft.CodeAnalysis.Text; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Differencing; using Microsoft.VisualStudio.Text.Editor; @@ -32,6 +33,7 @@ internal class PreviewFactoryService : AbstractPreviewFactoryService( IThreadingContext threadingContext, ITextBufferFactoryService textBufferFactoryService, + ITextBufferCloneService textBufferCloneService, IContentTypeRegistryService contentTypeRegistryService, IProjectionBufferFactoryService projectionBufferFactoryService, EditorOptionsService editorOptionsService, @@ -48,6 +48,7 @@ internal abstract class AbstractPreviewFactoryService( private const double DefaultZoomLevel = 0.75; private readonly ITextViewRoleSet _previewRoleSet = previewRoleSet; private readonly ITextBufferFactoryService _textBufferFactoryService = textBufferFactoryService; + private readonly ITextBufferCloneService _textBufferCloneService = textBufferCloneService; private readonly IContentTypeRegistryService _contentTypeRegistryService = contentTypeRegistryService; private readonly IProjectionBufferFactoryService _projectionBufferFactoryService = projectionBufferFactoryService; private readonly EditorOptionsService _editorOptionsService = editorOptionsService; @@ -637,7 +638,7 @@ private async ValueTask CreateNewPlainTextBufferAsync(TextDocument #pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task } - private async ValueTask CreateTextBufferCoreAsync(TextDocument document, IContentType? contentType, CancellationToken cancellationToken) + private async ValueTask CreateTextBufferCoreAsync(TextDocument document, IContentType contentType, CancellationToken cancellationToken) { ThreadingContext.ThrowIfNotOnUIThread(); @@ -645,7 +646,7 @@ private async ValueTask CreateTextBufferCoreAsync(TextDocument docu var text = await document.State.GetTextAsync(cancellationToken); #pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task - var buffer = _textBufferFactoryService.CreateTextBuffer(text.ToString(), contentType); + var buffer = _textBufferCloneService.Clone(text, contentType); // Associate buffer with a text document with random file path to satisfy extensibility points expecting absolute file path. _textDocumentFactoryService.CreateTextDocument(buffer, Path.GetTempFileName()); diff --git a/src/VisualStudio/Core/Def/Preview/FileChange.cs b/src/VisualStudio/Core/Def/Preview/FileChange.cs index 37c83672c3755..544e2c0a33130 100644 --- a/src/VisualStudio/Core/Def/Preview/FileChange.cs +++ b/src/VisualStudio/Core/Def/Preview/FileChange.cs @@ -6,15 +6,12 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Editor; using Microsoft.CodeAnalysis.Editor.Shared.Extensions; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; -using Microsoft.CodeAnalysis.Text.Shared.Extensions; using Microsoft.VisualStudio.ComponentModelHost; using Microsoft.VisualStudio.Language.Intellisense; using Microsoft.VisualStudio.LanguageServices.Implementation.Extensions; @@ -33,7 +30,6 @@ internal class FileChange : AbstractChange private readonly IComponentModel _componentModel; public readonly DocumentId Id; private readonly ITextBuffer _buffer; - private readonly Encoding _encoding; private readonly IVsImageService2 _imageService; public FileChange(TextDocument left, @@ -52,11 +48,12 @@ public FileChange(TextDocument left, _componentModel = componentModel; var bufferFactory = componentModel.GetService(); + var bufferCloneService = componentModel.GetService(); var bufferText = left != null ? left.GetTextSynchronously(CancellationToken.None) : right.GetTextSynchronously(CancellationToken.None); - _buffer = bufferFactory.CreateTextBuffer(bufferText.ToString(), bufferFactory.InertContentType); - _encoding = bufferText.Encoding; + + _buffer = bufferCloneService.Clone(bufferText, bufferFactory.InertContentType); this.Children = ComputeChildren(left, right, CancellationToken.None); this.parent = parent; @@ -191,7 +188,7 @@ private SourceText UpdateBufferText() edit.ApplyAndLogExceptions(); } - return SourceText.From(_buffer.CurrentSnapshot.GetText(), _encoding); + return _buffer.CurrentSnapshot.AsText(); } public TextDocument GetOldDocument() diff --git a/src/VisualStudio/Core/Impl/Options/AbstractOptionPreviewViewModel.cs b/src/VisualStudio/Core/Impl/Options/AbstractOptionPreviewViewModel.cs index 94ff8208e4117..36f36925b6f4b 100644 --- a/src/VisualStudio/Core/Impl/Options/AbstractOptionPreviewViewModel.cs +++ b/src/VisualStudio/Core/Impl/Options/AbstractOptionPreviewViewModel.cs @@ -40,7 +40,7 @@ internal abstract class AbstractOptionPreviewViewModel : AbstractNotifyPropertyC private readonly IContentType _contentType; private readonly IEditorOptionsFactoryService _editorOptions; private readonly ITextEditorFactoryService _textEditorFactoryService; - private readonly ITextBufferFactoryService _textBufferFactoryService; + private readonly ITextBufferCloneService _textBufferCloneService; private readonly IProjectionBufferFactoryService _projectionBufferFactory; private readonly IContentTypeRegistryService _contentTypeRegistryService; @@ -58,7 +58,7 @@ protected AbstractOptionPreviewViewModel(OptionStore optionStore, IServiceProvid _componentModel = (IComponentModel)serviceProvider.GetService(typeof(SComponentModel)); _contentTypeRegistryService = _componentModel.GetService(); - _textBufferFactoryService = _componentModel.GetService(); + _textBufferCloneService = _componentModel.GetService(); _textEditorFactoryService = _componentModel.GetService(); _projectionBufferFactory = _componentModel.GetService(); _editorOptions = _componentModel.GetService(); @@ -140,7 +140,7 @@ public void UpdatePreview(string text) var formattingOptions = formattingService.GetFormattingOptions(OptionStore, fallbackFormattingOptions); var formatted = Formatter.FormatAsync(document, formattingOptions, CancellationToken.None).WaitAndGetResult(CancellationToken.None); - var textBuffer = _textBufferFactoryService.CreateTextBuffer(formatted.GetTextSynchronously(CancellationToken.None).ToString(), _contentType); + var textBuffer = _textBufferCloneService.Clone(formatted.GetTextSynchronously(CancellationToken.None), _contentType); var container = textBuffer.AsTextContainer();