コード例 #1
0
            private async Task <ImmutableArray <SymbolReference> > GetReferencesForMatchingTypesAsync(SearchScope searchScope)
            {
                searchScope.CancellationToken.ThrowIfCancellationRequested();
                if (!_owner.CanAddImportForType(_diagnosticId, _node, out var nameNode))
                {
                    return(ImmutableArray <SymbolReference> .Empty);
                }

                CalculateContext(
                    nameNode, _syntaxFacts,
                    out var name, out var arity, out var inAttributeContext,
                    out var hasIncompleteParentMember, out var looksGeneric);

                if (ExpressionBinds(nameNode, checkForExtensionMethods: false, cancellationToken: searchScope.CancellationToken))
                {
                    // If the expression bound, there's nothing to do.
                    return(ImmutableArray <SymbolReference> .Empty);
                }

                var symbols = await searchScope.FindDeclarationsAsync(name, nameNode, SymbolFilter.Type).ConfigureAwait(false);

                // also lookup type symbols with the "Attribute" suffix if necessary.
                if (inAttributeContext)
                {
                    var attributeSymbols = await searchScope.FindDeclarationsAsync(name + AttributeSuffix, nameNode, SymbolFilter.Type).ConfigureAwait(false);

                    symbols = symbols.AddRange(
                        attributeSymbols.Select(r => r.WithDesiredName(r.DesiredName.GetWithoutAttributeSuffix(isCaseSensitive: false))));
                }

                var typeSymbols = OfType <ITypeSymbol>(symbols);

                var options = await _document.GetOptionsAsync(searchScope.CancellationToken).ConfigureAwait(false);

                var hideAdvancedMembers = options.GetOption(CompletionOptions.HideAdvancedMembers);
                var editorBrowserInfo   = new EditorBrowsableInfo(_semanticModel.Compilation);

                // Only keep symbols which are accessible from the current location and that are allowed by the current
                // editor browsable rules.
                var accessibleTypeSymbols = typeSymbols.WhereAsArray(
                    s => ArityAccessibilityAndAttributeContextAreCorrect(s.Symbol, arity, inAttributeContext, hasIncompleteParentMember, looksGeneric) &&
                    s.Symbol.IsEditorBrowsable(hideAdvancedMembers, _semanticModel.Compilation, editorBrowserInfo));

                // These types may be contained within namespaces, or they may be nested
                // inside generic types.  Record these namespaces/types if it would be
                // legal to add imports for them.

                var typesContainedDirectlyInNamespaces = accessibleTypeSymbols.WhereAsArray(s => s.Symbol.ContainingSymbol is INamespaceSymbol);
                var typesContainedDirectlyInTypes      = accessibleTypeSymbols.WhereAsArray(s => s.Symbol.ContainingType != null);

                var namespaceReferences = GetNamespaceSymbolReferences(searchScope,
                                                                       typesContainedDirectlyInNamespaces.SelectAsArray(r => r.WithSymbol(r.Symbol.ContainingNamespace)));

                var typeReferences = typesContainedDirectlyInTypes.SelectAsArray(
                    r => searchScope.CreateReference(r.WithSymbol(r.Symbol.ContainingType)));

                return(namespaceReferences.Concat(typeReferences));
            }
 private List <SymbolReference> GetProposedNamespaces(SearchScope scope, IEnumerable <SearchResult <INamespaceSymbol> > namespaces)
 {
     // We only want to offer to add a using if we don't already have one.
     return
         (namespaces.Where(n => !n.Symbol.IsGlobalNamespace)
          .Select(n => n.WithSymbol(_semanticModel.Compilation.GetCompilationNamespace(n.Symbol) ?? n.Symbol))
          .Where(n => n.Symbol != null && !_namespacesInScope.Contains(n.Symbol))
          .Select(n => scope.CreateReference(n))
          .ToList());
 }
 private ImmutableArray <SymbolReference> GetProposedNamespaces(SearchScope scope, IEnumerable <SymbolResult <INamespaceSymbol> > namespaces)
 {
     // We only want to offer to add a using if we don't already have one.
     return
         (namespaces.Where(n => !n.Symbol.IsGlobalNamespace)
          .Select(n => n.WithSymbol(MapToCompilationNamespaceIfPossible(n.Symbol)))
          .Where(n => n.Symbol != null && !_namespacesInScope.Contains(n.Symbol))
          .Select(n => scope.CreateReference(n))
          .ToImmutableArray());
 }
コード例 #4
0
            private ImmutableArray <SymbolReference> GetNamespaceSymbolReferences(
                SearchScope scope, ImmutableArray <SymbolResult <INamespaceSymbol> > namespaces)
            {
                using var _ = ArrayBuilder <SymbolReference> .GetInstance(out var references);

                foreach (var namespaceResult in namespaces)
                {
                    var symbol             = namespaceResult.Symbol;
                    var mappedResult       = namespaceResult.WithSymbol(MapToCompilationNamespaceIfPossible(namespaceResult.Symbol));
                    var namespaceIsInScope = _namespacesInScope.Contains(mappedResult.Symbol);
                    if (!symbol.IsGlobalNamespace && !namespaceIsInScope)
                    {
                        references.Add(scope.CreateReference(mappedResult));
                    }
                }

                return(references.ToImmutable());
            }
            private List <SymbolReference> GetProposedTypes(SearchScope searchScope, string name, List <SearchResult <ITypeSymbol> > accessibleTypeSymbols)
            {
                List <SymbolReference> result = null;

                if (accessibleTypeSymbols != null)
                {
                    foreach (var typeSymbol in accessibleTypeSymbols)
                    {
                        if (typeSymbol.Symbol?.ContainingType != null)
                        {
                            result = result ?? new List <SymbolReference>();
                            result.Add(searchScope.CreateReference(typeSymbol.WithSymbol(typeSymbol.Symbol.ContainingType)));
                        }
                    }
                }

                return(result);
            }