Esempio n. 1
0
        public static string GetUniqueName <T>([NotNull] T node, [CanBeNull] string baseName,
                                               NamedElementKinds elementKind, Action <INamesCollection> collectionModifier = null, Func <IDeclaredElement, bool> isConflictingElement = null) where T : ICSharpTreeNode
        {
            node.GetPsiServices().Locks.AssertMainThread();

            isConflictingElement = isConflictingElement ?? JetFunc <IDeclaredElement> .True;
            var namingManager   = node.GetPsiServices().Naming;
            var policyProvider  = namingManager.Policy.GetPolicyProvider(node.Language, node.GetSourceFile());
            var namingRule      = policyProvider.GetPolicy(elementKind).NamingRule;
            var namesCollection = namingManager.Suggestion.CreateEmptyCollection(PluralityKinds.Unknown, CSharpLanguage.Instance, true, policyProvider);

            if (baseName != null)
            {
                var name     = namingManager.Parsing.Parse(baseName, namingRule, policyProvider);
                var nameRoot = name.GetRootOrDefault(baseName);
                namesCollection.Add(nameRoot, new EntryOptions(PluralityKinds.Plural, SubrootPolicy.Decompose, emphasis: Emphasis.Good));
            }

            collectionModifier?.Invoke(namesCollection);
            var suggestionOptions = new SuggestionOptions
            {
                DefaultName          = baseName,
                UniqueNameContext    = node,
                IsConflictingElement = isConflictingElement
            };
            var namesSuggestion = namesCollection.Prepare(elementKind, ScopeKind.Common, suggestionOptions);

            return(namesSuggestion.FirstName());
        }
        private static void AddToCollector(CSharpCodeCompletionContext context, IItemsCollector collector, IReferenceName referenceName, NamedElementKinds elementKinds, ScopeKind localSelfScoped)
        {
            var referenceNameResolveResult = referenceName.Reference.Resolve();
            var referencedElementAsString  = referenceNameResolveResult.DeclaredElement.ConvertToString();

            if (referencedElementAsString == "Class:Moq.Mock`1")
            {
                var typeArgumentList = referenceName.TypeArgumentList;
                var typeArguments    = typeArgumentList.TypeArguments;

                if (typeArguments.Count == 1)
                {
                    var typeArgument = typeArguments[0];
                    var scalarType   = typeArgument.GetScalarType();

                    if (scalarType == null)
                    {
                        return;
                    }

                    var    genericTypeResolveResult = scalarType.Resolve();
                    var    namingManager            = typeArgument.GetPsiServices().Naming;
                    var    suggestionOptions        = new SuggestionOptions();
                    string proposedName;

                    if (genericTypeResolveResult.IsEmpty)
                    {
                        proposedName = namingManager.Suggestion.GetDerivedName(typeArgument.GetPresentableName(CSharpLanguage.Instance), elementKinds, localSelfScoped,
                                                                               CSharpLanguage.Instance, suggestionOptions, referenceName.GetSourceFile());
                    }
                    else
                    {
                        proposedName = namingManager.Suggestion.GetDerivedName(genericTypeResolveResult.DeclaredElement, elementKinds, localSelfScoped,
                                                                               CSharpLanguage.Instance, suggestionOptions, referenceName.GetSourceFile());
                    }

                    var textLookupItem = new TextLookupItem(proposedName);
                    textLookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                    textLookupItem.SetTopPriority();
                    collector.Add(textLookupItem);

                    var textLookupItem2 = new TextLookupItem(proposedName + "Mock");
                    textLookupItem2.InitializeRanges(context.CompletionRanges, context.BasicContext);
                    textLookupItem2.SetTopPriority();
                    collector.Add(textLookupItem2);
                }
            }
        }