Example #1
0
 private CompletionItem CreateCompletion(SyntaxContext syntaxContext, ISymbol symbol, int sorting)
 {
     return(CompletionItemHelper.CreateCompletionItem(symbol, syntaxContext,
                                                      unimported: false,
                                                      matchPriority: MatchPriority.Preselect,
                                                      sortingPriority: sorting));
 }
        private CompletionItem GetApplicableTypeCompletion(ITypeSymbol suggestedType, SyntaxContext syntaxContext, bool newKeywordRequired, Options.Options options)
        {
            var assignableSymbol = syntaxContext.SemanticModel.Compilation.GetAssignableSymbol(suggestedType, syntaxContext.InferredType);

            if (assignableSymbol != null)
            {
                var  symbolName       = assignableSymbol.Name;
                var  inferredTypeName = syntaxContext.InferredType.Name;
                bool unimported       = !syntaxContext.ImportedNamespaces.Contains(assignableSymbol.GetNamespace());

                int priority;
                if (symbolName == inferredTypeName || "I" + symbolName == inferredTypeName)
                {
                    priority = Sorting.NewSuggestion_MatchingName;
                }
                else if (!unimported)
                {
                    priority = Sorting.NewSuggestion_Default;
                }
                else
                {
                    priority = Sorting.NewSuggestion_Unimported;
                }

                return(CompletionItemHelper.CreateCompletionItem(assignableSymbol, syntaxContext,
                                                                 priority, MatchPriority.Preselect,
                                                                 newPositionOffset: 0,
                                                                 unimported: unimported,
                                                                 newCreationSyntax: newKeywordRequired,
                                                                 showParenthesisForNewCreations: options.AddParethesisForNewSuggestions));
            }
            return(null);
        }
Example #3
0
        private IEnumerable <CompletionItem> GetCompletionItems(SyntaxContext syntaxContext)
        {
            var hasStaticImports = syntaxContext.StaticImports.Count > 0;
            var hasAliases       = syntaxContext.Aliases.Count > 0;

            foreach (var typeSymbol in SymbolNavigator.GetAllTypes(syntaxContext))
            {
                if (typeSymbol.ContainingType == null)
                {
                    continue;
                }

                // Skip nested type if there is static using for parent type
                if (hasStaticImports && syntaxContext.StaticImports.Contains(typeSymbol.ContainingType))
                {
                    continue;
                }

                // Skip if alias present from current type
                if (hasAliases && syntaxContext.Aliases.ContainsKey(typeSymbol))
                {
                    continue;
                }

                var isImported = syntaxContext.IsNamespaceImported(typeSymbol.ContainingNamespace);

                var sorting = isImported ? Sorting.Default : Sorting.Last;

                yield return(CompletionItemHelper.CreateCompletionItem(typeSymbol, syntaxContext, sorting,
                                                                       unimported: !isImported));
            }
        }
        /// <summary>
        /// Return suggestions for special cases, such as array or collection initializers
        /// </summary>
        private IEnumerable <CompletionItem> GetSpecialCasesCompletions(ITypeSymbol typeSymbol, SyntaxContext syntaxContext)
        {
            if (typeSymbol is IArrayTypeSymbol)
            {
                return(new[] { CompletionItemHelper.CreateCompletionItem("new [] {}", Sorting.NewSuggestion_CollectionInitializer, newPositionOffset: -1) });
            }

            if (typeSymbol is INamedTypeSymbol namedTypeSymbol)
            {
                switch (namedTypeSymbol.Name)
                {
                case "List":
                case "IList":
                    var typeParameter = namedTypeSymbol.TypeArguments.FirstOrDefault();
                    if (typeParameter != null)
                    {
                        var displayName = typeParameter.ToMinimalDisplayString(syntaxContext.SemanticModel, syntaxContext.Position);
                        return(new[] { CompletionItemHelper.CreateCompletionItem($"new List<{displayName}> {{}}", Sorting.NewSuggestion_CollectionInitializer, newPositionOffset: -1) });
                    }
                    break;

                case "Boolean":
                    return(new[]
                    {
                        CompletionItemHelper.CreateCompletionItem("true", Sorting.NewSuggestion_Literal)
                        .WithTags(ImmutableArray.Create(WellKnownTags.Keyword)),
                        CompletionItemHelper.CreateCompletionItem("false", Sorting.NewSuggestion_Literal)
                        .WithTags(ImmutableArray.Create(WellKnownTags.Keyword))
                    });
                }
            }

            return(Enumerable.Empty <CompletionItem>());
        }
        public Task <IEnumerable <CompletionItem> > GetCompletionItemsAsync(SyntaxContext syntaxContext, Options.Options options)
        {
            var typeSymbol = syntaxContext.InferredInfo.Type;

            // Unwrap nullable enum
            if (typeSymbol is INamedTypeSymbol namedType &&
                namedType.Name == nameof(Nullable) &&
                namedType.TypeArguments.FirstOrDefault()?.TypeKind == TypeKind.Enum)
            {
                typeSymbol = namedType.TypeArguments[0];
            }

            if (typeSymbol?.TypeKind == TypeKind.Enum)
            {
                return(Task.FromResult(new[]
                {
                    CompletionItemHelper.CreateCompletionItem(typeSymbol, syntaxContext,
                                                              unimported: !syntaxContext.IsNamespaceImported(typeSymbol.ContainingNamespace),
                                                              matchPriority: MatchPriority.Preselect,
                                                              sortingPriority: Sorting.Suitable_Enum)
                }.AsEnumerable()));
            }

            return(Task.FromResult(Enumerable.Empty <CompletionItem>()));
        }
        private IEnumerable <CompletionItem> GetThisCompletionIfApplicable(SyntaxContext syntaxContext)
        {
            var enclosingSymbol = syntaxContext.SemanticModel.GetEnclosingSymbol(syntaxContext.Position, syntaxContext.CancellationToken);

            var methodSymbol = enclosingSymbol?.AncestorsAndSelf().OfType <IMethodSymbol>().FirstOrDefault();
            var typeSymbol   = enclosingSymbol?.ContainingType;

            if (typeSymbol == null || methodSymbol == null || methodSymbol.IsStatic)
            {
                return(Enumerable.Empty <CompletionItem>());
            }

            var typeMatches = syntaxContext.SemanticModel.Compilation
                              .ClassifyConversion(typeSymbol, syntaxContext.InferredInfo.Type).IsImplicit;

            if (!typeMatches)
            {
                return(Enumerable.Empty <CompletionItem>());
            }

            var newSuggestion = CompletionItemHelper.CreateCompletionItem(
                "this",
                Sorting.Suitable_Locals,
                ImmutableArray.Create(WellKnownTags.Keyword));

            return(new[] { newSuggestion });
        }
