Пример #1
0
        /// <summary>
        /// Process clean-up on file.
        /// </summary>
        /// <param name="projectFile">
        /// The project file to process.
        /// </param>
        /// <param name="rangeMarker">
        /// The range marker to process.
        /// </param>
        /// <param name="profile">
        /// The code cleanup settings to use.
        /// </param>
        /// <param name="progressIndicator">
        /// The progress indicator.
        /// </param>
        public void Process(
            IPsiSourceFile projectFile, IRangeMarker rangeMarker, CodeCleanupProfile profile, JetBrains.Application.Progress.IProgressIndicator progressIndicator)
        {
            if (!this.IsAvailable(projectFile))
            {
                return;
            }

            ISolution solution = projectFile.GetSolution();

            ICSharpFile file = projectFile.GetDominantPsiFile <CSharpLanguage>() as ICSharpFile;

            if (file == null)
            {
                return;
            }

            StyleCopCodeCleanupOptions options = profile.GetSetting(Descriptor);

            if (!options.FixViolations)
            {
                return;
            }

            var services = solution.GetPsiServices();

            services.Transactions.Execute("Code cleanup", () => this.InternalProcess(projectFile.ToProjectFile(), file, options.CreateXmlDocStubs));

            StyleCopTrace.Out();
        }
Пример #2
0
        /// <summary>
        /// Runs the analysis on a second thread.
        /// </summary>
        public void AnalyzeProc()
        {
            StyleCopTrace.In();

            try
            {
                if (this.full)
                {
                    this.core.FullAnalyze(this.projects);
                }
                else
                {
                    this.core.Analyze(this.projects);
                }
            }
            finally
            {
                if (this.Complete != null)
                {
                    this.Complete(this, new EventArgs());
                }
            }

            StyleCopTrace.Out();
        }
        /// <summary>
        /// Process clean-up on file.
        /// </summary>
        /// <param name="projectFile">
        /// The project file to process.
        /// </param>
        /// <param name="rangeMarker">
        /// The range marker to process.
        /// </param>
        /// <param name="profile">
        /// The code cleanup settings to use.
        /// </param>
        /// <param name="progressIndicator">
        /// The progress indicator.
        /// </param>
        public void Process(
            IPsiSourceFile projectFile, IRangeMarker rangeMarker, CodeCleanupProfile profile, JetBrains.Application.Progress.IProgressIndicator progressIndicator)
        {
            if (!this.IsAvailable(projectFile))
            {
                return;
            }

            ISolution solution = projectFile.GetSolution();

            ICSharpFile file = projectFile.GetDominantPsiFile <CSharpLanguage>() as ICSharpFile;

            if (file == null)
            {
                return;
            }


            if (!profile.GetSetting(FIX_VIOLATIONS))
            {
                return;
            }

            var services = solution.GetPsiServices();

            services.Transactions.Execute("Code cleanup", () => this.InternalProcess(projectFile.ToProjectFile(), file, profile.GetSetting(CREATE_XML_DOC_STUB)));

            StyleCopTrace.Out();
        }
Пример #4
0
        /// <summary>
        /// Runs StyleCop.
        /// </summary>
        /// <param name="document">
        /// The document we are checking.
        /// </param>
        private void RunStyleCop(IDocument document)
        {
            StyleCopTrace.In(document);

            try
            {
                CodeProject[] projects = Utils.GetProjects(this.StyleCopCore, this.file, document);

                string settingsFile = this.styleCopSettings.FindSettingsFilePath(this.file);
                StyleCopTrace.Info("In:  core.Analyze()");

                this.styleCopSettings.LoadSettingsFiles(projects, settingsFile);

                this.StyleCopCore.FullAnalyze(projects);

                StyleCopTrace.Info("Out: core.Analyze()");
            }
            catch (Exception exception)
            {
                JB::JetBrains.Util.Logger.LogException(exception);
            }
            finally
            {
                StyleCopTrace.Out();
            }
        }
Пример #5
0
        /// <summary>
        /// Process clean-up on file.
        /// </summary>
        /// <param name="projectFile">
        /// The project file to process.
        /// </param>
        /// <param name="rangeMarker">
        /// The range marker to process.
        /// </param>
        /// <param name="profile">
        /// The code cleanup settings to use.
        /// </param>
        /// <param name="progressIndicator">
        /// The progress indicator.
        /// </param>
        public void Process(
            IPsiSourceFile projectFile, IRangeMarker rangeMarker, CodeCleanupProfile profile, JB::JetBrains.Application.Progress.IProgressIndicator progressIndicator)
        {
            if (projectFile == null)
            {
                return;
            }

            if (!this.IsAvailable(projectFile))
            {
                return;
            }

            ISolution solution = projectFile.GetSolution();

            ICSharpFile file = projectFile.GetPsiFile <CSharpLanguage>() as ICSharpFile;

            if (file == null)
            {
                return;
            }

            PsiManager.GetInstance(solution).DoTransaction(() => this.InternalProcess(profile, file), "Code cleanup");

            StyleCopTrace.Out();
        }
