Tip: Switch Between Source and Header C++ Files

I was looking for a macro just to be able to switch quickly between a "cpp" and a "h" file. I found several, but they didn't work as I wanted. Some of them just look into the current folder for the partner file; this is a problem if you have some folder structure in your project.

I created two macros. The first one, "Proj_Switch_header_source," looks for the partner file in the project where the original file is. The second one, "Sln_Switch_header_source," looks for the partner file in the whole solution.

I use the first one all the time because normally in the structure of my projects the "cpp" and "h" files are in the same project. The second macro is less interesting but might be useful for other project structures where the "cpp" and "h" files are scattered in different projects in the same solution.

How to Install the Macros

These macros has been tested in Visual Studio 2005. I don't know whether they work in other versions.

  1. Go to "Tools->Macros->Macros IDE".
  2. In "Macros IDE", select the "MyMacros" module and go to "Project->Add Existing Item..." and browse for the attached file.
  3. Now, the macros are ready to use. You can do it just running them from the "Macro Explorer" or just attaching a shortcut to them. To attach a shortcut to the macros, close the "Macros IDE" and go back to Visual Studio, and then go to "Tools->Options" and click the "Keyboard" panel. In the "Show commands containing" field, type the name of the macros (given above), click on the macro, and assign a shortcut.

The Code

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module Navigating

   '//////////////////////////////////////////////////////////////
   '// Angel Tena
   '// Next Limit Technologies 1998
   '// Given a source or header file looks for the partner file
   '// in the current project and opens it.
   '//////////////////////////////////////////////////////////////

   Sub Proj_Switch_header_source()
      Dim fileName As String
      Dim fileNamePartner As String
      Dim projItem As ProjectItem = DTE.ActiveDocument.ProjectItem
      Dim proj As Project = projItem.ContainingProject
      fileName = DTE.ActiveDocument.Name.ToLower
      If (fileName.EndsWith(".h")) Then
         fileNamePartner = _
            fileName.Substring(0, fileName.Length() - 2) + ".cpp"
      End If
      If (fileName.EndsWith(".cpp")) Then
         fileNamePartner = _
            fileName.Substring(0, fileName.Length() - 4) + ".h"
      End If

      Dim filePathPartner As String
      Dim projItems As ProjectItems
      projItems = proj.ProjectItems
      FindFile(fileNamePartner, filePathPartner, projItems, 0)
      If Not filePathPartner Is Nothing Then
         DTE.ItemOperations.OpenFile(filePathPartner)
      End If
      If filePathPartner Is Nothing Then
         MsgBox("File " & fileNamePartner & _
                " not found in the project")
      End If
   End Sub

   '//////////////////////////////////////////////////////////////
   '// Angel Tena
   '// Next Limit Technologies 1998
   '// Given a source or header file looks for the partner file
   '// in the whole solution and opens it.
   '//////////////////////////////////////////////////////////////

   Sub Sln_Switch_header_source()
      Dim fileName As String
      Dim fileNamePartner As String
      fileName = DTE.ActiveDocument.Name.ToLower
      If (fileName.EndsWith(".h")) Then
         fileNamePartner = _
            fileName.Substring(0, fileName.Length() - 2) + ".cpp"
      End If
      If (fileName.EndsWith(".cpp")) Then
         fileNamePartner = _
            fileName.Substring(0, fileName.Length() - 4) + ".h"
      End If

      Dim soln As Solution = DTE.Solution
      Dim projs As Projects = soln.Projects
      Dim proj As Project
      Dim filePathPartner As String
      For Each proj In projs
         Dim projItems As ProjectItems
         projItems = proj.ProjectItems
         FindFile(fileNamePartner, filePathPartner, projItems, 0)
         If Not filePathPartner Is Nothing Then
            DTE.ItemOperations.OpenFile(filePathPartner)
            Exit For
         End If
      Next
      If filePathPartner Is Nothing Then
         MsgBox("File " & fileNamePartner & _
                " not found in the solution")
      End If
   End Sub

   Sub FindFile(ByVal fileName As String, _
                ByRef filePath As String, _
                ByVal projitems As ProjectItems, _
                ByVal Level As Integer)
      Dim projItem As ProjectItem
      For Each projItem In projitems
         Dim projItemName As String = projItem.Name.ToLower
         If projItemName.Equals(fileName) Then
            filePath = projItem.FileNames(1)
            Return
         End If
         Dim projItems2 As ProjectItems = projItem.ProjectItems
         Dim notsubcoll As Boolean = projItems2 Is Nothing
         If Not notsubcoll Then
            FindFile(fileName, filePath, projItems2, Level + 1)
         End If
      Next
   End Sub

End Module


Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

  • As everyone scrambles to protect customers and consumers from the Heartbleed virus, there will be a variety of mitigating solutions offered up to address this pesky bug. There are a variety of points within the data path where solutions could be put into place to mitigate this (and similar) vulnerabilities and customers must choose the most strategic point in the network at which to deploy their selected mitigation. Read this white paper to learn the ins and outs of mitigating the risk of Heartbleed and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds