UltraEdit 利用js脚本实现C/C++代码自动缩进
UltraEdit 提供了脚本功能,这样我们自己可以扩展很多的功能,以减少我们自己的纯体力劳动。下面是我自己写的一个针对一个函数后类声明完成自动缩进的功能。
PS:为什么只对函数而不是整个文件进行缩进?因为UE的脚本效率不是很高,如果对整个文件进行格式化UE会“死”在那里不动了,直到整个文件处理完毕。
作者:lysde
邮箱:
// ----------------------------------------------------------------------------
// File Name: autoindent.js
// Author: lysde
// Creation Date: 2008-6-4 17:59:32
// Last Modified: 2008-6-4 17:59:36
// Copyright (c)2008
// Purpose: auto indent codefile
// ----------------------------------------------------------------------------
//get line number.
var all_lines,startline,level; level = 0; startline = UltraEdit.activeDocument.currentLineNum; UltraEdit.activeDocument.bottom(); all_lines = UltraEdit.activeDocument.currentLineNum;
UltraEdit.activeDocument.top();
var i; var str_line; var re,comment,curline; comment = false; //comment?
for(i = startline; i <= all_lines; i++) { // UltraEdit.messageBox("ok");
UltraEdit.activeDocument.gotoLine(i,0); UltraEdit.activeDocument.selectLine(); str_line = UltraEdit.activeDocument.selection; if(!comment) { re = /^\/\//;
if (str_line.search(re) != -1) { //UltraEdit.messageBox("ok");
continue; } re = /^\/\*/; if (str_line.search(re) != -1) { commment = true; continue; } re = /^#/; if (str_line.search(re) != -1) { move(0); continue; } re = /\bpublic\b/; if (str_line.search(re) != -1) { move(0); continue; } re = /\bprivate\b/; if (str_line.search(re) != -1) { move(0); continue; } re = /\bprotected\b/; if (str_line.search(re) != -1) { move(0); continue; } move(level); re = /{/; if (str_line.search(re) != -1) { //UltraEdit.messageBox("{");
curline = i; level++; //continue;
} re = /}/; if (str_line.search(re) != -1) { //UltraEdit.messageBox("}");
level--; // UltraEdit.activeDocument.selectLine();
if(curline != i) move(level); if(level == 0) { UltraEdit.activeDocument.gotoLine(i + 1,0); break; } continue; } } else { re = /\*\/$/; if (str_line.search(re) != -1) { commment = false; continue; } } // move(level);
}
function move(lev) { UltraEdit.activeDocument.selectLine(); UltraEdit.activeDocument.reIndentSelection(); var i; for(i = 0; i < lev; i++) UltraEdit.activeDocument.write("\t"); }
|
阅读(6022) | 评论(0) | 转发(0) |