source insight3.0 c++脚本 通过函数声明 生成函数定义

实现思路分两部:

第一步:找到类名与函数名

第二步:进入对应cpp文件生成


使用方法:

定义一个快捷键给 函数(宏)   CreateClassFuncDefine 即可




//-------------------------------------------------------------------------------------------------------------
//通过空格区分 返回值与函数名,括号区分参数
//通过.分割类名


macro GetFuncInfo(line_str,class_and_func_name)
{


//
// return_type func_name(paramter)
func = nil
length  = strlen(line_str);
i=0;
content_start_pos = 0;
return_type_end_pos = 0;
parameter_start_pos = 0;


line_str=TrimLeft(line_str)
while(i < length)
{


if(return_type_end_pos == 0 && line_str[i]== " ")
{
// 返回值类型结束位置
return_type_end_pos = i;
}

if(line_str[i]== "(")
{// 函数名起始位置
parameter_start_pos = i;
}


if(line_str[i]==")")
{//
parameter_end_pos = i;
break;
}
i++
}


FuncInfo=nil
FuncInfo.return_type = strmid(line_str,content_start_pos,return_type_end_pos)
FuncInfo.parameters = strmid(line_str,parameter_start_pos+1,parameter_end_pos)
FuncInfo.class_name = class_and_func_name.class_name
FuncInfo.func_name = class_and_func_name.func_name
return FuncInfo
}
macro TrimLeft(szLine)
{
    nLen = strlen(szLine)
    if(nLen == 0)
    {
        return szLine
    }
    nIdx = 0
    while( nIdx < nLen )
    {
        if( ( szLine[nIdx] != " ") && (szLine[nIdx] != "\t") )
        {
            break
        }
        nIdx = nIdx + 1
    }
    return strmid(szLine,nIdx,nLen)
}


// 获取class 类定义的函数体
macro GetClassFuncDefineBody(function_info)
{
strline =cat(function_info.return_type" ")
strline=cat(strline,function_info.class_name)
strline = cat(strline,"::")
strline = cat(strline,function_info.func_name)
strline = cat(strline,"(");
strline =cat(strline,function_info.parameters)
strline =cat(strline,")")
return strline
}




// 从 classname.func 中提取类名与函数名
macro GetClassNameAndFuncName(str)
{
len =strlen(str)
i = 0;
point_pos = 0;
while(len > 0 && i < len)
{
if(str[i]== ".")
{
point_pos=i
break
}
i++
}


ret = nil
if(point_pos > 0)
{
ret.class_name = strmid(str,0,point_pos)

}
else
{
Msg("未发现类名")
stop
}
ret.func_name = strmid(str,point_pos+1,len)
return ret
}


// 从头文件中生成 函数定义,适用于c++项目
// 需准备:.cpp文件务必存在
macro CreateClassFuncDefine()
{
//从对象中取出类名 和函数名
fun = GetCurSymbol()
class_func_name = GetClassNameAndFuncName(fun);

// selcted = GetBufSelText(cur_file_buffer_handle);


//取出当前行所有内容
cur_file = GetCurrentBuf();
h_cur_wnd = GetCurrentWnd()
record = GetWndSel(h_cur_wnd)
selcted=GetBufLine(cur_file, record.lnFirst)
selcted = TrimLeft(selcted)
if(strlen(selected) < 1) {stop}
//提取函数所有需要的内容
func_inf = GetFuncInfo(selcted,class_func_name)
//转换为字符串
func_str=GetClassFuncDefineBody(func_inf)
 
opencorrespondingcpporhfile() //切换文件


//重新生成当前文件,并在最后一行生成函数体
h_buf = GetCurrentBuf();
lastline = GetBufLineCount(h_buf)

InsBufLine(h_buf,lastline,func_str);
InsBufLine(h_buf,lastline+1,"{");
InsBufLine(h_buf,lastline+2,"// ");
InsBufLine(h_buf,lastline+3,"}");
hwnd = GetCurrentWnd()

ScrollWndToLine(hwnd, lastline);


stop
}


