WebBiscuit

WebBiscuit

The Webbiscuit Blog: Taking the Internet crumb by crumb…

  • Home
  • Software
    • Palette Parser
    • XHTML Generator
  • Articles
    • The Strategy Pattern
    • The Factory Pattern
    • Memory leaks
  • About Web Biscuit
  • Contact

A macro to flip between the source and header file (and back again)

Posted in C++, CodeProject, IDE, macro, VB, Visual Studio by Daniel
Dec 01 2011
TrackBack Address.

The macro journey begins here, moving a function from a header file into its source file. The first problem presents itself as this: how can I get at the source file from the header file?

I have never known why this functionality has not been present in Visual Studio, perhaps it is harder than it appears! Still, Visual Assist has managed it, and a few people seem to want it. So, let’s add it!

The easiest thing to do is to take the filename in full, check if it ends in .cpp or .h, and then replace with .h or .cpp to get the paired source file. This is basically what the following code does (more or less based on the code found here)

    Private Function GetCorrespondingFilename(ByRef currentFilename As String) As String
        Dim correspondingFilename As String

        If (currentFilename.EndsWith("cpp", StringComparison.InvariantCultureIgnoreCase)) Then
            correspondingFilename = Left(currentFilename, Len(currentFilename) - 3) + "h"
        ElseIf (currentFilename.EndsWith("h", StringComparison.InvariantCultureIgnoreCase)) Then
            correspondingFilename = Left(currentFilename, Len(currentFilename) - 1) + "cpp"
        End If

        Return correspondingFilename

    End Function

This does have a few limitations though: what if the header is a .hpp file? What if the cpp file is cxx or c? What if the header and source files live in different folder locations?
These are all very interesting questions that I am going to ignore for now and go for the simplest case. Your headers and source live in the same folder (I am assuming). You use rigorous .h and .cpp naming standards for your header and source files respectively. Ah, life is easy.

Where I will deviate from the linked post is how we open the document. The original open method can be slow if you have many files open in visual studio, and if the file does not exist it will be created. Which can be nice, but not usually what we want. Here is the function to get the corresponding source file and open it:

    Public Sub ToggleBetweenHeaderAndSource()
        If (ActiveDocument Is Nothing) Then
            Return
        End If

        Dim otherFilename = GetCorrespondingFilename(ActiveDocument.FullName)

        If FileIO.FileSystem.FileExists(FileName) Then
            Application.Documents.Open(otherFilename, "Text")
        End If
    End Sub

We check that ActiveDocument Is Nothing because the nasty errors that visual studio throws at us when we run this macro with no file open aren’t very nice. We then get the corresponding filename and open it if it exists. Great!

We’ll be building on this macro next time to automatically generate an empty function body in the source file, all from the header definition. That is when things really get interesting.

Download Macro: SourceCodeHelpers1

Can you improve upon this macro? Leave your suggestions and comments below!

Share
Tagged as: header, source, vc++ macros
  • Pingback: A macro to create a C++ implementation from a header definition » » WebBiscuitWebBiscuit

RSS Other Nibbles

  • Palette Parser Update – Now supports CMYK format
  • Are you a biscuit?
  • Base64 Encoder and Boost
  • The Sleep Experiment
  • A macro to create a C++ implementation from a header declaration
  • A macro to flip between the source and header file (and back again)
  • An introduction to three VC++ Macros: How they came to be
  • Making Eclipse more like Visual Studio
  • A tokeniser using STL
  • More of WebBiscuit’s WordPress Plugins

Categories

  • Boost
  • C++
  • code
  • CodeProject
  • CSharp
  • domains
  • Eclipse
  • experiments
  • funny
  • games
  • IDE
  • macro
  • migrating
  • plugins
  • productivity
  • reviews
  • sleeping
  • STL
  • Test
  • tips
  • VB
  • Visual Studio
  • web tips
  • WebBiscuit Software
  • wordpress
  • workarounds

Blogroll

  • Create colour palettes with Kuler
  • Free Icons with Axialis Software

Stale Biscuits

  • December 2012 (1)
  • August 2012 (1)
  • April 2012 (1)
  • January 2012 (1)
  • December 2011 (2)
  • November 2011 (1)
  • September 2011 (1)
  • August 2011 (1)
  • July 2011 (9)

Eating the web bit by bit

Powered by WordPress | “Blend” from Spectacu.la WP Themes Club