Introduction
In Visual Studio .NET 2003/2005, if you want open the related .cpp file when you are editing a .h file, or if you want to open the related .h file when you are editing
a .cpp file, or if you want to compile the related .cpp file when you are editing a .h file, is there a simple way?
Usually, programmer opens the related file in Solution Explorer, or use Ctrl+Tab to find the related file. The is not very simple. So I wrote
a macro to find and open related files. After you import the macro into Macro Explorer, you can run it simply by double clicking it.
Of course, there is a more simple way. Follow the menu 'Tools->Option->Environment->Keyboard' to open the shortcut configuration dialog.
Then find the OpenRelatedFile
macro, give it a shortcut such as Ctrl+F6, and find the CompileImplFile
macro, give it a shortcut,
such as Ctrl+F7. OK, you can now use the shortcut to invoke the macro now.
Basic Opinion
To find the related file in a project, several steps are necessary:
- Get the file name of the currently opened source file and generate the related file names.
For example, if the current file is bla.h, we should look for bla.cpp, bla.c, etc.
- Create a map which contains all files we are looking for.
- Look for the files in the project.
- If a target file is found, open it or compile it.
Source Code
It's necessary to traverse all folders in a project to look for a file by recursion. For that, we need a recursion function:
Private Sub FindAndOpenFile(ByRef item As ProjectItem)
If filenames.IndexOf(item.Name.ToLower()) >= 0 Then
If Not item.Document Is Nothing Then
DTE.ItemOperations.OpenFile(item.FileNames(0))
Exit Sub
End If
End If
Dim i As ProjectItem
For Each i In item.ProjectItems
FindAndOpenFile(i)
Next
End Sub
The global variable filenames
contains some filenames which we want to look for. If the function finds one of the filenames, open it and try to find another one.
The function starts with a ProjectItems
object. Every item in the object is perhaps a filename or a folder. If it's a folder, its ProjectItems
property
should not be empty. Thus the function invokes itself with the item's ProjectItems
property as a parameter recursively.
Private Sub FindAndOpenFileInProject()
Dim projs As System.Array
Dim proj As Project
projs = DTE.ActiveSolutionProjects()
If projs.Length = 0 Then
Exit Sub
End If
proj = CType(projs.GetValue(0), EnvDTE.Project)
Dim item As ProjectItem
For Each item In proj.ProjectItems
FindAndOpenFile(item)
Next
End Sub
In the OpenRelatedFile
function, all related .cpp/.c/.h/.hpp files should be searched.
filenames.Clear()
filenames.Add(curName)
filenames.Add(curName & ".h")
filenames.Add(curName & ".cpp")
filenames.Add(curName & ".c")
filenames.Add(curName & ".hpp")
filenames.Remove(curFilename)
In the CompileImplFile
function, only the related .cpp/.c files should be searched, so there is a little difference when we set the filenames
list:
filenames.Clear()
filenames.Add(curName & ".cpp")
filenames.Add(curName & ".c")
If a .cpp/.c file is found and opened, the function executes the Build.Compile
command to compile it.
curFilename = DTE.ActiveDocument.Name
If (curFilename.EndsWith(".cpp") Or curFilename.EndsWith(".c")) Then
DTE.ExecuteCommand("Build.Compile")
End If
Update Log
- 2007-07-25: Optimized code.
- 2007-07-12: Fixed the bug that sometimes we cannot open the related .h file.