前言
文章
正文
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