前言
文章
正文
1.2 宏代码,修改上文使用的宏即可。
option strict off
option explicit off
imports system
imports envdte
imports envdte80
imports system.diagnostics
imports system.collections
public module jsmacros
sub outlineregions()
const region_start as string = "//region"
const region_end as string = "//endregion"
dim selection as envdte.textselection = dte.activedocument.selection
dim startregions as stack = new stack() '堆栈
dim intcollapsestart as integer = 0
dim intcollapsenum as integer = 0
dim strlines() as string
selection.startofdocument(true)
selection.selectall()
strlines = selection.text.split(vbcrlf) '获取所有行
for i = 0 to strlines.length - 1
if strlines(i).trimstart.startswith(region_start) then
startregions.push(i + 1) '保存行号
end if
if strlines(i).trimstart.startswith(region_end) then
intcollapsestart = startregions.pop() + 1 '返回行号
intcollapsenum = (i + 1) - intcollapsestart + 1 '返回要折叠的行数
selection.gotoline(intcollapsestart)
selection.linedown(true, intcollapsenum)
selection.swapanchor()
selection.outlinesection()
next
selection.startofdocument()
end sub
end module
1.3 注意
1.3.1. 由上文的"//#region" 、"//#endregion"修改成了本文的"//region"和"//endregion" 。
1.3.2 如果想把"//region"这一行也隐藏掉只剩下"...",只需要将宏代码"intcollapsestart = startregions.pop() + 1"后面的"+1"去掉即可。遗憾的是没能弄出c# 折叠的那种效果出来。
1.3.3 如果还想支持if for 等关键字的折叠,强烈推荐文章1,本文也是在此文的基础上修改的,改正了"//region"后面不能接注释的缺陷。
二、支持js的visual studio插件
2.1 scriptoutline 从试用的情况看来并没有折叠,但是他显示了方法大纲,且无需设置快捷键,作为插件和vs集成,同样能达到快速找到方法的目的。参照文章3。
2.1.2 拷贝压缩文件中的scriptoutline.addin、scriptoutline.dll到目录 c:\documents and settings\<username>\my documents\visual studio 2005\addins
如果addins目录没有的话自己建一个就行。
2.1.3 工具(tools) -> 外部程序管理器(add-in manager...),勾上scriptoutline插件,确定即可显示script outline窗口。
2.1.4 编写测试代码,效果如图:
藉此我们可以在方法间快速切换!注意这里使用的环境是microsoft visual studio 2005。
2.2 smartoutline
2.2.2 安装插件 smartoutline_v1.1.msi ,下一步下一步就行。工具栏会出现smalloutline,可能需要重启vs。
2.2.3 编写测试代码,依次按步骤:
2.2.3.1 选中要折叠的函数,出现如下提示
2.2.3.2 点击提示或输入组合快捷键 alt+s、alt+c ,弹出如下对话框,输入js代码折叠后显示的注释名
2.2.3.3 最终效果
2.2.4 总结
比较之下还是这个最好用,如下优点:
a). 不污染源代码,和c#里面写#region的效果一样。
b). 折叠效果好,能显示折叠后代码块的注释,不需要像宏那样关掉之后重新激活。
c). 此插件同时支持vs2005和vs2008,不仅如此,还支持c#、html、css等,可以从smalloutline -> general -> enable smalloutline for the following files下面的列表里看到支持的其他文件。
结束
宏是个好东东,虽然有现成的插件用,仍然假借这个机会来学习一翻,甚至有想法通过宏来辅助orm工具生成一些代码,以及对代码生成均有参考价值:)
转载:http://www.cnblogs.com/over140/archive/2009/06/22/1507564.html