【问题标题】:Visual Studio add-on to tag code segments?用于标记代码段的 Visual Studio 附加组件?
【发布时间】:2010-02-01 14:50:49
【问题描述】:

我想知道是否有任何 VS 插件可以用我选择的描述性文本替换/标记某些代码行?

理想的功能如下:

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

}

...看起来与此类似:

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

{

± Read from file

± Do the reports

}

± 符号会扩展/折叠替换的行。
还考虑了其​​他解决方法!
感谢您的帮助!

【问题讨论】:

  • 包含许多函数的区域可以说是代码异味。如果您需要区域在单个函数中找到自己的方式,那就大错特错了。
  • 我不介意在标题中使用 #region "private",即使其中包含大量模板函数。

标签: c++ visual-c++ visual-studio-addins


【解决方案1】:

区域功能与您描述的完全一样,并且内置于 Visual Studio 中。

以下内容将按照您的描述进行压缩:

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
}

【讨论】:

  • 我期待 #region 能够工作,但是我已经很久没有使用 c++ 了,我忘记了 #pragma
  • 嘿,是的,我实际上是使用#region 编写了答案,然后在我发现它是 C++ 后迅速查看了 MSDN。
【解决方案2】:

除了 Ryan 的回答,我可能应该指出,这在语言本身中是可能的。

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

我个人更喜欢这个区域。更直接地看到您没有从函数返回任何值。

在 Visual Studio 中,您可以使用 F12 跳转到函数的定义。

【讨论】:

  • 我同意你的一般观点。我尽可能多地使用它。但是我认为某些功能可能对此不切实际。例如,那些使用太多局部变量的函数需要传递给新函数。
  • @sevaxx:这就是结构和引用传递的意义所在。
【解决方案3】:

最简单的解决方案,我建议您使用的解决方案是创建函数ReadFileCreateReports。这也是一个更好的设计,并且具有在所有可能的 IDE 和语言中工作的额外好处。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 2011-02-04
    • 1970-01-01
    相关资源
    最近更新 更多