本文整理汇总了C#中Microsoft.CodeAnalysis.Document.GetLinkedDocumentIds方法的典型用法代码示例。如果您正苦于以下问题:C# Document.GetLinkedDocumentIds方法的具体用法?C# Document.GetLinkedDocumentIds怎么用?C# Document.GetLinkedDocumentIds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.Document
的用法示例。
在下文中一共展示了Document.GetLinkedDocumentIds方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetTransformedSolutionAsync
private static async Task<Solution> GetTransformedSolutionAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var solution = document.Project.Solution;
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var expectedFileName = diagnostic.Properties[SA1649FileNameMustMatchTypeName.ExpectedFileNameKey];
var newPath = document.FilePath != null ? Path.Combine(Path.GetDirectoryName(document.FilePath), expectedFileName) : null;
var newDocumentId = DocumentId.CreateNewId(document.Id.ProjectId);
var newSolution = solution
.RemoveDocument(document.Id)
.AddDocument(newDocumentId, expectedFileName, syntaxRoot, document.Folders, newPath);
// Make sure to also add the file to linked projects
foreach (var linkedDocumentId in document.GetLinkedDocumentIds())
{
DocumentId linkedExtractedDocumentId = DocumentId.CreateNewId(linkedDocumentId.ProjectId);
newSolution = newSolution.AddDocument(linkedExtractedDocumentId, expectedFileName, syntaxRoot, document.Folders);
}
return newSolution;
}
示例2: GetTransformedSolutionAsync
private static async Task<Solution> GetTransformedSolutionAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
SyntaxNode node = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
BaseTypeDeclarationSyntax baseTypeDeclarationSyntax = node as BaseTypeDeclarationSyntax;
DelegateDeclarationSyntax delegateDeclarationSyntax = node as DelegateDeclarationSyntax;
if (baseTypeDeclarationSyntax == null && delegateDeclarationSyntax == null)
{
return document.Project.Solution;
}
DocumentId extractedDocumentId = DocumentId.CreateNewId(document.Project.Id);
string extractedDocumentName = baseTypeDeclarationSyntax.Identifier.ValueText;
if (baseTypeDeclarationSyntax != null)
{
TypeDeclarationSyntax typeDeclarationSyntax = baseTypeDeclarationSyntax as TypeDeclarationSyntax;
if (typeDeclarationSyntax?.TypeParameterList?.Parameters.Count > 0)
{
extractedDocumentName += "`" + typeDeclarationSyntax.TypeParameterList.Parameters.Count;
}
}
else
{
if (delegateDeclarationSyntax.TypeParameterList?.Parameters.Count > 0)
{
extractedDocumentName += "`" + delegateDeclarationSyntax.TypeParameterList.Parameters.Count;
}
}
extractedDocumentName += ".cs";
List<SyntaxNode> nodesToRemoveFromExtracted = new List<SyntaxNode>();
SyntaxNode previous = node;
for (SyntaxNode current = node.Parent; current != null; previous = current, current = current.Parent)
{
foreach (SyntaxNode child in current.ChildNodes())
{
if (child == previous)
{
continue;
}
switch (child.Kind())
{
case SyntaxKind.NamespaceDeclaration:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.StructDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.DelegateDeclaration:
nodesToRemoveFromExtracted.Add(child);
break;
default:
break;
}
}
}
SyntaxNode extractedDocumentNode = root.RemoveNodes(nodesToRemoveFromExtracted, SyntaxRemoveOptions.KeepUnbalancedDirectives);
Solution updatedSolution = document.Project.Solution.AddDocument(extractedDocumentId, extractedDocumentName, extractedDocumentNode, document.Folders);
// Make sure to also add the file to linked projects
foreach (var linkedDocumentId in document.GetLinkedDocumentIds())
{
DocumentId linkedExtractedDocumentId = DocumentId.CreateNewId(linkedDocumentId.ProjectId);
updatedSolution = updatedSolution.AddDocument(linkedExtractedDocumentId, extractedDocumentName, extractedDocumentNode, document.Folders);
}
// Remove the type from its original location
updatedSolution = updatedSolution.WithDocumentSyntaxRoot(document.Id, root.RemoveNode(node, SyntaxRemoveOptions.KeepUnbalancedDirectives));
return updatedSolution;
}
示例3: EncapsulateFieldAsync
private async Task<Result> EncapsulateFieldAsync(IFieldSymbol field, Document document, bool updateReferences, CancellationToken cancellationToken)
{
var originalField = field;
var finalNames = GeneratePropertyAndFieldNames(field);
var finalFieldName = finalNames.Item1;
var generatedPropertyName = finalNames.Item2;
// Annotate the field declarations so we can find it after rename.
var fieldDeclaration = field.DeclaringSyntaxReferences.First();
var declarationAnnotation = new SyntaxAnnotation();
document = document.WithSyntaxRoot(fieldDeclaration.SyntaxTree.GetRoot(cancellationToken).ReplaceNode(fieldDeclaration.GetSyntax(cancellationToken),
fieldDeclaration.GetSyntax(cancellationToken).WithAdditionalAnnotations(declarationAnnotation)));
var solution = document.Project.Solution;
foreach (var linkedDocumentId in document.GetLinkedDocumentIds())
{
var linkedDocument = solution.GetDocument(linkedDocumentId);
var linkedRoot = await linkedDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var linkedFieldNode = linkedRoot.FindNode(fieldDeclaration.Span);
if (linkedFieldNode.Span != fieldDeclaration.Span)
{
continue;
}
var updatedRoot = linkedRoot.ReplaceNode(linkedFieldNode, linkedFieldNode.WithAdditionalAnnotations(declarationAnnotation));
solution = solution.WithDocumentSyntaxRoot(linkedDocumentId, updatedRoot);
}
document = solution.GetDocument(document.Id);
// Resolve the annotated symbol and prepare for rename.
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var compilation = semanticModel.Compilation;
field = field.GetSymbolKey().Resolve(compilation, cancellationToken: cancellationToken).Symbol as IFieldSymbol;
var solutionNeedingProperty = solution;
// We couldn't resolve field after annotating its declaration. Bail
if (field == null)
{
return null;
}
solutionNeedingProperty = await UpdateReferencesAsync(
updateReferences, solution, document, field, finalFieldName, generatedPropertyName, cancellationToken).ConfigureAwait(false);
document = solutionNeedingProperty.GetDocument(document.Id);
var markFieldPrivate = field.DeclaredAccessibility != Accessibility.Private;
var rewrittenFieldDeclaration = await RewriteFieldNameAndAccessibility(finalFieldName, markFieldPrivate, document, declarationAnnotation, cancellationToken).ConfigureAwait(false);
document = await Formatter.FormatAsync(document.WithSyntaxRoot(rewrittenFieldDeclaration), Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
solution = document.Project.Solution;
foreach (var linkedDocumentId in document.GetLinkedDocumentIds())
{
var linkedDocument = solution.GetDocument(linkedDocumentId);
var updatedLinkedRoot = await RewriteFieldNameAndAccessibility(finalFieldName, markFieldPrivate, linkedDocument, declarationAnnotation, cancellationToken).ConfigureAwait(false);
var updatedLinkedDocument = await Formatter.FormatAsync(linkedDocument.WithSyntaxRoot(updatedLinkedRoot), Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
solution = updatedLinkedDocument.Project.Solution;
}
document = solution.GetDocument(document.Id);
semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
compilation = semanticModel.Compilation;
var newRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var newDeclaration = newRoot.GetAnnotatedNodes<SyntaxNode>(declarationAnnotation).First();
field = semanticModel.GetDeclaredSymbol(newDeclaration, cancellationToken) as IFieldSymbol;
var generatedProperty = GenerateProperty(generatedPropertyName, finalFieldName, originalField.DeclaredAccessibility, originalField, field.ContainingType, new SyntaxAnnotation(), document, cancellationToken);
var solutionWithProperty = await AddPropertyAsync(document, document.Project.Solution, field, generatedProperty, cancellationToken).ConfigureAwait(false);
return new Result(solutionWithProperty, originalField.ToDisplayString(), Glyph.FieldPublic);
}
示例4: BuildContentAsync
protected override async Task<QuickInfoContent> BuildContentAsync(
Document document,
SyntaxToken token,
CancellationToken cancellationToken)
{
var linkedDocumentIds = document.GetLinkedDocumentIds();
var modelAndSymbols = await this.BindTokenAsync(document, token, cancellationToken).ConfigureAwait(false);
if ((modelAndSymbols.Item2 == null || modelAndSymbols.Item2.Count == 0) && !linkedDocumentIds.Any())
{
return null;
}
if (!linkedDocumentIds.Any())
{
return await CreateContentAsync(document.Project.Solution.Workspace,
token,
modelAndSymbols.Item1,
modelAndSymbols.Item2,
supportedPlatforms: null,
cancellationToken: cancellationToken).ConfigureAwait(false);
}
// Linked files/shared projects: imagine the following when FOO is false
// #if FOO
// int x = 3;
// #endif
// var y = x$$;
//
// 'x' will bind as an error type, so we'll show incorrect information.
// Instead, we need to find the head in which we get the best binding,
// which in this case is the one with no errors.
var candidateProjects = new List<ProjectId>() { document.Project.Id };
var invalidProjects = new List<ProjectId>();
var candidateResults = new List<Tuple<DocumentId, SemanticModel, IList<ISymbol>>>();
candidateResults.Add(Tuple.Create(document.Id, modelAndSymbols.Item1, modelAndSymbols.Item2));
foreach (var link in linkedDocumentIds)
{
var linkedDocument = document.Project.Solution.GetDocument(link);
var linkedToken = await FindTokenInLinkedDocument(token, linkedDocument, cancellationToken).ConfigureAwait(false);
if (linkedToken != default(SyntaxToken))
{
// Not in an inactive region, so this file is a candidate.
candidateProjects.Add(link.ProjectId);
var linkedModelAndSymbols = await this.BindTokenAsync(linkedDocument, linkedToken, cancellationToken).ConfigureAwait(false);
candidateResults.Add(Tuple.Create(link, linkedModelAndSymbols.Item1, linkedModelAndSymbols.Item2));
}
}
// Take the first result with no errors.
var bestBinding = candidateResults.FirstOrDefault(c => c.Item3.Count > 0 && !ErrorVisitor.ContainsError(c.Item3.FirstOrDefault()));
// Every file binds with errors. Take the first candidate, which is from the current file.
if (bestBinding == null)
{
bestBinding = candidateResults.First();
}
if (bestBinding.Item3 == null || !bestBinding.Item3.Any())
{
return null;
}
// We calculate the set of supported projects
candidateResults.Remove(bestBinding);
foreach (var candidate in candidateResults)
{
// Does the candidate have anything remotely equivalent?
if (!candidate.Item3.Intersect(bestBinding.Item3, LinkedFilesSymbolEquivalenceComparer.Instance).Any())
{
invalidProjects.Add(candidate.Item1.ProjectId);
}
}
var supportedPlatforms = new SupportedPlatformData(invalidProjects, candidateProjects, document.Project.Solution.Workspace);
return await CreateContentAsync(document.Project.Solution.Workspace, token, bestBinding.Item2, bestBinding.Item3, supportedPlatforms, cancellationToken).ConfigureAwait(false);
}