private bool FileIsValid()
        {
            PsiManager manager = PsiManager.GetInstance(this.daemonProcess.Solution);

            if (this.daemonProcess.ProjectFile == null)
            {
                return(false);
            }

            if (!this.daemonProcess.ProjectFile.IsValid)
            {
                return(false);
            }

            IFile file = manager.GetPsiFile(this.daemonProcess.ProjectFile, PsiLanguageType.GetByProjectFile(this.daemonProcess.ProjectFile));

            if (file == null)
            {
                return(false);
            }

            bool hasErrorElements = new RecursiveElementCollector <ErrorElement>().ProcessElement(file).GetResults().Any();

            StyleCopTrace.Info("File has error elements = {0}", hasErrorElements);

            return(!hasErrorElements);
        }
    public IEnumerable<RelatedFileOccurence> GetRelatedFiles(IProjectFile projectFile)
    {
      var typeNamesInFile = GetTypeNamesDefinedInFile(projectFile).ToList();

      // Look for the candidate types in the solution.
      var solution = projectFile.GetSolution();
      var candidateTypes = new LocalList<IClrDeclaredElement>();
      foreach (var candidateTypeName in GetTypeCandidates(typeNamesInFile))
      {
        var types = FindTypesByShortName(solution, candidateTypeName);
        candidateTypes.AddRange(types);
      }

      // Get the source files for each of the candidate types.
      var sourceFiles = new LocalList<IPsiSourceFile>();
      foreach (var type in candidateTypes)
      {
        var sourceFilesForCandidateType = type.GetSourceFiles();
        sourceFiles.AddRange(sourceFilesForCandidateType.ResultingList());
      }

      var elementCollector = new RecursiveElementCollector<ITypeDeclaration>();
      foreach (var psiSourceFile in sourceFiles)
      foreach (var file in psiSourceFile.EnumerateDominantPsiFiles())
      {
        elementCollector.ProcessElement(file);
      }

      var elements = elementCollector.GetResults();
      var projectFiles = elements.Select(declaration => declaration.GetSourceFile().ToProjectFile());

      var thisProjectName = projectFile.GetProject()?.Name;

      var occurences = new LocalList<RelatedFileOccurence>();

      foreach (var file in projectFiles.OfType<ProjectFileImpl>().Distinct(x => x.Location.FullPath))
      {
        // Remove all extensions (e.g.: .xaml.cs).
        var fileName = file.Name;
        var dotPos = fileName.IndexOf('.');
        if (dotPos != -1)
        {
          fileName = fileName.Substring(0, dotPos);
        }

        var relationKind = fileName.EndsWith("ViewModel") ? "ViewModel" : "View";

        var projectName = file.GetProject()?.Name;

        if (projectName != null &&
            !string.Equals(thisProjectName, projectName, StringComparison.OrdinalIgnoreCase))
        {
          relationKind += $" (in {projectName})";
        }

        occurences.Add(new RelatedFileOccurence(file, relationKind, projectFile));
      }

      return occurences.ReadOnlyList();
    }
