加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Source Insight中代码块注释(利用/**/)及取消注释实现方法

发布时间:2020-12-16 00:24:07 所属栏目:大数据 来源:网络整理
导读:用了许久source Insight写C/C++代码,发现其中没有块注释功能很不方便,于是今天研究了下怎样让sourceInsight实现块注释。 网上介绍了很多方法实现块注释,但是都是对代码利用“//”逐行注释,没有用“/* */”实现的,我个人比较倾向于用/* */注释代码块,所

用了许久source Insight写C/C++代码,发现其中没有块注释功能很不方便,于是今天研究了下怎样让sourceInsight实现块注释。

网上介绍了很多方法实现块注释,但是都是对代码利用“//”逐行注释,没有用“/* */”实现的,我个人比较倾向于用/* */注释代码块,所以今天自己动手写了利用”/* */“实现块注释代码。

好了,废话不多说,直接上宏代码,后面会介绍使用方法:

macro _tsGetTabSize()
{
	szTabSize = GetReg("TabSize");

	if (szTabSize != "")
	{
		tabSize = AsciiFromChar(szTabSize[0]) - AsciiFromChar("0");
	}
	else
	{
		tabSize = 4;
	}

	return tabSize;
}


macro CommentBlock_Joyce()
{
	hbuf = GetCurrentBuf();
	hwnd = GetCurrentWnd();

	sel = GetWndSel(hwnd);

	iLine = sel.lnFirst;
	
	// indicate the comment char according to the file type
	// for example,using "#" for perl file(.pl) and "/* */" for C/C++.
	filename = tolower(GetBufName(hbuf));
	suffix = "";
	len = strlen(filename);
	i = len - 1;
	while (i >= 0)
	{
		if (filename[i-1] == ".")
		{
			suffix = strmid(filename,i,len)
			break;
		}
		i = i -1;
	}
	if  ( suffix == "pl" )
	{
		filetype = 2; // PERL
	}
	else
	{
		filetype = 1; // C
	}

	szLine = GetBufLine(hbuf,iLine);
	if (filetype == 1) 	// C
	{
		szLine = cat("/*	",szLine);
	}
	else				// PERL
	{
		szLine = cat("#	",szLine);
	}
	PutBufLine(hbuf,iLine,szLine);
	iLine = sel.lnLast;
	szLine = GetBufLine(hbuf,iLine);
	if (filetype == 1) 	// C
	{
		szLine = cat(szLine,"*/	");
	}
	else				// PERL
	{
		szLine = cat("#	",szLine);



	if (sel.lnFirst == sel.lnLast)
	{
		tabSize = _tsGetTabSize() - 1;
		sel.ichFirst = sel.ichFirst + tabSize;
		sel.ichLim = sel.ichLim + tabSize;
	}
	SetWndSel(hwnd,sel);
}




//
// Undo the CommentBlock for the selected text.
//
macro UnCommentBlock_Joyce()
{
	hbuf = GetCurrentBuf();
	hwnd = GetCurrentWnd();
	
	sel = GetWndSel(hwnd);

	iLine = sel.lnFirst;


	// indicate the comment char according to the file type
	// for example,len)
			break;
		}
		i = i -1;
	}
	if  ( suffix == "pl" )
	{
		filetype = 2; // PERL
	}
	else
	{
		filetype = 1; // C
	}

	tabSize = 0;

	endLine = GetBufLine(hbuf,sel.lnLast);
	endLineLen = strlen(endLine);
	szLine = GetBufLine(hbuf,iLine);
	len = strlen(szLine);
	szNewLine = "";
	commentState = 1;

	if (szLine[0] == "/" && szLine[1] == "*")
	{
		if(endLine[endLineLen-2] == "/" && endLine[endLineLen-3] == "*")
		{
			if (filetype == 1) 	// C
			{
				if (len > 1)
				{
					if (szLine[0] == "/" && szLine[1] == "*")
					{
						if (len > 2)
						{
							if (AsciiFromChar(szLine[2]) == 9)
							{
								tabSize = _tsGetTabSize() - 1;
								szNewLine = strmid(szLine,3,strlen(szLine));
							}
						}

						if (szNewLine == "")
						{
							szNewLine = strmid(szLine,2,strlen(szLine));
							tabSize = 2;
						}
						
						PutBufLine(hbuf,szNewLine);
					}
				}
			}
			if (filetype == 2) 	// PERL
			{
				if (len > 0)
				{
					if (szLine[0] == "#")	
					{
						if (len > 1)
						{
							if (AsciiFromChar(szLine[1]) == 9)
							{
								tabSize = _tsGetTabSize() - 1;
								szNewLine = strmid(szLine,1,szNewLine);
					}
				}
			}

			iLine = sel.lnLast;
			szLine = GetBufLine(hbuf,iLine);
			len = strlen(szLine);
			szNewLine = "";
			if (filetype == 1) 	// C
			{
				if (len > 1)
				{
					if (szLine[strlen(szLine)-2] == "/" && szLine[strlen(szLine)-3] == "*")
					{
						if (len > 2)
						{
							if (AsciiFromChar(szLine[2]) == 9)
							{
								tabSize = _tsGetTabSize() - 1;
								szNewLine = strmid(szLine,strlen(szLine)-2);
							}
						}

						if (szNewLine == "")
						{
							szNewLine = strmid(szLine,strlen(szLine)-3);
							tabSize = 2;
						}
						
						PutBufLine(hbuf,szNewLine);
					}
				}
			}
		}

	}
	

	if (sel.lnFirst == sel.lnLast)
	{
		sel.ichFirst = sel.ichFirst - tabSize;
		sel.ichLim = sel.ichLim - tabSize;
	}

	SetWndSel(hwnd,sel);
}


下面介绍下使用方法:


1)??????首先,打开sourceInsight 的"项目->打开项目->base”中的Utils.em文件,将以上宏代码复制到文件末尾,然后保存。