Пример #6
0
        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="committer">
        /// The committer.
        /// </param>
        public void Execute(Action <DaemonStageResult> committer)
        {
            StyleCopTrace.In();
            try
            {
                if (this.daemonProcess == null)
                {
                    return;
                }

                if (this.daemonProcess.InterruptFlag)
                {
                    return;
                }

                DaemonData daemonData;
                bool       shouldProcessNow;

                // TODO: Lock proper object. But what?
                lock (this.file)
                {
                    daemonData = this.file.UserData.GetOrCreateData(
                        DaemonDataKey,
                        () => new DaemonData(this.lifetime, this.threading, this.daemon, this.daemonProcess.Document));
                    shouldProcessNow = daemonData.OnDaemonCalled();
                }

                if (shouldProcessNow)
                {
                    Lifetimes.Using(
                        apiLifetime =>
                    {
                        var runner = this.apiPool.GetInstance(apiLifetime).Runner;
                        runner.Execute(
                            this.daemonProcess.SourceFile.ToProjectFile(),
                            this.daemonProcess.Document,
                            this.file);

                        // TODO: Why is this a copy?
                        // Uh-oh. Looks like StyleCopRunnerInt shouldn't be shared. Need to check history
                        List <HighlightingInfo> violations =
                            (from info in runner.ViolationHighlights
                             select new HighlightingInfo(info.Range, info.Highlighting)).ToList();

                        committer(new DaemonStageResult(violations));
                    });
                }
                else
                {
                    // NOTE: This wouldn't be necessary if the StyleCop analysis were more lightweight,
                    // e.g. by using ReSharper's ASTs rather than re-parsing the file on each change
                    daemonData.ScheduleReHighlight();
                }
            }
            catch (JetBrains.Application.Progress.ProcessCancelledException)
            {
            }

            StyleCopTrace.Out();
        }
Пример #7
0
        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);
        }
Пример #8
0
        /// <summary>
        /// Called when the analyze thread has completed.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void AnalyzeCompleteMain(object sender, EventArgs e)
        {
            Param.AssertNotNull(sender, "sender");
            Param.Ignore(e);

            StyleCopTrace.In(sender, e);

            var pane = VSWindows.GetInstance(this.serviceProvider).OutputPane;

            if (pane != null)
            {
                if (this.core.Cancel)
                {
                    pane.OutputLine(string.Format(CultureInfo.InvariantCulture, Strings.MiniLogBreak, Strings.Cancelled));
                }
                else
                {
                    pane.OutputLine(string.Format(CultureInfo.InvariantCulture, Strings.MiniLogBreak, Strings.Done));
                    pane.OutputLine(string.Format(CultureInfo.InvariantCulture, Strings.ViolationCount, this.violationCount));
                }
            }

            if (this.violationCount > 0)
            {
                this.ProvideEndAnalysisResult(this.violations);
                this.violations = null;
            }

            StyleCopTrace.Out();
        }
        /// <summary>
        /// Searches directories of the project items project file and the parents thereof to see
        /// if a Settings file exists.
        /// </summary>
        /// <param name="projectItem">
        /// File being examined.
        /// </param>
        /// <returns>
        /// Path to the settings file.
        /// </returns>
        private string FindSettingsFilePath(IProjectItem projectItem)
        {
            StyleCopTrace.In(projectItem);

            string cacheKey = projectItem.Location.FullPath.ToLowerInvariant();

            string settings;

            if (this.settingsFilePathForProjectFile.TryGetValue(cacheKey, out settings))
            {
                StyleCopTrace.Out();

                return(settings);
            }

            IProject projectFile = projectItem.GetProject();

            string result = this.FindSettingsFilePath(projectFile);

            // TODO: This makes no sense
            // We cache, per source file location, the location of the settings.stylecop file.
            // We only look for the settings.stylecop file at the project root folder and above.
            // But we add a file watcher at the source file location
            // this.AddWatcherForSettingsFile(projectItem.Location.FullPath);
            this.settingsFilePathForProjectFile[cacheKey] = result;

            return(StyleCopTrace.Out(result));
        }
