Code annotation is the comment block of the C/C ++ language selected in the document window. The tabbars plug-in sets this function to simplify the operations in the programming process. Usually, you need to add/* at the beginning of the Code to comment a piece of code in the C style, and add/at the end of the Code */, in the C ++ style, add // at the beginning of each line. To restore the code, delete these symbols. The tabbars plug-in provides the ability to automatically add and remove these annotation symbols. It also supports inserting timestamps and other information at the beginning of the Code.
There are two types of annotation symbols: C style and C ++ style. Although tabbars provides two methods for adding annotation symbols, the processing method is the same in some cases, for example, when tabbars determines that the selected text is a piece of code in a row, it usually only uses the/**/method, because in most cases, the user chooses to comment out function parameters or replace the specifiers, use/**/is more in line with the user's intent.
To add a annotator, you must first obtain the selected text block from the document. You can use get_selection of itextdocument to obtain an itextselection object. Four attributes of the itextselection object are used: topline, Bottomline, currentline and currentcolumn can be used to obtain the four coordinates of N currently selected text. These coordinates are in the unit of rows and characters, the coordinates are the basis for tabbars to determine whether the user chooses a line of code or a piece of code. The following code is used to determine the user and add a C-style annotator to the text block:
Strtext = BSTR;
Long lstart = 0, lend = 0, icolend = 0, lcurrent;
Psel-> get_topline (& lstart );
Psel-> get_bottomline (& lend );
Psel-> get_currentline (& lcurrent );
Psel-> get_currentcolumn (& icolend );
If (lstart = lend) // select on a row
{
If (lcurrent = lend) & (icolend> 1) // not a whole line
{
Strtmp. Format (_ T ("/* % S */"), strtext); // still use /**/
BSTR = strtmp;
Psel-> put_text (BSTR );
}
Else if (lcurrent = (lend + 1) & (icolend = 1) // a whole line
{
Strtmp. Format (_ T ("// % s"), strtext); // use //
BSTR = strtmp;
Psel-> put_text (BSTR );
}
}
Else // select multiple rows
{
Int idx = 0;
Int totalline = Lend-lstart + 1;
Strtmp = _ T ("/*"); // annotation start symbol
If (g_baddtime & g_badduser) // determine whether to add additional comment information
{
Cstring strtt;
Strtmp + = _ T ("@ ** # ---"); // start flag of additional information
If (g_baddtime)
{
Systemtime st;
: Getlocaltime (& St );
Strtt. Format (_ T ("% 04d-% 02d-% 02d % 02d: % 02d: % 02d"), st. wyear, st. wmonth,
St. wday, st. whour, st. wminute, st. wsecond );
Strtmp + = strtt;
}
If (g_badduser)
{
Strtt. Format (_ T ("(% s)"), g_szuser );
Strtmp + = strtt;
}
Strtmp + = _ T ("--- # ** @"); // end mark of additional information
}
Strtmp + = _ T ("/R/N ");
Strtmp + = strtext;
Strtmp + = _ T ("*/"); // comment the end symbol
BSTR = strtmp;
Psel-> put_text (BSTR); // Replace the selected text
}
The C ++-style annotation process a single line of text is the same as the above process, but it is slightly different when processing multiple lines of text, specifically insert a // symbol at the beginning of each line.
The Code recovery process is to delete the annotation symbol from the selected text. The tabbars plug-in can automatically identify the annotation style and delete the annotation symbol. For texts that use/**/Annotations, you do not have to strictly select the text from/* start to */end when you cancel the annotation. It doesn't matter if you have a few more annotations, the tabbars plugin automatically matches the annotator. As for block comments in the C ++ style, tabbars will delete the first // match of each row without affecting other intra-row comments:
Strtmp = lpsznull;
Int idx = 0;
While (idx = strtext. Find (_ T ("//"), idx ))! =-1)
{
Bchange = true;
Strtmp + = strtext. Left (idx );
Strtext = strtext. mid (idx + 2); // filter //
Idx = strtext. Find (_ T ("/R/N"), 0); // jump to the end of the row
Strtmp + = strtext. Left (idx + 2 );//
Strtext = strtext. mid (idx + 2); // start from the new line
Idx = 0;
}
Strtmp + = strtext; // It is not a good idea, but can void your bugs