Example #7
0
        public IEnumerable <CompletionItem> GetCompletionItemsForType(INamedTypeSymbol typeSymbol, SyntaxContext syntaxContext, Options.Options options)
        {
            // Only attributes are permitted in attribute context
            if (syntaxContext.IsAttributeContext && (typeSymbol.IsAbstract || !typeSymbol.IsAttribute()))
            {
                return(null);
            }

            // Skip nested type if there is static using for parent type
            if (typeSymbol.ContainingType != null && syntaxContext.StaticImports.Contains(typeSymbol.ContainingType))
            {
                return(null);
            }

            if (!syntaxContext.IsNamespaceImported(typeSymbol.ContainingNamespace) &&
                !syntaxContext.Aliases.ContainsKey(typeSymbol))
            {
                return(new[] { CreateCompletionItemForSymbol(typeSymbol, syntaxContext, options) });
            }
            // If nested types suggestions are enabled, we should return imported suggestions as well
            else if (options.SuggestNestedTypes && typeSymbol.ContainingType != null)
            {
                return(new[] { CompletionItemHelper.CreateCompletionItem(typeSymbol, syntaxContext, unimported: false) });
            }

            return(null);
        }
        private CompletionItem CreateCompletionItemForSymbol(ISymbol typeSymbol, SyntaxContext context)
        {
            bool sortLast       = Options.SortCompletionsAfterImported;
            var  completionItem = CompletionItemHelper.CreateCompletionItem(typeSymbol, context, sortLast);

            var fullSymbolName = completionItem.Properties[CompletionItemProperties.FullSymbolName];

            _symbolMapping[fullSymbolName] = typeSymbol;

            return(completionItem);
        }
        public IEnumerable <CompletionItem> GetCompletionItems(SyntaxContext syntaxContext, Options.Options options)
        {
            if (syntaxContext.InferredType is INamedTypeSymbol namedType &&
                namedType.EnumUnderlyingType != null)
            {
                return(new[]
                {
                    CompletionItemHelper.CreateCompletionItem(namedType, syntaxContext,
                                                              unimported: !syntaxContext.ImportedNamespaces.Contains(namedType.GetNamespace()),
                                                              matchPriority: MatchPriority.Preselect,
                                                              sortingPriority: Sorting.Suitable_Enum)
                });
            }

            return(null);
        }
        public IEnumerable <CompletionItem> GetCompletionItemsForType(INamedTypeSymbol typeSymbol, SyntaxContext syntaxContext, Options.Options options)
        {
            if (!syntaxContext.IsAttributeContext ||
                (typeSymbol.IsAttribute() && !typeSymbol.IsAbstract))
            {
                if (!syntaxContext.IsNamespaceImported(typeSymbol))
                {
                    return(new[] { CreateCompletionItemForSymbol(typeSymbol, syntaxContext, options) });
                }
                // If nested types suggestions are enabled, we should return imported suggestions as well
                else if (options.SuggestNestedTypes && typeSymbol.ContainingType != null)
                {
                    return(new[] { CompletionItemHelper.CreateCompletionItem(typeSymbol, syntaxContext, unimported: false) });
                }
            }

            return(null);
        }
