Пример #1
0
 internal virtual void SaveFixLedger()
 {
     if (s_sourceFileFixLedger.Count > 0)
     {
         SdkUiUtilities.StoreObject <Dictionary <string, FixOffsetList> >(s_sourceFileFixLedger, s_sourceFileFixLedgerFileName);
     }
 }
Пример #2
0
        internal virtual void LoadFixLedger()
        {
            if (s_sourceFileFixLedger == null)
            {
                s_sourceFileFixLedger = SdkUiUtilities.GetStoredObject <Dictionary <string, FixOffsetList> >(s_sourceFileFixLedgerFileName) ?? new Dictionary <string, FixOffsetList>();

                // Remove entries where the last modified timestamps don't match
                // This could indicate that the file was modified or overwritten externally
                List <string> remove = s_sourceFileFixLedger
                                       .Where(e => !File.Exists(e.Key) || File.GetLastWriteTime(e.Key) != e.Value.LastModified)
                                       .Select(e => e.Key)
                                       .ToList();

                if (remove.Count > 0)
                {
                    remove.ForEach(p => s_sourceFileFixLedger.Remove(p));
                    SaveFixLedger();
                }
            }
        }
Пример #3
0
        private void SetSeverityHandler(object sender, EventArgs args)
        {
            var selectedItem   = (MenuCommand)sender;
            var selectedAction = MapSelectedItemToReportDiagnostic(selectedItem);

            if (!selectedAction.HasValue)
            {
                return;
            }

            var workspace = TryGetWorkspace() as VisualStudioWorkspaceImpl;

            if (workspace == null)
            {
                return;
            }

            foreach (var selectedDiagnostic in _tracker.SelectedDiagnosticItems)
            {
                var projectId     = selectedDiagnostic.ProjectId;
                var pathToRuleSet = workspace.TryGetRuleSetPathForProject(projectId);

                var project = workspace.CurrentSolution.GetProject(projectId);
                var pathToAnalyzerConfigDoc = project?.TryGetAnalyzerConfigPathForProjectConfiguration();

                if (pathToRuleSet == null && pathToAnalyzerConfigDoc == null)
                {
                    SendUnableToUpdateRuleSetNotification(workspace, SolutionExplorerShim.No_rule_set_file_is_specified_or_the_file_does_not_exist);
                    continue;
                }

                var componentModel     = (IComponentModel)_serviceProvider.GetService(typeof(SComponentModel));
                var waitIndicator      = componentModel.GetService <IWaitIndicator>();
                var editHandlerService = componentModel.GetService <ICodeActionEditHandlerService>();

                try
                {
                    var envDteProject = workspace.TryGetDTEProject(projectId);

                    if (pathToRuleSet == null || SdkUiUtilities.IsBuiltInRuleSet(pathToRuleSet, _serviceProvider))
                    {
                        // If project is using the default built-in ruleset or no ruleset, then prefer .editorconfig for severity configuration.
                        if (pathToAnalyzerConfigDoc != null)
                        {
                            waitIndicator.Wait(
                                title: ServicesVSResources.Updating_severity,
                                message: ServicesVSResources.Updating_severity,
                                allowCancel: true,
                                action: waitContext =>
                            {
                                var newSolution = selectedDiagnostic.GetSolutionWithUpdatedAnalyzerConfigSeverityAsync(selectedAction.Value, project, waitContext.CancellationToken).WaitAndGetResult(waitContext.CancellationToken);
                                var operations  = ImmutableArray.Create <CodeActionOperation>(new ApplyChangesOperation(newSolution));
                                editHandlerService.Apply(
                                    _workspace,
                                    fromDocument: null,
                                    operations: operations,
                                    title: ServicesVSResources.Updating_severity,
                                    progressTracker: waitContext.ProgressTracker,
                                    cancellationToken: waitContext.CancellationToken);
                            });
                            continue;
                        }

                        // Otherwise, fall back to using ruleset.
                        if (pathToRuleSet == null)
                        {
                            SendUnableToUpdateRuleSetNotification(workspace, SolutionExplorerShim.No_rule_set_file_is_specified_or_the_file_does_not_exist);
                            continue;
                        }

                        pathToRuleSet = CreateCopyOfRuleSetForProject(pathToRuleSet, envDteProject);
                        if (pathToRuleSet == null)
                        {
                            SendUnableToUpdateRuleSetNotification(workspace, string.Format(SolutionExplorerShim.Could_not_create_a_rule_set_for_project_0, envDteProject.Name));
                            continue;
                        }

                        var fileInfo = new FileInfo(pathToRuleSet);
                        fileInfo.IsReadOnly = false;
                    }

                    waitIndicator.Wait(
                        title: SolutionExplorerShim.Rule_Set,
                        message: string.Format(SolutionExplorerShim.Checking_out_0_for_editing, Path.GetFileName(pathToRuleSet)),
                        allowCancel: false,
                        action: c =>
                    {
                        if (envDteProject.DTE.SourceControl.IsItemUnderSCC(pathToRuleSet))
                        {
                            envDteProject.DTE.SourceControl.CheckOutItem(pathToRuleSet);
                        }
                    });

                    selectedDiagnostic.SetRuleSetSeverity(selectedAction.Value, pathToRuleSet);
                }
                catch (Exception e)
                {
                    SendUnableToUpdateRuleSetNotification(workspace, e.Message);
                }
            }
        }
