コード例 #1
0
        public async Task <QuickFixResponse> Handle(FindUsagesRequest request)
        {
            var document = _workspace.GetDocument(request.FileName);
            var response = new QuickFixResponse();

            if (document != null)
            {
                var locations     = new List <Location>();
                var semanticModel = await document.GetSemanticModelAsync();

                var sourceText = await document.GetTextAsync();

                var position = sourceText.Lines.GetPosition(new LinePosition(request.Line, request.Column));
                var symbol   = await SymbolFinder.FindSymbolAtPositionAsync(semanticModel, position, _workspace);

                var definition = await SymbolFinder.FindSourceDefinitionAsync(symbol, _workspace.CurrentSolution);

                var usages = request.OnlyThisFile
                    ? await SymbolFinder.FindReferencesAsync(definition ?? symbol, _workspace.CurrentSolution, ImmutableHashSet.Create(document))
                    : await SymbolFinder.FindReferencesAsync(definition ?? symbol, _workspace.CurrentSolution);

                foreach (var usage in usages.Where(u => u.Definition.CanBeReferencedByName || (symbol as IMethodSymbol)?.MethodKind == MethodKind.Constructor))
                {
                    foreach (var location in usage.Locations)
                    {
                        locations.Add(location.Location);
                    }

                    if (!request.ExcludeDefinition)
                    {
                        var definitionLocations = usage.Definition.Locations
                                                  .Where(loc => loc.IsInSource && (!request.OnlyThisFile || loc.SourceTree.FilePath == request.FileName));

                        foreach (var location in definitionLocations)
                        {
                            locations.Add(location);
                        }
                    }
                }

                var quickFixTasks = locations.Distinct().Select(async l => await QuickFixHelper.GetQuickFix(_workspace, l));

                var quickFixes = await Task.WhenAll(quickFixTasks);

                response = new QuickFixResponse(quickFixes.Distinct()
                                                .OrderBy(q => q.FileName)
                                                .ThenBy(q => q.Line)
                                                .ThenBy(q => q.Column));
            }

            return(response);
        }
コード例 #2
0
        public async Task <QuickFixResponse> Handle(GotoRegionRequest request)
        {
            var regions  = new List <QuickFix>();
            var document = _workspace.GetDocument(request.FileName);

            if (document != null)
            {
                var root = await document.GetSyntaxRootAsync();

                var regionTrivias = root.DescendantNodesAndTokens()
                                    .Where(node => node.HasLeadingTrivia)
                                    .SelectMany(node => node.GetLeadingTrivia())
                                    .Where(x => (x.RawKind == (int)SyntaxKind.RegionDirectiveTrivia ||
                                                 x.RawKind == (int)SyntaxKind.EndRegionDirectiveTrivia));

                foreach (var regionTrivia in regionTrivias.Distinct())
                {
                    regions.Add(await QuickFixHelper.GetQuickFix(_workspace, regionTrivia.GetLocation()));
                }
            }
            return(new QuickFixResponse(regions));
        }