mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-12-20 09:05:49 +08:00
Moved analyzer tests to its own project
Also moves tests to `./test` which makes it possible for all test projects to share the same .editorconfig files and similar.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis.CodeFixes;
|
||||
using Microsoft.VisualStudio.Composition;
|
||||
using Spectre.Console.Analyzer.FixProviders;
|
||||
|
||||
namespace Spectre.Console.Analyzer.Tests
|
||||
{
|
||||
internal static class CodeFixProviderDiscovery
|
||||
{
|
||||
private static readonly Lazy<IExportProviderFactory> _exportProviderFactory;
|
||||
|
||||
static CodeFixProviderDiscovery()
|
||||
{
|
||||
_exportProviderFactory = new Lazy<IExportProviderFactory>(
|
||||
() =>
|
||||
{
|
||||
var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance, isNonPublicSupported: true);
|
||||
var parts = Task.Run(() => discovery.CreatePartsAsync(typeof(SystemConsoleToAnsiConsoleFix).Assembly)).GetAwaiter().GetResult();
|
||||
var catalog = ComposableCatalog.Create(Resolver.DefaultInstance).AddParts(parts);
|
||||
|
||||
var configuration = CompositionConfiguration.Create(catalog);
|
||||
var runtimeComposition = RuntimeComposition.CreateRuntimeComposition(configuration);
|
||||
return runtimeComposition.CreateExportProviderFactory();
|
||||
},
|
||||
LazyThreadSafetyMode.ExecutionAndPublication);
|
||||
}
|
||||
|
||||
public static IEnumerable<CodeFixProvider> GetCodeFixProviders(string language)
|
||||
{
|
||||
var exportProvider = _exportProviderFactory.Value.CreateExportProvider();
|
||||
var exports = exportProvider.GetExports<CodeFixProvider, LanguageMetadata>();
|
||||
return exports.Where(export => export.Metadata.Languages.Contains(language)).Select(export => export.Value);
|
||||
}
|
||||
|
||||
private class LanguageMetadata
|
||||
{
|
||||
public LanguageMetadata(IDictionary<string, object> data)
|
||||
{
|
||||
if (!data.TryGetValue(nameof(ExportCodeFixProviderAttribute.Languages), out var languages))
|
||||
{
|
||||
languages = Array.Empty<string>();
|
||||
}
|
||||
|
||||
Languages = ((string[])languages).ToImmutableArray();
|
||||
}
|
||||
|
||||
public ImmutableArray<string> Languages { get; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user