Exemplo n.º 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));
            }
Exemplo n.º 2
0
                public Builder(Checksum checksum, string language, string genericTypeSuffix, EditorBrowsableInfo editorBrowsableInfo)
                {
                    _checksum            = checksum;
                    _language            = language;
                    _genericTypeSuffix   = genericTypeSuffix;
                    _editorBrowsableInfo = editorBrowsableInfo;

                    _itemsBuilder = ArrayBuilder <TypeImportCompletionItemInfo> .GetInstance();
                }
Exemplo n.º 3
0
 public static bool IsEditorBrowsable(
     this ISymbol symbol,
     bool hideAdvancedMembers,
     Compilation compilation,
     EditorBrowsableInfo editorBrowsableInfo = default)
 {
     return(IsEditorBrowsableWithState(
                symbol,
                hideAdvancedMembers,
                compilation,
                editorBrowsableInfo).isBrowsable);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Get appropriate completion items for all the visible top level types from given PE reference.
 /// </summary>
 private bool TryGetUpToDateCacheForPEReference(
     Compilation originCompilation,
     Solution solution,
     EditorBrowsableInfo editorBrowsableInfo,
     PortableExecutableReference peReference,
     CancellationToken cancellationToken,
     out TypeImportCompletionCacheEntry cacheEntry)
 {
     if (originCompilation.GetAssemblyOrModuleSymbol(peReference) is not IAssemblySymbol assemblySymbol)
     {
         cacheEntry = default;
         return(false);
     }
Exemplo n.º 5
0
        // In addition to given symbol's browsability, also returns its EditorBrowsableState if it contains EditorBrowsableAttribute.
        public static (bool isBrowsable, bool isEditorBrowsableStateAdvanced) IsEditorBrowsableWithState(
            this ISymbol symbol,
            bool hideAdvancedMembers,
            Compilation compilation,
            EditorBrowsableInfo editorBrowsableInfo = default
            )
        {
            // Namespaces can't have attributes, so just return true here.  This also saves us a
            // costly check if this namespace has any locations in source (since a merged namespace
            // needs to go collect all the locations).
            if (symbol.Kind == SymbolKind.Namespace)
            {
                return(isBrowsable : true, isEditorBrowsableStateAdvanced : false);
            }

            // check for IsImplicitlyDeclared so we don't spend time examining VB's embedded types.
            // This saves a few percent in typing scenarios.  An implicitly declared symbol can't
            // have attributes, so it can't be hidden by them.
            if (symbol.IsImplicitlyDeclared)
            {
                return(isBrowsable : true, isEditorBrowsableStateAdvanced : false);
            }

            if (editorBrowsableInfo.IsDefault)
            {
                editorBrowsableInfo = new EditorBrowsableInfo(compilation);
            }

            // Ignore browsability limiting attributes if the symbol is declared in source.
            // Check all locations since some of VB's embedded My symbols are declared in
            // both source and the MyTemplateLocation.
            if (symbol.Locations.All(loc => loc.IsInSource))
            {
                // The HideModuleNameAttribute still applies to Modules defined in source
                return(
                    !IsBrowsingProhibitedByHideModuleNameAttribute(
                        symbol,
                        editorBrowsableInfo.HideModuleNameAttribute
                        ),
                    isEditorBrowsableStateAdvanced : false
                    );
            }

            var(isProhibited, isEditorBrowsableStateAdvanced) = IsBrowsingProhibited(
                symbol,
                hideAdvancedMembers,
                editorBrowsableInfo
                );

            return(!isProhibited, isEditorBrowsableStateAdvanced);
        }
        private async Task <ImmutableArray <SymbolResult> > GetMatchingTypesAsync(
            Document document, SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var project     = document.Project;
            var syntaxFacts = project.LanguageServices.GetRequiredService <ISyntaxFactsService>();

            syntaxFacts.GetNameAndArityOfSimpleName(node, out var name, out var arity);
            var looksGeneric = syntaxFacts.LooksGeneric(node);

            var symbols = await DeclarationFinder.FindAllDeclarationsWithNormalQueryAsync(
                project, SearchQuery.Create(name, IgnoreCase),
                SymbolFilter.Type, cancellationToken).ConfigureAwait(false);

            // also lookup type symbols with the "Attribute" suffix.
            var inAttributeContext = syntaxFacts.IsAttributeName(node);

            if (inAttributeContext)
            {
                var attributeSymbols = await DeclarationFinder.FindAllDeclarationsWithNormalQueryAsync(
                    project, SearchQuery.Create(name + "Attribute", IgnoreCase),
                    SymbolFilter.Type, cancellationToken).ConfigureAwait(false);

                symbols = symbols.Concat(attributeSymbols);
            }

            var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

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

            var validSymbols = symbols
                               .OfType <INamedTypeSymbol>()
                               .Where(s => IsValidNamedTypeSearchResult(semanticModel, arity, inAttributeContext, looksGeneric, s) &&
                                      s.IsEditorBrowsable(hideAdvancedMembers, semanticModel.Compilation, editorBrowserInfo))
                               .ToImmutableArray();

            // Check what the current node binds to.  If it binds to any symbols, but with
            // the wrong arity, then we don't want to suggest fully qualifying to the same
            // type that we're already binding to.  That won't address the WrongArity problem.
            var currentSymbolInfo = semanticModel.GetSymbolInfo(node, cancellationToken);

            if (currentSymbolInfo.CandidateReason == CandidateReason.WrongArity)
            {
                validSymbols = validSymbols.WhereAsArray(
                    s => !currentSymbolInfo.CandidateSymbols.Contains(s));
            }

            return(validSymbols.SelectAsArray(s => new SymbolResult(s, weight: TypeWeight)));
        }
Exemplo n.º 7
0
        private static (bool isProhibited, bool isEditorBrowsableStateAdvanced) IsBrowsingProhibited(
            ISymbol symbol,
            bool hideAdvancedMembers,
            EditorBrowsableInfo editorBrowsableInfo
            )
        {
            var attributes = symbol.GetAttributes();

            if (attributes.Length == 0)
            {
                return(isProhibited : false, isEditorBrowsableStateAdvanced : false);
            }

            var(isProhibited, isEditorBrowsableStateAdvanced) =
                IsBrowsingProhibitedByEditorBrowsableAttribute(
                    attributes,
                    hideAdvancedMembers,
                    editorBrowsableInfo.EditorBrowsableAttributeConstructor
                    );

            return(
                (
                    isProhibited ||
                    IsBrowsingProhibitedByTypeLibTypeAttribute(
                        attributes,
                        editorBrowsableInfo.TypeLibTypeAttributeConstructors
                        ) ||
                    IsBrowsingProhibitedByTypeLibFuncAttribute(
                        attributes,
                        editorBrowsableInfo.TypeLibFuncAttributeConstructors
                        ) ||
                    IsBrowsingProhibitedByTypeLibVarAttribute(
                        attributes,
                        editorBrowsableInfo.TypeLibVarAttributeConstructors
                        ) ||
                    IsBrowsingProhibitedByHideModuleNameAttribute(
                        symbol,
                        editorBrowsableInfo.HideModuleNameAttribute,
                        attributes
                        )
                ),
                isEditorBrowsableStateAdvanced
                );
        }