Re: RichEdit syntax hilighting??

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Well, I wrote a small test program and used a ~1MB XML (a good 25000 lines of text) document as the input. It took about 71.9 seconds to search through the doc and highlight each XML tag blue.

Now, it originally exhibited the behavior you are seeing and took exponentially longer with the length of the file. I used the EM_SETUNDOLIMIT message to set the undo limit to zero. That sped it up quite a bit.

Here's the code:

//// CODE FRAGMENT ////

	CFileDialog dlg(
	 TRUE,
	 NULL,
	 NULL,
	 0,
	 _T("All Files (*.*)|*.*||"),
	 this);
	
	if(dlg.DoModal()!=IDCANCEL){
	 CFile file;
	 if(file.Open(dlg.GetPathName(), CFile::modeRead)){
	  file.SeekToBegin();

	  EDITSTREAM es={0};
	  es.dwCookie=reinterpret_cast<DWORD>(&file);
	  es.pfnCallback=StreamInCallback;

	  m_Edit.SendMessage(
	   EM_STREAMIN,
	   SF_TEXT,
	   reinterpret_cast<LPARAM>(&es));

	  file.Close();
	 }
	}

	int nTabSize=8;

	m_Edit.SendMessage(
	 EM_SETTABSTOPS,
	 1,
	 reinterpret_cast<LPARAM>(&nTabSize));

	LockWindowUpdate();

	FINDTEXTEX find;
	find.chrg.cpMin=0;
	find.chrg.cpMax=-1;

	m_Edit.SendMessage(
	 EM_SETUNDOLIMIT,
	 0,
	 0);

	clock_t s=clock(), e=0;

	for( ; ; ){
	 CHARRANGE fmt={0};

	 find.lpstrText=_T("<");

	 int nRet=m_Edit.SendMessage(
	  EM_FINDTEXTEX,
	  FR_DOWN,
	  reinterpret_cast<LPARAM>(&find));

	 if(nRet==-1)
	  break;

	 fmt.cpMin=find.chrgText.cpMin;
	 find.chrg.cpMin=find.chrgText.cpMax;
	 find.lpstrText=_T(">");

	 nRet=m_Edit.SendMessage(
	  EM_FINDTEXTEX,
	  FR_DOWN,
	  reinterpret_cast<LPARAM>(&find));

	 if(nRet==-1)
	  break;

	 fmt.cpMax=find.chrgText.cpMax;
	 find.chrg.cpMin=find.chrgText.cpMax;
	 find.chrg.cpMax=-1;

	 m_Edit.SendMessage(
	  EM_SETSEL,
	  fmt.cpMin,
	  fmt.cpMax);

	 CHARFORMAT c={0};
	 c.cbSize=sizeof(c);
	 c.dwMask=CFM_COLOR;
	 c.crTextColor=RGB(0, 0, 255);

	 m_Edit.SendMessage(
	  EM_SETCHARFORMAT,
	  SCF_SELECTION,
	  reinterpret_cast<LPARAM>(&c));
	}

	e=clock();

	TRACE(_T(">> %.1fs\n"), double(e-s)/CLOCKS_PER_SEC);

	UnlockWindowUpdate();

//// END FRAGMENT ////

bartt wrote:
Thanks for the reply Randy-
I am trying to highlight the entire contents of the control. But it seems to take geometrically longer the larger the file gets( eg. <1sec for about a hundred chars, ~10 sec for 800 chars, and >5min for 32k chars). It took so long for the last test that I thought it had broken.


I am trying to highlight XML, to differentiate the values from the tags.
So I am just walking the contents looking for the < and > chars, and changing the font color on the value not the tags. To do this I have to set the selected text for each occurance and change the font color.


My loop is pretty tight and I have examined it in detail to make sure that there isn't something obviously wrong.

Aside from only hightlighting the displayed portion of the control, is there another way to do this that might be faster.? Is using the selected text an expensive way to approach this? Perhaps I could try a different technique besides a color change?

Any ideas are appreciated.


Randy Widell wrote:

bartt wrote:

I am using a control that is derived from RichEdit to display some file contents and allow edits. So far so good.
I added some code to do Syntax Hilighting and found that it is terribly slow.
I am running XP pro with the latest SP's..
There is a riched20.dll (8/4/2004) file in my system32 dir which I am assuming is where the control code comes from.


Can anyone point me to a newer dll or some example code on how to make this fly..?

TIA..



Are you sure it is the RichEditCtrl and not the highlighting algorithm or whatever you are using to find tokens?


I wrote a class derived from CRichEditCtrl to syntax highlight mathematical expressions. One thing I thought of doing, but decided not to since my expected input is usually < 256 chars, is localizing the highlighting updates. You can try taking advantage of assumptions that can be made about your input to determine the smallest amount of text that needs to be updated around the cursor position when the user edits the text.

Also, if you have a lot of text, you should only update the visible text, only text that is revealed by scrolling, etc.

Oh, also try putting LockWindowUpdate() and UnlockWindowUpdate() around your highlighting code to keep the control from updating at the same time you are setting text styles.
.



Relevant Pages

  • Re: Problem with code in Firefox, works OK in Netscape 7.1
    ... Thank you very much for taking the trouble to reply in such detail. ... has allowed the script to complete, which was the main problem I was ... through the checker at http://validator.w3.org/ didn't highlight any ... but not with the tags). ...
    (comp.lang.javascript)
  • Re: macro to highlight text in carrots
    ... I need everything inside the carrots to be highlighted dark gray. ... In your example, it would be easiest to highlight everything, and then ... remove the highlight from the tags with a wildcard replacement: ... If you just want to highlight the innermost tags, ...
    (microsoft.public.word.vba.general)
  • Re: Problem with code in Firefox, works OK in Netscape 7.1
    ... through the checker at http://validator.w3.org/ didn't highlight any ... but not with the tags). ... Stephane Moriaux et son vieux Mac ...
    (comp.lang.javascript)
  • Re: Tag&Rename - adding composer?
    ... spewed forth these words of wisdom: ... >HIGHLIGHT the item. ... IOW, ... Checking a file is used for working with the tags. ...
    (microsoft.public.windowsxp.music)
  • Re: Highlight Row
    ... Remote Programming and Training ... Mary Fetsch wrote: ... In a datasheet, what you could do, is highlight a main field on the row...maybe even use an unbound control first as an indicator. ... TO HIGHLIGHT THE CURRENT RECORD IN A CONTINUOUS FORM ...
    (microsoft.public.access.formscoding)