//从当前文件缓存中 切换.h 与.c .cpp文件  
macro opencorrespondingcpporhfile()
{
    hcurrentbuf = getcurrentbuf ()
    if( hcurrentbuf == 0)
    stop


    Msg("opencorrespondingcpporhfile")
    bname = getbufname (hcurrentbuf)
    len = strlen (bname)


    if (".cpp" == strmid (bname, len-4, len))
{
    filename = strmid (bname, 0, len-4)#".h"
hbufnext = openbuf (filename)
if (hbufnext != 0)
{
            setcurrentbuf(hbufnext)
        }
    }
    else if(".c" == strmid (bname, len-2, len))
{
    filename = strmid (bname, 0, len-2)#".h"
hbufnext = openbuf (filename)
if (hbufnext != 0)//hnil 视乎有问题改为0
        {
            setcurrentbuf(hbufnext)
        }


    }
    else if (".h" == strmid (bname, len-2, len))
{
   filename = strmid (bname, 0, len-2)#".cpp"
   hbufnext = openbuf (filename)
  if (hbufnext != 0)
     {
            setcurrentbuf (hbufnext)
       }
else
{
       filename = strmid (bname, 0, len-2)#".c"
       hbufnext = openbuf (filename)
   if (hbufnext != 0)
       {
           setcurrentbuf (hbufnext)
       }
        }
}
    else
    {}
}

//

//[结束]-----------------------------------------------------------





//附加souceinsight知识点

Record 字符串的生成 与提取

在source Insight中很多函数 返回的类型的都为 Record的字符串,以GetWinSel()为例,其返回值为Selection Record

其中有多个属性与字段

Field

Description

lnFirst

the first line number

ichFirst

the index of the first character on the line lnFirst


如何qu

示例:

select_str = GetWndSel(hwnd)

line_first_num = select_str.lnFirst


如何取值


示例一:这个不建议,这样用

select ="lnFirst=\"@lntostart@\";ichFirst=\"@lntoend@\";lnLast=\"@lntostart@\";ichLim=\"@lntoend2@\";fExtended=\"0\";fRect=\"0\""

示例二: 

select = nil 

select.lnFirst=lntostart

... 依次赋值即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Source Insight 3.5.63 汉化绿色版 安装: 双击install.reg将文件导入注册表即完成软件安装与注册。 程序介绍: 程序编辑器和代码浏览器 Source Insight是一个面向项目开发的程序编辑器和代码浏览器,它拥有内置的对C/C++, C#和Java等程序的分析。Source Insight能分析你的源代码并在你工作的同时动态维护它自己的符号数据库,并自动为你显示有用的上下文信息。 Source Insight不仅仅是一个强大的程序编辑器,它还能显示reference trees,class inheritance diagrams和call trees。Source Insight提供了最快速的对源代码的导航和任何程序编辑器的源信息。 Source Insight提供了快速和革新的访问源代码和源信息的能力。与众多其它编辑器产品不同,Source Insight能在你编辑的同时分析你的源代码,为你提供实用的信息并立即进行分析。 技术特点: 介绍 Source Insight是如今最好用的语言编辑器,支持几乎所有的语言, 如C、C++、ASM、PAS、ASP、HTML等常见的,还支持自己定义关键字,如果您 是一个程序员或者网页制作人,这个软件对您都是有帮助的! Source Insight为您提供了可快速访问源代码和源信息的功能。Source Insight不象其它的编辑器产品,它还可以帮您分析源代码,并在您编辑的同时立刻提供给您有用的信息和分析。 Up-To-Date Information (improved!) 更新的信息(增强!) Source Insight自动创建并维护它自己高性能的符号数据库,包括函数、method、全局变量、结构、类和工程源文件定义的其它类型的符号。Source Insight 可以迅速地更新您的文件信息,即使在您编辑代码的时候。而且,符号数据库的符号可以自动创建倒您的工程文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值