mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 07:47:54 +01:00
This commit is contained in:
@@ -17,10 +17,10 @@ using JetBrains.Annotations;
|
||||
|
||||
public sealed class ProjectBuilder
|
||||
{
|
||||
private string _sourceCode;
|
||||
private string _expectedFixedCode;
|
||||
private DiagnosticAnalyzer _analyzer;
|
||||
private CodeFixProvider _codeFix;
|
||||
private string? _sourceCode;
|
||||
private string? _expectedFixedCode;
|
||||
private DiagnosticAnalyzer? _analyzer;
|
||||
private CodeFixProvider? _codeFix;
|
||||
|
||||
public ProjectBuilder WithSourceCode (string source)
|
||||
{
|
||||
@@ -59,20 +59,22 @@ public sealed class ProjectBuilder
|
||||
}
|
||||
|
||||
// Parse original document
|
||||
var document = CreateDocument (_sourceCode);
|
||||
var compilation = await document.Project.GetCompilationAsync ();
|
||||
Document document = CreateDocument (_sourceCode);
|
||||
Compilation? compilation = await document.Project.GetCompilationAsync ();
|
||||
|
||||
var diagnostics = compilation.GetDiagnostics ();
|
||||
var errors = diagnostics.Where (d => d.Severity == DiagnosticSeverity.Error);
|
||||
ImmutableArray<Diagnostic> diagnostics = compilation!.GetDiagnostics ();
|
||||
IEnumerable<Diagnostic> errors = diagnostics.Where (d => d.Severity == DiagnosticSeverity.Error);
|
||||
|
||||
if (errors.Any ())
|
||||
IEnumerable<Diagnostic> enumerable = errors as Diagnostic [] ?? errors.ToArray ();
|
||||
|
||||
if (enumerable.Any ())
|
||||
{
|
||||
var errorMessages = string.Join (Environment.NewLine, errors.Select (e => e.ToString ()));
|
||||
string errorMessages = string.Join (Environment.NewLine, enumerable.Select (e => e.ToString ()));
|
||||
throw new Exception ("Compilation failed with errors:" + Environment.NewLine + errorMessages);
|
||||
}
|
||||
|
||||
// Run analyzer
|
||||
var analyzerDiagnostics = await GetAnalyzerDiagnosticsAsync (compilation, _analyzer);
|
||||
ImmutableArray<Diagnostic> analyzerDiagnostics = await GetAnalyzerDiagnosticsAsync (compilation, _analyzer);
|
||||
|
||||
Assert.NotEmpty (analyzerDiagnostics);
|
||||
|
||||
@@ -83,83 +85,87 @@ public sealed class ProjectBuilder
|
||||
throw new InvalidOperationException ("Expected code fix but none was set.");
|
||||
}
|
||||
|
||||
var fixedDocument = await ApplyCodeFixAsync (document, analyzerDiagnostics.First (), _codeFix);
|
||||
Document? fixedDocument = await ApplyCodeFixAsync (document, analyzerDiagnostics.First (), _codeFix);
|
||||
|
||||
var formattedDocument = await Formatter.FormatAsync (fixedDocument);
|
||||
var fixedSource = (await formattedDocument.GetTextAsync ()).ToString ();
|
||||
if (fixedDocument is { })
|
||||
{
|
||||
Document formattedDocument = await Formatter.FormatAsync (fixedDocument);
|
||||
string fixedSource = (await formattedDocument.GetTextAsync ()).ToString ();
|
||||
|
||||
Assert.Equal (_expectedFixedCode, fixedSource);
|
||||
Assert.Equal (_expectedFixedCode, fixedSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Document CreateDocument (string source)
|
||||
{
|
||||
var dd = typeof (Enumerable).GetTypeInfo ().Assembly.Location;
|
||||
var coreDir = Directory.GetParent (dd) ?? throw new Exception ($"Could not find parent directory of dotnet sdk. Sdk directory was {dd}");
|
||||
string dd = typeof (Enumerable).GetTypeInfo ().Assembly.Location;
|
||||
DirectoryInfo coreDir = Directory.GetParent (dd) ?? throw new Exception ($"Could not find parent directory of dotnet sdk. Sdk directory was {dd}");
|
||||
|
||||
var workspace = new AdhocWorkspace ();
|
||||
var projectId = ProjectId.CreateNewId ();
|
||||
var documentId = DocumentId.CreateNewId (projectId);
|
||||
AdhocWorkspace workspace = new AdhocWorkspace ();
|
||||
ProjectId projectId = ProjectId.CreateNewId ();
|
||||
DocumentId documentId = DocumentId.CreateNewId (projectId);
|
||||
|
||||
var references = new List<MetadataReference> ()
|
||||
{
|
||||
MetadataReference.CreateFromFile(typeof(Button).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(View).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(System.IO.FileSystemInfo).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(System.Linq.Enumerable).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(MarshalByValueComponent).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(ObservableCollection<string>).Assembly.Location),
|
||||
List<MetadataReference> references =
|
||||
[
|
||||
MetadataReference.CreateFromFile (typeof (Button).Assembly.Location),
|
||||
MetadataReference.CreateFromFile (typeof (View).Assembly.Location),
|
||||
MetadataReference.CreateFromFile (typeof (System.IO.FileSystemInfo).Assembly.Location),
|
||||
MetadataReference.CreateFromFile (typeof (System.Linq.Enumerable).Assembly.Location),
|
||||
MetadataReference.CreateFromFile (typeof (object).Assembly.Location),
|
||||
MetadataReference.CreateFromFile (typeof (MarshalByValueComponent).Assembly.Location),
|
||||
MetadataReference.CreateFromFile (typeof (ObservableCollection<string>).Assembly.Location),
|
||||
|
||||
// New assemblies required by Terminal.Gui version 2
|
||||
MetadataReference.CreateFromFile(typeof(Size).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(CanBeNullAttribute).Assembly.Location),
|
||||
MetadataReference.CreateFromFile (typeof (Size).Assembly.Location),
|
||||
MetadataReference.CreateFromFile (typeof (CanBeNullAttribute).Assembly.Location),
|
||||
|
||||
|
||||
MetadataReference.CreateFromFile(Path.Combine(coreDir.FullName, "mscorlib.dll")),
|
||||
MetadataReference.CreateFromFile(Path.Combine(coreDir.FullName, "System.Runtime.dll")),
|
||||
MetadataReference.CreateFromFile(Path.Combine(coreDir.FullName, "System.Collections.dll")),
|
||||
MetadataReference.CreateFromFile(Path.Combine(coreDir.FullName, "System.Data.Common.dll")),
|
||||
MetadataReference.CreateFromFile (Path.Combine (coreDir.FullName, "mscorlib.dll")),
|
||||
MetadataReference.CreateFromFile (Path.Combine (coreDir.FullName, "System.Runtime.dll")),
|
||||
MetadataReference.CreateFromFile (Path.Combine (coreDir.FullName, "System.Collections.dll")),
|
||||
MetadataReference.CreateFromFile (Path.Combine (coreDir.FullName, "System.Data.Common.dll"))
|
||||
|
||||
// Add more as necessary
|
||||
};
|
||||
];
|
||||
|
||||
|
||||
var projectInfo = ProjectInfo.Create (
|
||||
projectId,
|
||||
VersionStamp.Create (),
|
||||
"TestProject",
|
||||
"TestAssembly",
|
||||
LanguageNames.CSharp,
|
||||
compilationOptions: new CSharpCompilationOptions (OutputKind.DynamicallyLinkedLibrary),
|
||||
metadataReferences: references);
|
||||
ProjectInfo projectInfo = ProjectInfo.Create (
|
||||
projectId,
|
||||
VersionStamp.Create (),
|
||||
"TestProject",
|
||||
"TestAssembly",
|
||||
LanguageNames.CSharp,
|
||||
compilationOptions: new CSharpCompilationOptions (OutputKind.DynamicallyLinkedLibrary),
|
||||
metadataReferences: references);
|
||||
|
||||
var solution = workspace.CurrentSolution
|
||||
.AddProject (projectInfo)
|
||||
.AddDocument (documentId, "Test.cs", SourceText.From (source));
|
||||
Solution solution = workspace.CurrentSolution
|
||||
.AddProject (projectInfo)
|
||||
.AddDocument (documentId, "Test.cs", SourceText.From (source));
|
||||
|
||||
return solution.GetDocument (documentId)!;
|
||||
}
|
||||
|
||||
private static async Task<ImmutableArray<Diagnostic>> GetAnalyzerDiagnosticsAsync (Compilation compilation, DiagnosticAnalyzer analyzer)
|
||||
{
|
||||
var compilationWithAnalyzers = compilation.WithAnalyzers (ImmutableArray.Create (analyzer));
|
||||
CompilationWithAnalyzers compilationWithAnalyzers = compilation.WithAnalyzers ([analyzer]);
|
||||
return await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync ();
|
||||
}
|
||||
|
||||
private static async Task<Document> ApplyCodeFixAsync (Document document, Diagnostic diagnostic, CodeFixProvider codeFix)
|
||||
private static async Task<Document?> ApplyCodeFixAsync (Document document, Diagnostic diagnostic, CodeFixProvider codeFix)
|
||||
{
|
||||
CodeAction _codeAction = null;
|
||||
var context = new CodeFixContext ((TextDocument)document, diagnostic, (action, _) => _codeAction = action, CancellationToken.None);
|
||||
CodeAction? codeAction = null;
|
||||
var context = new CodeFixContext ((TextDocument)document, diagnostic, (action, _) => codeAction = action, CancellationToken.None);
|
||||
|
||||
await codeFix.RegisterCodeFixesAsync (context);
|
||||
|
||||
if (_codeAction == null)
|
||||
if (codeAction == null)
|
||||
{
|
||||
throw new InvalidOperationException ("Code fix did not register a fix.");
|
||||
}
|
||||
|
||||
var operations = await _codeAction.GetOperationsAsync (CancellationToken.None);
|
||||
var solution = operations.OfType<ApplyChangesOperation> ().First ().ChangedSolution;
|
||||
ImmutableArray<CodeActionOperation> operations = await codeAction.GetOperationsAsync (CancellationToken.None);
|
||||
Solution solution = operations.OfType<ApplyChangesOperation> ().First ().ChangedSolution;
|
||||
return solution.GetDocument (document.Id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user