views:

72

answers:

3

Hi, I wonder if there exists any add-on for VS that can substitute/tag some lines of code with a descriptive text of my choice ?

Ideally a function like the one below :

bool CreateReportFiles(LPCTSTR fn_neighbours, ULONG nItems, ULONG* items)

{

// Read from file

CFile cf_neighbours;

if (!cf_neighbours.Open(fn_neighbours, CFile::modeRead))

  return false;

cf.Read(items, sizeof(ULONG) * nItems);

cf.Close();




// Create reports

DoReport_1(items, nItems);

DoReport_2(items, nItems);

DoReport_3(items, nItems);

FinalizeReports();

}

...would look similar to this :

bool CreateReportFiles(LPCTSTR fn_neighbours, ULONG nItems, ULONG* items)

{

± Read from file

± Do the reports

}

The ± signs would expand / collapse the substituted lines.
Other workarounds also considered !
Thanks for your help !

+5  A: 

The region functionality does pretty much precisely what you describe, and is built into Visual Studio.

The following will compress as you described:

bool CreateReportFiles(LPCTSTR fn_neighbours, ULONG nItems, ULONG* items)

{

#pragma region ReadFile
// Read from file

CFile cf_neighbours;

if (!cf_neighbours.Open(fn_neighbours, CFile::modeRead))

  return false;

cf.Read(items, sizeof(ULONG) * nItems);

cf.Close();

#pragma endregion ReadFile

#pragma region CreateReports

// Create reports

DoReport_1(items, nItems);

DoReport_2(items, nItems);

DoReport_3(items, nItems);

FinalizeReports();

#pragma endregion CreateReports
}
Ryan Brunner
I was expecting `#region` to work, but it's been so long since I did c++ I'd forgotten about `#pragma`
ChrisF
Heh, yeah, I actually wrote the answer using #region, then quickly checked MSDN once I noticed it was C++.
Ryan Brunner
+4  A: 

In addition to Ryan's answer, I should probably point out that this is possible in the language itself.

bool CreateReportFiles(LPCTSTR fn_neighbours, ULONG nItems, ULONG* items)
{
    ReadFromFile(fn_neighbours, nItems, items);
    CreateReports(items, nItems);
}

I personally prefer this to regions. It is also more straightforward to see that you're not returning any value from the function.

In Visual Studio, you can use F12 to jump to the function's definition.

avakar
I agree with your general view. And I use this as much as possible. However I think some functions may be impractical for this. For example, those functions that use too many local variables which would need to be passed over to the new function.
sevaxx
@sevaxx: That's what structures and passing by reference are about.
280Z28
+1  A: 

The simplest solution, and the one I would advice you to use is creating functions ReadFile and CreateReports. This would be a better design, as well, and has the additional benefit of working in all possible IDEs and languages.

David Rodríguez - dribeas