2

I'm looking for the source code to collapse every methods of my active document using the VS2010 Addin.
For the moment I parse the text content of the document trying to match if the line is a method signature. If it is the case, I collapse the method.

TextSelection selection = (TextSelection)_applicationObject.ActiveDocument.Selection;
var editPoint = selection.ActivePoint.CreateEditPoint();
editPoint.MoveToLineAndOffset(1, 1);

while (!editPoint.AtEndOfDocument)
{
    editPoint.StartOfLine();
    var line = editPoint.GetText(editPoint.LineLength).TrimStart();

    if (line.StartsWith("public"))
    {
        selection.MoveToLineAndOffset(editPoint.Line, 1);
        _applicationObject.ExecuteCommand("Edit.ToggleOutliningExpansion");
    }

    // go to the next line
}

Does anyone could tell me if I'm on the good way or if there is an easiest way ?

Nicolas
  • 6,289
  • 4
  • 36
  • 51

1 Answers1

0

Maybe I asked not so well my question. My real goal was to collapse all the code : properties, methods, comments with ///, using; but not the regions.
Here is one solution :

// reduce everything like Ctrl+M+O
_applicationObject.ExecuteCommand("Edit.CollapsetoDefinitions");

// save the cursor position
TextSelection selection = (TextSelection)_applicationObject.ActiveDocument.Selection;
var selectedLine = selection.ActivePoint.Line;
var selectedColumn = selection.ActivePoint.DisplayColumn;

// open the regions
selection.StartOfDocument();
while (selection.FindText("#region", (int)vsFindOptions.vsFindOptionsMatchInHiddenText))
{
    // do nothing since FindText automatically expands any found #region
}

// put back the cursor at its original position
selection.MoveToDisplayColumn(selectedLine, selectedColumn);

I hope this could help

Nicolas
  • 6,289
  • 4
  • 36
  • 51