eclipse plugin: Rework a bit the Hyperlink Helper...

...to conform to the new node model
This commit is contained in:
Timotei Dolean 2011-07-16 20:57:44 +00:00
parent 5b4d5addc7
commit 720a70edfc
2 changed files with 112 additions and 92 deletions

View file

@ -12,7 +12,7 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.text.Region;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.hyperlinking.HyperlinkHelper;
@ -23,120 +23,127 @@ import org.wesnoth.preferences.Preferences;
import org.wesnoth.preferences.Preferences.Paths;
import org.wesnoth.preprocessor.Define;
import org.wesnoth.projects.ProjectUtils;
import org.wesnoth.ui.Messages;
import org.wesnoth.ui.WMLUtil;
import org.wesnoth.wml.WMLMacroCall;
public class WMLHyperlinkHelper extends HyperlinkHelper
{
@Override
public void createHyperlinksByOffset(XtextResource resource, int offset,
IHyperlinkAcceptor acceptor)
{
super.createHyperlinksByOffset(resource, offset, acceptor);
//TODO: test this ( the root node is get correct)
ICompositeNode rootNode = resource.getParseResult( ).getRootNode( );
if (rootNode == null)
return;
@Override
public void createHyperlinksByOffset(XtextResource resource, int offset,
IHyperlinkAcceptor acceptor)
{
super.createHyperlinksByOffset(resource, offset, acceptor);
ILeafNode node = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset);
if (node == null)
return;
EObject object = WMLUtil.EObjectUtils( ).resolveElementAt( resource, offset );
if ( object == null || object instanceof WMLMacroCall == false )
return;
WMLMacroCall macroCall = ( WMLMacroCall ) object;
IFile file = WMLUtil.getActiveEditorFile();
if (file == null)
{
Logger.getInstance().logError(Messages.WMLHyperlinkHelper_0);
if ( file == null ){
Logger.getInstance().logError( "FATAL! file is null (and it shouldn't) ");
return;
}
Paths paths = Preferences.getPaths( WesnothInstallsUtils.getInstallNameForResource( file ) );
createMacroHyperlink( file, paths, node, acceptor, resource );
ILeafNode prevNode = NodeModelUtils.findLeafNodeAtOffset(rootNode,
node.getOffset() - 1);
if(prevNode == null)
return;
ICompositeNode node = NodeModelUtils.getNode( macroCall );
createMapHyperlink( paths, prevNode, node, acceptor, resource );
}
createMapHyperlink( paths, macroCall, acceptor, node );
createMacroHyperlink( paths, file, macroCall, acceptor, node );
}
/**
* Creates a hyperlink for opening the macro definition
* @param prevNode
* @param node
* @param acceptor
* @param resource
*/
private void createMacroHyperlink( IFile file, Paths paths, ILeafNode node,
IHyperlinkAcceptor acceptor, XtextResource resource)
{
if ( node instanceof ICompositeNode == false)
return;
/**
* Creates a hyperlink for opening the macro definition
* @param paths The paths variable for the current install
* @param file The current edited file
* @param macro The macro object
* @param acceptor The hyperlink acceptor
* @param node The node model representation of the macro
*/
private void createMacroHyperlink( Paths paths, IFile file, WMLMacroCall macro,
IHyperlinkAcceptor acceptor, ICompositeNode node )
{
// get the define for the macro
Define define = ProjectUtils.getCacheForProject(
file.getProject() ).getDefines().get( macro.getName() );
if ( define == null ||
define.getLocation( ).length( ) <= 2 )
return;
//TODO: test
ICompositeNode container = (ICompositeNode)node;
EObject grammarElement = container.getGrammarElement( );
if ( grammarElement instanceof WMLMacroCall == false)
return;
String filePath = define.getLocation();
WMLMacroCall macro = (WMLMacroCall) grammarElement;
if ( filePath.charAt( 0 ) == '~' ) {
// expand the '~' character to user data dir
filePath = filePath.replaceFirst( "~", paths.getUserDataDir( ) );
} else if ( filePath.startsWith( "core/" ) ) {
// expand the data/core path
filePath = filePath.replaceFirst( "core/", paths.getCoreDir( ) );
}
// get the define for the macro
Define define = ProjectUtils.getCacheForProject(file.getProject()).getDefines().get(macro.getName());
if (define == null)
{
//TODO: handle macro include call - open folder?
//Logger.getInstance().log("No macro with that name found.");
return;
}
FileLocationOpenerHyperlink macroTarget = new FileLocationOpenerHyperlink();
macroTarget.setHyperlinkRegion( new Region( node.getOffset( ), node.getLength( ) ) );
macroTarget.setFilePath(filePath);
macroTarget.setLinenumber(define.getLineNum());
acceptor.accept(macroTarget);
}
if (define.getLocation().isEmpty() == true)
return;
/**
* Creates a hyperlink for opening the map ( if applying )
* @param paths The paths variable for the current install
* @param macro The macro object
* @param acceptor The hyperlink acceptor
* @param node The node model representation of the macro
*/
private void createMapHyperlink( Paths paths, WMLMacroCall macro,
IHyperlinkAcceptor acceptor, ICompositeNode node )
{
INode previousNode = node.getPreviousSibling( );
if ( previousNode == null ||
! ( "map_data".equals( previousNode.getText( ) ) ) )
return;
String filePath = define.getLocation().split(" ")[0]; //$NON-NLS-1$
String mapLocation = node.getText();
if (filePath.startsWith("~")) { // user addon relative location //$NON-NLS-1$
// too few characters
if ( mapLocation.length( ) <= 2 )
return;
filePath = filePath.replaceFirst("~", //$NON-NLS-1$
paths.getUserDir( ).replace('\\', '/') + "/data/"); //$NON-NLS-1$
}
else if (filePath.startsWith("core")) { // data/core relative location //$NON-NLS-1$
filePath = filePath.replaceFirst("core", //$NON-NLS-1$
paths.getWorkingDir( ).replace('\\', '/') + "/data/core/"); //$NON-NLS-1$
}
// trim the " and the { (if any exist)
int indexStart = 0;
int indexEnd = 0;
FileLocationOpenerHyperlink macroTarget = new FileLocationOpenerHyperlink();
macroTarget.setHyperlinkRegion(new Region(container.getOffset(), container.getLength()));
macroTarget.setFilePath(filePath);
macroTarget.setLinenumber(define.getLineNum());
acceptor.accept(macroTarget);
}
if ( mapLocation.charAt( 0 ) == '"' ) {
indexStart = 1;
/**
* Creates a hyperlink for opening the map.
* @param key The key (must me 'map_data' in this case)
* @param value The value of key, that is, the location of the map
*/
private void createMapHyperlink( Paths paths, ILeafNode key, ILeafNode value,
IHyperlinkAcceptor acceptor, XtextResource resource )
{
if (!(key.getText().equals("map_data"))) //$NON-NLS-1$
return;
if ( mapLocation.charAt( 1 ) == '{' ){
indexStart = 2;
}
}
// trim the " and the { (if any exist)
String mapLocation = value.getText();
if (mapLocation.startsWith("\"")) //$NON-NLS-1$
mapLocation = mapLocation.substring(1, value.getLength() - 1);
if (mapLocation.startsWith("{")) //$NON-NLS-1$
mapLocation = mapLocation.substring(1, mapLocation.length( ) - 1);
if ( mapLocation.charAt( mapLocation.length( ) - 1 ) == '"') {
indexEnd = mapLocation.length( ) - 1;
mapLocation = mapLocation.replaceFirst("~", //$NON-NLS-1$
paths.getUserDir( ).replace('\\','/') + "/data/"); //$NON-NLS-1$
if ( mapLocation.charAt( mapLocation.length( ) - 2 ) == '}' ) {
indexEnd = mapLocation.length( ) - 2;
}
}
MapOpenerHyperlink hyperlink = new MapOpenerHyperlink();
hyperlink.setHyperlinkRegion(new Region(value.getOffset(), value.getLength()));
hyperlink.setLocation(mapLocation);
acceptor.accept(hyperlink);
}
mapLocation = mapLocation.substring( indexStart, indexEnd );
if ( mapLocation.charAt( 0 ) == '~' ) {
// expand the '~' character to user data dir
mapLocation = mapLocation.replaceFirst( "~", paths.getUserDataDir( ) );
} else if ( mapLocation.startsWith( "campaigns/" ) ) {
// expand the campaigns path
mapLocation = mapLocation.replaceFirst( "campaigns/", paths.getCampaignDir( ) );
}
MapOpenerHyperlink hyperlink = new MapOpenerHyperlink();
hyperlink.setHyperlinkRegion( new Region(node.getOffset( ), node.getLength( ) ) );
hyperlink.setLocation(mapLocation);
acceptor.accept(hyperlink);
}
}

View file

@ -171,9 +171,18 @@ public class Preferences extends AbstractPreferenceInitializer
*/
public String getAddonsDir()
{
return getUserDir( ) + "data/add-ons/"; //$NON-NLS-1$
return getUserDataDir( ) + "add-ons/"; //$NON-NLS-1$
}
/**
* Returns the data user directory
* @return Returns the data user directory
*/
public String getUserDataDir()
{
return getUserDir( ) + "data/" ;
}
/**
* Returns the campaign directory
* @return Returns the campaign directory
@ -183,6 +192,10 @@ public class Preferences extends AbstractPreferenceInitializer
return getWorkingDir( ) + "data/campaigns/"; //$NON-NLS-1$
}
/**
* Returns the 'data/core' directory
* @return Returns the 'data/core' directory
*/
public String getCoreDir()
{
return getWorkingDir( ) + "data/core/"; //$NON-NLS-1$