/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/EditorFeatures/TestUtilities/Workspaces/TestWorkspaceFixture.cs
129 строк
5 KB
Tomáš Matoušek
Revert "Revert "Update VSSDK to 18.9.496-Preview"" (#84403)
09 июл 2026, 02:44
Не верифицирован
09 июл 2026, 02:44
16f6817
Код
Авторство
О чём код?
// 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. #nullable disable using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Xml.Linq; using Microsoft.CodeAnalysis.Text; using Microsoft.VisualStudio.Text; using Roslyn.Test.Utilities; namespace Microsoft.CodeAnalysis.Test.Utilities; public abstract class TestWorkspaceFixture : IDisposable { public int Position; public string Code; public ImmutableArray<TextSpan> Spans; private EditorTestWorkspace _workspace; private EditorTestHostDocument _currentDocument; public EditorTestHostDocument CurrentDocument => _currentDocument ?? _workspace.Documents.Single(); public EditorTestWorkspace GetWorkspace(TestComposition composition = null) { _workspace ??= CreateWorkspace(composition); return _workspace; } public EditorTestWorkspace GetWorkspace(string markup, TestComposition composition = null, string workspaceKind = null) { // If it looks like XML, we'll treat it as XML; any parse error would be rejected and will throw. // We'll do a case insensitive search here so if somebody has a lowercase W it'll be tried (and // rejected by the XML parser) rather than treated as regular text. if (markup.TrimStart().StartsWith("<Workspace>", StringComparison.OrdinalIgnoreCase)) { CloseTextView(); _workspace?.Dispose(); _workspace = EditorTestWorkspace.CreateWorkspace(XElement.Parse(markup), composition: composition, workspaceKind: workspaceKind); _currentDocument = _workspace.Documents.First(d => d.CursorPosition.HasValue); Position = _currentDocument.CursorPosition.Value; Spans = [.. CurrentDocument.SelectedSpans]; Code = _currentDocument.GetTextBuffer().CurrentSnapshot.GetText(); return _workspace; } else { MarkupTestFile.GetPositionAndSpans(markup.NormalizeLineEndings(), out Code, out Position, out Spans); var workspace = GetWorkspace(composition); _currentDocument = workspace.Documents.Single(); return workspace; } } protected abstract EditorTestWorkspace CreateWorkspace(TestComposition composition); public void Dispose() { if (_workspace is null) return; try { CloseTextView(); _currentDocument = null; Code = null; Position = 0; _workspace?.Dispose(); } finally { _workspace = null; } } public Document UpdateDocument(string text, SourceCodeKind sourceCodeKind, bool cleanBeforeUpdate = true) { var hostDocument = _currentDocument ?? (GetWorkspace()).Documents.Single(); // clear the document if (cleanBeforeUpdate) { UpdateText(hostDocument.GetTextBuffer(), string.Empty); } // and set the content UpdateText(hostDocument.GetTextBuffer(), text); GetWorkspace().OnDocumentSourceCodeKindChanged(hostDocument.Id, sourceCodeKind); return GetWorkspace().CurrentSolution.GetDocument(hostDocument.Id); } private static void UpdateText(ITextBuffer textBuffer, string text) { using var edit = textBuffer.CreateEdit(); edit.Replace(0, textBuffer.CurrentSnapshot.Length, text); edit.Apply(); } private void CloseTextView() { // The standard use for TestWorkspaceFixture is to call this method in the test's dispose to make sure it's ready to be used for // the next test. But some tests in a test class won't use it, so _workspace might still be null. if (_workspace?.Documents != null) { foreach (var document in _workspace?.Documents) { document.CloseTextView(); } } // The editor caches TextFormattingRunProperties instances for better perf, but since things like // Brushes are DispatcherObjects, they are tied to the thread they are created on. Since we're going // to be run on a different thread, clear out their collection. var textFormattingRunPropertiesType = typeof(VisualStudio.Text.Formatting.TextFormattingRunProperties); var existingPropertiesField = textFormattingRunPropertiesType.GetField("s_existingProperties", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); // The field is lazily initialized by the editor, so it may be null if no properties have been cached yet. var existingProperties = (System.Collections.IList)existingPropertiesField.GetValue(null); existingProperties?.Clear(); } }