Monday, July 16, 2007

Select C comments in my code...

So you think hitting Ctrl-Shift-R to record a macro, then Ctrl-F to find "/*" and then something-something, and then Ctrl-F to find "*/" to finish the selection would be enough.

Huh? Let me explain myself.

As we transitioned from FS9 to FSX, one of the most major pieces of the work was to replace all the FSUIPC code in our SimLibrary with SimConnect code. As this wasn't going to happen overnight (or even in a week) but instead took almost a year (yes, that's right - people said it'd be easy), I gradually went ahead and wrote all my SimConnect code snippets below their FSUIPC equivalent, commenting out with /* ... */ in the process, so in case of a bug (and there were a few), I could have easy access to "the older way". That also allowed "mix-and-match" code so we could test without needing to finish the transition first.

The end result was that our .cpp files, when all was said and done, were littered with multi-line comment areas such as this one:

/*
if (theFSUIPC)
{
theFSUIPC->Write(PMDG_CFSUIPC::PMDG_FIRE_CONTROLS_SWITCHES, 1, &val, &m_result);
if (m_processNow)
theFSUIPC->Process(&m_result);
}
*/

m_SimConnectMiscData->setFireControlSystem_Switches_PMDG(val);

I had always thought that it'd be easy to simply delete the commented areas when the transition was over, to clean up our C++ source files. Yes. So easy, he said.

(Disclaimer - I am NOT a VB guy, but I know how to read VB and do tiny bits).

I spent hours trying to figure out if and how I could record a macro that would enable the following in a couple key presses:

1) Find next commented code area
2) Select it
3) Delete it (if old code)

Ideally, I'd like to press "Ctrl-Shift-P" (for Macro Playback) to find the next area, then "delete" if needed or "Ctrl-Shift-P" again if this wasn't a comment to-be-deleted.

Sounds simple? Turns out that there's no way (that I could find) to record a "start-select" / "stop-select" keyboard activity. Instead, after hours of searching the net, I came up with this (admittedly easy) piece of code that I have now placed under a nice macro of my own (using VS2005's nice Macro Editor/Debugger):

Sub TemporaryMacro()
Dim objSel As TextSelection = DTE.ActiveDocument.Selection
objSel.LineDown()

'Look for the beginning
objSel.FindPattern("/*")
Dim lStartLine As Long = objSel.TopPoint.Line
Dim lStartColumn As Long = objSel.TopPoint.LineCharOffset

' Look for the end.
If objSel.FindPattern("*/") Then
' Select the entire section.
objSel.SwapAnchor()
objSel.MoveToLineAndOffset(lStartLine, lStartColumn, True)
End If
End Sub

Soooo simple. And yet, it took me so long to figure it out. Maybe I am getting old(er).

1 comment:

Mats J said...

Hey LK

So you use Visual Basic in Visual Studio to program macros for C++ code! Hilarious if you ask me! ;-)

Cheers,
Mats