实现思路分两部:
第一步:找到类名与函数名
第二步:进入对应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
... 依次赋值即可