Source Insight宏-格式化源文件

  功能:对整个源码文件进行格式化,使得对齐错落有致,功能类似于VC6.0/VS2005/VS2008的Alt+F8功能。

  因为设计功底较差,脚本写的较乱!

/**
	对文件进行排版,按格式对齐
	
	作者:王丰亮
	时间:2011-11-10
*/

macro files_formatting()
{
	hwnd = GetCurrentWnd()
	hbuf = GetCurrentBuf()
	if(hbuf ==0)
		stop 
	
	result = one_file_formatting(hbuf)	// 返回为0,表示括号对应,否则文件结构错误
 
	if(result != 0)
		Msg("括号数不成对,请进行检查!")

	SaveBuf(hbuf)
	SetCurrentBuf(hbuf)
	
}

// 对当前文件进行格式化对齐处理
macro one_file_formatting(hbuf)
{
	const_space = " "
	const_brace1 = "{"
	const_brace2 = "}"
		
	const_comments_single = "//" 			// 单行注释符号
	const_comments_multi_begin = "/*" 		// 多行注释符号-开始符
	const_comments_multi_end = "*/" 		// 多行注释符号-结束符
	
	
	const_padding_num = 4						// 每次缩进量(空格)
	current_padding_num = 0 					// 当前缩进量(空格)
	
	
	isMultiComment = 0 
	isSingleComment = 0
		
	
	cur_line_index = 0 ;
	total_line_num = GetBufLineCount(hbuf)
	Msg(total_line_num)
	while(cur_line_index < total_line_num)
	{
		dest_text = ""
		text = GetBufLine(hbuf, cur_line_index)

		// 将开头及尾部的空格去掉
		text_no_space = string_trim(text, const_space)
	
		if(strlen(text_no_space) == 0)										// 全是空格,则转换为空行
		{
			dest_text = ""
		}
		else if(isMultiComment == 1)
		{
			dest_text = text
		}
		else if(text_no_space == const_brace2)								// "}"单独在一行时,缩进单独处理
		{
			pad_str = padding_string(current_padding_num-const_padding_num, const_space) ;	// 填充字符串
			dest_text = cat(pad_str, text_no_space)							// 目标字符串	
		}
		else 
		{
			pad_str = padding_string(current_padding_num, const_space) ;	// 填充字符串
			dest_text = cat(pad_str, text_no_space)							// 目标字符串	
		}
			
		PutBufLine(hbuf, cur_line_index, dest_text) 						// 保存
		
		// 解析是否包含'{',或'}',暂时不考虑注释
		
		sub_index = 0  
		while(sub_index < strlen(text_no_space))
		{
			if(isMultiComment == 1)								// 寻找结束符
			{
				if(sub_index < strlen(text_no_space)-1)
				{
					if(strmid(text_no_space, sub_index, sub_index+2) == const_comments_multi_end)	// 以“*/”结束符
					{
						isSingleComment = 0
						isMultiComment = 0

						sub_index = sub_index + 1
					}
					sub_index = sub_index + 1
					
					continue
				}
				
				break  // 本行结束
			}
			// 忽略注释
			if(sub_index < strlen(text_no_space)-1)							// 至少2个字符才判断注释,否则越界
			{
				if (strmid(text_no_space, sub_index, sub_index+2) == const_comments_single)	// 以“//”开头的注释
				{
					isSingleComment = 1 
					isMultiComment = 0 
					sub_index = sub_index + 2 
					
					continue
				}
				else if(strmid(text_no_space, sub_index, sub_index+2) == const_comments_multi_begin)	// 以“/*”开头的注释
				{
					// 需要查找对应的注释结束符
					isSingleComment = 0 
					isMultiComment = 1 
					sub_index = sub_index + 2

					continue
				} 
			}				
			// 匹配brace
			if(strmid(text_no_space, sub_index, sub_index+1) == const_brace1)		// '{'
			{
				current_padding_num = current_padding_num + const_padding_num
			}
			else if(strmid(text_no_space, sub_index, sub_index+1) == const_brace2)	// '}'
			{
				current_padding_num = current_padding_num - const_padding_num
			}
			sub_index = sub_index + 1
		}
	
		cur_line_index = cur_line_index + 1
	}
	
	return current_padding_num
}

// 去掉开头及尾部特定字符(如:空格)
macro string_trim(s, ts)
{
	trim_s = string_trim_left(s, ts)
	
	return string_trim_right(trim_s, ts)
}

// 去掉开头特定字符(如:空格)
macro string_trim_left(s, ts)
{
	strim_s = s
	if(strlen(strim_s) == 0)
		return strim_s
		
	// 遍历开头的空格
	sub_indexSpace = 0 					
	while(sub_indexSpace < strlen(strim_s))
	{
		if(strmid(strim_s, sub_indexSpace, sub_indexSpace+1) != ts) 
			break 
			
		sub_indexSpace = sub_indexSpace + 1 
	}
	return strmid(strim_s, sub_indexSpace, strlen(strim_s))

}
// 去掉尾部特定字符(如:空格)
macro string_trim_right(s, ts)
{
	strim_s = s
	if(strlen(strim_s) == 0)
		return strim_s

	// 遍历尾部的空格
	sub_indexSpace = strlen(strim_s) 					
	while(sub_indexSpace > 0)
	{
		if(strmid(strim_s, sub_indexSpace-1, sub_indexSpace) != ts) 
			break 
			
		sub_indexSpace = sub_indexSpace - 1 
	}
	
	return strmid(strim_s, 0, sub_indexSpace)
}

// 填充current_padding_num数量的字符/字符串(ch)
macro padding_string(current_padding_num, ch)
{
	s = ""
	i = 0 
	while(i < current_padding_num)
	{
		s = cat(s, ch)
		i = i + 1 
	}
		
	return s
}



sourceinsight40114-setup 是 SourceInsight 软件的一个安装程序。SourceInsight 是一款专业的源代码阅读和分析工具,主要用于开发人员在软件项目中查看、编辑和理解源代码。 sourceinsight40114-setup 是该软件的一个特定版本的安装文件。它包含 SourceInsight 4.0.114 版本的所有必要组件和文件,可以通过运行这个安装程序将该版本的 SourceInsight 软件成功地安装到计算机中。 安装 SourceInsight 的步骤通常很简单。首先,双击运行 sourceinsight40114-setup 安装程序,然后按照安装向导的指示逐步进行安装。安装过程中需要选择安装路径,并可以选择是否创建桌面快捷方式和启动菜单项等。完成安装后,可以通过桌面快捷方式或者启动菜单中的图标来打开 SourceInsight 软件。 安装 SourceInsight 后,用户可以在其界面中打开源代码文件进行阅读和编辑。SourceInsight 提供了丰富的功能,如代码导航、代码分析、符号查询、变量跟踪、代码片段注释等,方便开发人员更好地理解和修改代码。此外,SourceInsight 还支持多种编程语言,如C、C++、C#、Java 等,可以满足不同项目的需求。 总之,sourceinsight40114-setup 是 SourceInsight 软件特定版本的安装程序,通过运行它可以将 SourceInsight 4.0.114 版本的软件成功地安装到计算机中,开发人员可以使用它来阅读、编辑和分析源代码,提高代码开发和维护的效率。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值