public static MemberWithAccess FromDeclaredElement(IDeclaredElement declaredElement)
        {
            Contract.Requires(declaredElement != null);

            var clrDeclaredElement = declaredElement as IClrDeclaredElement;

            if (clrDeclaredElement == null)
            {
                return(null);
            }

            var accessRightsOwner = declaredElement as IAccessRightsOwner;

            if (accessRightsOwner == null)
            {
                return(null);
            }


            var declaringTypeAccessRights = declaredElement.GetDeclarations()
                                            .FirstOrDefault()
                                            .With(x => x.GetContainingNode <IClassLikeDeclaration>())
                                            .With(x => (AccessRights?)x.GetAccessRights());

            if (declaringTypeAccessRights == null)
            {
                return(null);
            }

            return(new MemberWithAccess(clrDeclaredElement, declaringTypeAccessRights.Value,
                                        GetMemberType(declaredElement),
                                        accessRightsOwner.GetAccessRights()));
        }
        public static MemberWithAccess FromDeclaredElement(IDeclaredElement declaredElement)
        {
            Contract.Requires(declaredElement != null);

            var clrDeclaredElement = declaredElement as IClrDeclaredElement;
            if (clrDeclaredElement == null)
                return null;

            var accessRightsOwner = declaredElement as IAccessRightsOwner;
            if (accessRightsOwner == null)
                return null;


            var declaringTypeAccessRights = declaredElement.GetDeclarations()
                .FirstOrDefault()
                .With(x => x.GetContainingNode<IClassLikeDeclaration>())
                .With(x => (AccessRights?)x.GetAccessRights());

            if (declaringTypeAccessRights == null)
                return null;

            return new MemberWithAccess(clrDeclaredElement, declaringTypeAccessRights.Value,
                GetMemberType(declaredElement), 
                accessRightsOwner.GetAccessRights());
        }
Exemple #3
0
        private static ITreeNode GetCaptureHighlightingRange(
            [NotNull] ITreeNode topDeclaration, [CanBeNull] IParametersOwner thisElement, [NotNull] IDeclaredElement capture, out DocumentRange range)
        {
            var declarations = capture.GetDeclarations();

            if (declarations.Count == 0) // accessors 'value' parameter
            {
                if (thisElement is IAccessor accessor && Equals(accessor.ValueVariable, capture))
                {
                    var identifier = ((IAccessorDeclaration)topDeclaration).NameIdentifier;
                    range = identifier.GetDocumentRange();
                    return(identifier);
                }

                range = DocumentRange.InvalidRange;
                return(null);
            }

            var declaration = declarations[0];

            range = declaration.GetNameDocumentRange();
#if RESHARPER2016_3
            var nameEndOffset = range.EndOffset;
#else
            var nameEndOffset = range.TextRange.EndOffset;
#endif

            if (declaration is ILocalVariableDeclaration variableDeclaration)
            {
                var multiple = MultipleLocalVariableDeclarationNavigator.GetByDeclarator(variableDeclaration);
                if (multiple != null && multiple.Declarators[0] == variableDeclaration)
                {
                    var documentRange = multiple.GetTypeRange();
                    range = documentRange.SetEndTo(nameEndOffset);
                    return(variableDeclaration);
                }

                return(null);
            }

            if (declaration is IRegularParameterDeclaration parameterDeclaration)
            {
                if (range.TextRange.Length < 3)
                {
                    range = parameterDeclaration.TypeUsage.GetDocumentRange().SetEndTo(nameEndOffset);
                }

                return(parameterDeclaration);
            }

            if (declaration is IAnonymousMethodParameterDeclaration anonymousParameter)
            {
                range = anonymousParameter.TypeUsage.GetDocumentRange().SetEndTo(nameEndOffset);
                return(anonymousParameter);
            }

            return(declaration);
        }
        public override void Init()
        {
            try
            {
                this.ClassName = this.ClassSpecifier.DeclaredName;

                this.AstState    = AstState.InClass;
                this.CurrentType = this.ClassName;

                BaseClause baseClause = this.ClassSpecifier.GetBaseClause();

                if (baseClause != null)
                {
                    foreach (BaseSpecifier baseNode in baseClause.BaseSpecifierNodes)
                    {
                        QualifiedBaseTypeReference baseReference = baseNode.QualifiedBaseTypeReferenceNode;

                        if (baseReference != null)
                        {
                            CppQualifiedName qualifiedName = baseReference.GetQualifiedName();

                            this.BaseClass = qualifiedName.GetNameStr();
                        }
                        else
                        {
                            LogManager.Self.Log("PenWebClassSpecifier() baseReference is null");
                        }
                    }
                }
                else
                {
                    LogManager.Self.Log("PenWebClassSpecifier() baseClause is null");
                }

                base.Init();

                this.CppFunctionCatagory = CppFunctionCatagory.ClassDef;

                if (!String.IsNullOrWhiteSpace(this.ClassName) && !String.IsNullOrWhiteSpace(this.BaseClass))
                {
                    this.SaveToJson = true;
                }

                IDeclaredElement declarationElement = this.ClassSpecifier.DeclaredElement;

                foreach (IDeclaration declaration in declarationElement.GetDeclarations())
                {
                }
            }
            catch (Exception e)
            {
                LogManager.Self.Log("PenWebClassSpecifier Exception", e);
            }

            this.ClassSpecifier = null;
        }
        private static TreeOffset GetCaptureStartOffset([NotNull] IDeclaredElement capture)
        {
            var declarations = capture.GetDeclarations();

            if (declarations.Count == 0)
            {
                return(TreeOffset.Zero);
            }

            return(declarations[0].GetTreeStartOffset());
        }