Пример #10
0
        /// <summary>
        /// Executes the cleanup rules.
        /// </summary>
        /// <param name="options">
        /// The options.
        /// </param>
        /// <param name="file">
        /// The file to process.
        /// </param>
        public void Execute(ReadabilityOptions options, ICSharpFile file)
        {
            StyleCopTrace.In(options, file);
            bool dontPrefixCallsWithBaseUnlessLocalImplementationExists = options.SA1100DoNotPrefixCallsWithBaseUnlessLocalImplementationExists;
            bool codeMustNotContainEmptyStatements             = options.SA1106CodeMustNotContainEmptyStatements;
            bool blockStatementsMustNotContainEmbeddedComments = options.SA1108BlockStatementsMustNotContainEmbeddedComments;
            bool blockStatementsMustNotContainEmbeddedRegions  = options.SA1109BlockStatementsMustNotContainEmbeddedRegions;
            bool commentsMustContainText        = options.SA1120CommentsMustContainText;
            bool useBuiltInTypeAlias            = options.SA1121UseBuiltInTypeAlias;
            bool useStringEmptyForEmptyStrings  = options.SA1122UseStringEmptyForEmptyStrings;
            bool dontPlaceRegionsWithinElements = options.SA1123DoNotPlaceRegionsWithinElements;
            bool codeMustNotContainEmptyRegions = options.SA1124CodeMustNotContainEmptyRegions;

            if (dontPlaceRegionsWithinElements)
            {
                this.DoNotPlaceRegionsWithinElements(file.FirstChild);
            }

            if (blockStatementsMustNotContainEmbeddedComments)
            {
                this.BlockStatementsMustNotContainEmbeddedComments(file.FirstChild);
            }

            if (blockStatementsMustNotContainEmbeddedRegions)
            {
                this.BlockStatementsMustNotContainEmbeddedRegions(file.FirstChild);
            }

            if (codeMustNotContainEmptyStatements)
            {
                this.CodeMustNotContainEmptyStatements(file.FirstChild);
            }

            if (codeMustNotContainEmptyRegions)
            {
                this.CodeMustNotContainEmptyRegions(file.FirstChild);
            }

            if (useStringEmptyForEmptyStrings)
            {
                this.ReplaceEmptyStringsWithStringDotEmpty(file.FirstChild);
            }

            if (useBuiltInTypeAlias)
            {
                SwapToBuiltInTypeAlias(file.FirstChild);
            }

            if (commentsMustContainText)
            {
                RemoveEmptyComments(file.FirstChild);
            }

            if (dontPrefixCallsWithBaseUnlessLocalImplementationExists)
            {
                this.DoNotPrefixCallsWithBaseUnlessLocalImplementationExists(file.FirstChild);
            }

            StyleCopTrace.Out();
        }
Пример #11
0
        /// <summary>
        /// Runs the analyzers against the given document.
        /// </summary>
        /// <param name="document">
        /// The document to analyze.
        /// </param>
        /// <param name="parser">
        /// The parser that created the document.
        /// </param>
        /// <param name="analyzers">
        /// The analyzers to run against the document.
        /// </param>
        /// <param name="passNumber">
        /// The current pass number.
        /// </param>
        /// <returns>
        /// Returns true if analysis was run, or false if analysis was delayed until the next pass.
        /// </returns>
        private bool TestAndRunAnalyzers(CodeDocument document, SourceParser parser, IEnumerable <SourceAnalyzer> analyzers, int passNumber)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parser, "parser");
            Param.Ignore(analyzers);
            Param.Ignore(passNumber);
            StyleCopTrace.In(document, parser, analyzers, passNumber);

            if (analyzers == null)
            {
                return(StyleCopTrace.Out(true));
            }

            // Determine whether any of the analyzers wish to delay analysis until the next pass.
            bool delay = false;

            foreach (SourceAnalyzer analyzer in analyzers)
            {
                if (analyzer.DelayAnalysis(document, passNumber))
                {
                    delay = true;
                    break;
                }
            }

            if (!delay)
            {
                this.RunAnalyzers(document, parser, analyzers);
            }

            return(StyleCopTrace.Out(!delay));
        }
Пример #12
0
        /// <summary>
        /// Executes <see cref="styleCopCore"/> within the <see cref="OnViolationEncountered"/>.
        /// </summary>
        /// <remarks>
        /// Violations are raised as events, handled by <see cref="IProjectFile"/>.
        /// </remarks>
        /// <param name="projectFile">
        /// <see cref="StyleCopStageProcess"/>representing the file currently being parsed by ReSharper.
        /// </param>
        /// <param name="document">
        /// The document being checked.
        /// </param>
        public void Execute(IProjectFile projectFile, IDocument document)
        {
            StyleCopTrace.In(projectFile, document);

            if (projectFile == null)
            {
                return;
            }

            this.Initialize();

            this.violationHighlights.Clear();

            if (!this.styleCopSettings.SkipAnalysisForDocument(projectFile))
            {
                FileHeader fileHeader = new FileHeader(Utils.GetCSharpFile(projectFile.GetSolution(), document));

                if (!fileHeader.UnStyled && StyleCopReferenceHelper.EnsureStyleCopIsLoaded())
                {
                    this.file     = projectFile;
                    this.document = document;
                    this.RunStyleCop(document);
                }
            }

            StyleCopTrace.Out();
        }
