MedAl
MedAl

Reputation: 475

Freezing UI when drawing on editor

I am writing an Eclipse plugin, intended to make the Eclipse XML editor behave like the Java editor. One of the functionalities required is to highlight all the occurrences of the selected region (a word, a line, a character ...). This works fine, as long as you work on small files.

But when I begin to work with large files (e.g. 2000 lines of code), the UI freezes during the time required to process the highlighting. It is only a matter of 1 second or less, but it could be very irritant for the user. Those "freezes" occurs when my listeners are triggered, so :

My current code initiating the process of highlight is the following one :

try{
        if (isValidContext(fText, input))
            {
                if (textSelection == null)
                    xmlH = new XMLHighlighter(fText, input);
                else
                    xmlH = new XMLHighlighter(fText, input, textSelection);

                Display.getDefault().asyncExec(new Runnable(){
                    @Override
                    public void run(){
                        try{
                            xmlH.highligth();
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                });
            }
        } catch (Exception e){
            e.printStackTrace();
    }

I thought running a new Runnable would make this action completely independent of the other actions performed in the UI at the same time. Am I wrong ?

This piece of code is what I THINK is responsible of the lags. Perhaps would you see it otherwise ?

Thanks for reading.

Upvotes: 0

Views: 38

Answers (1)

greg-449
greg-449

Reputation: 111216

Display.asyncExec runs the Runnable in the UI thread as soon as possible. Using it when you are already in the UI thread does not really achieve anything in this case.

You must use a background thread, or an Eclipse Job, or ProgressMonitorDialog or IProgressService.run or even CompleteableFuture to run your long running code in the background. Use Display.asyncExec from your background code to update the UI.

Also note that the Eclipse text editor support code provides a very elaborate system for doing syntax highlighting and the like in the background. Look at things like SourceViewerConfiguration, IPresentationReconciler, IPartitionTokenScanner, IPresentationDamager, IPresentationRepairer amongst others. The Eclipse 'New Plug-in Project' wizard will even create the outline of an XML editor using these classes for you as one of the example.

Upvotes: 3

Related Questions