Skip to content

Commit

Permalink
Fix up a couple places that create a buffer or sourcetext from strings (
Browse files Browse the repository at this point in the history
#73537)

For SourceText->ITextBuffer, use ITextBufferCloneService.Clone
For ITextSnapshot -> SourceText, use ITextSnaphshot.AsText extension method
  • Loading branch information
ToddGrun authored May 17, 2024
1 parent f51cb52 commit 444a029
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/EditorFeatures/Core.Wpf/Preview/PreviewFactoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +33,7 @@ internal class PreviewFactoryService : AbstractPreviewFactoryService<IWpfDiffere
public PreviewFactoryService(
IThreadingContext threadingContext,
ITextBufferFactoryService textBufferFactoryService,
ITextBufferCloneService textBufferCloneService,
IContentTypeRegistryService contentTypeRegistryService,
IProjectionBufferFactoryService projectionBufferFactoryService,
ITextEditorFactoryService textEditorFactoryService,
Expand All @@ -43,6 +45,7 @@ public PreviewFactoryService(
IWpfDifferenceViewerFactoryService differenceViewerService)
: base(threadingContext,
textBufferFactoryService,
textBufferCloneService,
contentTypeRegistryService,
projectionBufferFactoryService,
editorOptionsService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.SolutionCrawler;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
using Microsoft.CodeAnalysis.Utilities;
Expand All @@ -36,6 +35,7 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.Preview;
internal abstract class AbstractPreviewFactoryService<TDifferenceViewer>(
IThreadingContext threadingContext,
ITextBufferFactoryService textBufferFactoryService,
ITextBufferCloneService textBufferCloneService,
IContentTypeRegistryService contentTypeRegistryService,
IProjectionBufferFactoryService projectionBufferFactoryService,
EditorOptionsService editorOptionsService,
Expand All @@ -48,6 +48,7 @@ internal abstract class AbstractPreviewFactoryService<TDifferenceViewer>(
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;
Expand Down Expand Up @@ -637,15 +638,15 @@ private async ValueTask<ITextBuffer> CreateNewPlainTextBufferAsync(TextDocument
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task
}

private async ValueTask<ITextBuffer> CreateTextBufferCoreAsync(TextDocument document, IContentType? contentType, CancellationToken cancellationToken)
private async ValueTask<ITextBuffer> CreateTextBufferCoreAsync(TextDocument document, IContentType contentType, CancellationToken cancellationToken)
{
ThreadingContext.ThrowIfNotOnUIThread();

#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task (containing method uses JTF)
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());
Expand Down
11 changes: 4 additions & 7 deletions src/VisualStudio/Core/Def/Preview/FileChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -52,11 +48,12 @@ public FileChange(TextDocument left,

_componentModel = componentModel;
var bufferFactory = componentModel.GetService<ITextBufferFactoryService>();
var bufferCloneService = componentModel.GetService<ITextBufferCloneService>();
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;
Expand Down Expand Up @@ -191,7 +188,7 @@ private SourceText UpdateBufferText()
edit.ApplyAndLogExceptions();
}

return SourceText.From(_buffer.CurrentSnapshot.GetText(), _encoding);
return _buffer.CurrentSnapshot.AsText();
}

public TextDocument GetOldDocument()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -58,7 +58,7 @@ protected AbstractOptionPreviewViewModel(OptionStore optionStore, IServiceProvid
_componentModel = (IComponentModel)serviceProvider.GetService(typeof(SComponentModel));

_contentTypeRegistryService = _componentModel.GetService<IContentTypeRegistryService>();
_textBufferFactoryService = _componentModel.GetService<ITextBufferFactoryService>();
_textBufferCloneService = _componentModel.GetService<ITextBufferCloneService>();
_textEditorFactoryService = _componentModel.GetService<ITextEditorFactoryService>();
_projectionBufferFactory = _componentModel.GetService<IProjectionBufferFactoryService>();
_editorOptions = _componentModel.GetService<IEditorOptionsFactoryService>();
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 444a029

Please sign in to comment.