Пример #13
0
        /// <summary>
        /// Initialises this IShellComponent.
        /// </summary>
        public void Init()
        {
            if (StyleCopOptions.Instance.AutomaticallyCheckForUpdates)
            {
                var lastUpdateCheckDate     = DateTime.Parse(StyleCopOptions.Instance.LastUpdateCheckDate);
                var daysBetweenUpdateChecks = StyleCopOptions.Instance.DaysBetweenUpdateChecks;

                if (StyleCopOptions.Instance.AlwaysCheckForUpdatesWhenVisualStudioStarts || DateTime.UtcNow > lastUpdateCheckDate.AddDays(daysBetweenUpdateChecks))
                {
                    try
                    {
                        new StyleCop.AutoUpdater().CheckForUpdate(QuestionText);

                        if (!StyleCopOptions.Instance.AlwaysCheckForUpdatesWhenVisualStudioStarts)
                        {
                            StyleCopOptions.Instance.LastUpdateCheckDate = DateTime.UtcNow.ToString("yyyy-MM-dd");
                        }
                    }
                    catch (Exception exception)
                    {
                        StyleCopTrace.Info(exception.Message);
                    }
                }
            }
        }
Пример #14
0
        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="committer">
        /// The committer.
        /// </param>
        public void Execute(Action <DaemonStageResult> committer)
        {
            StyleCopTrace.In();

            // inverse the performance value - to ensure that "more resources" actually evaluates to a lower number
            // whereas "less resources" actually evaluates to a higher number. If Performance is set to max, then execute as normal.
            if ((StyleCopOptions.Instance.ParsingPerformance == StyleCopStageProcess.MaxPerformanceValue) ||
                (performanceStopWatch.Elapsed > new TimeSpan(0, 0, 0, StyleCopStageProcess.MaxPerformanceValue - StyleCopOptions.Instance.ParsingPerformance)))
            {
                if (this.daemonProcess.InterruptFlag)
                {
                    return;
                }

                if (!this.FileIsValid())
                {
                    return;
                }

                styleCopRunnerInternal.Execute(this.daemonProcess.SourceFile.ToProjectFile(), this.daemonProcess.Document);

                List <HighlightingInfo> violations =
                    (from info in styleCopRunnerInternal.ViolationHighlights
                     let range = info.Range
                                 let highlighting = info.Highlighting
                                                    select new HighlightingInfo(range, highlighting)).ToList();

                committer(new DaemonStageResult(violations));

                ResetPerformanceStopWatch();
            }

            StyleCopTrace.Out();
        }
Пример #15
0
        /// <summary>
        /// Adds a list of violations to the task provider.
        /// </summary>
        /// <param name="violations">The list of violations to add.</param>
        public void AddResults(List <ViolationInfo> violations)
        {
            Param.AssertNotNull(violations, "violations");
            StyleCopTrace.In(violations);

            this.SuspendRefresh();

            var hierarchyItems = new Dictionary <string, IVsHierarchy>();

            for (int index = 0; index < violations.Count; index++)
            {
                ViolationInfo violation     = violations[index];
                IVsHierarchy  hierarchyItem = hierarchyItems.ContainsKey(violation.File) ? hierarchyItems[violation.File] : null;

                var task = new ViolationTask(this.serviceProvider, violation, hierarchyItem);

                hierarchyItem = task.HierarchyItem;

                if (!hierarchyItems.ContainsKey(violation.File))
                {
                    hierarchyItems.Add(violation.File, hierarchyItem);
                }

                this.Tasks.Add(task);
            }

            this.ResumeRefresh();

            StyleCopTrace.Out();
        }
Пример #16
0
        /// <summary>
        /// Executes <see cref="styleCopCore"/> within the <see cref="OnViolationEncountered"/>.
        /// </summary>
        /// <remarks>
        /// Violations are raised as events, handled by <see cref="IProjectFile"/>.
        /// </remarks>
        /// <param name="projectFile">
        /// <see cref="StyleCopStageProcess"/>representing the file currently being parsed by ReSharper.
        /// </param>
        /// <param name="document">
        /// The document being checked.
        /// </param>
        /// <param name="file">
        /// The file to analyze.
        /// </param>
        public void Execute(IProjectFile projectFile, IDocument document, ICSharpFile file)
        {
            StyleCopTrace.In(projectFile, document);

            if (projectFile == null)
            {
                return;
            }

            if (this.StyleCopCore != null)
            {
                this.violationHighlights.Clear();

                if (!this.styleCopSettings.SkipAnalysisForDocument(projectFile))
                {
                    FileHeader fileHeader = new FileHeader(file);

                    if (!fileHeader.UnStyled && StyleCopReferenceHelper.EnsureStyleCopIsLoaded())
                    {
                        this.file     = projectFile;
                        this.document = document;
                        this.RunStyleCop(document);
                    }
                }
            }

            StyleCopTrace.Out();
        }
