eclipse plugin: Create a wrapper method over the...

...'resolveElementAt' to prevent the NPE bug in xtext
This commit is contained in:
Timotei Dolean 2011-08-04 19:16:46 +00:00
parent 9216c01fb8
commit f51eb6a9d4
4 changed files with 33 additions and 23 deletions

View file

@ -8,6 +8,15 @@
*******************************************************************************/
package org.wesnoth.ui.labeling.wmldoc;
import org.wesnoth.Logger;
import org.wesnoth.installs.WesnothInstallsUtils;
import org.wesnoth.preprocessor.Define;
import org.wesnoth.projects.ProjectUtils;
import org.wesnoth.ui.editor.WMLEditor;
import org.wesnoth.utils.WMLUtils;
import org.wesnoth.wml.WMLMacroCall;
import org.wesnoth.wml.WMLTag;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
@ -19,14 +28,6 @@ import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.utils.EditorUtils;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
import org.wesnoth.Logger;
import org.wesnoth.installs.WesnothInstallsUtils;
import org.wesnoth.preprocessor.Define;
import org.wesnoth.projects.ProjectUtils;
import org.wesnoth.ui.editor.WMLEditor;
import org.wesnoth.utils.WMLUtils;
import org.wesnoth.wml.WMLMacroCall;
import org.wesnoth.wml.WMLTag;
/**
* A handler that handles pressing F2 on a resource in the editor
@ -52,7 +53,7 @@ public class WMLDocHandler extends AbstractHandler
Point positionAbsolute = editor.getInternalSourceViewer().getTextWidget().toDisplay(positionRelative);
positionAbsolute.y += 20;
EObject grammarElement = WMLUtils.EObjectUtils( ).resolveElementAt( resource, selection.getOffset( ) );
EObject grammarElement = WMLUtils.resolveElementAt( resource, selection.getOffset( ) );
if ( grammarElement == null )
return;

View file

@ -36,7 +36,7 @@ public class WMLHyperlinkHelper extends HyperlinkHelper
{
super.createHyperlinksByOffset(resource, offset, acceptor);
EObject object = WMLUtils.EObjectUtils( ).resolveElementAt( resource, offset );
EObject object = WMLUtils.resolveElementAt( resource, offset );
if ( object == null )
return;

View file

@ -10,6 +10,10 @@ package org.wesnoth.ui.syntax;
import java.util.Iterator;
import org.wesnoth.ui.editor.WMLEditor;
import org.wesnoth.utils.WMLUtils;
import org.wesnoth.wml.WMLTag;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.text.IDocument;
@ -19,9 +23,6 @@ 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.editor.WMLEditor;
import org.wesnoth.utils.WMLUtils;
import org.wesnoth.wml.WMLTag;
public class WMLCharacterPairMatcher extends DefaultCharacterPairMatcher
{
@ -70,12 +71,7 @@ public class WMLCharacterPairMatcher extends DefaultCharacterPairMatcher
public synchronized void computeMatchingRegion(XtextResource state, int offset)
{
EObject object = null;
try {
WMLUtils.EObjectUtils( ).resolveElementAt( state, offset );
} catch ( NullPointerException e ) {
// fix a bug in xtext when the document has syntax errors
}
EObject object = WMLUtils.resolveElementAt( state, offset );
// do nothing if we clicked the same tag
if ( currentTag_ == object )

View file

@ -8,15 +8,17 @@
*******************************************************************************/
package org.wesnoth.utils;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
import org.wesnoth.wml.WMLExpression;
import org.wesnoth.wml.WMLKey;
import org.wesnoth.wml.WMLKeyValue;
import org.wesnoth.wml.WMLTag;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
import org.eclipse.xtext.resource.XtextResource;
public class WMLUtils
{
private static EObjectAtOffsetHelper eObjectAtOffsetHelper_;
@ -29,6 +31,17 @@ public class WMLUtils
return eObjectAtOffsetHelper_;
}
public static EObject resolveElementAt( XtextResource resource, int offset ) {
try{
return EObjectUtils( ).resolveElementAt( resource, offset );
}
catch( NullPointerException e ) {
// in xtext 2.0, there is a bug, that if the file
// contains errors, this method may throw a null pointer exception
return null;
}
}
/**
* Returns the child key of the specified tag by its name.
* @param tag The tag to search into