Пример #4
0
        private void SetSeverityHandler(object sender, EventArgs args)
        {
            var selectedItem = (MenuCommand)sender;
            ReportDiagnostic?selectedAction = MapSelectedItemToReportDiagnostic(selectedItem);

            if (!selectedAction.HasValue)
            {
                return;
            }

            var workspace = TryGetWorkspace() as VisualStudioWorkspaceImpl;

            if (workspace == null)
            {
                return;
            }

            foreach (var selectedDiagnostic in _tracker.SelectedDiagnosticItems)
            {
                var projectId     = selectedDiagnostic.ProjectId;
                var pathToRuleSet = workspace.TryGetRuleSetPathForProject(projectId);

                if (pathToRuleSet == null)
                {
                    SendUnableToUpdateRuleSetNotification(workspace, SolutionExplorerShim.No_rule_set_file_is_specified_or_the_file_does_not_exist);
                    continue;
                }

                try
                {
                    var envDteProject = workspace.TryGetDTEProject(projectId);

                    if (SdkUiUtilities.IsBuiltInRuleSet(pathToRuleSet, _serviceProvider))
                    {
                        pathToRuleSet = CreateCopyOfRuleSetForProject(pathToRuleSet, envDteProject);
                        if (pathToRuleSet == null)
                        {
                            SendUnableToUpdateRuleSetNotification(workspace, string.Format(SolutionExplorerShim.Could_not_create_a_rule_set_for_project_0, envDteProject.Name));
                            continue;
                        }

                        var fileInfo = new FileInfo(pathToRuleSet);
                        fileInfo.IsReadOnly = false;
                    }

                    var componentModel = (IComponentModel)_serviceProvider.GetService(typeof(SComponentModel));
                    var waitIndicator  = componentModel.GetService <IWaitIndicator>();
                    waitIndicator.Wait(
                        title: SolutionExplorerShim.Rule_Set,
                        message: string.Format(SolutionExplorerShim.Checking_out_0_for_editing, Path.GetFileName(pathToRuleSet)),
                        allowCancel: false,
                        action: c =>
                    {
                        if (envDteProject.DTE.SourceControl.IsItemUnderSCC(pathToRuleSet))
                        {
                            envDteProject.DTE.SourceControl.CheckOutItem(pathToRuleSet);
                        }
                    });

                    selectedDiagnostic.SetSeverity(selectedAction.Value, pathToRuleSet);
                }
                catch (Exception e)
                {
                    SendUnableToUpdateRuleSetNotification(workspace, e.Message);
                }
            }
        }