Exemple #3
0
        public IEnumerable <JetTuple <IProjectFile, string, IProjectFile> > GetRelatedFiles(IProjectFile projectFile)
        {
            var typeNamesInFile = GetTypeNamesDefinedInFile(projectFile).ToList();

            var candidateTypeNames = GetTypeCandidates(typeNamesInFile);

            // Look for the candidate types throught the solution.
            var solution       = projectFile.GetSolution();
            var candidateTypes = new List <IClrDeclaredElement>();

            foreach (var candidateTypeName in candidateTypeNames)
            {
                var types = FindType(solution, candidateTypeName);
                candidateTypes.AddRange(types);
            }

            // Get the source files for each of the candidate types.
            var sourceFiles = new List <IPsiSourceFile>();

            foreach (var type in candidateTypes)
            {
                var sourceFilesForCandidateType = type.GetSourceFiles();
                sourceFiles.AddRange(sourceFilesForCandidateType);
            }

            var elementCollector = new RecursiveElementCollector <ITypeDeclaration>();

            foreach (var psiSourceFile in sourceFiles)
            {
                foreach (var file in psiSourceFile.EnumerateDominantPsiFiles())
                {
                    elementCollector.ProcessElement(file);
                }
            }

            var elements = elementCollector.GetResults();
            IEnumerable <IProjectFile> projectFiles = elements.Select(declaration => declaration.GetSourceFile().ToProjectFile());

            var rval = new List <JetTuple <IProjectFile, string, IProjectFile> >();

            foreach (var file in projectFiles.OfType <ProjectFileImpl>().Distinct(pf => pf.Location.FullPath))
            {
                // Remove all extensions (e.g.: .xaml.cs).
                var fn     = file.Name;
                var dotPos = fn.IndexOf('.');
                if (dotPos != -1)
                {
                    fn = fn.Substring(0, dotPos);
                }
                var display = fn.EndsWith("ViewModel") ? "ViewModel" : "View";

                var tuple = JetTuple.Of((IProjectFile)file, display, projectFile);

                rval.Add(tuple);
            }

            return(rval);
        }
        private void CollectPrefixes()
        {
            var declarations = new RecursiveElementCollector <PrefixDeclaration>().ProcessElement(this).GetResults();

            foreach (var declaration in declarations)
            {
                string s = declaration.DeclaredName;
                this.myPrefixes[s] = declaration.DeclaredElement;
            }
        }
Exemple #5
0
        /// <summary>
        /// Checks the given file is valid to check.
        /// </summary>
        /// <param name="sourceFile">
        /// THe file to check.
        /// </param>
        /// <returns>
        /// True if its valid.
        /// </returns>
        private bool FileIsValid(ICSharpFile sourceFile)
        {
            if (sourceFile == null)
            {
                return(false);
            }

            bool hasErrorElements = new RecursiveElementCollector <IErrorElement>(null).ProcessElement(sourceFile).GetResults().Any();

            return(!hasErrorElements);
        }
Exemple #6
0
        /// <summary>
        /// Checks the given file is valid to check.
        /// </summary>
        /// <param name="sourceFile">
        /// THe file to check.
        /// </param>
        /// <returns>
        /// True if its valid.
        /// </returns>
        private bool FileIsValid(IPsiSourceFile sourceFile)
        {
            if (sourceFile == null)
            {
                return(false);
            }

            ICSharpFile file = CSharpDaemonStageBase.GetPsiFile(sourceFile);

            if (file == null)
            {
                return(false);
            }

            bool hasErrorElements = new RecursiveElementCollector <IErrorElement>(null).ProcessElement(file).GetResults().Any();

            return(!hasErrorElements);
        }
