Chinaunix首页 | 论坛 | 博客
  • 博客访问: 149840
  • 博文数量: 19
  • 博客积分: 964
  • 博客等级: 准尉
  • 技术积分: 181
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-02 19:26
文章分类

全部博文(19)

文章存档

2011年(2)

2010年(1)

2008年(6)

2007年(10)

分类: C/C++

2008-06-10 08:53:16

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) |
0

上一篇:为灾区的人们祈祷

下一篇:最牛的语录

给主人留下些什么吧!~~