Пример #5
0
        private async Task SetSeverityHandlerAsync(VisualStudioWorkspaceImpl workspace, MenuCommand selectedItem, ArrayBuilder <string> notificationMessages)
        {
            var componentModel            = (IComponentModel)_serviceProvider.GetService(typeof(SComponentModel));
            var uiThreadOperationExecutor = componentModel.GetService <VSUtilities.IUIThreadOperationExecutor>();

            using var context = uiThreadOperationExecutor.BeginExecute(
                      title: ServicesVSResources.Updating_severity,
                      defaultDescription: "",
                      allowCancellation: true,
                      showProgress: true);

            var selectedAction = MapSelectedItemToReportDiagnostic(selectedItem);

            if (!selectedAction.HasValue)
            {
                return;
            }

            foreach (var selectedDiagnostic in _tracker.SelectedDiagnosticItems)
            {
                var projectId     = selectedDiagnostic.ProjectId;
                var pathToRuleSet = workspace.TryGetRuleSetPathForProject(projectId);

                var project = workspace.CurrentSolution.GetProject(projectId);
                var pathToAnalyzerConfigDoc = project?.TryGetAnalyzerConfigPathForProjectConfiguration();

                if (pathToRuleSet == null && pathToAnalyzerConfigDoc == null)
                {
                    notificationMessages.Add(SolutionExplorerShim.No_rule_set_file_is_specified_or_the_file_does_not_exist);
                    continue;
                }

                var editHandlerService = componentModel.GetService <ICodeActionEditHandlerService>();

                try
                {
                    var envDteProject = workspace.TryGetDTEProject(projectId);

                    if (pathToRuleSet == null || SdkUiUtilities.IsBuiltInRuleSet(pathToRuleSet, _serviceProvider))
                    {
                        // If project is using the default built-in ruleset or no ruleset, then prefer .editorconfig for severity configuration.
                        if (pathToAnalyzerConfigDoc != null)
                        {
                            using var scope1 = context.AddScope(allowCancellation: true, description: "");
                            var newSolution = await selectedDiagnostic.GetSolutionWithUpdatedAnalyzerConfigSeverityAsync(selectedAction.Value, project, context.UserCancellationToken).ConfigureAwait(false);

                            var operations = ImmutableArray.Create <CodeActionOperation>(new ApplyChangesOperation(newSolution));
                            await editHandlerService.ApplyAsync(
                                _workspace,
                                fromDocument : null,
                                operations : operations,
                                title : ServicesVSResources.Updating_severity,
                                progressTracker : new UIThreadOperationContextProgressTracker(scope1),
                                cancellationToken : context.UserCancellationToken).ConfigureAwait(true);

                            continue;
                        }

                        // Otherwise, fall back to using ruleset.
                        if (pathToRuleSet == null)
                        {
                            notificationMessages.Add(SolutionExplorerShim.No_rule_set_file_is_specified_or_the_file_does_not_exist);
                            continue;
                        }

                        pathToRuleSet = CreateCopyOfRuleSetForProject(pathToRuleSet, envDteProject);
                        if (pathToRuleSet == null)
                        {
                            notificationMessages.Add(string.Format(SolutionExplorerShim.Could_not_create_a_rule_set_for_project_0, envDteProject.Name));
                            continue;
                        }

                        var fileInfo = new FileInfo(pathToRuleSet);
                        fileInfo.IsReadOnly = false;
                    }

                    using var scope2 = context.AddScope(
                              allowCancellation: false,
                              string.Format(SolutionExplorerShim.Checking_out_0_for_editing, Path.GetFileName(pathToRuleSet)));

                    if (envDteProject.DTE.SourceControl.IsItemUnderSCC(pathToRuleSet))
                    {
                        envDteProject.DTE.SourceControl.CheckOutItem(pathToRuleSet);
                    }

                    selectedDiagnostic.SetRuleSetSeverity(selectedAction.Value, pathToRuleSet);
                }
                catch (Exception e)
                {
                    notificationMessages.Add(e.Message);
                }
            }
        }
        private void SetSeverityHandler(object sender, EventArgs args)
        {
            var selectedItem = (MenuCommand)sender;
            ReportDiagnostic?selectedAction = MapSelectedItemToReportDiagnostic(selectedItem);

            if (!selectedAction.HasValue)
            {
                return;
            }

            var workspace = TryGetWorkspace() as VisualStudioWorkspaceImpl;

            if (workspace == null)
            {
                return;
            }

            foreach (var selectedDiagnostic in _tracker.SelectedDiagnosticItems)
            {
                var projectId = selectedDiagnostic.AnalyzerItem.AnalyzersFolder.ProjectId;
                var project   = (AbstractProject)workspace.GetHostProject(projectId);

                if (project == null)
                {
                    SendUnableToUpdateRuleSetNotification(workspace, string.Format(SolutionExplorerShim.AnalyzersCommandHandler_CouldNotFindProject, projectId));
                    continue;
                }

                var pathToRuleSet = project.RuleSetFile?.FilePath;

                if (pathToRuleSet == null)
                {
                    SendUnableToUpdateRuleSetNotification(workspace, SolutionExplorerShim.AnalyzersCommandHandler_NoRuleSetFile);
                    continue;
                }

                try
                {
                    EnvDTE.Project envDteProject;
                    project.Hierarchy.TryGetProject(out envDteProject);

                    if (SdkUiUtilities.IsBuiltInRuleSet(pathToRuleSet, _serviceProvider))
                    {
                        pathToRuleSet = CreateCopyOfRuleSetForProject(pathToRuleSet, envDteProject);
                        if (pathToRuleSet == null)
                        {
                            SendUnableToUpdateRuleSetNotification(workspace, string.Format(SolutionExplorerShim.AnalyzersCommandHandler_CouldNotCreateRuleSetFile, envDteProject.Name));
                            continue;
                        }

                        var fileInfo = new FileInfo(pathToRuleSet);
                        fileInfo.IsReadOnly = false;
                    }

                    var componentModel = (IComponentModel)_serviceProvider.GetService(typeof(SComponentModel));
                    var waitIndicator  = componentModel.GetService <IWaitIndicator>();
                    waitIndicator.Wait(
                        title: SolutionExplorerShim.AnalyzersCommandHandler_RuleSet,
                        message: string.Format(SolutionExplorerShim.AnalyzersCommandHandler_CheckingOutRuleSet, Path.GetFileName(pathToRuleSet)),
                        allowCancel: false,
                        action: c =>
                    {
                        if (envDteProject.DTE.SourceControl.IsItemUnderSCC(pathToRuleSet))
                        {
                            envDteProject.DTE.SourceControl.CheckOutItem(pathToRuleSet);
                        }
                    });

                    selectedDiagnostic.SetSeverity(selectedAction.Value, pathToRuleSet);
                }
                catch (Exception e)
                {
                    SendUnableToUpdateRuleSetNotification(workspace, e.Message);
                }
            }
        }