Exemple #7
0
        private bool FileIsValid()
        {
            PsiManager manager = PsiManager.GetInstance(this.daemonProcess.Solution);

            if (!this.daemonProcess.SourceFile.ToProjectFile().IsValid())
            {
                return(false);
            }

            IFile file = this.daemonProcess.SourceFile.GetPsiFile(CSharpLanguage.Instance);

            if (file == null)
            {
                return(false);
            }

            bool hasErrorElements = new RecursiveElementCollector <ErrorElement>().ProcessElement(file).GetResults().Any();

            return(!hasErrorElements);
        }
        private void CollectUriIdentifiers()
        {
            var uriIdentifiers =
                new RecursiveElementCollector <IUriIdentifier>()
                .ProcessElement(this)
                .GetResults();

            foreach (var uriIdentifier in uriIdentifiers)
            {
                var fullName = uriIdentifier.GetUri();
                if (fullName != null)
                {
                    IList <IDeclaredElement> elements;
                    if (!this.myUriIdentifiers.TryGetValue(fullName, out elements))
                    {
                        this.myUriIdentifiers[fullName] = elements = new List <IDeclaredElement>();
                    }

                    elements.Add(uriIdentifier.DescendantDeclaredElement);
                }
            }
        }
        private static bool IsOrCanBecameIterator([NotNull] ICSharpFunctionDeclaration declaration,
                                                  [NotNull] IType returnType)
        {
            if (declaration.IsIterator)
            {
                return(true);
            }
            if (declaration.IsAsync)
            {
                return(false);
            }

            if (returnType.IsGenericIEnumerable() || returnType.IsIEnumerable() ||
                returnType.IsGenericIEnumerator() || returnType.IsIEnumerator())
            {
                var collector = new RecursiveElementCollector <IReturnStatement>();
                collector.ProcessElement(declaration);
                return(collector.GetResults().Count == 0);
            }

            return(false);
        }
        private bool FileIsValid()
        {
            PsiManager manager = PsiManager.GetInstance(this.daemonProcess.Solution);

            if (this.daemonProcess.ProjectFile == null)
            {
                return false;
            }

            if (!this.daemonProcess.ProjectFile.IsValid)
            {
                return false;
            }

            IFile file = manager.GetPsiFile(this.daemonProcess.ProjectFile, PsiLanguageType.GetByProjectFile(this.daemonProcess.ProjectFile));

            if (file == null)
            {
                return false;
            }

            bool hasErrorElements = new RecursiveElementCollector<ErrorElement>().ProcessElement(file).GetResults().Any();
            StyleCopTrace.Info("File has error elements = {0}", hasErrorElements);

            return !hasErrorElements;
        }
        private static ICSharpExpression FixExpression(
      [NotNull] ICSharpExpression expression, [CanBeNull] ITreeNode reference)
        {
            var referenceExpression = reference as IReferenceExpression;
              if (referenceExpression != null)
              {
            var marker = new TreeNodeMarker<IReferenceExpression>(referenceExpression);
            var exprCopy = expression.Copy(expression);
            var refCopy = marker.GetAndDispose(exprCopy);

            var exprToFix = refCopy.QualifierExpression.NotNull();
            LowLevelModificationUtil.ReplaceChildRange(refCopy, refCopy, exprToFix);

            if (exprToFix.NextSibling is IErrorElement)
              LowLevelModificationUtil.DeleteChild(exprToFix.NextSibling);

            return exprCopy;
              }

              var referenceName = reference as IReferenceName;
              if (referenceName != null)
              {
            var marker = new TreeNodeMarker<IReferenceName>(referenceName);
            var exprCopy = expression.Copy(expression);
            var refCopy = marker.GetAndDispose(exprCopy);

            var exprToFix = refCopy.Qualifier.NotNull();
            LowLevelModificationUtil.ReplaceChildRange(refCopy, refCopy, exprToFix);

            if (exprToFix.NextSibling is IErrorElement)
              LowLevelModificationUtil.DeleteChild(exprToFix.NextSibling);

            if (exprToFix.Parent != null && exprToFix.Parent.NextSibling is IErrorElement)
              LowLevelModificationUtil.DeleteChild(exprToFix.Parent.NextSibling);

            return exprCopy;
              }
              else
              {
            var errorsCollector = new RecursiveElementCollector<IErrorElement>();
            expression.ProcessDescendants(errorsCollector);
            var errorElements = errorsCollector.GetResults().Select(
              errorElement => new TreeNodeMarker<IErrorElement>(errorElement)).ToList();

            var exprCopy = expression.Copy(expression);
            foreach (var errorMarker in errorElements)
            {
              var element = errorMarker.GetAndDispose(exprCopy);
              LowLevelModificationUtil.DeleteChild(element);
            }

            return exprCopy;
              }
        }
