eclipse plugin: Re-implement the start-end tag...

...matching since the previous one made the CPU to stay on 100% and
keep spawning threads for the modified xtext resource model
This commit is contained in:
Timotei Dolean 2011-07-26 15:29:22 +00:00
parent ae52e466d4
commit 858eced060
6 changed files with 95 additions and 49 deletions

View file

@ -9,10 +9,11 @@
package org.wesnoth.ui;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EObject;
import org.wesnoth.ui.syntax.WMLHighlightingConfiguration;
/**
* A simple WML adapter that holds a coloring id
* A simple WML adapter that holds a coloring id on the specified resource
*/
public class WMLSyntaxColoringAdapter extends AdapterImpl
{
@ -22,14 +23,13 @@ public class WMLSyntaxColoringAdapter extends AdapterImpl
public String ColorId;
/**
* True whether this coloring applies to the start
* of the tag or to the end tag
* The object to color
*/
public boolean PaintStart;
public EObject TargetEObject;
public WMLSyntaxColoringAdapter( String id, boolean paintStart)
public WMLSyntaxColoringAdapter( String id, EObject target)
{
ColorId = id;
PaintStart = paintStart;
TargetEObject = target;
}
}

View file

@ -23,12 +23,14 @@ import org.eclipse.xtext.ui.editor.folding.DefaultFoldingRegionProvider;
import org.eclipse.xtext.ui.editor.hyperlinking.HyperlinkHelper;
import org.eclipse.xtext.ui.editor.syntaxcoloring.AbstractAntlrTokenToAttributeIdMapper;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfiguration;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingHelper;
import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;
import org.wesnoth.ui.autoedit.WMLAutoEditStrategy;
import org.wesnoth.ui.contentassist.WMLContentAssistContext;
import org.wesnoth.ui.contentassist.WMLProposalComparator;
import org.wesnoth.ui.editor.WMLAbstractDirtyEditorCallback;
import org.wesnoth.ui.editor.WMLEditor;
import org.wesnoth.ui.editor.WMLHighlightingHelper;
import org.wesnoth.ui.folding.WMLFoldingRegionProvider;
import org.wesnoth.ui.labeling.WMLLabelProvider;
import org.wesnoth.ui.navigation.WMLHyperlinkHelper;
@ -59,6 +61,12 @@ public class WMLUiModule extends org.wesnoth.ui.AbstractWMLUiModule
super.configure(binder);
}
@Override
public Class<? extends IHighlightingHelper> bindIHighlightingHelper()
{
return WMLHighlightingHelper.class;
}
public Class<? extends IHighlightingConfiguration> bindIHighlightingConfiguration()
{
return WMLHighlightingConfiguration.class;

View file

@ -33,10 +33,8 @@ import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
@ -50,6 +48,7 @@ import org.eclipse.ui.ide.FileStoreEditorInput;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingHelper;
import org.eclipse.xtext.ui.editor.utils.EditorUtils;
import org.wesnoth.Logger;
import org.wesnoth.WesnothPlugin;
@ -66,6 +65,8 @@ public class WMLEditor extends XtextEditor
@Inject
private IExtXtextEditorCustomizer editorCustomizer;
protected HighlightingHelper highlightingHelper_;
public WMLEditor()
{
super();
@ -81,15 +82,10 @@ public class WMLEditor extends XtextEditor
super.createPartControl(parent);
}
public StyledText getTextWidget()
{
return getSourceViewer().getTextWidget();
}
public ISourceViewer getSourceViewer_()
{
return getSourceViewer();
}
public HighlightingHelper getHighlightingHelper()
{
return highlightingHelper_;
}
@Override
public boolean equals(Object obj)

View file

@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2011 by Timotei Dolean <timotei21@gmail.com>
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.wesnoth.ui.editor;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.XtextSourceViewer;
import org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingHelper;
public class WMLHighlightingHelper extends HighlightingHelper
{
@Override
public void install( XtextEditor editor, XtextSourceViewer sourceViewer )
{
super.install( editor, sourceViewer );
// set the highlighting helper in our editor
// so we can access the highlightingReconcilier in order
// to be able to refresh the semantic highlighting
if ( editor instanceof WMLEditor ) {
( ( WMLEditor ) editor ).highlightingHelper_ = this;
}
}
}

View file

@ -17,14 +17,15 @@ import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.model.XtextDocument;
import org.eclipse.xtext.ui.editor.utils.EditorUtils;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
import org.wesnoth.ui.WMLSyntaxColoringAdapter;
import org.wesnoth.ui.editor.WMLEditor;
import org.wesnoth.utils.WMLUtils;
import org.wesnoth.wml.WMLTag;
public class WMLCharacterPairMatcher extends DefaultCharacterPairMatcher
{
private WMLSyntaxColoringAdapter currentAdapter_;
private WMLTag currentTag_;
private int matchCnt = 0;
@ -32,7 +33,6 @@ public class WMLCharacterPairMatcher extends DefaultCharacterPairMatcher
public WMLCharacterPairMatcher( char[] chars )
{
super( chars );
currentAdapter_ = null;
currentTag_ = null;
}
@ -42,57 +42,66 @@ public class WMLCharacterPairMatcher extends DefaultCharacterPairMatcher
++ matchCnt;
IRegion region = super.match( doc, offset );
if ( region == null && doc instanceof XtextDocument ) {
if ( region == null && doc instanceof XtextDocument &&
doc.getLength( ) > 0 ) {
if ( matchCnt == 2 ) {
matchCnt = 0;
} else {
( ( XtextDocument ) doc ).modify( new IUnitOfWork<IRegion, XtextResource>(){
( ( XtextDocument ) doc ).readOnly( new IUnitOfWork<Boolean, XtextResource>(){
@Override
public IRegion exec( XtextResource state ) throws Exception
public Boolean exec( XtextResource state ) throws Exception
{
return computeMatchingRegion( state, offset );
computeMatchingRegion( state, offset );
return true;
}
});
// refresh the highlighting
WMLEditor currentEditor = ( WMLEditor ) EditorUtils.getActiveXtextEditor( );
if ( currentEditor != null &&
currentEditor.getHighlightingHelper( ) != null &&
currentEditor.getHighlightingHelper( ).getReconciler( ) != null )
currentEditor.getHighlightingHelper( ).getReconciler( ).refresh( );
}
}
return region;
}
public IRegion computeMatchingRegion(XtextResource state, int offset)
public synchronized void computeMatchingRegion(XtextResource state, int offset)
{
EObject object = WMLUtils.EObjectUtils( ).resolveElementAt( state, offset );
// do nothing if we clicked the same tag
if ( currentTag_ == object )
return null;
// remove current colored tag ( if any )
if ( currentTag_ != null ) {
synchronized ( currentTag_ ) {
Iterator<Adapter> itor = currentTag_.eAdapters( ).iterator( );
while ( itor.hasNext( ) ) {
if ( itor.next( ) instanceof WMLSyntaxColoringAdapter ) {
itor.remove( );
}
}
currentAdapter_ = null;
currentTag_ = null;
}
}
return;
if ( object instanceof WMLTag ) {
WMLTag tag = ( WMLTag ) object;
currentAdapter_ = new WMLSyntaxColoringAdapter( WMLHighlightingConfiguration.RULE_MATCH_TAG, true );
currentTag_ = tag;
tag.eAdapters( ).add( currentAdapter_ );
}
for ( Adapter adapter : state.eAdapters( ) ) {
if ( adapter instanceof WMLSyntaxColoringAdapter ){
return null;
( ( WMLSyntaxColoringAdapter ) adapter ).TargetEObject = object;
return; // done here
}
}
state.eAdapters( ).add( new WMLSyntaxColoringAdapter(
WMLHighlightingConfiguration.RULE_MATCH_TAG, object ) );
} else {
// nothing new selected, just remove current adapter
Iterator<Adapter> itor = state.eAdapters( ).iterator( );
while ( itor.hasNext( ) ) {
if ( itor.next( ) instanceof WMLSyntaxColoringAdapter ) {
itor.remove( );
break;
}
}
currentTag_ = null;
}
}
}

View file

@ -90,12 +90,16 @@ public class WMLSemanticHighlightingCalculator implements ISemanticHighlightingC
}
// check if we have any information specific information for highlighting
for ( Adapter adapter : current.eAdapters( ) ) {
for ( Adapter adapter : resource.eAdapters( ) ) {
if ( adapter instanceof WMLSyntaxColoringAdapter ) {
WMLSyntaxColoringAdapter wmlAdapter = ( WMLSyntaxColoringAdapter ) adapter;
for ( Pair<INode, String> pair : toColor ) {
pair.Second = wmlAdapter.ColorId;
if ( wmlAdapter.TargetEObject == current ) {
for ( Pair<INode, String> pair : toColor ) {
pair.Second = wmlAdapter.ColorId;
}
}
break;