本文整理汇总了C#中Microsoft.CodeAnalysis.Project.GetDocument方法的典型用法代码示例。如果您正苦于以下问题:C# Project.GetDocument方法的具体用法?C# Project.GetDocument怎么用?C# Project.GetDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.Project
的用法示例。
在下文中一共展示了Project.GetDocument方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessProject
private static Solution ProcessProject(Project project)
{
var solution = project.Solution;
foreach (var documentId in project.DocumentIds)
{
var document = project.GetDocument(documentId);
if (document.Name.EndsWith(".Designer.cs") || document.Name.EndsWith(".Designer.vb"))
{
continue;
}
var newDocument = Formatter.FormatAsync(document).GetAwaiter().GetResult();
if (newDocument != document && newDocument.GetTextAsync().Result.ToString() != document.GetTextAsync().Result.ToString())
{
Write("Formatting: " + document.FilePath);
}
var newDocument2 = RemoveConsecutiveEmptyLinesWorker.Process(newDocument);
if (newDocument2 != newDocument && newDocument2.GetTextAsync().Result.ToString() != newDocument.GetTextAsync().Result.ToString())
{
Write("Removing empty lines: " + document.FilePath);
}
project = newDocument2.Project;
solution = project.Solution;
}
return solution;
}
示例2: GetRelativePathInProject
public static string GetRelativePathInProject(SyntaxTree syntaxTree, Project project)
{
var document = project.GetDocument(syntaxTree);
return GetRelativeFilePathInProject(document);
}
示例3: RemoveNonExistingDocuments
private static Solution RemoveNonExistingDocuments(Project project)
{
foreach (var documentId in project.DocumentIds.ToArray())
{
var document = project.GetDocument(documentId);
if (!File.Exists(document.FilePath))
{
Log.Message("Document doesn't exist on disk: " + document.FilePath);
project = project.RemoveDocument(documentId);
}
}
return project.Solution;
}
示例4: FixAllAnalyerDiagnosticsInScopeAsync
private static async Task<Project> FixAllAnalyerDiagnosticsInScopeAsync(FixAllScope scope, ImmutableArray<DiagnosticAnalyzer> analyzers, CodeFixProvider codeFixProvider, int? codeFixIndex, Project project, int numberOfIterations, CancellationToken cancellationToken)
{
int expectedNumberOfIterations = numberOfIterations;
if (numberOfIterations < 0)
{
numberOfIterations = -numberOfIterations;
}
var previousDiagnostics = ImmutableArray.Create<Diagnostic>();
var fixAllProvider = codeFixProvider.GetFixAllProvider();
if (fixAllProvider == null)
{
return null;
}
bool done;
do
{
var analyzerDiagnostics = await GetSortedDiagnosticsFromDocumentsAsync(analyzers, project.Documents.ToArray(), cancellationToken).ConfigureAwait(false);
if (analyzerDiagnostics.Length == 0)
{
break;
}
if (!AreDiagnosticsDifferent(analyzerDiagnostics, previousDiagnostics))
{
break;
}
if (--numberOfIterations < 0)
{
Assert.True(false, "The upper limit for the number of fix all iterations was exceeded");
}
Diagnostic firstDiagnostic = null;
string equivalenceKey = null;
foreach (var diagnostic in analyzerDiagnostics)
{
if (!codeFixProvider.FixableDiagnosticIds.Contains(diagnostic.Id))
{
// do not pass unsupported diagnostics to a code fix provider
continue;
}
var actions = new List<CodeAction>();
var context = new CodeFixContext(project.GetDocument(diagnostic.Location.SourceTree), diagnostic, (a, d) => actions.Add(a), cancellationToken);
await codeFixProvider.RegisterCodeFixesAsync(context).ConfigureAwait(false);
if (actions.Count > (codeFixIndex ?? 0))
{
firstDiagnostic = diagnostic;
equivalenceKey = actions[codeFixIndex ?? 0].EquivalenceKey;
break;
}
}
if (firstDiagnostic == null)
{
return project;
}
previousDiagnostics = analyzerDiagnostics;
done = true;
FixAllContext.DiagnosticProvider fixAllDiagnosticProvider = TestDiagnosticProvider.Create(analyzerDiagnostics);
IEnumerable<string> analyzerDiagnosticIds = analyzers.SelectMany(x => x.SupportedDiagnostics).Select(x => x.Id);
IEnumerable<string> compilerDiagnosticIds = codeFixProvider.FixableDiagnosticIds.Where(x => x.StartsWith("CS", StringComparison.Ordinal));
IEnumerable<string> disabledDiagnosticIds = project.CompilationOptions.SpecificDiagnosticOptions.Where(x => x.Value == ReportDiagnostic.Suppress).Select(x => x.Key);
IEnumerable<string> relevantIds = analyzerDiagnosticIds.Concat(compilerDiagnosticIds).Except(disabledDiagnosticIds).Distinct();
FixAllContext fixAllContext = new FixAllContext(project.GetDocument(firstDiagnostic.Location.SourceTree), codeFixProvider, scope, equivalenceKey, relevantIds, fixAllDiagnosticProvider, cancellationToken);
CodeAction action = await fixAllProvider.GetFixAsync(fixAllContext).ConfigureAwait(false);
if (action == null)
{
return project;
}
var fixedProject = await ApplyFixAsync(project, action, cancellationToken).ConfigureAwait(false);
if (fixedProject != project)
{
done = false;
project = await RecreateProjectDocumentsAsync(fixedProject, cancellationToken).ConfigureAwait(false);
}
}
while (!done);
if (expectedNumberOfIterations >= 0)
{
Assert.Equal($"{expectedNumberOfIterations} iterations", $"{expectedNumberOfIterations - numberOfIterations} iterations");
}
return project;
}
示例5: FixEachAnalyzerDiagnosticAsync
private static async Task<Project> FixEachAnalyzerDiagnosticAsync(ImmutableArray<DiagnosticAnalyzer> analyzers, CodeFixProvider codeFixProvider, int? codeFixIndex, Project project, int numberOfIterations, CancellationToken cancellationToken)
{
int expectedNumberOfIterations = numberOfIterations;
if (numberOfIterations < 0)
{
numberOfIterations = -numberOfIterations;
}
var previousDiagnostics = ImmutableArray.Create<Diagnostic>();
bool done;
do
{
var analyzerDiagnostics = await GetSortedDiagnosticsFromDocumentsAsync(analyzers, project.Documents.ToArray(), cancellationToken).ConfigureAwait(false);
if (analyzerDiagnostics.Length == 0)
{
break;
}
if (!AreDiagnosticsDifferent(analyzerDiagnostics, previousDiagnostics))
{
break;
}
if (--numberOfIterations < 0)
{
Assert.True(false, "The upper limit for the number of code fix iterations was exceeded");
}
previousDiagnostics = analyzerDiagnostics;
done = true;
foreach (var diagnostic in analyzerDiagnostics)
{
if (!codeFixProvider.FixableDiagnosticIds.Contains(diagnostic.Id))
{
// do not pass unsupported diagnostics to a code fix provider
continue;
}
var actions = new List<CodeAction>();
var context = new CodeFixContext(project.GetDocument(diagnostic.Location.SourceTree), diagnostic, (a, d) => actions.Add(a), cancellationToken);
await codeFixProvider.RegisterCodeFixesAsync(context).ConfigureAwait(false);
if (actions.Count > 0)
{
var fixedProject = await ApplyFixAsync(project, actions.ElementAt(codeFixIndex.GetValueOrDefault(0)), cancellationToken).ConfigureAwait(false);
if (fixedProject != project)
{
done = false;
project = await RecreateProjectDocumentsAsync(fixedProject, cancellationToken).ConfigureAwait(false);
break;
}
}
}
}
while (!done);
if (expectedNumberOfIterations >= 0)
{
Assert.Equal($"{expectedNumberOfIterations} iterations", $"{expectedNumberOfIterations - numberOfIterations} iterations");
}
return project;
}
示例6: MakeContractsClassFor
private static Compilation MakeContractsClassFor(Project project, Compilation original, string interfacename, string filename, string parameters)
{
#region CodeContracts
Contract.Requires(project != null);
Contract.Requires(original != null);
Contract.Requires(interfacename != null);
Contract.Requires(filename != null);
Contract.Ensures(Contract.Result<Compilation>() != null);
#endregion CodeContracts
var st = original.SyntaxTrees.Where(x => x.FilePath.Equals(filename, StringComparison.OrdinalIgnoreCase)).First();
var doc = project.GetDocument(st);
var sm = original.GetSemanticModel(st);
var node = st.GetRoot().DescendantNodes().Where(x => x.CSharpKind() == SyntaxKind.InterfaceDeclaration
&& sm.GetDeclaredSymbol(x).GetDocumentationCommentId().Equals(interfacename)).First() as InterfaceDeclarationSyntax;
var parent = node.Parent;
var newclass = MakeContractsClassForNode(interfacename, parameters);
var mem = newclass as MemberDeclarationSyntax;
SyntaxNode newnode = null;
if (parent.CSharpKind() == SyntaxKind.ClassDeclaration)
{
var classdecl = parent as ClassDeclarationSyntax;
var newdecl = classdecl.AddMembers(mem);
newnode = parent.Parent.ReplaceNode(parent, newdecl);
}
if (parent.CSharpKind() == SyntaxKind.NamespaceDeclaration)
{
var namedecl = parent as NamespaceDeclarationSyntax;
var newdecl = namedecl.AddMembers(mem);
newnode = parent.Parent.ReplaceNode(parent, newdecl);
}
var newst = CSharpSyntaxTree.Create(newnode.SyntaxTree.GetRoot() as CSharpSyntaxNode, null, st.FilePath, null);
return original.ReplaceSyntaxTree(st, newst);
}
示例7: AddContractsClassAttributeToInterface
private static Compilation AddContractsClassAttributeToInterface(Project project, Compilation original, string filename, string interfacename, string parameterstring)
{
#region CodeContracts
Contract.Requires(project != null);
Contract.Requires(original != null);
Contract.Requires(filename != null);
Contract.Requires(interfacename != null);
Contract.Ensures(Contract.Result<Compilation>() != null);
#endregion CodeContracts
var interfaceShortName = InterfaceAnnotationHelpers.GetUnqualifiedName(interfacename);
var st = original.SyntaxTrees.Where(x => x.FilePath.Equals(filename, StringComparison.OrdinalIgnoreCase)).First();
var doc = project.GetDocument(st);
var sm = original.GetSemanticModel(st);
var node = st.GetRoot().DescendantNodes().Where(x => x.CSharpKind() == SyntaxKind.InterfaceDeclaration
&& sm.GetDeclaredSymbol(x).GetDocumentationCommentId().Equals(interfacename)).First() as InterfaceDeclarationSyntax;
Contract.Assert(node != null);
var parameters = "";
var numberparams = GetNumberParameters(parameterstring);
if (numberparams > 0)
{
parameters = "<";
var commas = new String(',', numberparams - 1);
parameters += commas;
parameters += ">";
}
var attr_name = SyntaxFactory.ParseName("ContractClass");
var attr_args = SyntaxFactory.ParseAttributeArgumentList(String.Format("(typeof({0}Contracts{1}))", interfaceShortName, parameters));
var attr = SyntaxFactory.Attribute(attr_name, attr_args);
var attributes = SyntaxFactory.SeparatedList<AttributeSyntax>().Add(attr);
var attr_list = SyntaxFactory.AttributeList(attributes);
var newnode = node.AddAttributeLists(attr_list) as SyntaxNode;
newnode = node.SyntaxTree.GetRoot().ReplaceNode(node, newnode);
var newst = CSharpSyntaxTree.Create(newnode.SyntaxTree.GetRoot() as CSharpSyntaxNode, null, st.FilePath, null);
return original.ReplaceSyntaxTree(st, newst);
}
示例8: ReformatProjectDocumentsAsync
/// <summary>
/// Formats the whitespace in all documents of the specified <see cref="Project"/>.
/// </summary>
/// <param name="project">The project to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated <see cref="Project"/>.</returns>
private static async Task<Project> ReformatProjectDocumentsAsync(Project project, CancellationToken cancellationToken)
{
foreach (var documentId in project.DocumentIds)
{
var document = project.GetDocument(documentId);
document = await Formatter.FormatAsync(document, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
project = document.Project;
}
return project;
}
示例9: RecreateProjectDocumentsAsync
/// <summary>
/// Implements a workaround for issue #936, force re-parsing to get the same sort of syntax tree as the original document.
/// </summary>
/// <param name="project">The project to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated <see cref="Project"/>.</returns>
private static async Task<Project> RecreateProjectDocumentsAsync(Project project, CancellationToken cancellationToken)
{
foreach (var documentId in project.DocumentIds)
{
var document = project.GetDocument(documentId);
document = await RecreateDocumentAsync(document, cancellationToken).ConfigureAwait(false);
project = document.Project;
}
return project;
}