Exemple #12
0
        private string TryFindUri(NTriplesPrefixReference reference)
        {
            var file = (INTriplesFile)this.myElement.GetContainingFile();

            if (file == null)
            {
                throw new FormatException("The element has no file assigned.");
            }

            var name = reference.GetName();

            if (!string.IsNullOrEmpty(name))
            {
                // Looking for declarations of the same name
                var cache = this.myElement.GetSolution().GetComponent <NTriplesCache>();
                var uris  = cache.GetPrefixDeclarationSymbols(name).Select(s => s.Uri).Distinct().ToArray();
                if (uris.Length == 0)
                {
                    return(null);
                }

                if (uris.Length == 1)
                {
                    return(uris[0]);
                }

                // narrowing the search using local names
                var prefixesWithSameName =
                    new RecursiveElementCollector <IPrefixName>(p => p.GetText() == name).ProcessElement(file).GetResults();
                var localNamesInThePrefix =
                    prefixesWithSameName.Select(p => ((IUriIdentifier)p.Parent).GetLocalName())
                    .Distinct()
                    .Where(n => !string.IsNullOrEmpty(n))
                    .ToArray();

                if (localNamesInThePrefix.Length == 0)
                {
                    return(uris[0]);
                }

                var bestUri    = uris[0];
                int bestMetric = 0;
                foreach (var uri in uris)
                {
                    var metric =
                        cache.GetAllUriIdentifiersInNamespace(uri)
                        .Select(s => s.LocalName)
                        .Intersect(localNamesInThePrefix)
                        .Count();
                    if (metric > bestMetric)
                    {
                        bestMetric = metric;
                        bestUri    = uri;
                    }
                }

                return(bestUri);
            }

            return(null);
        }
        private bool FileIsValid()
        {
            PsiManager manager = PsiManager.GetInstance(this.daemonProcess.Solution);

            if (!this.daemonProcess.SourceFile.ToProjectFile().IsValid())
            {
                return false;
            }

            IFile file = this.daemonProcess.SourceFile.GetPsiFile(CSharpLanguage.Instance);

            if (file == null)
            {
                return false;
            }

            bool hasErrorElements = new RecursiveElementCollector<ErrorElement>().ProcessElement(file).GetResults().Any();

            return !hasErrorElements;
        }
 private void CollectPrefixes()
 {
     var declarations = new RecursiveElementCollector<PrefixDeclaration>().ProcessElement(this).GetResults();
     foreach (var declaration in declarations)
     {
         string s = declaration.DeclaredName;
         this.myPrefixes[s] = declaration.DeclaredElement;
     }
 }
        private void CollectUriIdentifiers()
        {
            var uriIdentifiers =
                new RecursiveElementCollector<IUriIdentifier>()
                    .ProcessElement(this)
                    .GetResults();
            foreach (var uriIdentifier in uriIdentifiers)
            {
                var fullName = uriIdentifier.GetUri();
                if (fullName != null)
                {
                    IList<IDeclaredElement> elements;
                    if (!this.myUriIdentifiers.TryGetValue(fullName, out elements))
                    {
                        this.myUriIdentifiers[fullName] = elements = new List<IDeclaredElement>();
                    }

                    elements.Add(uriIdentifier.DescendantDeclaredElement);
                }
            }
        }
        /// <summary>
        /// Checks the given file is valid to check.
        /// </summary>
        /// <param name="sourceFile">
        /// THe file to check.
        /// </param>
        /// <returns>
        /// True if its valid.
        /// </returns>
        private bool FileIsValid(IPsiSourceFile sourceFile)
        {
            if (sourceFile == null)
            {
                return false;
            }

            ICSharpFile file = CSharpDaemonStageBase.GetPsiFile(sourceFile);

            if (file == null)
            {
                return false;
            }

            bool hasErrorElements = new RecursiveElementCollector<IErrorElement>(null).ProcessElement(file).GetResults().Any();

            return !hasErrorElements;
        }
        public IEnumerable<Tuple<IProjectFile, string, IProjectFile>> GetRelatedFiles(IProjectFile projectFile)
        {
            var typeNamesInFile = GetTypeNamesDefinedInFile(projectFile).ToList();

            var candidateTypeNames = GetTypeCandidates(typeNamesInFile);

            // Look for the candidate types in the solution.
            var solution = projectFile.GetSolution();
            var candidateTypes = new List<IClrDeclaredElement>();
            foreach (var candidateTypeName in candidateTypeNames)
            {
                var types = FindType(solution, candidateTypeName);
                candidateTypes.AddRange(types);
            }

            // Get the source files for each of the candidate types.
            var sourceFiles = new List<IPsiSourceFile>();
            foreach (var type in candidateTypes)
            {
                var sourceFilesForCandidateType = type.GetSourceFiles();
                sourceFiles.AddRange(sourceFilesForCandidateType);
            }

            var elementCollector = new RecursiveElementCollector<ITypeDeclaration>();
            foreach (var psiSourceFile in sourceFiles)
                foreach (var file in psiSourceFile.EnumerateDominantPsiFiles())
                    elementCollector.ProcessElement(file);

            var elements = elementCollector.GetResults();
            var projectFiles = elements.Select(declaration => declaration.GetSourceFile().ToProjectFile());

            var thisProjectName = projectFile.GetProject()?.Name;

            var rval = new List<Tuple<IProjectFile, string, IProjectFile>>();
            foreach (var file in projectFiles.OfType<ProjectFileImpl>().Distinct(pf => pf.Location.FullPath))
            {
                // Remove all extensions (e.g.: .xaml.cs).
                var fn = file.Name;
                var dotPos = fn.IndexOf('.');
                if (dotPos != -1)
                {
                    fn = fn.Substring(0, dotPos);
                }

                var display = fn.EndsWith("ViewModel") ? "ViewModel" : "View";

                var projectName = file.GetProject()?.Name;

                if (projectName != null &&
                    !string.Equals(thisProjectName, projectName, StringComparison.OrdinalIgnoreCase))
                {
                    display += $" (in {projectName})";
                }

                var tuple = Tuple.Create((IProjectFile)file, display, projectFile);

                rval.Add(tuple);
            }

            return rval;
        }
