Aptana studio 是一款非常优秀的web前端IDE,功能类似Eclipse,编辑界面的许多提示和快捷键让人使用起来非常方便,很好很强大。
不过最近发现Aptana没有很好的添加注释的快捷键,例如Eclipse中的添加注释快捷键[Alt + Shift + J]。
幸运的是,Aptana 2.0以上版本都集成有Eclipse Monkey,我们可以用monkey脚本自己编写快捷键脚本文件(注意不要与Aptana默认快捷键冲突):
1. addComment.js — 添加注释,快捷键[Alt + Shift + J]
/** * Menu:addComment * Kudos: James * License: EPL 1.0 * Key: M3+M2+J * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript * OnLoad: main() */ function main(){ var sourceEditor = editors.activeEditor; //定义活动窗体 var range = sourceEditor.selectionRange; //选中区域 var startLine = sourceEditor.getLineAtOffset(range.startingOffset); //光标当前所在起始行 var offset = sourceEditor.getOffsetAtLine(startLine); var source = sourceEditor.source; //获取当前屏幕的所有string类型 var date = new Date(); var year = date.getFullYear(); var mouth =date.getMonth(); var day = date.getDay(); var hours = date.getHours(); var min = date.getMinutes() < 10 ? "0"+date.getMinutes() : date.getMinutes(); var sec = date.getSeconds() < 10 ? "0"+date.getSeconds() : date.getSeconds(); var time = year+"-"+mouth +"-"+ day +" "+ hours +":"+ min +":"+sec; for(var i=0;i<source.length;i++){ if(source.substring(offset+i,offset+i+1)!=" "){ break; } } var blank=source.substring(offset,offset+i); var str = blank+"/**\n"+ blank+" * Acthor: James\n"+ blank+" * Modified: "+time+"\n" + blank+" * Action: \n" + blank+" */\n"; //要插入的字符串 sourceEditor.applyEdit(offset,0,str); }
2. openDirectory.js — 打开当前文件所在的目录,快捷键[Alt + Shift + D]
/** * Menu:openDirectory * Kudos: James * License: EPL 1.0 * Key: M3+M2+D * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript * OnLoad: main() */ function GetFullPathOfEditor(editor){ return editor.editorInput.file.location.toString(); } function GetFullPathOfActiveEditor(){ return GetFullPathOfEditor(window.activePage.activeEditor); } function SplitPath(path){ var idx=path.lastIndexOf("/"); var filedir=path.substr(0,idx); var filename=path.substr(idx+1); return {dir:filedir,name:filename}; } function ToWindowsPath(path){ return path.replace(/\//g,"\\"); } function Run(path){ java.lang.Runtime.getRuntime().exec(path); } function main(){ var editor=editors.activeEditor; var path=GetFullPathOfActiveEditor(); var dir=SplitPath(path).dir; var winDir=ToWindowsPath(dir); Run("rundll32 SHELL32.DLL,ShellExec_RunDLL Explorer.exe " + winDir); //Explorer.exe后面有个空格,要注意 }
脚本文件配置方法1:
1. 在工程目录下建立一个 scripts 目录或者 monkey 目录。
2. 在该目录下创建脚本JS文件,扩展名是 *.js 或者 *.em(如上述两个文件),编写并保存。
3. 在Aptana界面上Scripts目录下就会显示该脚本,点击运行,或使用快捷键来运行即可。
脚本文件配置方法2:
1. 编写脚本JS文件。
2. 将文件放在C:\Program Files\Aptana\Aptana Studio 2.0\plugins\com.aptana.ide.scripting_2.0.0.1278523018.jar\monkey\目录下(我个人的目录);重新启动Aptana。
3. 开启Aptana后,在Scripts目录下会显示该脚本,点击运行,或使用快捷键运行。
关键点:
1. 在编写脚本JS文件时,一定要配置正确的metedata 信息。
(详见:http://docs.aptana.com/docs/index.php/Adding_metadata_to_an_Eclipse_Monkey_script
http://blog.sina.com.cn/s/blog_53d96fe30100bi9l.html)
如上述脚本文件中,开头部分的注释信息:
/**
* Menu:addComment
* Kudos: James
* License: EPL 1.0
* Key: M3+M2+J
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
* OnLoad: main()
*/
这些信息都非常关键,缺一不可。关于其中快捷键的注解:
M1 (Command or Ctrl)
M2 (Shift)
M3 (Option or Alt)
M4 (Ctrl on Mac)
2. 脚本文件中,main方法是一定要提供的。