Пример #17
0
        /// <summary>
        /// This method provides a <see cref="IDaemonStageProcess"/> instance which is assigned to highlighting a single document.
        /// </summary>
        /// <param name="process">
        /// Current Daemon Process.
        /// </param>
        /// <param name="settingsStore">
        /// The settingsStore store to use.
        /// </param>
        /// <param name="processKind">
        /// The process kind.
        /// </param>
        /// <param name="file">
        /// The file to analyze.
        /// </param>
        /// ///
        /// <returns>
        /// The current <see cref="IDaemonStageProcess"/>.
        /// </returns>
        protected override IDaemonStageProcess CreateProcess(
            IDaemonProcess process, IContextBoundSettingsStore settingsStore, DaemonProcessKind processKind, ICSharpFile file)
        {
            StyleCopTrace.In(process, settingsStore, processKind, file);

            if (process == null)
            {
                throw new ArgumentNullException("process");
            }

            try
            {
                if (processKind == DaemonProcessKind.OTHER)
                {
                    StyleCopTrace.Info("ProcessKind Other.");
                    StyleCopTrace.Out();
                    return(null);
                }

                if (!this.IsAnalysisEnabled(settingsStore, file))
                {
                    StyleCopTrace.Info("Analysis disabled.");
                    StyleCopTrace.Out();
                    return(null);
                }

                if (!this.IsSupported(process.SourceFile))
                {
                    StyleCopTrace.Info("File type not supported.");
                    StyleCopTrace.Out();
                    return(null);
                }

                if (!this.FileIsValid(file))
                {
                    StyleCopTrace.Info("Source file not valid.");
                    StyleCopTrace.Out();
                    return(null);
                }

                if (!settingsStore.GetValue <StyleCopOptionsSettingsKey, bool>(key => key.AnalyzeReadOnlyFiles))
                {
                    if (process.SourceFile.Properties.IsNonUserFile)
                    {
                        StyleCopTrace.Info("Not analysing non user files.");
                        StyleCopTrace.Out();
                        return(null);
                    }
                }

                IDaemon daemon = file.GetSolution().GetComponent <IDaemon>();

                return(StyleCopTrace.Out(new StyleCopStageProcess(this.lifetime, this.apiPool, daemon, process, this.threading, file)));
            }
            catch (JetBrains.Application.Progress.ProcessCancelledException)
            {
                return(null);
            }
        }
Пример #18
0
        /// <summary>
        /// Runs the list of analyzers against the given document.
        /// </summary>
        /// <param name="document">
        /// The document to analyze.
        /// </param>
        /// <param name="parser">
        /// The parser that created the document.
        /// </param>
        /// <param name="analyzers">
        /// The list of analyzers to run against the document.
        /// </param>
        private void RunAnalyzers(CodeDocument document, SourceParser parser, IEnumerable <SourceAnalyzer> analyzers)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parser, "parser");
            Param.Ignore(analyzers, "analyzers");
            StyleCopTrace.In(document, parser, analyzers);

            if (analyzers != null)
            {
                if (parser.SkipAnalysisForDocument(document.SourceCode))
                {
                    string format = string.Format(CultureInfo.CurrentCulture, "Skipping: {0} - {1}", document.SourceCode.Project.Location.SubstringAfterLast('\\'), GetRelativeFileName(document.SourceCode));
                    this.data.Core.SignalOutput(MessageImportance.Normal, format);
                }
                else
                {
                    // Loop through each of the parser's analyzers.
                    // Only call analyzers that are also in the enabled list.
                    foreach (SourceAnalyzer analyzer in parser.Analyzers)
                    {
                        SourceAnalyzer localAnalyzer = analyzer;
                        if (analyzers.Any(enabledAnalyzers => enabledAnalyzers.Id == localAnalyzer.Id))
                        {
                            // Make sure the user hasn't cancelled us.
                            if (this.data.Core.Cancel)
                            {
                                break;
                            }

                            SourceParser.ClearAnalyzerTags(document);
                            try
                            {
                                if (analyzer.DoAnalysis(document))
                                {
                                    analyzer.AnalyzeDocument(document);
                                }
                            }
                            catch (Exception ex)
                            {
                                StringBuilder details = new StringBuilder();
                                details.AppendLine(string.Format(CultureInfo.CurrentCulture, "Exception thrown by analyzer '{0}' while processing '{1}'.", analyzer.Name, document.SourceCode.Path));

                                // Add exception message for help on bugfix.
                                if (!string.IsNullOrEmpty(ex.Message))
                                {
                                    details.AppendLine(string.Format(CultureInfo.CurrentCulture, "Exception message : {0}", ex.Message));
                                }

                                this.data.Core.SignalOutput(MessageImportance.High, details.ToString());
                                throw;
                            }
                        }
                    }
                }
            }

            StyleCopTrace.Out();
        }
