mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-11 22:51:25 +00:00
eclipse plugin: Make an EObjectAtOffsetHelper instance...
...singleton so it can be accessed from all places without the need to instantiate. Also refactor the getActiveFile method to let it get the editor as parameter.
This commit is contained in:
parent
3535576918
commit
5b4d5addc7
@ -14,10 +14,22 @@ import org.eclipse.xtext.nodemodel.ICompositeNode;
|
|||||||
import org.eclipse.xtext.nodemodel.ILeafNode;
|
import org.eclipse.xtext.nodemodel.ILeafNode;
|
||||||
import org.eclipse.xtext.nodemodel.INode;
|
import org.eclipse.xtext.nodemodel.INode;
|
||||||
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
|
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
|
||||||
|
import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
|
||||||
|
import org.eclipse.xtext.ui.editor.XtextEditor;
|
||||||
import org.eclipse.xtext.ui.editor.utils.EditorUtils;
|
import org.eclipse.xtext.ui.editor.utils.EditorUtils;
|
||||||
|
|
||||||
public class WMLUtil
|
public class WMLUtil
|
||||||
{
|
{
|
||||||
|
private static EObjectAtOffsetHelper eObjectAtOffsetHelper_;
|
||||||
|
|
||||||
|
public static EObjectAtOffsetHelper EObjectUtils(){
|
||||||
|
if ( eObjectAtOffsetHelper_ == null ) {
|
||||||
|
eObjectAtOffsetHelper_ = new EObjectAtOffsetHelper( );
|
||||||
|
}
|
||||||
|
|
||||||
|
return eObjectAtOffsetHelper_;
|
||||||
|
}
|
||||||
|
|
||||||
public static String debug(EObject root)
|
public static String debug(EObject root)
|
||||||
{
|
{
|
||||||
ICompositeNode node = NodeModelUtils.getNode(root);
|
ICompositeNode node = NodeModelUtils.getNode(root);
|
||||||
@ -40,9 +52,13 @@ public class WMLUtil
|
|||||||
*/
|
*/
|
||||||
public static IFile getActiveEditorFile()
|
public static IFile getActiveEditorFile()
|
||||||
{
|
{
|
||||||
if (EditorUtils.getActiveXtextEditor() == null)
|
return getEditorFile( EditorUtils.getActiveXtextEditor() );
|
||||||
return null;
|
}
|
||||||
return (IFile)EditorUtils.getActiveXtextEditor()
|
|
||||||
.getEditorInput().getAdapter(IFile.class);
|
public static IFile getEditorFile( XtextEditor editor )
|
||||||
|
{
|
||||||
|
if ( editor == null )
|
||||||
|
return null;
|
||||||
|
return ( IFile ) editor.getEditorInput( ).getAdapter( IFile.class );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,10 @@ package org.wesnoth.ui.labeling.wmldoc;
|
|||||||
import org.eclipse.core.commands.AbstractHandler;
|
import org.eclipse.core.commands.AbstractHandler;
|
||||||
import org.eclipse.core.commands.ExecutionEvent;
|
import org.eclipse.core.commands.ExecutionEvent;
|
||||||
import org.eclipse.core.commands.ExecutionException;
|
import org.eclipse.core.commands.ExecutionException;
|
||||||
|
import org.eclipse.core.resources.IFile;
|
||||||
import org.eclipse.emf.ecore.EObject;
|
import org.eclipse.emf.ecore.EObject;
|
||||||
import org.eclipse.jface.text.ITextSelection;
|
import org.eclipse.jface.text.ITextSelection;
|
||||||
import org.eclipse.swt.graphics.Point;
|
import org.eclipse.swt.graphics.Point;
|
||||||
import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
|
|
||||||
import org.eclipse.xtext.resource.XtextResource;
|
import org.eclipse.xtext.resource.XtextResource;
|
||||||
import org.eclipse.xtext.ui.editor.XtextEditor;
|
import org.eclipse.xtext.ui.editor.XtextEditor;
|
||||||
import org.eclipse.xtext.ui.editor.utils.EditorUtils;
|
import org.eclipse.xtext.ui.editor.utils.EditorUtils;
|
||||||
@ -32,20 +32,13 @@ import org.wesnoth.wml.WMLTag;
|
|||||||
*/
|
*/
|
||||||
public class WMLDocHandler extends AbstractHandler
|
public class WMLDocHandler extends AbstractHandler
|
||||||
{
|
{
|
||||||
private EObjectAtOffsetHelper eObjectAtOffsetHelper;
|
|
||||||
|
|
||||||
public WMLDocHandler()
|
|
||||||
{
|
|
||||||
eObjectAtOffsetHelper = new EObjectAtOffsetHelper( );
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object execute(ExecutionEvent event) throws ExecutionException
|
public Object execute(ExecutionEvent event) throws ExecutionException
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
|
final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
|
||||||
final String installName =
|
final IFile editedFile = WMLUtil.getEditorFile( editor );
|
||||||
WesnothInstallsUtils.getInstallNameForResource( WMLUtil.getActiveEditorFile( ) );
|
final String installName = WesnothInstallsUtils.getInstallNameForResource( editedFile );
|
||||||
|
|
||||||
editor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>()
|
editor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>()
|
||||||
{
|
{
|
||||||
@ -58,7 +51,7 @@ public class WMLDocHandler extends AbstractHandler
|
|||||||
Point positionAbsolute = editor.getInternalSourceViewer().getTextWidget().toDisplay(positionRelative);
|
Point positionAbsolute = editor.getInternalSourceViewer().getTextWidget().toDisplay(positionRelative);
|
||||||
positionAbsolute.y += 20;
|
positionAbsolute.y += 20;
|
||||||
|
|
||||||
EObject grammarElement = eObjectAtOffsetHelper.resolveElementAt( resource, selection.getOffset( ) );
|
EObject grammarElement = WMLUtil.EObjectUtils( ).resolveElementAt( resource, selection.getOffset( ) );
|
||||||
if ( grammarElement == null )
|
if ( grammarElement == null )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -66,15 +59,13 @@ public class WMLDocHandler extends AbstractHandler
|
|||||||
{
|
{
|
||||||
WMLMacroCall macro = (WMLMacroCall) grammarElement;
|
WMLMacroCall macro = (WMLMacroCall) grammarElement;
|
||||||
Define define = ProjectUtils.getCacheForProject(
|
Define define = ProjectUtils.getCacheForProject(
|
||||||
WMLUtil.getActiveEditorFile().getProject())
|
editedFile.getProject()).getDefines().get(macro.getName());
|
||||||
.getDefines().get(macro.getName());
|
|
||||||
if (define != null)
|
if (define != null)
|
||||||
{
|
{
|
||||||
if (presenter_ == null)
|
if (presenter_ == null)
|
||||||
{
|
{
|
||||||
presenter_ = new WMLDocInformationPresenter(
|
presenter_ = new WMLDocInformationPresenter(
|
||||||
editor.getSite().getShell(),
|
editor.getSite().getShell(), new WMLDocMacro(define),
|
||||||
new WMLDocMacro(define),
|
|
||||||
positionAbsolute);
|
positionAbsolute);
|
||||||
presenter_.create();
|
presenter_.create();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user