沿著aptana的腳步,我遇到了Snippets,開始我還以為是aptana所特有的東西,原來Snippets來自Monkey-Eclipse Monkey。
什么是Monkey呢?
官方網(wǎng)址為http://www.eclipse.org/dash/
Monkey是一個(gè)用于自動(dòng)完成繁瑣的編程任務(wù)的動(dòng)枋態(tài)腳本工具。他使用javascript的語法,有其自帶的開發(fā)庫又可以使用某些java的函數(shù)。因?yàn)樗暮唵涡裕艺J(rèn)為它是區(qū)別于plugs-in的另外一種對(duì)eclipse的擴(kuò)展的好工具。
來,看看Eclipse Monkey Example帶給了我們什么應(yīng)用。
屬于Editors的命令有:Commont Lines,Leading Spaces to Tabs,Leading Tabs to Spaces。
屬于Experimental的命令有:Colorize as HTML,Exercise Editor,Load Metadata,Show Lexemes,Show Parse Tree.
......
不一一列舉了。
學(xué)習(xí)的資料是少,我看了看代碼,理解出了用法。仿照著寫了兩個(gè)Snippets,分別完成兩個(gè)單獨(dú)的功能。注意,由我自己的經(jīng)驗(yàn)是需要3.2以上版本才對(duì)aptana與monkey有很好的支持。如果先裝了aptana之后再裝monkey的話,如monkey的文檔所說到的monkey example很難加載到菜單上,需要手動(dòng)修改配置,具體方法還沒確定,如果先裝了monkey,就可以先新建出monkey example再裝aptana,這樣我們開發(fā)Snippets就簡單多了,直接生成了個(gè)workplace中的項(xiàng)目,寫完Snippets保存,馬上就能通過Snippets視圖中雙擊加載上的Snippets來運(yùn)行。就建出的項(xiàng)目的Snippets也能馬上保存到Snippets視圖中。
一.給某行代碼加上"http://"的注釋符號(hào),這樣類似于eclipse中的Ctrl+?鍵的功能,但簡單實(shí)現(xiàn)當(dāng)以經(jīng)是"http://"注釋的時(shí)候依然加上"http://",文件所處的位置對(duì)Snippets沒影響。
/*
* Menu: Editor > lines
* Kudos: Kevin Lindsey
* License: EPL 1.0
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
* DOM: http://localhost/com.aptana.ide.scripting
*/
//文件頭必須這樣定義好,在開頭,還有其它可定義的屬性,Menu就是加載到eclipse中的執(zhí)行命令的位置
//DOM應(yīng)該是類似于類庫的一樣?xùn)|西,選擇需要加入
/**
* main
*/
//main是執(zhí)行的方法的入口
function main()
{
var editor = editors.activeEditor;
var range = editor.selectionRange;
var startLine = editor.getLineAtOffset(range.startingOffset);
editor.beginCompoundChange();
var offset = editor.getOffsetAtLine(startLine)
editor.applyEdit(offset,0,"http://");
editor.endCompoundChange();
}
二.這是一個(gè)很有用的功能,打開所編輯文件的所在文件夾,并對(duì)該文件選定。不知道為什么 editors.activeEditor.textEditor.getFileContext()只對(duì)aptana方式打開的文件有效。
/*
* Menu: File > OpenDir
* Kudos: Kevin Lindsey
* License: EPL 1.0
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
* DOM: http://localhost/com.aptana.ide.scripting
*/
/**
* main
*/
function main()
{
try {
//獲取文件路徑
var filename = editors.activeEditor.textEditor.getFileContext().getSourceProvider().getSourceURI();
filename = filename.replace("%20"," ");
//調(diào)用系統(tǒng)命令
java.lang.Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL Explorer.exe /select," + filename);
} catch(e) {
Packages.org.eclipse.jface.dialogs.MessageDialog.openInformation(
window.getShell(),
"Monkey Dialog",
e
)
}
}