2)?????? 启用宏。? 菜单 “Options” -> “Key assignment”(中文版是选项->菜单关联)。? 在列表框中找到下面的宏:CommentBlock_Joyce、UnCommentBlock_Joyce

3)?给这些宏分配按键。点击“键..”,选中你需要分配按键的宏,点击“分配新键..”,然后在键盘上选择你喜欢的按键吧~设置好之后,点击“好”。

好了,设置完毕,试试吧~

下面把网上有大神写的单行注释和利用“//”进行多行注释的代码一起贴上来,方便大家使用,设置方法和前面的一样。

macro SingleLineComment()
{
szMyName = "Joyce"
// Get a handle to the current file buffer and the name
// and location of the current symbol where the cursor is.
hbuf = GetCurrentBuf()
ln = GetBufLnCur(hbuf)

// Get current time
szTime = GetSysTime(1)
Hour = szTime.Hour
Minute = szTime.Minute
Second = szTime.Second
Day = szTime.Day
Month = szTime.Month
Year = szTime.Year
if (Day < 10)
szDay = "0@Day@"
else
szDay = Day
//szMonth = NumToName(Month)
if (Month < 10)
     szMonth = "0@Month@"
else
szMonth = Month

szDescription = Ask("请输入修改原因")
// begin assembling the title string
InsBufLine(hbuf,ln+1,"/*@szDescription@ @szMyName@.xmyanfa @Year@-@szMonth@-@szDay@*/")
}

macro MultiLineCommentHeader()
{
szMyName = "Joyce"
// Get a handle to the current file buffer and the name
// and location of the current symbol where the cursor is.
hbuf = GetCurrentBuf()
ln = GetBufLnCur(hbuf)

// Get current time
szTime = GetSysTime(1)
Hour = szTime.Hour
Minute = szTime.Minute
Second = szTime.Second
Day = szTime.Day
Month = szTime.Month
Year = szTime.Year
if (Day < 10)
szDay = "0@Day@"
else
szDay = Day
//szMonth = NumToName(Month)
if (Month < 10)
     szMonth = "0@Month@"
else
szMonth = Month

szDescription = Ask("请输入修改原因:")
// begin assembling the title string
InsBufLine(hbuf,ln + 1,"/*@szDescription@ @szMyName@.xmyanfa @Year@-@szMonth@-@szDay@ begin*/")
}

