Switch the source and header file in Visual studio

Just for a reminder. If you needed, please note the originally referenced.

If it doesn't work, restart the visual studio and try again.


http://www.codeguru.com/cpp/v-s/devstudio_macros/codenavigation/article.php/c14759/Tip-Switch-Between-Source-and-Header-C-Files.htm


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/2008/2010. 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

 
 
  1. Imports System
  2. Imports EnvDTE
  3. Imports EnvDTE80
  4. Imports System.Diagnostics
  5.  
  6. Public Module Navigating
  7.  
  8. '//
  9. '// Angel Tena
  10. '// Next Limit Technologies 1998
  11. '// Given a source or header file looks for the partner file
  12. '// in the current project and opens it.
  13. '//
  14.  
  15. Sub Proj_Switch_header_source()
  16. Dim fileName As String
  17. Dim fileNamePartner As String
  18. Dim projItem As ProjectItem = DTE.ActiveDocument.ProjectItem
  19. Dim proj As Project = projItem.ContainingProject
  20. fileName = DTE.ActiveDocument.Name.ToLower
  21. If (fileName.EndsWith(".h")) Then
  22. fileNamePartner = _
  23. fileName.Substring(0, fileName.Length() - 2) + ".cpp"
  24. End If
  25. If (fileName.EndsWith(".cpp")) Then
  26. fileNamePartner = _
  27. fileName.Substring(0, fileName.Length() - 4) + ".h"
  28. End If
  29.  
  30. Dim filePathPartner As String
  31. Dim projItems As ProjectItems
  32. projItems = proj.ProjectItems
  33. FindFile(fileNamePartner, filePathPartner, projItems, 0)
  34. If Not filePathPartner Is Nothing Then
  35. DTE.ItemOperations.OpenFile(filePathPartner)
  36. End If
  37. If filePathPartner Is Nothing Then
  38. MsgBox("File " & fileNamePartner & _
  39. " not found in the project")
  40. End If
  41. End Sub
  42.  
  43. '//
  44. '// Angel Tena
  45. '// Next Limit Technologies 1998
  46. '// Given a source or header file looks for the partner file
  47. '// in the whole solution and opens it.
  48. '//
  49.  
  50. Sub Sln_Switch_header_source()
  51. Dim fileName As String
  52. Dim fileNamePartner As String
  53. fileName = DTE.ActiveDocument.Name.ToLower
  54. If (fileName.EndsWith(".h")) Then
  55. fileNamePartner = _
  56. fileName.Substring(0, fileName.Length() - 2) + ".cpp"
  57. End If
  58. If (fileName.EndsWith(".cpp")) Then
  59. fileNamePartner = _
  60. fileName.Substring(0, fileName.Length() - 4) + ".h"
  61. End If
  62.  
  63. Dim soln As Solution = DTE.Solution
  64. Dim projs As Projects = soln.Projects
  65. Dim proj As Project
  66. Dim filePathPartner As String
  67. For Each proj In projs
  68. Dim projItems As ProjectItems
  69. projItems = proj.ProjectItems
  70. FindFile(fileNamePartner, filePathPartner, projItems, 0)
  71. If Not filePathPartner Is Nothing Then
  72. DTE.ItemOperations.OpenFile(filePathPartner)
  73. Exit For
  74. End If
  75. Next
  76. If filePathPartner Is Nothing Then
  77. MsgBox("File " & fileNamePartner & _
  78. " not found in the solution")
  79. End If
  80. End Sub
  81.  
  82. Sub FindFile(ByVal fileName As String, _
  83. ByRef filePath As String, _
  84. ByVal projitems As ProjectItems, _
  85. ByVal Level As Integer)
  86. Dim projItem As ProjectItem
  87. For Each projItem In projitems
  88. Dim projItemName As String = projItem.Name.ToLower
  89. If projItemName.Equals(fileName) Then
  90. filePath = projItem.FileNames(1)
  91. Return
  92. End If
  93. Dim projItems2 As ProjectItems = projItem.ProjectItems
  94. Dim notsubcoll As Boolean = projItems2 Is Nothing
  95. If Not notsubcoll Then
  96. FindFile(fileName, filePath, projItems2, Level + 1)
  97. End If
  98. Next
  99. End Sub
  100.  
  101. End Module
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值