/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/EditorFeatures/Text/Implementation/TextBufferFactoryService/TextBufferCloneServiceFactory.cs
58 строк
2 KB
Todd Grunke
Reduce allocations in TextBufferFactoryCloneService.Clone (#82777)
17 мар 2026, 03:46
Не верифицирован
17 мар 2026, 03:46
d2d357d
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Composition; using Microsoft.CodeAnalysis.Editor; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Utilities; namespace Microsoft.CodeAnalysis.Text.Implementation.TextBufferFactoryService; [Export(typeof(ITextBufferCloneService)), Shared] internal sealed class TextBufferCloneService : ITextBufferCloneService { private readonly ITextBufferFactoryService3 _textBufferFactoryService; private readonly IContentType _roslynContentType; private readonly IContentType _unknownContentType; [ImportingConstructor] [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] public TextBufferCloneService( ITextBufferFactoryService3 textBufferFactoryService, IContentTypeRegistryService contentTypeRegistryService) { _textBufferFactoryService = textBufferFactoryService; _roslynContentType = contentTypeRegistryService.GetContentType(ContentTypeNames.RoslynContentType); _unknownContentType = contentTypeRegistryService.UnknownContentType; } public ITextBuffer CloneWithUnknownContentType(SnapshotSpan span) => _textBufferFactoryService.CreateTextBuffer(span, _unknownContentType); public ITextBuffer CloneWithUnknownContentType(ITextImage textImage) => Clone(textImage, _unknownContentType); public ITextBuffer CloneWithRoslynContentType(SourceText sourceText) => Clone(sourceText, _roslynContentType); public ITextBuffer Clone(SourceText sourceText, IContentType contentType) { // see whether we can do directly via a text image var textImage = sourceText.TryFindCorrespondingEditorTextImage(); if (textImage != null) { return Clone(textImage, contentType); } // we can't, so do it through a text reader. var reader = new SourceTextReader(sourceText); return _textBufferFactoryService.CreateTextBuffer(reader, contentType); } private ITextBuffer Clone(ITextImage textImage, IContentType contentType) => _textBufferFactoryService.CreateTextBuffer(textImage, contentType); }