当前位置: 首页>>代码示例>>C#>>正文


C# Solution.GetDocument方法代码示例

本文整理汇总了C#中Microsoft.CodeAnalysis.Solution.GetDocument方法的典型用法代码示例。如果您正苦于以下问题:C# Solution.GetDocument方法的具体用法?C# Solution.GetDocument怎么用?C# Solution.GetDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.CodeAnalysis.Solution的用法示例。


在下文中一共展示了Solution.GetDocument方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RenameFields

            private async Task<Solution> RenameFields(Solution solution, DocumentId documentId, int count, CancellationToken cancellationToken)
            {
                Solution oldSolution = null;
                for (int i = 0; i < count; i++)
                {
                    oldSolution = solution;

                    var semanticModel = await solution.GetDocument(documentId).GetSemanticModelAsync(cancellationToken);
                    var root = await semanticModel.SyntaxTree.GetRootAsync(cancellationToken);
                    var declaration = root.GetAnnotatedNodes(s_markerAnnotation).ElementAt(i);

                    // Make note, VB represents "fields" marked as "WithEvents" as properties, so don't be
                    // tempted to treat this as a IFieldSymbol. We only need the name, so ISymbol is enough.
                    var fieldSymbol = semanticModel.GetDeclaredSymbol(declaration, cancellationToken);
                    var newName = GetNewFieldName(fieldSymbol);

                    // Can happen with pathologically bad field names like _
                    if (newName == fieldSymbol.Name)
                    {
                        continue;
                    }

                    solution = await Renamer.RenameSymbolAsync(solution, fieldSymbol, newName, solution.Workspace.Options, cancellationToken).ConfigureAwait(false);
                    solution = await CleanSolutionAsync(solution, oldSolution, cancellationToken);
                }

                return solution;
            }
开发者ID:patricksadowski,项目名称:codeformatter,代码行数:28,代码来源:PrivateFieldNamingRule.cs

示例2: RenameFields

            private async Task<Solution> RenameFields(Solution solution, DocumentId documentId, int count, CancellationToken cancellationToken)
            {
                Solution oldSolution = null;
                for (int i = 0; i < count; i++)
                {
                    oldSolution = solution;

                    var semanticModel = await solution.GetDocument(documentId).GetSemanticModelAsync(cancellationToken);
                    var root = await semanticModel.SyntaxTree.GetRootAsync(cancellationToken);
                    var declaration = root.GetAnnotatedNodes(s_markerAnnotation).ElementAt(i);
                    var fieldSymbol = (IFieldSymbol)semanticModel.GetDeclaredSymbol(declaration, cancellationToken);
                    var newName = GetNewFieldName(fieldSymbol);

                    // Can happen with pathologically bad field names like _
                    if (newName == fieldSymbol.Name)
                    {
                        continue;
                    }

                    solution = await Renamer.RenameSymbolAsync(solution, fieldSymbol, newName, solution.Workspace.Options, cancellationToken).ConfigureAwait(false);
                    solution = await CleanSolutionAsync(solution, oldSolution, cancellationToken);
                }

                return solution;
            }
开发者ID:jimdeselms,项目名称:codeformatter,代码行数:25,代码来源:PrivateFieldNamingRule.cs

示例3: 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;
        }
开发者ID:robinsedlaczek,项目名称:StyleCopAnalyzers,代码行数:8,代码来源:CodeFixEquivalenceGroup.cs

示例4: ChangeTypeAccessibilityInDocumentAsync

        private static async Task<Document> ChangeTypeAccessibilityInDocumentAsync(Solution solution, SyntaxTokenList newAccessibilityModifiers, Location typeLocation, CancellationToken cancellationToken)
        {
            var document = solution.GetDocument(typeLocation.SourceTree);
            var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var newRoot = ChangeTypeAccessibilityInSyntaxRoot(syntaxRoot, newAccessibilityModifiers, typeLocation, cancellationToken);

            return document.WithSyntaxRoot(newRoot);
        }
开发者ID:JeanLLopes,项目名称:code-cracker,代码行数:9,代码来源:InconsistentAccessibilityCodeFixProvider.cs

示例5: FixDocumentAsync

        private static async Task<Solution> FixDocumentAsync(Solution solution, DocumentId documentId, ImmutableArray<Diagnostic> diagnostics, string codeActionEquivalenceKey, CancellationToken cancellationToken)
        {
            if (diagnostics.IsEmpty)
            {
                return solution;
            }

            string equivalenceKey = nameof(SA1412CodeFixProvider) + "." + diagnostics[0].Properties[SA1412StoreFilesAsUtf8.EncodingProperty];
            if (codeActionEquivalenceKey != equivalenceKey)
            {
                return solution;
            }

            Document document = solution.GetDocument(documentId);
            return await SA1412CodeFixProvider.GetTransformedSolutionAsync(document, cancellationToken).ConfigureAwait(false);
        }
