KnowDotNet

Detect Compiler Errors in Application From an Add-In

View and Read The Error List Tool Window

by Les Smith

How can I determine if an application has errors from an add-in?  Read the items in the Error List.

Sometimes, in an add-in, you need to know if an application in the IDE has compiler errors.  For example, if your add-in is using the CodeModel to explore and manipulate code, you have to realize that the CodeModel may not return code that will not compile.  Basically, the CodeModel may not "see" code that does not compile.  For that reason the your add-in has to know if there are errors in the application.

At least one of the methods for determining if there are errors in an application is to read the Error List Tool Window.  The code for this article will show you how to not only determine if there are errors, it will be able to tell you the project file that is in error.  In an add-in, which I have developed, I am using the FileCodeModel to retrieve all of the code from the active code window.  If the active code window has errors in it, I need to warn the developer of the possible code loss if they proceed with the use of the add-in to load and then save the code back to the window.  Obviously, if I don't retrieve all of the code and then overwrite the code window with the code that I retrieved, then I will have lost code that would not compile.  That could be as little as one line, in the case of a variable, or many lines if a whole method will not compile.  For example, if there is an error in the definition of a function, then the FileCodeModel will not show that function at all.

First, I will show you the code for the method for determining if there are errors in the application.

   Private Function IsCompilerErrors(ByVal oVS2 As EnvDTE.DTE) As String
      Dim ide As DTE2
      ide =
CType(oVS2, DTE2)
      
Dim myErrors As ErrorList

      ide.ExecuteCommand(
"View.ErrorList", " ")
      myErrors = ide.ToolWindows.ErrorList
      
Dim count As Integer = count = myErrors.ErrorItems.Count
      
Dim currentFile As String = ide.ActiveDocument.Name.ToLower
      
For k As Integer = 1 To count
        
Try
            If myErrors.ErrorItems.Item(k).ErrorLevel = _
               vsBuildErrorLevel.vsBuildErrorLevelHigh
Then
               Dim fn As String = _
                  IO.Path.GetFileName(myErrors.ErrorItems.Item(k).FileName).ToLower
              
If fn.Equals(currentFile) Then
                  ide.ExecuteCommand("View.ViewCode")
                  
Return currentFile
              
End If
            End If
         Catch
         End Try
      Next
      ide.ExecuteCommand("View.ViewCode")

      
Return String.Empty
  
End Function

The method, shown above, assumes that the application object (IDE instance object) is passed in as EnvDTE.DTE (Visual Studio 1.1).  I cast this variable to type EnvDTE80.DTE2.  I could have passed in that type of object, but just happened to have the other object available.  Please note that the Error List Tool Window is new in Visual Studio 2.0 so this add-in is obviously written for that version of Visual Studiot.  Next, I create an instance of the Error List Tool Window and set the count of items in the window.  The Error List contains Errors, Warnings, and Messages, so as I read through the items in the Error List, I check to see if the item has an error level of "high".  That means that the item represents an error.  Once I find an error, I check the description of the error item which just happens to have the file name of the file containing the error.  Since I have already saved the name of the active code window, I compare it to the name of the file with the error item in it.  If I find an error item in a file name matching the name of the active code window, I am ready to exit the function and return the file name of the active code window which will be used for emphasis as I prompt the user with the problematic condition.

There is just one other thing that I must do before exiting the function.  Creating an instance of the Error List Tool Window causes it to open the Tool Window.  this obviously takes focus away from the active code window.  Since the add-in is about to retrivee code from the code window, it must have focus to use the FileCodeModel or other extensibility objects to access the code window.  To put the focus back on the code window, I simply use the ExecuteCommand method of the IDE object.

The following code snippet is used to call the IsCompilerErrors method and display the file name of the code window with errors in it.  If the method returns an empty string, there are no errors in the actiive code window and the add-in can proceed without prompting the user.

      Dim fileName As String = IsCompilerErrors(oVB2)
      
If fileName.Length > 0 Then
         If MsgBox("Your application has compile errors.  " & _
                  
"In particular, the current file: " & fileName _
                   &
" has errors.  Saving a file with errors from " & _
                  
"Class Organzier may lose code.  Do you want to proceed?", _
                   MsgBoxStyle.Question
Or _
                   MsgBoxStyle.YesNo
Or _
                   MsgBoxStyle.DefaultButton2, _
                  
"Confirm Proceeding With Errors") = MsgBoxResult.No _
                  
Then Exit Sub
      End If

If the code executes the Exit Sub, then that says that the add-in is not going to proceed with the operation.

Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog.