private Rules GetAnalyzerRules(DiagnosticAnalyzer analyzer)
        {
            // For info on SonarQube rules see http://docs.sonarqube.org/display/SONAR/Rules

            Rules rules = new Rules();

            foreach (DiagnosticDescriptor diagnostic in analyzer.SupportedDiagnostics)
            {
                if (String.IsNullOrWhiteSpace(diagnostic.Id))
                {
                    logger.LogWarning(UIResources.RuleGen_EmptyKey, analyzer.ToString());
                    continue;
                }

                Rule newRule = new Rule
                {
                    Key         = diagnostic.Id,
                    InternalKey = diagnostic.Id,

                    Description = GetDescriptionAsRawHtml(diagnostic),

                    Name     = diagnostic.Title.ToString(CultureInfo.InvariantCulture),
                    Severity = GetSonarQubeSeverity(diagnostic.DefaultSeverity),

                    // Rule XML properties that don't have an obvious Diagnostic equivalent:
                    Cardinality = Cardinality,
                    Status      = Status
                };

                // Diagnostic properties that don't have an obvious Rule xml equivalent:
                //  diagnostic.Category
                //  diagnostic.IsEnabledByDefault
                //  diagnostic.MessageFormat

                /* Remark: Custom tags are used so that Visual Studio handles diagnostics and are not equivalent to SonarQube's tags
                 *
                 * http://stackoverflow.com/questions/24257222/relevance-of-new-parameters-for-diagnosticdescriptor-constructor
                 * customTags is a general way to mark that a diagnostic should be treated or displayed somewhat
                 * different than normal diagnostics. The "unnecessary" tag means that in the IDE we fade out the span
                 * that the diagnostic applies to: this is how we fade out unnecessary usings or casts or such in the IDE.
                 * In some fancy scenarios you might want to define your own, but for the most part you'll either leave that empty
                 * or pass Unnecessary if you want the different UI handling.
                 * The EditAndContinue tag is for errors that are created if an edit-and-continue edit can't be applied
                 * (which are also displayed somewhat differently)...that's just for us (n.b. Roslyn) to use.
                 */

                rules.Add(newRule);
            }
            return(rules);
        }
Example #2
0
        private IEnumerable <DiagnosticAnalyzer> InstantiateDiagnosticAnalyzers(Assembly analyserAssembly, string language)
        {
            Debug.Assert(analyserAssembly != null);

            List <DiagnosticAnalyzer> analyzers = new List <DiagnosticAnalyzer>();

            // It is assumed that analyserAssembly is valid. FileNotFoundException will be thrown if dependency resolution fails.
            foreach (Type type in analyserAssembly.GetExportedTypes())
            {
                if (!type.IsAbstract &&
                    type.IsSubclassOf(typeof(DiagnosticAnalyzer)) &&
                    DiagnosticMatchesLanguage(type, language))
                {
                    DiagnosticAnalyzer analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(type);
                    analyzers.Add(analyzer);

                    this.logger.LogDebug(UIResources.Scanner_AnalyzerLoaded, analyzer.ToString());
                }
            }

            return(analyzers);
        }
Example #3
0
 private static string GetProjectLogMessage(Project project, DiagnosticAnalyzer analyzer)
 {
     return(string.Format("project: {0}, {1}", project.FilePath ?? project.Name, analyzer.ToString()));
 }
Example #4
0
 private static string GetSemanticLogMessage(Document document, TextSpan?span, DiagnosticAnalyzer analyzer)
 {
     return(string.Format("semantic: {0}, {1}, {2}", document.FilePath ?? document.Name, span.HasValue ? span.Value.ToString() : "Full", analyzer.ToString()));
 }
 private static string GetProjectLogMessage(Project project, DiagnosticAnalyzer analyzer)
 {
     return string.Format("project: {0}, {1}", project.FilePath ?? project.Name, analyzer.ToString());
 }
 private static string GetSemanticLogMessage(Document document, TextSpan? span, DiagnosticAnalyzer analyzer)
 {
     return string.Format("semantic: {0}, {1}, {2}", document.FilePath ?? document.Name, span.HasValue ? span.Value.ToString() : "Full", analyzer.ToString());
 }
 private static string GetDocumentLogMessage(string title, Document document, DiagnosticAnalyzer analyzer)
 {
     return(string.Format($"{title}: {document.FilePath ?? document.Name}, {analyzer.ToString()}"));
 }