Exemple #6
0
        static TextRange GetTextRange(IDeclaredElement field)
        {
            var declarations = field.GetDeclarations();

            if (declarations.Count > 0)
            {
                return(declarations[0].GetDocumentRange().TextRange);
            }

            return(new TextRange());
        }
Exemple #7
0
        public override UnitTestElementDisposition GetDisposition()
        {
            IDeclaredElement element = GetDeclaredElement();

            if (element == null || !element.IsValid())
            {
#if RESHARPER_31 || RESHARPER_40 || RESHARPER_41
                return(UnitTestElementDisposition.ourInvalidDisposition);
#else
                return(UnitTestElementDisposition.InvalidDisposition);
#endif
            }

            List <UnitTestElementLocation> locations = new List <UnitTestElementLocation>();

            foreach (IDeclaration declaration in element.GetDeclarations())
            {
                if (declaration.IsValid())
                {
                    IFile file = declaration.GetContainingFile();

                    if (file != null && file.IsValid())
                    {
#if !RESHARPER_50_OR_NEWER
                        var nameRange = declaration.GetNameRange();
#else
                        var nameRange = declaration.GetNameDocumentRange().TextRange;
#endif
                        var containingRange = declaration.GetDocumentRange().TextRange;
#if RESHARPER_31 || RESHARPER_40 || RESHARPER_41
                        if (nameRange.IsValid && containingRange.IsValid)
#elif RESHARPER_45
                        if (nameRange.IsValid() && containingRange.IsValid())
#else
                        if (nameRange.IsValid && containingRange.IsValid)
#endif
                        {
                            locations.Add(new UnitTestElementLocation(
#if RESHARPER_31
                                              file.ProjectItem,
#else
                                              file.ProjectFile,
#endif
                                              nameRange, containingRange));
                        }
                    }
                }
            }

            return(new UnitTestElementDisposition(locations, this));
        }
        public static IEnumerable <string> GetPossibleSourceNames([NotNull] IDeclaredElement element)
        {
            var names = new List <string> {
                element.ShortName
            };

            var constructor = element as IConstructor;
            var typeElement = constructor?.GetContainingType();

            if (typeElement != null)
            {
                names.Add(typeElement.ShortName);
            }

            if (element is ITypeElement type)
            {
                var typeShortName = type.ShortName;
                if (typeShortName.EndsWith(FSharpImplUtil.AttributeSuffix))
                {
                    names.Add(typeShortName.SubstringBeforeLast(FSharpImplUtil.AttributeSuffix, StringComparison.Ordinal));
                }

                names.AddRange(
                    FSharpTypeAbbreviationsUtil.AbbreviatedTypes.TryGetValue(type.GetClrName(), EmptyArray <string> .Instance));
            }
            if (element is IAttributesOwner attrOwner)
            {
                if (GetAttributeValue(attrOwner, SourceNameAttributeAttr) is string sourceName)
                {
                    names.Add(sourceName);
                }
                if (GetAttributeValue(attrOwner, CompilationRepresentationAttr) is int reprFlag && reprFlag == ModuleSuffixFlag)
                {
                    names.Add(element.ShortName.SubstringBeforeLast(ModuleSuffix, StringComparison.Ordinal));
                }
            }

            foreach (var declaration in element.GetDeclarations())
            {
                if (declaration is IFSharpDeclaration fsDeclaration)
                {
                    names.Add(fsDeclaration.SourceName);
                }
            }

            // todo: type abbreviations
            return(names);
        }
        public virtual IEnumerable <UnitTestElementLocation> GetLocations(IDeclaredElement element)
        {
            var locations = new List <UnitTestElementLocation>();

            element.GetDeclarations().ForEach(declaration =>
            {
                IFile file = declaration.GetContainingFile();
                if (file != null)
                {
                    locations.Add(new UnitTestElementLocation(
                                      file.GetSourceFile().ToProjectFile(),
                                      declaration.GetNameDocumentRange().TextRange,
                                      declaration.GetDocumentRange().TextRange));
                }
            });
            return(locations);
        }
        public static IEnumerable <string> GetPossibleSourceNames([NotNull] IDeclaredElement element)
        {
            var names = new HashSet <string> {
                element.ShortName
            };

            var typeElement = (element as IConstructor)?.GetContainingType();

            if (typeElement != null)
            {
                GetPossibleSourceNames(typeElement, names);
            }

            if (element is ITypeElement type)
            {
                GetPossibleSourceNames(type, names);
            }

            if (element is IAttributesOwner attrOwner)
            {
                if (GetAttributeValue(attrOwner, SourceNameAttributeAttr) is string sourceName)
                {
                    names.Add(sourceName);
                }
                if (GetAttributeValue(attrOwner, CompilationRepresentationAttr) is int reprFlag && reprFlag == ModuleSuffixFlag)
                {
                    names.Add(element.ShortName.SubstringBeforeLast(ModuleSuffix, StringComparison.Ordinal));
                }
            }

            foreach (var declaration in element.GetDeclarations())
            {
                if (declaration is IFSharpDeclaration fsDeclaration)
                {
                    names.Add(fsDeclaration.SourceName);
                }
            }

            var result = new List <string>(names.Count * 2);

            result.AddRange(names);
            result.AddRange(names.Select(n => $"``{n}``"));

            return(result);
        }
        public override UnitTestElementDisposition GetDisposition()
        {
            IDeclaredElement element = GetDeclaredElement();

            if (element != null && element.IsValid())
            {
                var locations = new List <UnitTestElementLocation>();
                foreach (IDeclaration declaration in element.GetDeclarations())
                {
                    IFile file = declaration.GetContainingFile();
                    if (file != null)
                    {
                        locations.Add(new UnitTestElementLocation(file.ProjectFile, declaration.GetNameDocumentRange().TextRange, declaration.GetDocumentRange().TextRange));
                    }
                }
                return(new UnitTestElementDisposition(locations, this));
            }
            return(UnitTestElementDisposition.InvalidDisposition);
        }
