You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Roslyn can batch fix results into one result. But I can't figure out how it works and how to test it.
Testing the project and getting diagnostics (document-level and project-level).
var diagnostics = await RoslynTestingUtils.AnalyzeAsync( Project, Analyzers, default ).ConfigureAwait( false );
//diagnostics = diagnostics.Distinct().Where( i => i.Location != Location.None ).ToArray();
Testing the FixAllProvider.
var document = Project.GetDocument( diagnostics.First().Location.SourceTree ) ?? throw new Exception( "Document is not found" );
var fixer = new ExampleCodeFixProvider();
var fixAllProvider = fixer.GetFixAllProvider();
var context = new FixAllContext( document, fixer, FixAllScope.Solution, fixer.FixableDiagnosticIds.Join(), fixer.FixableDiagnosticIds, new DiagnosticProvider( diagnostics ), default );
var action = await fixAllProvider.GetFixAsync( context ).ConfigureAwait( false ) ?? throw new Exception( "FixAction is null" );
var operations = await action.GetOperationsAsync( default ).ConfigureAwait( false );
var operation = operations.Cast<ApplyChangesOperation>().Single();
var newProject = operation.ChangedSolution.Projects.First();
The DiagnosticProvider.
internal sealed class DiagnosticProvider : FixAllContext.DiagnosticProvider {
private Diagnostic[] Diagnostics { get; }
public DiagnosticProvider(Diagnostic[] diagnostics) {
Diagnostics = diagnostics;
}
public override Task<IEnumerable<Diagnostic>> GetAllDiagnosticsAsync(Project project, CancellationToken cancellationToken) {
return Task.FromResult( Diagnostics.AsEnumerable() );
}
public override Task<IEnumerable<Diagnostic>> GetProjectDiagnosticsAsync(Project project, CancellationToken cancellationToken) {
// todo: is it ok that project is unused?
return Task.FromResult( Diagnostics.Where( i => !i.Location.IsInSource ) );
}
public override Task<IEnumerable<Diagnostic>> GetDocumentDiagnosticsAsync(Document document, CancellationToken cancellationToken) {
return Task.FromResult( Diagnostics.Where( i => i.Location.GetLineSpan().Path == document.Name ) );
}
}
There are a couple of things that I don't understand.
What document I should pass if all diagnostics are project-level or pointing on different documents?
Or should I pass only diagnostics pointing on the same document?
Or even should I pass only diagnostics with the same Id?
new FixAllContext( document, fixer, FixAllScope.Solution, fixer.FixableDiagnosticIds.Join(), fixer.FixableDiagnosticIds, new DiagnosticProvider( diagnostics ), default );
Why the FixAllProvider.GetFixAsync(...) returns null?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The Roslyn can batch fix results into one result. But I can't figure out how it works and how to test it.
FixAllProvider
.There are a couple of things that I don't understand.
Or should I pass only diagnostics pointing on the same document?
Or even should I pass only diagnostics with the same Id?
new FixAllContext( document, fixer, FixAllScope.Solution, fixer.FixableDiagnosticIds.Join(), fixer.FixableDiagnosticIds, new DiagnosticProvider( diagnostics ), default );
FixAllProvider.GetFixAsync(...)
returns null?Beta Was this translation helpful? Give feedback.
All reactions