Пример #19
0
        /// <summary>
        /// The skip analysis for document.
        /// </summary>
        /// <param name="projectFile">
        /// The project file.
        /// </param>
        /// <returns>
        /// True if analysis should be skipped.
        /// </returns>
        public bool SkipAnalysisForDocument(IProjectFile projectFile)
        {
            StyleCopTrace.In(projectFile);

            string cacheKey = string.Format("{0}::{1}", "SkipAnalysisForDocument", projectFile.Location.FullPath.ToLowerInvariant());

            bool result;

            if (BoolCache.TryGetValue(cacheKey, out result))
            {
                StyleCopTrace.Out();
                return(result);
            }

            if (projectFile.Name.EndsWith(".cs"))
            {
                IContextBoundSettingsStore settingsStore = projectFile.ToSourceFile().GetSettingsStore();
                if (!settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.UseExcludeFromStyleCopSetting) || !projectFile.ProjectFileIsExcludedFromStyleCop())
                {
                    bool analyzeDesignerFiles  = true;
                    bool analyzeGeneratedFiles = false;

                    BooleanProperty analyzeDesignerFilesSetting = this.GetParserSetting(projectFile, "AnalyzeDesignerFiles") as BooleanProperty;

                    if (analyzeDesignerFilesSetting != null)
                    {
                        analyzeDesignerFiles = analyzeDesignerFilesSetting.Value;
                    }

                    if (analyzeDesignerFiles || !projectFile.Name.EndsWith(".Designer.cs", StringComparison.OrdinalIgnoreCase))
                    {
                        BooleanProperty analyzeGeneratedFilesSetting = this.GetParserSetting(projectFile, "AnalyzeGeneratedFiles") as BooleanProperty;

                        if (analyzeGeneratedFilesSetting != null)
                        {
                            analyzeGeneratedFiles = analyzeGeneratedFilesSetting.Value;
                        }

                        if (analyzeGeneratedFiles ||
                            (!projectFile.Name.EndsWith(".g.cs", StringComparison.OrdinalIgnoreCase) &&
                             !projectFile.Name.EndsWith(".generated.cs", StringComparison.OrdinalIgnoreCase)))
                        {
                            BoolCache[cacheKey] = false;

                            StyleCopTrace.Out();

                            return(false);
                        }
                    }
                }
            }

            BoolCache[cacheKey] = true;

            StyleCopTrace.Out();

            return(true);
        }
Пример #20
0
        /// <summary>
        /// The Execute method.
        /// </summary>
        /// <param name="options">
        /// The options.
        /// </param>
        /// <param name="file">
        /// The file.
        /// </param>
        public void Execute(LayoutOptions options, ICSharpFile file)
        {
            StyleCopTrace.In(options, file);

            Param.RequireNotNull(options, "options");
            Param.RequireNotNull(file, "file");

            bool curlyBracketsForMultiLineStatementsMustNotShareLine  = options.SA1500CurlyBracketsForMultiLineStatementsMustNotShareLine;
            bool openingCurlyBracketsMustNotBePrecededByBlankLine     = options.SA1509OpeningCurlyBracketsMustNotBePrecededByBlankLine;
            bool chainedStatementBlocksMustNotBePrecededByBlankLine   = options.SA1510ChainedStatementBlocksMustNotBePrecededByBlankLine;
            bool whileDoFooterMustNotBePrecededByBlankLine            = options.SA1511WhileDoFooterMustNotBePrecededByBlankLine;
            bool singleLineCommentsMustNotBeFollowedByBlankLine       = options.SA1512SingleLineCommentsMustNotBeFollowedByBlankLine;
            bool closingCurlyBracketMustBeFollowedByBlankLine         = options.SA1513ClosingCurlyBracketMustBeFollowedByBlankLine;
            bool elementDocumentationHeadersMustBePrecededByBlankLine = options.SA1514ElementDocumentationHeaderMustBePrecededByBlankLine;
            bool singleLineCommentsMustBeProceededByBlankLine         = options.SA1515SingleLineCommentMustBeProceededByBlankLine;

            if (singleLineCommentsMustBeProceededByBlankLine)
            {
                this.CommentsMustBePreceededByBlankLine(file.FirstChild);
            }

            if (singleLineCommentsMustNotBeFollowedByBlankLine)
            {
                this.CommentsMustNotBeFollowedByBlankLine(file.FirstChild);
            }

            if (closingCurlyBracketMustBeFollowedByBlankLine)
            {
                ClosingCurlyBracketMustBeFollowedByBlankLine(file.FirstChild);
            }

            if (whileDoFooterMustNotBePrecededByBlankLine)
            {
                this.WhileDoFooterMustNotBePrecededByBlankLine(file.FirstChild);
            }

            if (chainedStatementBlocksMustNotBePrecededByBlankLine)
            {
                this.ChainedStatementBlocksMustNotBePrecededByBlankLine(file.FirstChild);
            }

            if (openingCurlyBracketsMustNotBePrecededByBlankLine)
            {
                this.OpeningCurlyBracketsMustNotBePrecededByBlankLine(file.FirstChild);
            }

            if (elementDocumentationHeadersMustBePrecededByBlankLine)
            {
                this.ElementDocumentationHeadersMustBePrecededByBlankLine(file.FirstChild);
            }

            if (curlyBracketsForMultiLineStatementsMustNotShareLine)
            {
                this.CurlyBracketsForMultiLineStatementsMustNotShareLine(file.FirstChild);
            }

            StyleCopTrace.Out();
        }
