创建一个 VS2010 插件来折叠我的活动文档的每个方法

发布于 2024-12-16 13:27:28 字数 679 浏览 1 评论 0原文

我正在寻找源代码来使用 VS2010 Addin 折叠活动文档的每个方法。
目前,我解析文档的文本内容,尝试匹配该行是否是方法签名。如果是这样的话,我会折叠该方法。

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
}

有谁能告诉我我是否走得好或者是否有最简单的方法?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2024-12-23 13:27:28

也许我的问题问得不太好。我真正的目标是折叠所有代码:属性、方法、带有 /// 的注释、using;但不是地区。
这是一种解决方案:

// 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);

我希望这能有所帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文