Example #11
0
        private IEnumerable <CompletionItem> GetStaticMethodsAndPropertiesCompletions(SyntaxContext syntaxContext, ITypeSymbol typeSymbol)
        {
            // Do not suggest completions for static methods or properties for build-in types.
            // There's too much of them, and they only pollute IntelliSence
            if (typeSymbol.IsBuiltInType())
            {
                return(Enumerable.Empty <CompletionItem>());
            }

            var factorySymbols = typeSymbol.GetMembers()
                                 .Where(symbol => symbol.IsStatic &&
                                        symbol.DeclaredAccessibility == Accessibility.Public &&
                                        (((symbol as IMethodSymbol)?.MethodKind == MethodKind.Ordinary &&
                                          (symbol as IMethodSymbol)?.ReturnType == typeSymbol) ||
                                         (symbol as IPropertySymbol)?.Type == typeSymbol));

            return(factorySymbols.Select(symbol => CompletionItemHelper.CreateCompletionItem(symbol, syntaxContext,
                                                                                             Sorting.NewSuggestion_FactoryMethod,
                                                                                             MatchPriority.Preselect,
                                                                                             unimported: !syntaxContext.IsNamespaceImported(symbol.ContainingNamespace),
                                                                                             includeContainingClass: true)));
        }
Example #12
0
        public IEnumerable <CompletionItem> GetCompletionItemsForType(INamedTypeSymbol typeSymbol, SyntaxContext syntaxContext, Options.Options options)
        {
            // Only attributes are permitted in attribute context
            if (syntaxContext.IsAttributeContext && (typeSymbol.IsAbstract || !typeSymbol.IsAttribute()))
            {
                return(null);
            }

            // Skip nested type if there is static using for parent type
            if (typeSymbol.ContainingType != null && syntaxContext.StaticImports.Contains(typeSymbol.ContainingType))
            {
                return(null);
            }

            // Skip if alias present from current type
            if (syntaxContext.Aliases.ContainsKey(typeSymbol))
            {
                return(null);
            }

            var isImported = syntaxContext.IsNamespaceImported(typeSymbol.ContainingNamespace);

            // Add nested type independently whether imported or not
            if (options.SuggestNestedTypes && typeSymbol.ContainingType != null)
            {
                var sorting = isImported ? Sorting.Default : Sorting.Last;
                return(new[] { CompletionItemHelper.CreateCompletionItem(typeSymbol, syntaxContext, sorting,
                                                                         unimported: !isImported) });
            }
            else if (options.SuggestUnimportedTypes && !isImported)
            {
                return(new[] { CreateCompletionItemForSymbol(typeSymbol, syntaxContext, options) });
            }

            return(null);
        }
        private CompletionItem CreateCompletionItemForSymbol(ISymbol typeSymbol, SyntaxContext context, Options.Options options)
        {
            int sorting = options.SortCompletionsAfterImported ? Sorting.Last : Sorting.Default;

            return(CompletionItemHelper.CreateCompletionItem(typeSymbol, context, sorting));
        }
Example #14
0
 private CompletionItem CreateCompletionItemForSymbol(ISymbol typeSymbol, SyntaxContext context, Options.Options options)
 {
     return(CompletionItemHelper.CreateCompletionItem(typeSymbol, context, Sorting.Default));
 }