Пример #21
0
        /// <summary>
        /// Called when a file being watched gets renamed.
        /// </summary>
        /// <param name="source">
        /// The object being renamed.
        /// </param>
        /// <param name="e">
        /// The RenamedEventArgs for the file.
        /// </param>
        private static void FileRenamed(object source, RenamedEventArgs e)
        {
            StyleCopTrace.In(source, e);

            StringCache.Clear();
            SettingsCache.Clear();

            StyleCopTrace.Out();
        }
Пример #22
0
        /// <summary>
        /// Called when a file being watched changes.
        /// </summary>
        /// <param name="source">
        /// The object being changed.
        /// </param>
        /// <param name="e">
        /// The FileSystemEventArgs for the file.
        /// </param>
        private static void FileChanged(object source, FileSystemEventArgs e)
        {
            StyleCopTrace.In(source, e);

            StringCache.Clear();
            SettingsCache.Clear();

            StyleCopTrace.Out();
        }
Пример #23
0
        /// <summary>
        /// Run the OrderingRules Fix.
        /// </summary>
        /// <param name="options">
        /// OrderingOptions for the Fix.
        /// </param>
        /// <param name="file">
        /// File that the fix will be performed on.
        /// </param>
        public void Execute(OrderingOptions options, ICSharpFile file)
        {
            StyleCopTrace.In(options, file);

            OrderUsings(options, file);

            this.OrderPropertyIndexerAndEventDeclarations(options, file);
            StyleCopTrace.Out();
        }
Пример #24
0
        /// <summary>
        /// Initializes a new instance of the StyleCopStageProcess class, using the specified <see cref="IDaemonProcess"/> .
        /// </summary>
        /// <param name="daemonProcess">
        /// <see cref="IDaemonProcess"/> to execute within.
        /// </param>
        public StyleCopStageProcess(IDaemonProcess daemonProcess)
        {
            StyleCopTrace.In(daemonProcess);

            this.daemonProcess = daemonProcess;
            InitialiseTimers();

            StyleCopTrace.Out();
        }
        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="committer">
        /// The committer.
        /// </param>
        public void Execute(Action <DaemonStageResult> committer)
        {
            StyleCopTrace.In();
            try
            {
                if (this.daemonProcess == null)
                {
                    return;
                }

                if (this.daemonProcess.InterruptFlag)
                {
                    return;
                }

                DaemonData daemonData;
                bool       shouldProcessNow;

                daemonData = this.file.UserData.GetOrCreateDataUnderLock(
                    DaemonDataKey,
                    () => new DaemonData(this.lifetime, this.threading, this.daemon, this.daemonProcess.Document));
                shouldProcessNow = daemonData.OnDaemonCalled();

                if (shouldProcessNow)
                {
                    Lifetimes.Using(
                        apiLifetime =>
                    {
                        var runner = this.apiPool.GetInstance(apiLifetime).Runner;
                        runner.Execute(
                            this.daemonProcess.SourceFile.ToProjectFile(),
                            this.daemonProcess.Document,
                            this.file);

                        // This filters the highlights, based on "ReSharper disable" comments, etc.
                        var filteringConsumer = new FilteringHighlightingConsumer(this, this.settings, this.file);
                        foreach (var highlightingInfo in runner.ViolationHighlights)
                        {
                            filteringConsumer.ConsumeHighlighting(highlightingInfo);
                        }

                        committer(new DaemonStageResult(filteringConsumer.Highlightings));
                    });
                }
                else
                {
                    // NOTE: This wouldn't be necessary if the StyleCop analysis were more lightweight,
                    // e.g. by using ReSharper's ASTs rather than re-parsing the file on each change
                    daemonData.ScheduleReHighlight();
                }
            }
            catch (JetBrains.Application.Progress.ProcessCancelledException)
            {
            }

            StyleCopTrace.Out();
        }
