Home > Plug-in Development > How to use IssueProvider DXCore component

How to use IssueProvider DXCore component

December 2nd, 2010

Let’s assume that we need to highlight all local variables with incorrect names using CodeRush Code Issues technology. For example, the name is incorrect if it starts from an upper case letter instead of a lower case letter. Follow these steps to get the IssueProvider component working:

  • Create a new DXCore plug-in if you haven’t done it yet
  • Drop the IssueProvider control on the plug-in designer surface
  • Fill the ProviderName property of the IssueProvider
  • Fill the Description property of the IssueProvider if needed

  • Subscribe to the CheckCodeIssues event:

CSharp code:

private void issueProvider1_CheckCodeIssues(object sender, CheckCodeIssuesEventArgs ea)
{
}

Visual Basic code:

Private Sub IssueProvider1_CheckCodeIssues(ByVal sender As Object, ByVal ea As CheckCodeIssuesEventArgs) Handles IssueProvider1.CheckCodeIssues
End Sub
  • Next, add a new Class into your project and make it a descendant of the BaseCodeIssueSearcher
  • Override its CheckCodeIssues method
  • In the CheckCodeIssues event handler type the following code:

CSharp code:

private void issueProvider1_CheckCodeIssues(object sender, CheckCodeIssuesEventArgs ea)
{
  MyIssuesSearcher searcher = new MyIssuesSearcher();
  searcher.CheckCodeIssues(ea);
}

Visual Basic code:

Private Sub IssueProvider1_CheckCodeIssues(ByVal sender As Object, ByVal ea As CheckCodeIssuesEventArgs) Handles IssueProvider1.CheckCodeIssues
  Dim searcher As New MyIssuesSearcher()
  searcher.CheckCodeIssues(ea)
End Sub

Now, inside the CheckCodeIssues overridden method we need to implement our issues searcher logic:

CSharp code:

public class MyIssuesSearcher : BaseCodeIssueSearcher
{
  public override void CheckCodeIssues(CheckCodeIssuesEventArgs ea)
  {
    // Specify elements to work with - variables in this case...
    LanguageElementType[] types = new LanguageElementType[] { LanguageElementType.Variable, LanguageElementType.InitializedVariable, LanguageElementType.ImplicitVariable };
    ElementTypeFilter elementFilter = new ElementTypeFilter(types);

    // Enumerate the scope (a source file) with the specified elements...
    IEnumerable enumerable = ea.GetEnumerable(ea.Scope, elementFilter);
    foreach (IElement element in enumerable)
    {
      // Let's work with the correct variable element instance...
      IVariableDeclarationStatement variableDeclaration = element as IVariableDeclarationStatement;
      if (variableDeclaration == null)
        continue;
      // Check if it is a local variable...
      if (!variableDeclaration.IsLocal)
        continue;

      // Check its name...
      string variableName = variableDeclaration.Name;
      if (!String.IsNullOrEmpty(variableName))
        if (variableName.Length > 0)
          if (Char.IsUpper(variableName[0]))
            ea.AddWarning(variableDeclaration.FirstNameRange, "Incorrect variable name");
    }
  }
}

Visual Basic code:

Public Class MyIssuesSearcher
  Inherits BaseCodeIssueSearcher
  Public Overrides Sub CheckCodeIssues(ByVal ea As CheckCodeIssuesEventArgs)
    ' Specify elements to work with - variables in this case...
    Dim types As LanguageElementType() = New LanguageElementType() {LanguageElementType.Variable, LanguageElementType.InitializedVariable, LanguageElementType.ImplicitVariable}
    Dim elementFilter As New ElementTypeFilter(types)

    ' Enumerate the scope (a source file) with the specified elements...
    Dim enumerable As IEnumerable(Of IElement) = ea.GetEnumerable(ea.Scope, elementFilter)
    For Each element As IElement In enumerable
      ' Let's work with the correct variable element instance...
      Dim variableDeclaration As IVariableDeclarationStatement = TryCast(element, IVariableDeclarationStatement)
      If (variableDeclaration Is Nothing) Then Continue For

      ' Check if it is a local variable...
      If (Not variableDeclaration.IsLocal) Then Continue For

      ' Check its name...
      Dim variableName As String = variableDeclaration.Name
      If (Not String.IsNullOrEmpty(variableName)) Then
        If (variableName.Length > 0) Then
          If (Char.IsUpper(variableName(0))) Then
            ea.AddWarning(variableDeclaration.FirstNameRange, "Incorrect variable name")
          End If
        End If
      End If
    Next
  End Sub
End Class

That’s it.

  • Compile the plug-in
  • Open a new instance of the Visual Studio IDE
  • Load IDE tools if they’re not loaded automatically
  • Make sure Code Issues Analysis is turned on
  • Open any project and check how the new code issue works:

CodeRush Code Issue preview

The sample plug-in with the source code is attached (38,051 bytes, C# and VB, Visual Studio 2010). If you have any issues or need any further assistance – you are always welcome to contact DevExpress Support Services or me directly.

This post is a part of post series about CodeRush Code Issues technology. To learn more about code issues, please refer to the appropriate topics:

  1. Code Issues technology overview
  2. Code Issues types overview
  3. Code Issues visual presentation
  4. Code Issues fixes and suppression
  5. Code Issues navigation techniques
  6. Code Issues tool window
  7. Code Issues configuration and options
  8. Code Issues overall list
  9. Creating your own code issue (current post)
—–
Products: DXCore, CodeRush Pro
Versions: 10.1 and up
VS IDEs: any
Updated: Jul/25/2011
ID: D041

Similar Posts:

  1. Robert Leahey
    December 6th, 2010 at 05:57 | #1

    Hi Alex; can you say where BaseCodeIssuesSearcher is located? I’m using dx2010.2, but reference is not found.

  2. Robert Leahey
    December 6th, 2010 at 05:58 | #2

    Ahh, “BaseCodeIssuesSearcher” is mis-named. Should be BaseCodeIssueSearcher in CodeRush.Core.

  3. December 6th, 2010 at 08:56 | #3

    @Robert Leahey Hi Robert, thanks for noticing this – it is corrected now.

  4. December 14th, 2010 at 16:21 | #4

    I’ve been able to follow your steps in creating a new plugin with an issue provider. After following your steps however, I find that the CheckCodeIssues has an enumerable variable that shows a message “Enumerable yielded no results” (when debugging the code) after loading the plugin and loading a .cs file that includes local variables. As a result, the code inside the for loop never runs. Any suggestions?

  5. December 15th, 2010 at 03:16 | #5

    @Mike Berenson
    I have noticed that after I tried to test the code once again. I did a small correction in the code to make it always work (on the 10th of Dec). Are you using the corrected code? (See the first two code lines inside of the CheckCodeIssues method).

  6. December 15th, 2010 at 06:17 | #6

    Excellent, thanks for the heads up on the corrected code Alex. It works great now.