Exemple #18
0
        /// <summary>
        /// Checks the given file is valid to check.
        /// </summary>
        /// <param name="sourceFile">
        /// THe file to check.
        /// </param>
        /// <returns>
        /// True if its valid.
        /// </returns>
        private bool FileIsValid(ICSharpFile sourceFile)
        {
            if (sourceFile == null)
            {
                return false;
            }

            bool hasErrorElements = new RecursiveElementCollector<IErrorElement>(null).ProcessElement(sourceFile).GetResults().Any();

            return !hasErrorElements;
        }
        private string TryFindUri(NTriplesPrefixReference reference)
        {
            var file = (INTriplesFile)this.myElement.GetContainingFile();
            if (file == null)
            {
                throw new FormatException("The element has no file assigned.");
            }

            var name = reference.GetName();

            if (!string.IsNullOrEmpty(name))
            {
                // Looking for declarations of the same name
                var cache = this.myElement.GetSolution().GetComponent<NTriplesCache>();
                var uris = cache.GetPrefixDeclarationSymbols(name).Select(s => s.Uri).Distinct().ToArray();
                if (uris.Length == 0)
                {
                    return null;
                }

                if (uris.Length == 1)
                {
                    return uris[0];
                }

                // narrowing the search using local names
                var prefixesWithSameName =
                    new RecursiveElementCollector<IPrefixName>(p => p.GetText() == name).ProcessElement(file).GetResults();
                var localNamesInThePrefix =
                    prefixesWithSameName.Select(p => ((IUriIdentifier)p.Parent).GetLocalName())
                                        .Distinct()
                                        .Where(n => !string.IsNullOrEmpty(n))
                                        .ToArray();

                if (localNamesInThePrefix.Length == 0)
                {
                    return uris[0];
                }

                var bestUri = uris[0];
                int bestMetric = 0;
                foreach (var uri in uris)
                {
                    var metric =
                        cache.GetAllUriIdentifiersInNamespace(uri)
                             .Select(s => s.LocalName)
                             .Intersect(localNamesInThePrefix)
                             .Count();
                    if (metric > bestMetric)
                    {
                        bestMetric = metric;
                        bestUri = uri;
                    }
                }

                return bestUri;
            }

            return null;
        }