Example #1
0
        private static bool isTypeParameter(ICSharpTypeMemberDeclaration declaration, string word)
        {
            IMethodDeclaration method = declaration as IMethodDeclaration;
            if (method != null)
            {
                foreach (ITypeParameterOfMethodDeclaration decl in method.TypeParameterDeclarations)
                {
                    if (decl.DeclaredName == word)
                    {
                        return true;
                    }
                }
            }

            ICSharpTypeDeclaration containingType = declaration.GetContainingTypeDeclaration();
            if (containingType != null)
            {
                IClassLikeDeclaration classDecl = containingType as IClassLikeDeclaration;
                if (classDecl != null)
                {
                    foreach (ITypeParameterOfTypeDeclaration decl in classDecl.TypeParameters)
                    {
                        if (decl.DeclaredName == word)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
Example #2
0
 public static bool IsIdentifier(ICSharpTypeMemberDeclaration declaration, ISolution solution, string word, IdentifierLookupScopes scope = IdentifierLookupScopes.ProjectAndReferencedLibraries)
 {
     return isParameter(declaration, word) ||
            isTypeParameter(declaration, word) ||
            isClassMemberDeclaration(declaration, word) ||
            getDeclaredElements(word, declaration, solution, scope).Count > 0;
 }
Example #3
0
        public static IList<string> GetReplaceFormats(ICSharpTypeMemberDeclaration declaration, ISolution solution,
            string word, IdentifierLookupScopes scope = IdentifierLookupScopes.ProjectAndReferencedLibraries)
        {
            List<string> replaceFormats = new List<string>();

            if (isParameter(declaration, word))
            {
                replaceFormats.Add("<paramref name=\"{0}\"/>");
            }

            if (isTypeParameter(declaration, word))
            {
                replaceFormats.Add("<typeparamref name=\"{0}\"/>");
            }

            if (isClassMemberDeclaration(declaration, word))
            {
                replaceFormats.Add("<see cref=\"{0}\"/>");
            }

            // Find out whether the given element is available in the current file.
            List<TypeAndNamespace> typesAndNamespaces = getDeclaredElements(word, declaration, solution, scope);
            foreach (TypeAndNamespace typeAndNamespace in typesAndNamespaces)
            {
                // It's already imported
                replaceFormats.Add("<see cref=\"" + typeAndNamespace.XmlDocId + "\"/>");
            }

            if (IsKeyword(declaration, solution, word))
            {
                replaceFormats.Add("<see langword=\"{0}\"/>");
            }

            return replaceFormats;
        }
Example #4
0
 private static List<TypeAndNamespace> getDeclaredElements(string word, ICSharpTypeMemberDeclaration declaration, ISolution solution, IdentifierLookupScopes scope = IdentifierLookupScopes.ProjectAndReferencedLibraries)
 {
     //solution.GetPsiServices().SolutionProject.ProjectFile.
     //IModuleReferenceResolveContext context = declaration.DeclaredElement.GetResolveContext();
     IClrDeclaredElement[] declaredElements = solution.GetPsiServices()
                              .Symbols.GetSymbolScope(scope.AsLibrarySymbolScope(), true)
                              .GetElementsByShortName(word);
     return getTypeAndNamespaces(declaredElements, declaration, solution, scope);
 }
        /// <summary>
        /// Reflow the given comment block to fit within the given maximum line length
        /// </summary>
        /// <param name="blockNode">The comment block to reflow</param>
        /// <param name="maxLineLength">The maximum line length</param>
        /// <returns>The text for the new reflown comment.</returns>
        public string ReflowAndRetag(IDocCommentBlockNode blockNode, int maxLineLength)
        {
            ITreeNode parent = blockNode.Parent;
            ICSharpTypeMemberDeclaration parentDeclaration = parent as IClassMemberDeclaration;

            if (parentDeclaration == null)
            {
                IMultipleFieldDeclaration multipleFieldDeclaration = parent as IMultipleFieldDeclaration;
                if (multipleFieldDeclaration != null)
                {
                    foreach (IFieldDeclaration field in multipleFieldDeclaration.Children <IFieldDeclaration>())
                    {
                        parentDeclaration = field;
                        break;
                    }
                }

                IEnumMemberDeclaration enumMemberDeclaration = parent as IEnumMemberDeclaration;
                if (enumMemberDeclaration != null)
                {
                    parentDeclaration = enumMemberDeclaration;
                }
            }

            // get the xml from the comment
            XmlNode node = blockNode.GetXML(null);

            // Walk the xml tree and process elements as we go. Use a recursive algo for now - comments shouldn't be that complex.
            XmlCommentOptions options = new XmlCommentOptions();

            options.Declaration           = parentDeclaration;
            options.IdentifierLookupScope = IdentifierLookupScopes.ProjectAndUsings;
            options.Solution = blockNode.GetSolution();

            List <Regex> ignoreList = new List <Regex>(_settings.CompiledWordsToIgnoreForMetatagging);

            ignoreList.Add(new Regex("^[Aa]$"));
            ignoreList.Add(new Regex("^[iI]f$"));
            ignoreList.Add(new Regex("^[tT]his$"));
            ignoreList.Add(new Regex("^[eE]lse$"));
            ignoreList.Add(new Regex("^[lL]ong$"));
            ignoreList.Add(new Regex("^[wW]hile$"));
            ignoreList.Add(new Regex("^[lL]ock$"));
            ignoreList.Add(new Regex("^[fF]ixed$"));
            ignoreList.Add(new Regex("^[bB]ase$"));
            ignoreList.Add(new Regex("^[oO]bject$"));

            options.IdentifiersToIgnoreForMetaTagging = ignoreList;

            options.Settings = _reflowSettings;

            XmlComments.XmlComment comment = new XmlComments.XmlComment(options);
            comment.FromXml(node);
            comment.InsertMissingTags();
            return(comment.ToXml(0, maxLineLength, 0));
        }
Example #6
0
        public static void SetDocComment(IDocCommentBlockOwnerNode docCommentBlockOwnerNode, string text, ISolution solution)
        {
            text = String.Format("///{0}\r\nclass Tmp {{}}", text.Replace("\n", "\n///"));

            ICSharpTypeMemberDeclaration declaration =
                CSharpElementFactory.GetInstance(docCommentBlockOwnerNode.GetPsiModule()).CreateTypeMemberDeclaration(text, new object[0]);

            docCommentBlockOwnerNode.SetDocCommentBlockNode(
                ((IDocCommentBlockOwnerNode)declaration).GetDocCommentBlockNode());
        }
        public static string ContractCRef(string input, ICSharpTypeMemberDeclaration declaration, ISolution solution, IdentifierLookupScopes scope = IdentifierLookupScopes.ProjectAndReferencedLibraries)
        {
            string methodArgs = null;
            string methodName = null;
            string crefText   = input;

            if (crefText == null)
            {
                return(null);
            }
            if (crefText.Length > 2 && crefText[1] == ':')
            {
                if (crefText[0] == 'M' || crefText.Contains("("))
                {
                    methodArgs = crefText.Substring(crefText.IndexOf('('));
                    crefText   = crefText.Substring(0, crefText.IndexOf('('));
                    methodName = crefText.Substring(crefText.LastIndexOf('.') + 1);
                    crefText   = crefText.Substring(0, crefText.LastIndexOf('.'));
                }
                crefText = crefText.Substring(2);
            }

            IModuleReferenceResolveContext    context          = declaration.DeclaredElement.ResolveContext;
            ICollection <IClrDeclaredElement> declaredElements = solution.GetPsiServices()
                                                                 .Symbols.GetSymbolScope(scope.AsLibrarySymbolScope(), true, context)
                                                                 .GetElementsByQualifiedName(crefText);

            if (declaredElements.Count == 0)
            {
                return(crefText);
            }

            List <TypeAndNamespace> typesAndNamespaces = getTypeAndNamespaces(declaredElements, declaration, solution, scope);

            if (typesAndNamespaces.Count > 0)
            {
                TypeAndNamespace first = typesAndNamespaces[0];

                if (methodName == null)
                {
                    return(first.XmlDocId);
                }

                IClass cls = first.TypeElement as IClass;
                if (cls != null && cls.Methods.Count(x => x.ShortName == methodName) == 1)
                {
                    return(string.Format("{0}.{1}", first.XmlDocId, methodName));
                }
                return(string.Format("{0}.{1}{2}", first.XmlDocId, methodName, methodArgs));
            }
            return(crefText);
        }
        internal ContractPublicPropertyNameHighlighing(IFieldDeclaration fieldDeclaration, 
            string referingFieldOrPropertyName,
            ICSharpTypeMemberDeclaration referingFieldOrProperty)
        {
            Contract.Requires(fieldDeclaration != null);
            Contract.Requires(referingFieldOrPropertyName != null);

            _fieldDeclaration = fieldDeclaration;
            _referingFieldOrProperty = referingFieldOrProperty;

            FieldName = fieldDeclaration.NameIdentifier.With(x => x.Name);
            PropertyName = referingFieldOrPropertyName;
            FieldType = fieldDeclaration.Type.GetLongPresentableName(CSharpLanguage.Instance);
        }
Example #9
0
        internal ContractPublicPropertyNameHighlighing(IFieldDeclaration fieldDeclaration,
                                                       string referingFieldOrPropertyName,
                                                       ICSharpTypeMemberDeclaration referingFieldOrProperty)
        {
            Contract.Requires(fieldDeclaration != null);
            Contract.Requires(referingFieldOrPropertyName != null);

            _range                   = fieldDeclaration.GetHighlightingRange();
            _fieldDeclaration        = fieldDeclaration;
            _referingFieldOrProperty = referingFieldOrProperty;

            FieldName    = fieldDeclaration.NameIdentifier.With(x => x.Name);
            PropertyName = referingFieldOrPropertyName;
            FieldType    = fieldDeclaration.Type.GetLongPresentableName(CSharpLanguage.Instance);
        }
Example #10
0
 private static bool isClassMemberDeclaration(ICSharpTypeMemberDeclaration declaration, string word)
 {
     ICSharpTypeDeclaration containingType = declaration.GetContainingTypeDeclaration();
     if (containingType != null)
     {
         string withDot = "." + word;
         foreach (ICSharpTypeMemberDeclaration decl in containingType.MemberDeclarations)
         {
             if (decl.DeclaredName == word || decl.DeclaredName.EndsWith(withDot))
             {
                 return true;
             }
         }
     }
     return false;
 }
Example #11
0
        private static bool HasSerializedFieldAttribute(ICSharpTypeMemberDeclaration declaration)
        {
            foreach (var attribute in declaration.AttributesEnumerable)
            {
                var result = attribute.TypeReference?.Resolve();
                if (result != null && result.ResolveErrorType.IsAcceptable)
                {
                    if (result.DeclaredElement is IClass declaredElement &&
                        Equals(declaredElement.GetClrName(), KnownTypes.SerializeField))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Example #12
0
        private static bool isParameter(ICSharpTypeMemberDeclaration decl, string word)
        {
            IParametersOwnerDeclaration methodDecl = decl as IParametersOwnerDeclaration;

            if (methodDecl != null)
            {
                foreach (IRegularParameterDeclaration parm in methodDecl.ParameterDeclarations)
                {
                    if (parm.DeclaredName == word)
                    {
                        return true;
                    }
                }
            }

            return false;
        }
Example #13
0
 public MissingXmlDocHighlighting(ICSharpTypeMemberDeclaration declaration)
     : base(declaration, FormatMessage(declaration.DeclaredElement.NotNull()))
 {
 }
Example #14
0
 public MakeMethodVirtualSuggestion(ICSharpTypeMemberDeclaration memberDeclaration)
 {
     Declaration = memberDeclaration;
 }
 public MakeMethodVirtualSuggestion(ICSharpTypeMemberDeclaration memberDeclaration)
 {
     Declaration = memberDeclaration;
 }
Example #16
0
        /*private static bool isADeclaredElement(string word, ISolution solution, DeclarationCacheLibraryScope scope = DeclarationCacheLibraryScope.FULL)
        {
            CacheManager cacheManager = solution.GetPsiServices().CacheManager;
            IDeclarationsCache declarationsCache = cacheManager.GetDeclarationsCache(scope, true);
            IDeclaredElement[] declaredElements = declarationsCache.GetElementsByShortName(word);
            return declaredElements != null && declaredElements.Length > 0;
        }
        */
        private static List<TypeAndNamespace> getTypeAndNamespaces(IEnumerable<IClrDeclaredElement> declaredElements, ICSharpTypeMemberDeclaration declaration, ISolution solution, IdentifierLookupScopes scope = IdentifierLookupScopes.ProjectAndReferencedLibraries)
        {
            ICSharpFile file = declaration.GetContainingFile() as ICSharpFile;

            List<TypeAndNamespace> inFileResults = new List<TypeAndNamespace>();
            List<TypeAndNamespace> importResults = new List<TypeAndNamespace>();
            if (declaredElements == null) return inFileResults;
            foreach (IClrDeclaredElement element in declaredElements)
            {
                // Find out whether the given element is available in the current file.
                TypeAndNamespace typeAndNamespace = GetAccessableTypeElementAndNamespace(declaration, solution, file, element, scope);
                if (typeAndNamespace == null) continue;
                if (typeAndNamespace.RequiredNamespaceImport == null)
                {
                    inFileResults.Add(typeAndNamespace);
                }
                else
                {
                    importResults.Add(typeAndNamespace);
                }
            }

            inFileResults.Sort((x, y) => x.XmlDocId.CompareTo(y.XmlDocId));
            importResults.Sort((x, y) => x.XmlDocId.CompareTo(y.XmlDocId));
            inFileResults.AddRange(importResults);
            return inFileResults;
        }
Example #17
0
 public static bool IsKeyword(ICSharpTypeMemberDeclaration declaration, ISolution solution, string word)
 {
     return KeywordUtil.IsKeyword(word);
 }
Example #18
0
        private static TypeAndNamespace GetAccessableTypeElementAndNamespace(ICSharpTypeMemberDeclaration declaration, ISolution solution, ICSharpFile file, IClrDeclaredElement element, IdentifierLookupScopes scope)
        {
            //IPsiModule module = element.Module;

            IXmlDocIdOwner idOwner = element as IXmlDocIdOwner;
            string docId = idOwner == null ? element.ShortName : idOwner.XMLDocId;

            // Get the defining type.
            ITypeElement typeElement = element as ITypeElement ?? element.GetContainingType();
            if (typeElement == null) return null;

            // Create the result
            TypeAndNamespace result = new TypeAndNamespace
                                          {
                                              XmlDocId = docId,
                                              TypeElement = element
                                          };

            // Get the namespace it belongs to.
            INamespace namespaceElement = typeElement.GetContainingNamespace();
            string namespaceName = namespaceElement.QualifiedName;

            // Check if we're ignoring this namespace
            foreach (string namespacePrefix in NamespacePrefixesToIgnore)
            {
                if (namespaceName.StartsWith(namespacePrefix)) return null;
            }

            // Check if it would be possible to access the type
            AccessRights elementAccessRights = GetAccessRights(element);
            if (elementAccessRights == AccessRights.PRIVATE)
            {
                return null;
            }

            // Check if the type is defined in this solution
            IList<IDeclaration> declarations = element.GetDeclarations();
            if (declarations.Count == 0)
            {
                // Assembly is an import so no internal things allowed
                if (elementAccessRights == AccessRights.INTERNAL) return null;
            }

            // Check if the given namespace is already imported in this file.
            if (UsingUtil.CheckAlreadyImported(file, new DeclaredElementInstance(namespaceElement)) || declaration.GetContainingNamespaceDeclaration().QualifiedName.StartsWith(namespaceName))
            {
                string newDocId = docId[1] == ':' ? docId.Substring(2) : docId;
                if (newDocId.StartsWith(namespaceName + ".")) newDocId = newDocId.Substring(namespaceName.Length + 1);
                result.XmlDocId = newDocId;
                return result;
            }

            // If we require it to be in project or using scope then this is not a match
            if (scope == IdentifierLookupScopes.ProjectAndUsings || scope == IdentifierLookupScopes.ProjectOnly)
            {
                return null;
            }

            // No - so add in the namespace.
            result.RequiredNamespaceImport = namespaceElement;

            return result;
        }