Exemple #12
0
        public static IEnumerable <ProjectFileTextRange> GetProjectFileTextRangesByDeclaredElement(ISolution solution,
                                                                                                   IDeclaredElement declaredElement)
        {
            IEnumerable <IDeclaration> declarations = declaredElement.GetDeclarations();
            var navigationRanges =
                declarations.Select(declaration => declaration.GetNavigationRange()).Where(range => range.IsValid());
            var psiModule       = declarations.Select(decl => decl.GetPsiModule()).FirstOrDefault();
            var documentManager = solution.GetComponent <DocumentManager>();

            return(navigationRanges.Select(range =>
            {
                var projectFile = documentManager.TryGetProjectFile(range.Document);

                return projectFile == null
                  ? ProjectFileTextRange.Invalid
                  : new ProjectFileTextRange(projectFile, range.TextRange,
                                             psiModule != null ? psiModule.TargetFrameworkId : null);
            }).Where(range => range.IsValid));
        }
Exemple #13
0
        public override void Init()
        {
            try
            {
                this.ItemName = this.InitDeclarator.DeclaredName;

                IDeclaredElement declaredElement = this.InitDeclarator.DeclaredElement;

                if (declaredElement != null)
                {
                    foreach (IDeclaration declaration in declaredElement.GetDeclarations())
                    {
                        IDeclaredElement childDeclaredElement = declaration.DeclaredElement;
                        string           childDeclarationName = declaration.DeclaredName;
                    }
                }

                ICppExpressionsArgumentListNode arguementList = this.InitDeclarator.ArgumentList;

                if (arguementList != null)
                {
                    foreach (ITreeNode attribute in arguementList.Children())
                    {
                        var typeId = attribute.GetType().Name;
                        var text   = attribute.GetText();
                    }
                }


                base.Init();

                this.CppFunctionCatagory = CppFunctionCatagory.None;
                this.SaveToJson          = true;
            }
            catch (Exception e)
            {
                LogManager.Self.Log("PenWebInitDeclarator Exception", e);
            }

            this.InitDeclarator = null;
        }
        /// <summary>
        /// Gets the location associated with a declared element.
        /// </summary>
        /// <param name="declaredElement">The declared element.</param>
        /// <returns>The resulting code location.</returns>
        protected static CodeLocation GetDeclaredElementSourceLocation(IDeclaredElement declaredElement)
        {
            IList <IDeclaration> decl = declaredElement.GetDeclarations();

            if (decl.Count == 0)
            {
                return(CodeLocation.Unknown);
            }

            ReSharperDocumentRange range = decl[0].GetDocumentRange();

#if RESHARPER_31 || RESHARPER_40 || RESHARPER_41
            bool isValid = range.IsValid;
#else
            bool isValid = range.IsValid();
#endif
            if (isValid)
            {
                return(CodeLocation.Unknown);
            }

#if RESHARPER_60_OR_NEWER
            var projectFile = decl[0].GetSourceFile().ToProjectFile();
#else
            var projectFile = decl[0].GetProjectFile();
#endif
            if (projectFile == null)
            {
                return(CodeLocation.Unknown);
            }

            string         filename = projectFile.Location.FullPath;
            DocumentCoords start    = range.Document.GetCoordsByOffset(range.TextRange.StartOffset);

#if !RESHARPER_50_OR_NEWER
            return(new CodeLocation(filename, start.Line, start.Column));
#else
            return(new CodeLocation(filename, (int)start.Line.Plus1(), (int)start.Column.Plus1()));
#endif
        }
