您的位置:首页 > 编程语言 > Qt开发

为Qt Creator 开发astyle代码格式化插件

2015-12-16 16:17 531 查看
与VS2008系列的IDE相比Qt
Creator开发代码插件比较简单
 
闲话不说,看我的步骤:
(1)Qt Creator中“菜单”-“工具”-“选项”-“环境”-“外部工具”-“文本”

 


(2)接下来说明一下如何写一个工具处理输入的文本
这个输入文本通过控制台的管道传入到工具程序中也就是stdout ,从工具那边就是stdin可以读取到使用如下代码进行读取输入文本:
 
void _FormatCode(constCStringA& IN strSelectCode, CStringA& OUT strOutput);
#define  MAX_CODE_LEN (3*1024*1024)
int _tmain(int argc, _TCHAR*argv[])
{
#ifdef _DEBUG
        MessageBox(NULL,L"srcFormater Attach", L"please Attach", 0);
#endif
        //通过stdin读取输入的代码
        //
        char*pbuf = new(nothrow) char[MAX_CODE_LEN];
 
        if(pbuf)
        {
               intp = 0;
 
               while(TRUE)
               {
                       intc = fgetc(stdin);
 
                       if(c== EOF)
                       {
                               break;
                       }
 
                       pbuf[p++]= (char)c;
               }
 
               pbuf[p]= 0;
               intcodeLen = p;
               CStringAstrInput = pbuf;
               //将pbuf的字符串进行astyle格式化
               //
               //
               CStringAstrOutput;
               _FormatCode(strInput,strOutput);
 
               //输出格式化后的代码
               //
               if(!strOutput.IsEmpty())
               {
                       printf(strOutput);
               }
               else
               {
                       printf(strInput);//没有输入就把输入输出,当作没有变化
               }
 
               delete[]pbuf;
        }
 
        return0;
}
 
(3)_FormatCode 这个函数大家去自己实现一个就行了,可以调用astyle等工具进行处理,最后通过printf打印出来,注意QtCreator的换行符是"\n"如果你指定"\r\n"则会产生多余一个空行
 
astyle格式化工具的VS2008插件的代码在me的资源中有
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  QT astyle format 排版 代码