开发者ID:Romanx,项目名称:StyleCopAnalyzers,代码行数:16,代码来源:SA1412FixAllProvider.cs

示例6: GetPreferredSourceLocations

		public static IEnumerable<Location> GetPreferredSourceLocations(Solution solution, ISymbol symbol)
		{
			// Prefer non-generated source locations over generated ones.

			var sourceLocations = GetPreferredSourceLocations(symbol);

			var candidateLocationGroups = from c in sourceLocations
				let doc = solution.GetDocument(c.SourceTree)
					where doc != null
				group c by IsFileNameForGeneratedCode(doc.FilePath);

			var generatedSourceLocations = candidateLocationGroups.SingleOrDefault(g => g.Key) ?? SpecializedCollections.EmptyEnumerable<Location>();
			var nonGeneratedSourceLocations = candidateLocationGroups.SingleOrDefault(g => !g.Key) ?? SpecializedCollections.EmptyEnumerable<Location>();

			return nonGeneratedSourceLocations.Any() ? nonGeneratedSourceLocations : generatedSourceLocations;
		}
开发者ID:pabloescribanoloza,项目名称:monodevelop,代码行数:16,代码来源:GeneratedCodeRecognitionService.cs

示例7: ReplaceNodesWithLiteralAsync

        public static async Task<Solution> ReplaceNodesWithLiteralAsync(Solution solution, IEnumerable<SyntaxNodeOrToken> nodes, Func<SyntaxNodeOrToken, string> computeReplacement, CancellationToken cancellationToken)
        {
            var groupedByDocument = nodes.GroupBy(n => solution.GetDocument(n.SyntaxTree));

            var updatedSolution = solution;
            foreach (var nodesForDocument in groupedByDocument)
            {
                var document = nodesForDocument.Key;
                var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken);

                var changes = nodesForDocument.Select(node => new TextChange(node.Span, computeReplacement(node)));
                var newText = syntaxRoot.GetText().WithChanges(changes);
                updatedSolution = updatedSolution.WithDocumentText(document.Id, newText);
            }

            return updatedSolution;
        }
开发者ID:FrankBakkerNl,项目名称:IntelliFind,代码行数:17,代码来源:ReplaceHelpers.cs

示例8: Format

        private async Task<Solution> Format(Solution solution, bool runFormatter)
        {
            var documentIds = solution.Projects.SelectMany(p => p.DocumentIds);

            foreach (var id in documentIds)
            {
                var document = solution.GetDocument(id);
                document = await RewriteDocumentAsync(document);
                if (runFormatter)
                {
                    document = await Formatter.FormatAsync(document);
                }

                solution = document.Project.Solution;
            }

            return solution;
        }
开发者ID:chuck-mitchell,项目名称:codeformatter,代码行数:18,代码来源:CodeFormattingTestBase.cs

示例9: GetCalledMethodSymbolsAsync

        private static async Task<IEnumerable<ISymbol>> GetCalledMethodSymbolsAsync(ISymbol symbol, Solution solution, CancellationToken cancellationToken)
        {
            var symbols = new List<ISymbol>();

            foreach (var reference in symbol.DeclaringSyntaxReferences)
            {
                var semanticModel = await solution.GetDocument(reference.SyntaxTree).GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
                foreach (var syntaxNode in reference.GetSyntax(cancellationToken).DescendantNodes())
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var newSymbol = semanticModel.GetSymbolInfo(syntaxNode, cancellationToken).Symbol;
                    if (newSymbol != null && newSymbol is IMethodSymbol &&
                        (newSymbol.CanBeReferencedByName || ((IMethodSymbol)newSymbol).MethodKind == MethodKind.Constructor))
                    {
                        symbols.Add(newSymbol);
                    }
                }
            }

            return symbols;
        }
开发者ID:Rickinio,项目名称:roslyn,代码行数:22,代码来源:CallsGraphQuery.cs

示例10: CleanSolutionDocument

            private async Task<Solution> CleanSolutionDocument(Solution solution, DocumentId documentId, CancellationToken cancellationToken)
            {
                var document = solution.GetDocument(documentId);
                var syntaxNode = await document.GetSyntaxRootAsync(cancellationToken);
                if (syntaxNode == null)
                {
                    return solution;
                }

                var newNode = RemoveRenameAnnotations(syntaxNode);
                return solution.WithDocumentSyntaxRoot(documentId, newNode);
            }
开发者ID:jimdeselms,项目名称:codeformatter,代码行数:12,代码来源:PrivateFieldNamingRule.cs

示例11: GetObservedSyntaxTreeRootAsync

 private ObjectReference GetObservedSyntaxTreeRootAsync(Solution solution, DocumentId documentId)
 {
     var observedTree = solution.GetDocument(documentId).GetSyntaxRootAsync().Result;
     return new ObjectReference(observedTree);
 }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:5,代码来源:SolutionTests.cs

