Esempio n. 1
0
 private static void ReportAnonymousTypes([NotNull] ClosureInspector inspector, [NotNull] IHighlightingConsumer consumer)
 {
     foreach (var queryClause in inspector.AnonymousTypes)
     {
         var highlighting = new ObjectAllocationHighlighting(queryClause, "transparent identifier anonymous type instantiation");
         consumer.AddHighlighting(highlighting, queryClause.GetDocumentRange());
     }
 }
Esempio n. 2
0
        private static void ReportClosureAllocations(
            [NotNull] ITreeNode topDeclaration, [CanBeNull] IParametersOwner thisElement, [CanBeNull] ILocalScope topScope,
            [NotNull] ClosureInspector inspector, [NotNull] IHighlightingConsumer consumer)
        {
            var scopesMap     = new Dictionary <IDeclaredElement, ILocalScope>();
            var captureScopes = new Dictionary <ILocalScope, JetHashSet <IDeclaredElement> >();

            // group captures by their scope, report non-cached delegates
            foreach (var closure in inspector.Closures)
            {
                foreach (var capture in closure.Value)
                {
                    ILocalScope scope = null;

                    if (capture is IFunction)
                    {
                        scope = topScope; // 'this' capture
                    }
                    else
                    {
                        var declarations = capture.GetDeclarations();
                        if (declarations.Count == 0) // accessors 'value' parameter
                        {
                            if (thisElement is IAccessor accessor && Equals(accessor.ValueVariable, capture))
                            {
                                scope = topScope;
                            }
                        }
                        else
                        {
                            foreach (var declaration in declarations)
                            {
                                if (declaration is IRegularParameterDeclaration)
                                {
                                    scope = topScope;
                                }
                                else
                                {
#if RESHARPER2017_1
                                    scope = declaration.GetContainingScope <ILocalScope>();
#else
                                    scope = declaration.GetContainingNode <ILocalScope>();
#endif
                                }

                                break;
                            }
                        }
                    }

                    if (scope == null)
                    {
                        continue;
                    }

                    if (!captureScopes.TryGetValue(scope, out var captures))
                    {
                        captureScopes[scope] = captures = new JetHashSet <IDeclaredElement>();
                    }

                    captures.Add(capture);
                    scopesMap[capture] = scope;
                }

                {
                    var highlightingRange = GetClosureRange(closure.Key);
                    if (highlightingRange.IsValid())
                    {
                        if (IsExpressionLambda(closure.Key))
                        {
                            var highlighting = new ObjectAllocationHighlighting(closure.Key, "expression tree construction");
                            consumer.AddHighlighting(highlighting, highlightingRange);
                        }
                        else
                        {
                            var description  = FormatClosureDescription(closure.Value);
                            var highlighting = new DelegateAllocationHighlighting(closure.Key, "capture of " + description);
                            consumer.AddHighlighting(highlighting, highlightingRange);

                            ReportClosurelessOverloads(closure.Key, consumer);
                        }
                    }
                }
            }

            // highlight first captured entity per every scope
            foreach (var scopeToCaptures in captureScopes)
            {
                var firstOffset = TreeOffset.MaxValue;
                IDeclaredElement firstCapture = null;

                foreach (var capture in scopeToCaptures.Value)
                {
                    if (capture is IFunction)
                    {
                        continue;
                    }

                    var offset = GetCaptureStartOffset(capture);
                    if (offset < firstOffset)
                    {
                        firstOffset  = offset;
                        firstCapture = capture;
                    }
                }

                var scopeClosure = FormatClosureDescription(scopeToCaptures.Value);

                // collect outer captures
                JetHashSet <IDeclaredElement> outerCaptures = null;
                foreach (var closureToCaptures in inspector.Closures)
                {
                    if (!scopeToCaptures.Key.Contains(closureToCaptures.Key))
                    {
                        continue;
                    }

                    foreach (var capture in closureToCaptures.Value)
                    {
                        if (!scopesMap.TryGetValue(capture, out var scope))
                        {
                            continue;
                        }
                        if (scopeToCaptures.Key.Contains(scope))
                        {
                            continue;
                        }

                        outerCaptures = outerCaptures ?? new JetHashSet <IDeclaredElement>();
                        outerCaptures.Add(capture);
                    }
                }

                if (outerCaptures != null)
                {
                    var description = FormatClosureDescription(outerCaptures);
                    scopeClosure += $" + (outer closure of {description})";
                }

                if (firstCapture != null)
                {
                    var anchor = GetCaptureHighlightingRange(topDeclaration, thisElement, firstCapture, out var highlightingRange);
                    if (anchor != null && highlightingRange.IsValid())
                    {
                        consumer.AddHighlighting(new ClosureAllocationHighlighting(anchor, scopeClosure), highlightingRange);
                    }
                }
            }
        }