This C# example formats C-Sharp code in a very simple way.
It normalizes all paragraphs in the text. It also removes the character attributes from all characters and make the complete text use Courier New, 9pt. The code remove all spaces from the beginning of each paragraph and detect comments and nesting using { }. Depending on the nesting level tabs are inserted. We have applied this method to the code itself to format it.
IWPMemo Memo = wpdllInt1.Memo;
IWPTextCursor TextCursor = Memo.TextCursor;
IWPParInterface par = Memo.CurrPar;
TextCursor.CPPosition = 0;
int nesting = 0;
bool neststart;
while(par!=null)
{
// Remove all paragraph and character attributes
par.ParAClear(1);
// and apply new attributes as paragraph attributes
par.ParASet((int)WPAT.CharFont, par.ConvertFontnameToIndex("Courier New"));
par.ParASet((int)WPAT.CharFontSize, 9 * 100); // ==9pt
// Remove all spaces at the beginning
while((par.GetChar(0)<=32)&&(par.GetChar(0)>0))
par.DeleteChar(0,1);
// This paragraph starts with // - make it green + italic since it is a comment
if ((par.GetChar(0)==(int)'/')&&(par.GetChar(1)==(int)'/'))
{
par.ParASet((int)WPAT.CharColor, par.ConvertColorToIndex(0x00005E00));
par.ParAAddBits((int)WPAT.CharStyleON, 2);
par.ParAAddBits((int)WPAT.CharStyleMask, 2);
}
// else it is a closing }
else if (par.GetChar(0)==(int)'}') nesting--;
neststart = (par.GetChar(0)==(int)'{');
// Insert tabs according to nesting
for (int i = 0; i < nesting; i++)
par.InsertText(0,"\t",-2);
if (neststart) nesting++;
// Move to next line
if (!par.SelectNextPar(true)) break;
}
Memo.ReformatAll(true,true);
wpdllInt1.ReleaseInt(Memo);
wpdllInt1.ReleaseInt(TextCursor);
wpdllInt1.ReleaseInt(par);