示例12: GetObservedTextAsync

        private ObjectReference GetObservedTextAsync(Solution solution, DocumentId documentId, string expectedText = null)
        {
            var observedText = solution.GetDocument(documentId).GetTextAsync().Result;

            if (expectedText != null)
            {
                Assert.Equal(expectedText, observedText.ToString());
            }

            return new ObjectReference(observedText);
        }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:11,代码来源:SolutionTests.cs

示例13: TestRecoverableSyntaxTree

        private void TestRecoverableSyntaxTree(Solution sol, DocumentId did)
        {
            // get it async and wait for it to get GC'd
            var observed = GetObservedSyntaxTreeRootAsync(sol, did);
            StopObservingAndWaitForReferenceToGo(observed);

            var doc = sol.GetDocument(did);

            // access the tree & root again (recover it)
            var tree = doc.GetSyntaxTreeAsync().Result;

            // this should cause reparsing
            var root = tree.GetRoot();

            // prove that the new root is correctly associated with the tree
            Assert.Equal(tree, root.SyntaxTree);

            // reset the syntax root, to make it 'refactored' by adding an attribute
            var newRoot = doc.GetSyntaxRootAsync().Result.WithAdditionalAnnotations(SyntaxAnnotation.ElasticAnnotation);
            var doc2 = doc.Project.Solution.WithDocumentSyntaxRoot(doc.Id, newRoot, PreservationMode.PreserveValue).GetDocument(doc.Id);

            // get it async and wait for it to get GC'd
            var observed2 = GetObservedSyntaxTreeRootAsync(doc2.Project.Solution, did);
            StopObservingAndWaitForReferenceToGo(observed2);

            // access the tree & root again (recover it)
            var tree2 = doc2.GetSyntaxTreeAsync().Result;

            // this should cause deserialization
            var root2 = tree2.GetRoot();

            // prove that the new root is correctly associated with the tree
            Assert.Equal(tree2, root2.SyntaxTree);
        }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:34,代码来源:SolutionTests.cs

示例14: GetDocumentIdClassDeclarationMapping

        private static Dictionary<DocumentId, List<ClassDeclarationSyntax>> GetDocumentIdClassDeclarationMapping(
            List<ClassDeclarationSyntax> classDeclarations, Solution currentSolution)
        {
            var mapping = new Dictionary<DocumentId, List<ClassDeclarationSyntax>>();
            foreach (var classDeclaration in classDeclarations)
            {
                var documentId = currentSolution.GetDocument(classDeclaration.SyntaxTree).Id;
                if (!mapping.ContainsKey(documentId))
                {
                    mapping.Add(documentId, new List<ClassDeclarationSyntax>());
                }
                mapping[documentId].Add(classDeclaration);
            }

            return mapping;
        }
开发者ID:Godin,项目名称:sonarlint-vs,代码行数:16,代码来源:GenericReadonlyFieldPropertyAssignmentCodeFixProvider.cs

示例15: TryStartRenameSession

			async void TryStartRenameSession (Workspace workspace, Solution oldSolution, Solution newSolution, CancellationToken cancellationToken)
			{
				var changedDocuments = GetChangedDocuments (newSolution, oldSolution);
				foreach (var documentId in changedDocuments) {
					var document = newSolution.GetDocument (documentId);
					var root = await document.GetSyntaxRootAsync (cancellationToken).ConfigureAwait (false);
					SyntaxToken? renameTokenOpt = root.GetAnnotatedNodesAndTokens (RenameAnnotation.Kind)
					                                  .Where (s => s.IsToken)
					                                  .Select (s => s.AsToken ())
					                                  .Cast<SyntaxToken?> ()
					                                  .FirstOrDefault ();
					if (renameTokenOpt.HasValue) {
						var latestDocument = workspace.CurrentSolution.GetDocument (documentId);
						var latestModel = await latestDocument.GetSemanticModelAsync (cancellationToken).ConfigureAwait (false);
						var latestRoot = await latestDocument.GetSyntaxRootAsync (cancellationToken).ConfigureAwait (false);
						Application.Invoke (delegate {
							try {
								var node = latestRoot.FindNode (renameTokenOpt.Value.Parent.Span, false, false);
								if (node == null)
									return;
								var info = latestModel.GetSymbolInfo (node);
								var sym = info.Symbol ?? latestModel.GetDeclaredSymbol (node);
								if (sym != null) 
									new MonoDevelop.Refactoring.Rename.RenameRefactoring ().Rename (sym);
							} catch (Exception ex) {
								LoggingService.LogError ("Error while renaming " + renameTokenOpt.Value.Parent, ex);
							}
						});
						return;
					}
				}
			}
开发者ID:zenek-y,项目名称:monodevelop,代码行数:32,代码来源:CodeActionEditorExtension.cs


注:本文中的Microsoft.CodeAnalysis.Solution.GetDocument方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。