Ejemplo n.º 1
0
        /// <summary>
        /// Tries the fix the issues passed in <paramref name="issues"/>.
        /// </summary>
        /// <param name="issues">The issues to fix.</param>
        /// <returns>The fix issues.</returns>
        public IEnumerable <ActionSummary> TryFixIssues(IEnumerable <ActionSummary> actions)
        {
            if (actions == null)
            {
                throw new ArgumentNullException("actions");
            }

            // enumerate once
            var actionSummaries = actions as IList <ActionSummary> ?? actions.ToArray();
            var issueSummaries  = actionSummaries.Select(action => action.IssueSummary).ToArray();
            var files           = issueSummaries.Select(issue => issue.File).ToList();

            monitor.BeginTask("Applying fixes", files.Count);

            var appliedActions = new List <ActionSummary> (issueSummaries.Length);

            Parallel.ForEach(files, file => {
                monitor.Step(1);

                var fileSummaries = issueSummaries.Where(summary => summary.File == file);
                var inspectorIds  = new HashSet <string> (fileSummaries.Select(summary => summary.InspectorIdString));

                bool hadBom;
                Encoding encoding;
                bool isOpen;
                var data = TextFileProvider.Instance.GetTextEditorData(file.FilePath, out hadBom, out encoding, out isOpen);
                object refactoringContext;
                var realActions = GetIssues(data, file, inspectorIds, out refactoringContext).SelectMany(issue => issue.Actions).ToList();
                if (realActions.Count == 0 || refactoringContext == null)
                {
                    return;
                }

                var fileActionSummaries = actionSummaries.Where(summary => summary.IssueSummary.File == file).ToList();
                var matches             = matcher.Match(fileActionSummaries, realActions).ToList();

                var appliedFixes = RefactoringService.ApplyFixes(matches.Select(match => match.Action), refactoringContext);
                appliedActions.AddRange(matches.Where(match => appliedFixes.Contains(match.Action)).Select(match => match.Summary));

                if (!isOpen)
                {
                    // If the file is open we leave it to the user to explicitly save the file
                    TextFileUtility.WriteText(file.Name, data.Text, encoding, hadBom);
                }
            });
            return(appliedActions);
        }