Exemple #15
0
        //------------------------------------------------------------------------------------------------------------------------
        private static IList <IProjectFile> GetProjectFiles(DocumentManager documentManager, IDeclaredElement declaredElement)
        {
            IList <IProjectFile> results = new List <IProjectFile>();

            foreach (var declaration in declaredElement.GetDeclarations())
            {
                DocumentRange documentRange = declaration.GetNavigationRange();
                if (!documentRange.IsValid())
                {
                    documentRange = TreeNodeExtensions.GetDocumentRange(declaration);
                }

                if (documentRange.IsValid())
                {
                    IProjectFile projectFile = documentManager.TryGetProjectFile(documentRange.Document);
                    if (projectFile != null)
                    {
                        results.Add(projectFile);
                    }
                }
            }
            return(results);
        }
        public IEnumerable <Pair <IDeclaredElement, Predicate <FindResult> > > GetRelatedDeclaredElements(IDeclaredElement element)
        {
            var uriIdentifier = element as IUriIdentifierDeclaredElement;

            if (uriIdentifier != null)
            {
                var declarations       = element.GetDeclarations();
                var actualDecalrations = NTriplesIdentifierFilter.GetTypeDeclarations(declarations).ToArray();
                if (!actualDecalrations.Any())
                {
                    var subjects =
                        declarations.OfType <IUriIdentifierDeclaredElement>().Where(d => d.GetKind() == IdentifierKind.Subject).ToArray();
                    if (subjects.Any())
                    {
                        actualDecalrations = subjects;
                    }
                    else
                    {
                        actualDecalrations = new[] { (IUriIdentifierDeclaredElement)declarations.First() };
                    }
                }

                Func <IDeclaration, Pair <IDeclaredElement, Predicate <FindResult> > > selector =
                    e => new Pair <IDeclaredElement, Predicate <FindResult> >(e.DeclaredElement, JetPredicate <FindResult> .True);
                if (actualDecalrations.Any())
                {
                    return(actualDecalrations.Cast <IDeclaration>().Select(selector));
                }

                if (declarations.Any())
                {
                    return(declarations.Select(selector));
                }
            }

            return(new[] { new Pair <IDeclaredElement, Predicate <FindResult> >(element, JetPredicate <FindResult> .True) });
        }
        public override UnitTestElementDisposition GetDisposition()
        {
            IDeclaredElement element = GetDeclaredElement();

            if (element == null || !element.IsValid())
            {
                return(UnitTestElementDisposition.InvalidDisposition);
            }

            var locations = new List <UnitTestElementLocation>();

            element.GetDeclarations().ForEach(declaration =>
            {
                IFile file = declaration.GetContainingFile();
                if (file != null)
                {
                    locations.Add(new UnitTestElementLocation(file.ProjectFile,
                                                              declaration.GetNavigationRange().TextRange,
                                                              declaration.GetDocumentRange().TextRange));
                }
            });

            return(new UnitTestElementDisposition(locations, this));
        }
    static TextRange GetTextRange(IDeclaredElement field)
    {
      var declarations = field.GetDeclarations();
      if (declarations.Count > 0)
      {
        return declarations[0].GetDocumentRange().TextRange;
      }

      return new TextRange();
    }