macro MultiLineCommentEnd()
{
szMyName = "Joyce"
// Get a handle to the current file buffer and the name
// and location of the current symbol where the cursor is.
hbuf = GetCurrentBuf()
ln = GetBufLnCur(hbuf)

// Get current time
szTime = GetSysTime(1)
Hour = szTime.Hour
Minute = szTime.Minute
Second = szTime.Second
Day = szTime.Day
Month = szTime.Month
Year = szTime.Year
if (Day < 10)
szDay = "0@Day@"
else
szDay = Day
//szMonth = NumToName(Month)
if (Month < 10)
     szMonth = "0@Month@"
else
szMonth = Month

InsBufLine(hbuf,"/*@szMyName@.xmyanfa @Year@-@szMonth@-@szDay@ end*/")
}


//
// Comment the selected block of text using single line comments and indent it
//
macro CommentBlock()
{
	hbuf = GetCurrentBuf();
	hwnd = GetCurrentWnd();

	sel = GetWndSel(hwnd);

	iLine = sel.lnFirst;
	
	// added by Yongqiang,indicate the comment char according to the file type
	// for example,using "#" for perl file(.pl) and "//" for others.
	filename = tolower(GetBufName(hbuf));
	suffix = "";
	len = strlen(filename);
	i = len - 1;
	while (i >= 0)
	{
		if (filename[i-1] == ".")
		{
			suffix = strmid(filename,len)
			break;
		}
		i = i -1;
	}
	if  ( suffix == "pl" )
	{
		filetype = 2; // PERL
	}
	else
	{
		filetype = 1; // C
	}

	while (iLine <= sel.lnLast)
	{
		szLine = GetBufLine(hbuf,iLine);
		if (filetype == 1) 	// C
		{
			szLine = cat("//	",szLine);
		}
		else				// PERL
		{
			szLine = cat("#	",szLine);
		}
		PutBufLine(hbuf,szLine);
		iLine = iLine + 1;
	}

	if (sel.lnFirst == sel.lnLast)
	{
		tabSize = _tsGetTabSize() - 1;
		sel.ichFirst = sel.ichFirst + tabSize;
		sel.ichLim = sel.ichLim + tabSize;
	}
	SetWndSel(hwnd,sel);
}


//
// Undo the CommentBlock for the selected text.
//
macro UnCommentBlock()
{
	hbuf = GetCurrentBuf();
	hwnd = GetCurrentWnd();
	
	sel = GetWndSel(hwnd);

	iLine = sel.lnFirst;


	// added by Yongqiang,len)
			break;
		}
		i = i -1;
	}
	if  ( suffix == "pl" )
	{
		filetype = 2; // PERL
	}
	else
	{
		filetype = 1; // C
	}

	tabSize = 0;
	while (iLine <= sel.lnLast)
	{
		szLine = GetBufLine(hbuf,iLine);
		len = strlen(szLine);
		szNewLine = "";
		if (filetype == 1) 	// C
		{
			if (len > 1)
			{
				if (szLine[0] == "/" && szLine[1] == "/")
				{
					if (len > 2)
					{
						if (AsciiFromChar(szLine[2]) == 9)
						{
							tabSize = _tsGetTabSize() - 1;
							szNewLine = strmid(szLine,strlen(szLine));
						}
					}

					if (szNewLine == "")
					{
						szNewLine = strmid(szLine,strlen(szLine));
						tabSize = 2;
					}
					
					PutBufLine(hbuf,szNewLine);
				}
			}
		}
		if (filetype == 2) 	// PERL
		{
			if (len > 0)
			{
				if (szLine[0] == "#")	
				{
					if (len > 1)
					{
						if (AsciiFromChar(szLine[1]) == 9)
						{
							tabSize = _tsGetTabSize() - 1;
							szNewLine = strmid(szLine,szNewLine);
				}
			}
		}
		iLine = iLine + 1;
	}

	if (sel.lnFirst == sel.lnLast)
	{
		sel.ichFirst = sel.ichFirst - tabSize;
		sel.ichLim = sel.ichLim - tabSize;
	}

	SetWndSel(hwnd,sel);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读