Example #8
0
 private static string GetDocumentLogMessage(string title, Document document, DiagnosticAnalyzer analyzer)
 {
     return($"{title}: ({document.Id}, {document.Project.Id}), ({analyzer.ToString()})");
 }
 private static string GetDocumentLogMessage(string title, Document document, DiagnosticAnalyzer analyzer)
 {
     return string.Format($"{title}: {document.FilePath ?? document.Name}, {analyzer.ToString()}");
 }
 private static string GetDocumentLogMessage(string title, Document document, DiagnosticAnalyzer analyzer)
 {
     return($"{title}: ({document.FilePath ?? document.Name}), ({analyzer.ToString()})");
 }
        /// <summary>
        ///     The initialize.
        /// </summary>
        protected override void Initialize()
        {
            try
            {
                Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this));
                base.Initialize();

                this.SetupMenuCommands();
                this.dte2 = (DTE2)GetGlobalService(typeof(DTE));

                if (this.dte2 == null)
                {
                    return;
                }

                try
                {
                    this.visualStudioInterface = new VsPropertiesHelper(this.dte2, this);
                    this.VsEvents = new VsEvents(this.visualStudioInterface, this.dte2, this);
                    var bar = this.GetService(typeof(SVsStatusbar)) as IVsStatusbar;
                    this.StatusBar = new VSSStatusBar(bar, this.dte2);


                    var extensionRunningPath = Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty).ToString();

                    var uniqueId = this.dte2.Version;

                    if (extensionRunningPath.ToLower().Contains(this.dte2.Version + "exp"))
                    {
                        uniqueId += "Exp";
                    }

                    SonarQubeViewModelFactory.StartupModelWithVsVersion(uniqueId).InitModelFromPackageInitialization(this.visualStudioInterface, this.StatusBar, this, this.AssemblyDirectory);

                    DColor defaultBackground = VSColorTheme.GetThemedColor(EnvironmentColors.ToolWindowBackgroundColorKey);
                    DColor defaultForeground = VSColorTheme.GetThemedColor(EnvironmentColors.ToolWindowTextColorKey);

                    SonarQubeViewModelFactory.SQViewModel.UpdateTheme(ToMediaColor(defaultBackground), ToMediaColor(defaultForeground));


                    // force analysis to come to vsix
                    var i    = 0;
                    var data = string.Empty;
                    foreach (var item in SonarQubeViewModelFactory.StartupModelWithVsVersion(uniqueId).VSonarQubeOptionsViewData.RoslynModel.ExtensionDiagnostics)
                    {
                        i += item.Value.AvailableChecks.Count;

                        foreach (var check in item.Value.AvailableChecks)
                        {
                            DiagnosticAnalyzer analyser = check;
                            data += analyser.ToString();
                        }
                    }

                    this.StatusBar.DisplayMessage("checks : " + i);

                    try
                    {
                        var outWindow  = GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow;
                        var customGuid = new Guid("CDA8E85D-C469-4855-878B-0E778CD0DD53");
                        if (outWindow != null)
                        {
                            outWindow.CreatePane(ref customGuid, "VSSonarQube Output", 1, 1);
                            IVsOutputWindowPane customPane;
                            outWindow.GetPane(ref customGuid, out customPane);
                            ((VsPropertiesHelper)this.visualStudioInterface).CustomPane = customPane;
                            ((VsPropertiesHelper)this.visualStudioInterface).CustomPane.Activate();
                        }

                        SonarQubeViewModelFactory.SQViewModel.PluginRequest += this.LoadPluginIntoNewToolWindow;
                        this.CloseToolWindow();

                        this.StartSolutionListeners(this.visualStudioInterface);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    UserExceptionMessageBox.ShowException("SonarQubeExtension not able to start", ex);
                }
            }
            catch (Exception ex)
            {
                UserExceptionMessageBox.ShowException("Extension Failed to Start", ex);
                throw;
            }
        }