Пример #26
0
        /// <summary>
        /// This method provides a <see cref="IDaemonStageProcess"/> instance which is assigned to highlighting a single document.
        /// </summary>
        /// <param name="process">
        /// Current Daemon Process.
        /// </param>
        /// <param name="settingsStore">
        /// The settingsStore store to use.
        /// </param>
        /// <param name="processKind">
        /// The process kind.
        /// </param>
        /// <returns>
        /// The current <see cref="IDaemonStageProcess"/>.
        /// </returns>
        public override IDaemonStageProcess CreateProcess(IDaemonProcess process, IContextBoundSettingsStore settingsStore, DaemonProcessKind processKind)
        {
            StyleCopTrace.In(process, settingsStore, processKind);

            if (process == null)
            {
                throw new ArgumentNullException("process");
            }

            try
            {
                if (processKind == DaemonProcessKind.OTHER)
                {
                    StyleCopTrace.Info("ProcessKind Other.");
                    StyleCopTrace.Out();
                    return(null);
                }

                if (!settingsStore.GetValue <StyleCopOptionsSettingsKey, bool>(key => key.AnalysisEnabled))
                {
                    StyleCopTrace.Info("Analysis disabled.");
                    StyleCopTrace.Out();
                    return(null);
                }

                if (!this.IsSupported(process.SourceFile))
                {
                    StyleCopTrace.Info("File type not supported.");
                    StyleCopTrace.Out();
                    return(null);
                }

                if (!this.FileIsValid(process.SourceFile))
                {
                    StyleCopTrace.Info("Source file not valid.");
                    StyleCopTrace.Out();
                    return(null);
                }

                if (!settingsStore.GetValue <StyleCopOptionsSettingsKey, bool>(key => key.AnalyzeReadOnlyFiles))
                {
                    if (process.SourceFile.Properties.IsNonUserFile)
                    {
                        StyleCopTrace.Info("Not analysing non user files.");
                        StyleCopTrace.Out();
                        return(null);
                    }
                }

                return(StyleCopTrace.Out(new StyleCopStageProcess(process, settingsStore)));
            }
            catch (ProcessCancelledException)
            {
                return(null);
            }
        }
Пример #27
0
        /// <summary>
        /// The create.
        /// </summary>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <param name="project">
        /// The project.
        /// </param>
        /// <param name="parser">
        /// The parser.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// A new StringBasedSourceCode object.
        /// </returns>
        public SourceCode Create(string path, CodeProject project, SourceParser parser, object context)
        {
            StyleCopTrace.In();

            string source = (string)context;

            StyleCopTrace.Out();

            return(new StringBasedSourceCode(project, parser, path, source));
        }
Пример #28
0
        /// <summary>
        /// The create.
        /// </summary>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <param name="project">
        /// The project.
        /// </param>
        /// <param name="parser">
        /// The parser.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// A new StringBasedSourceCode object.
        /// </returns>
        public SourceCode Create(string path, CodeProject project, SourceParser parser, object context)
        {
            StyleCopTrace.In();

            string source = (string)context;

            SourceCode sourceCode = source == null ? (SourceCode) new CodeFile(path, project, parser) : new StringBasedSourceCode(project, parser, path, source);

            return(StyleCopTrace.Out(sourceCode));
        }
Пример #29
0
        /// <summary>
        /// Provides the end analysis result to the user by adding violations to the error list.
        /// </summary>
        /// <param name="violations">The violations.</param>
        protected override void ProvideEndAnalysisResult(List <ViolationInfo> violations)
        {
            Param.RequireNotNull(violations, "violations");
            StyleCopTrace.In(violations);

            this.TaskProvider.AddResults(violations);
            this.TaskProvider.Show();

            StyleCopTrace.Out();
        }
Пример #30
0
        /// <summary>
        /// Returns true if the project file has been excluded from stylecop in the csproj file.
        /// </summary>
        /// <param name="projectFile">
        /// The IProjectFile to check.
        /// </param>
        /// <returns>
        /// True if its been excluded.
        /// </returns>
        public static bool ProjectFileIsExcludedFromStyleCop(this IProjectFile projectFile)
        {
            StyleCopTrace.In();

            if (projectFile == null)
            {
                StyleCopTrace.Out();

                return(false);
            }

            IProject project = projectFile.GetProject();

            if (project == null)
            {
                StyleCopTrace.Out();

                return(false);
            }

            if (project.ProjectFile == null)
            {
                StyleCopTrace.Out();

                return(false);
            }

            try
            {
                XmlDocument xmlDocument = new XmlDocument();

                string relativePathToCsFile = projectFile.Location.ConvertToRelativePath(project.Location).ToString();

                xmlDocument.Load(project.ProjectFile.Location.FullPath);

                XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
                namespaceManager.AddNamespace("a", "http://schemas.microsoft.com/developer/msbuild/2003");
                XmlNode xmlNode =
                    xmlDocument.SelectSingleNode(
                        string.Format("//a:Project/a:ItemGroup/a:Compile[@Include='{0}'][a:ExcludeFromStyleCop='true']", relativePathToCsFile), namespaceManager);

                if (xmlNode != null)
                {
                    StyleCopTrace.Out();

                    return(true);
                }
            }
            catch
            {
            }

            StyleCopTrace.Out();
            return(false);
        }