本文整理汇总了C#中Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider类的典型用法代码示例。如果您正苦于以下问题:C# CodeFixProvider类的具体用法?C# CodeFixProvider怎么用?C# CodeFixProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodeFixProvider类属于Microsoft.CodeAnalysis.CodeFixes命名空间,在下文中一共展示了CodeFixProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VerifyFixInternalAsync
private async Task VerifyFixInternalAsync(string language, ImmutableArray<DiagnosticAnalyzer> analyzers, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex,
bool allowNewCompilerDiagnostics, int maxNumberOfIterations, Func<ImmutableArray<DiagnosticAnalyzer>, CodeFixProvider, int?, CancellationToken, Document, int, Task<Document>> getFixedDocument, CancellationToken cancellationToken)
{
var document = this.CreateDocument(oldSource, language);
var compilerDiagnostics = await GetCompilerDiagnosticsAsync(document, cancellationToken).ConfigureAwait(false);
document = await getFixedDocument(analyzers, codeFixProvider, codeFixIndex, cancellationToken, document, maxNumberOfIterations).ConfigureAwait(false);
var newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, await GetCompilerDiagnosticsAsync(document, cancellationToken).ConfigureAwait(false));
// check if applying the code fix introduced any new compiler diagnostics
if (!allowNewCompilerDiagnostics && newCompilerDiagnostics.Any())
{
// Format and get the compiler diagnostics again so that the locations make sense in the output
document = await Formatter.FormatAsync(document, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, await GetCompilerDiagnosticsAsync(document, cancellationToken).ConfigureAwait(false));
string message =
string.Format("Fix introduced new compiler diagnostics:\r\n{0}\r\n\r\nNew document:\r\n{1}\r\n",
string.Join("\r\n", newCompilerDiagnostics.Select(d => d.ToString())),
(await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false)).ToFullString());
Assert.True(false, message);
}
// after applying all of the code fixes, compare the resulting string to the inputted one
var actual = await GetStringFromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
Assert.Equal(newSource, actual);
}
示例2: FixAllState
private FixAllState(
FixAllProvider fixAllProvider,
Document document,
Project project,
CodeFixProvider codeFixProvider,
FixAllScope scope,
string codeActionEquivalenceKey,
IEnumerable<string> diagnosticIds,
FixAllContext.DiagnosticProvider fixAllDiagnosticProvider)
{
Contract.ThrowIfNull(project);
if (diagnosticIds == null)
{
throw new ArgumentNullException(nameof(diagnosticIds));
}
if (diagnosticIds.Any(d => d == null))
{
throw new ArgumentException(WorkspacesResources.Supplied_diagnostic_cannot_be_null, nameof(diagnosticIds));
}
this.FixAllProvider = fixAllProvider;
this.Document = document;
this.Project = project;
this.CodeFixProvider = codeFixProvider ?? throw new ArgumentNullException(nameof(codeFixProvider));
this.Scope = scope;
this.CodeActionEquivalenceKey = codeActionEquivalenceKey;
this.DiagnosticIds = ImmutableHashSet.CreateRange(diagnosticIds);
this.DiagnosticProvider = fixAllDiagnosticProvider ?? throw new ArgumentNullException(nameof(fixAllDiagnosticProvider));
}
示例3: VerifyFix
protected void VerifyFix(string language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex, bool allowNewCompilerDiagnostics)
{
Document document = CreateDocument(oldSource, language);
VerifyFix(document, analyzer, codeFixProvider, newSource, codeFixIndex, useCompilerAnalyzerDriver: true, allowNewCompilerDiagnostics: allowNewCompilerDiagnostics);
VerifyFix(document, analyzer, codeFixProvider, newSource, codeFixIndex, useCompilerAnalyzerDriver: false, allowNewCompilerDiagnostics: allowNewCompilerDiagnostics);
}
示例4: CreateAsync
internal static async Task<ImmutableList<CodeFixEquivalenceGroup>> CreateAsync(CodeFixProvider codeFixProvider, IEnumerable<Diagnostic> allDiagnostics, Solution solution)
{
var fixAllProvider = codeFixProvider.GetFixAllProvider();
var relevantDiagnostics = allDiagnostics.Where(diagnostic => codeFixProvider.FixableDiagnosticIds.Contains(diagnostic.Id)).ToImmutableArray();
if (fixAllProvider == null)
{
return ImmutableList.Create<CodeFixEquivalenceGroup>();
}
List<CodeAction> actions = new List<CodeAction>();
foreach (var diagnostic in relevantDiagnostics)
{
actions.AddRange(await GetFixesAsync(solution, codeFixProvider, diagnostic).ConfigureAwait(false));
}
List<CodeFixEquivalenceGroup> groups = new List<CodeFixEquivalenceGroup>();
foreach (var item in actions.GroupBy(x => x.EquivalenceKey))
{
groups.Add(new CodeFixEquivalenceGroup(item.Key, solution, fixAllProvider, codeFixProvider, relevantDiagnostics));
}
return groups.ToImmutableList();
}
示例5: VerifyFix
internal void VerifyFix(CodeFixProvider codeFixProvider,
DiagnosticAnalyzer diagnosticAnalyzer,
string language,
string oldSource,
string newSource,
int? codeFixIndex = null,
string[] allowedNewCompilerDiagnosticsId = null)
{
CodeFixProvider = codeFixProvider;
DiagnosticAnalyzer = diagnosticAnalyzer;
if (allowedNewCompilerDiagnosticsId == null || !allowedNewCompilerDiagnosticsId.Any())
{
VerifyFix(language, DiagnosticAnalyzer, CodeFixProvider, oldSource, newSource, codeFixIndex, false);
}
else
{
var document = DiagnosticVerifier.CreateDocument(oldSource, language);
var compilerDiagnostics = GetCompilerDiagnostics(document).ToArray();
VerifyFix(language, DiagnosticAnalyzer, CodeFixProvider, oldSource, newSource, codeFixIndex, true);
var newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document)).ToList();
if (newCompilerDiagnostics.Any(diagnostic => allowedNewCompilerDiagnosticsId.Any(s => s == diagnostic.Id)))
{
Assert.AreEqual(document.GetSyntaxRootAsync().Result.ToFullString(),
string.Join(Environment.NewLine, newCompilerDiagnostics.Select(d => d.ToString())),
"Fix introduced new compiler diagnostics");
}
}
}
示例6: VerifyFix
/// <summary>
/// General verifier for codefixes.
/// Creates a Document from the source string, then gets diagnostics on it and applies the relevant codefixes.
/// Then gets the string after the codefix is applied and compares it with the expected result.
/// Note: If any codefix causes new diagnostics to show up, the test fails unless allowNewCompilerDiagnostics is set to true.
/// </summary>
/// <param name="language">The language the source code is in</param>
/// <param name="analyzer">The analyzer to be applied to the source code</param>
/// <param name="codeFixProvider">The codefix to be applied to the code wherever the relevant Diagnostic is found</param>
/// <param name="oldSource">A class in the form of a string before the CodeFix was applied to it</param>
/// <param name="newSource">A class in the form of a string after the CodeFix was applied to it</param>
/// <param name="codeFixIndex">Index determining which codefix to apply if there are multiple</param>
/// <param name="allowNewCompilerDiagnostics">A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied</param>
private void VerifyFix(string language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex, bool allowNewCompilerDiagnostics)
{
Document document = DiagnosticVerifier.CreateDocument(oldSource, language);
Diagnostic[] analyzerDiagnostics = DiagnosticVerifier.GetSortedDiagnosticsFromDocuments(analyzer, new[]
{
document
});
IEnumerable<Diagnostic> compilerDiagnostics = CodeFixVerifier.GetCompilerDiagnostics(document);
int attempts = analyzerDiagnostics.Length;
for (int i = 0; i < attempts; ++i)
{
List<CodeAction> actions = new List<CodeAction>();
CodeFixContext context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None);
codeFixProvider.RegisterCodeFixesAsync(context).Wait();
if (!actions.Any())
{
break;
}
if (codeFixIndex != null)
{
document = CodeFixVerifier.ApplyFix(document, actions.ElementAt((int)codeFixIndex));
break;
}
document = CodeFixVerifier.ApplyFix(document, actions.ElementAt(0));
analyzerDiagnostics = DiagnosticVerifier.GetSortedDiagnosticsFromDocuments(analyzer, new[]
{
document
});
IEnumerable<Diagnostic> newCompilerDiagnostics = CodeFixVerifier.GetNewDiagnostics(compilerDiagnostics, CodeFixVerifier.GetCompilerDiagnostics(document));
// check if applying the code fix introduced any new compiler diagnostics
if (!allowNewCompilerDiagnostics && newCompilerDiagnostics.Any())
{
// Format and get the compiler diagnostics again so that the locations make sense in the output
document = document.WithSyntaxRoot(Formatter.Format(document.GetSyntaxRootAsync().Result, Formatter.Annotation, document.Project.Solution.Workspace));
newCompilerDiagnostics = CodeFixVerifier.GetNewDiagnostics(compilerDiagnostics, CodeFixVerifier.GetCompilerDiagnostics(document));
Assert.IsTrue(false,
string.Format("Fix introduced new compiler diagnostics:\r\n{0}\r\n\r\nNew document:\r\n{1}\r\n",
string.Join("\r\n", newCompilerDiagnostics.Select(d => d.ToString())),
document.GetSyntaxRootAsync().Result.ToFullString()));
}
// check if there are analyzer diagnostics left after the code fix
if (!analyzerDiagnostics.Any())
{
break;
}
}
// after applying all of the code fixes, compare the resulting string to the inputted one
string actual = CodeFixVerifier.GetStringFromDocument(document);
Assert.AreEqual(newSource, actual);
}
示例7: GetFixesAsync
private static async Task<IEnumerable<CodeAction>> GetFixesAsync(Solution solution, CodeFixProvider codeFixProvider, Diagnostic diagnostic)
{
List<CodeAction> codeActions = new List<CodeAction>();
await codeFixProvider.RegisterCodeFixesAsync(new CodeFixContext(solution.GetDocument(diagnostic.Location.SourceTree), diagnostic, (a, d) => codeActions.Add(a), CancellationToken.None));
return codeActions;
}
示例8: VerifyFix
/// <summary>
/// General verifier for code fixes.
/// Creates a Document from the source string, then gets diagnostics on it and applies the relevant code fixes.
/// Then gets the string after the code fix is applied and compares it with the expected result.
/// Note: If any code fix causes new diagnostics to show up, the test fails unless allowNewCompilerDiagnostics is set to true.
/// </summary>
/// <param name="language">The language the source code is in.</param>
/// <param name="analyzer">The analyzer to be applied to the source code.</param>
/// <param name="codeFixProvider">The code fix to be applied to the code wherever the relevant Diagnostic is found.</param>
/// <param name="oldSource">A class in the form of a string before the code fix was applied to it.</param>
/// <param name="newSource">A class in the form of a string after the code fix was applied to it.</param>
/// <param name="codeFixIndex">Index determining which code fix to apply if there are multiple.</param>
/// <param name="allowNewCompilerDiagnostics">A boolean controlling whether or not the test will fail if the code fix introduces other warnings after being applied.</param>
private void VerifyFix(string language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex, bool allowNewCompilerDiagnostics)
{
var document = CreateDocument(oldSource, language);
var analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document });
var compilerDiagnostics = GetCompilerDiagnostics(document);
var attempts = analyzerDiagnostics.Length;
for (int i = 0; i < attempts; ++i)
{
var actions = new List<CodeAction>();
var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None);
codeFixProvider.RegisterCodeFixesAsync(context).Wait();
if (!actions.Any())
{
break;
}
if (codeFixIndex != null)
{
document = ApplyFix(document, actions.ElementAt((int)codeFixIndex));
break;
}
document = ApplyFix(document, actions.ElementAt(0));
analyzerDiagnostics = CodeFixVerifier.GetSortedDiagnosticsFromDocuments(analyzer, new[] { document });
var newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document));
// Check if applying the code fix introduced any new compiler diagnostics
if (!allowNewCompilerDiagnostics && newCompilerDiagnostics.Any())
{
// Format and get the compiler diagnostics again so that the locations make sense in the output
document = document.WithSyntaxRoot(Formatter.Format(document.GetSyntaxRootAsync().Result, Formatter.Annotation, document.Project.Solution.Workspace));
newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document));
string message =
FormattableString.Invariant(
[email protected]"Fix introduced new compiler diagnostics:
{string.Join("\r\n", newCompilerDiagnostics.Select(d => d.ToString()))}
New document:
{document.GetSyntaxRootAsync().Result.ToFullString()}
");
Execute.Assertion.FailWith(message);
}
// Check if there are analyzer diagnostics left after the code fix
if (!analyzerDiagnostics.Any())
{
break;
}
}
// After applying all of the code fixes, compare the resulting string to the inputted one
var actual = GetStringFromDocument(document);
actual.Should().Be(newSource);
}
示例9: FixMultipleContext
private FixMultipleContext(
Project triggerProject,
CodeFixProvider codeFixProvider,
string codeActionEquivalenceKey,
ImmutableHashSet<string> diagnosticIds,
FixMultipleDiagnosticProvider diagnosticProvider,
CancellationToken cancellationToken)
: base(triggerProject, codeFixProvider, FixAllScope.Custom, codeActionEquivalenceKey, diagnosticIds, diagnosticProvider, cancellationToken)
{
_diagnosticProvider = diagnosticProvider;
}
示例10: Create
/// <summary>
/// Creates a new <see cref="FixMultipleContext"/>.
/// Use this overload when applying fix multiple diagnostics with a source location.
/// </summary>
/// <param name="diagnosticsToFix">Specific set of diagnostics to fix. Must be a non-empty set.</param>
/// <param name="codeFixProvider">Underlying <see cref="CodeFixes.CodeFixProvider"/> which triggered this fix all.</param>
/// <param name="codeActionEquivalenceKey">The <see cref="CodeAction.EquivalenceKey"/> value expected of a <see cref="CodeAction"/> participating in this fix all.</param>
/// <param name="cancellationToken">Cancellation token for fix all computation.</param>
public static FixMultipleContext Create(
ImmutableDictionary<Project, ImmutableArray<Diagnostic>> diagnosticsToFix,
CodeFixProvider codeFixProvider,
string codeActionEquivalenceKey,
CancellationToken cancellationToken)
{
var triggerProject = diagnosticsToFix.First().Key;
var diagnosticIds = GetDiagnosticsIds(diagnosticsToFix.Values);
var diagnosticProvider = new FixMultipleDiagnosticProvider(diagnosticsToFix);
return new FixMultipleContext(triggerProject, codeFixProvider, codeActionEquivalenceKey, diagnosticIds, diagnosticProvider, cancellationToken);
}
示例11: IsInternalCodeFixProvider
private static bool IsInternalCodeFixProvider(CodeFixProvider fixer)
{
var exportAttributes = fixer.GetType().GetCustomAttributes(typeof(ExportCodeFixProviderAttribute), false);
if (exportAttributes != null && exportAttributes.Length > 0)
{
var exportAttribute = (ExportCodeFixProviderAttribute)exportAttributes[0];
return s_predefinedCodeFixProviderNames.Contains(exportAttribute.Name);
}
return false;
}
示例12: IsInternalCodeFixProvider
private static bool IsInternalCodeFixProvider(CodeFixProvider fixer)
{
var exportAttributes = fixer.GetType().GetTypeInfo().GetCustomAttributes(typeof(ExportCodeFixProviderAttribute), false);
if (exportAttributes?.Any() == true)
{
var exportAttribute = (ExportCodeFixProviderAttribute)exportAttributes.First();
return s_predefinedCodeFixProviderNames.Contains(exportAttribute.Name);
}
return false;
}
示例13: FixAllContext
internal FixAllContext(
Project project,
CodeFixProvider codeFixProvider,
FixAllScope scope,
string codeActionId,
IEnumerable<string> diagnosticIds,
Func<Document, ImmutableHashSet<string>, CancellationToken, Task<IEnumerable<Diagnostic>>> getDocumentDiagnosticsAsync,
Func<Project, bool, ImmutableHashSet<string>, CancellationToken, Task<IEnumerable<Diagnostic>>> getProjectDiagnosticsAsync,
CancellationToken cancellationToken)
: this(null, project, codeFixProvider, scope, codeActionId, diagnosticIds, getDocumentDiagnosticsAsync, getProjectDiagnosticsAsync, cancellationToken)
{
}
示例14: GetCodeFixProvider
public CodeFixProvider GetCodeFixProvider ()
{
if (instance == null) {
try {
instance = (CodeFixProvider)Activator.CreateInstance (codeFixProviderType);
} catch (InvalidCastException) {
LoggingService.LogError (codeFixProviderType + " can't be cast to CodeFixProvider.");
throw;
}
}
return instance;
}
示例15: Create
internal static FixAllCodeActionContext Create(
Project project,
FixAllProviderInfo fixAllProviderInfo,
CodeFixProvider originalFixProvider,
IEnumerable<Diagnostic> originalFixDiagnostics,
Func<Document, ImmutableHashSet<string>, CancellationToken, Task<IEnumerable<Diagnostic>>> getDocumentDiagnosticsAsync,
Func<Project, bool, ImmutableHashSet<string>, CancellationToken, Task<IEnumerable<Diagnostic>>> getProjectDiagnosticsAsync,
CancellationToken cancellationToken)
{
var diagnosticIds = GetFixAllDiagnosticIds(fixAllProviderInfo, originalFixDiagnostics).ToImmutableHashSet();
var diagnosticProvider = new FixAllDiagnosticProvider(diagnosticIds, getDocumentDiagnosticsAsync, getProjectDiagnosticsAsync);
return new FixAllCodeActionContext(project, fixAllProviderInfo, originalFixProvider, originalFixDiagnostics, diagnosticIds, diagnosticProvider, cancellationToken);
}