eclipse plugin: Reorganize the WML Utils class...

...and cleanup the WMLUtil toString code for WML node model
This commit is contained in:
Timotei Dolean 2011-07-16 20:58:56 +00:00
parent 239a0f7d33
commit e46bed8e9d
7 changed files with 60 additions and 130 deletions

View file

@ -1,64 +0,0 @@
/*******************************************************************************
* Copyright (c) 2010 - 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;
import org.eclipse.core.resources.IFile;
import org.eclipse.emf.ecore.EObject;
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.EObjectAtOffsetHelper;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.utils.EditorUtils;
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)
{
ICompositeNode node = NodeModelUtils.getNode(root);
Iterable<INode> contents = node.getAsTreeIterable( );
StringBuffer text = new StringBuffer();
for (INode abstractNode : contents)
{
if (abstractNode instanceof ILeafNode)
{
System.out.println((((ILeafNode) abstractNode).getText()));
text.append(((ILeafNode) abstractNode).getText());
}
}
return text.toString();
}
/**
* Gets current edited file
* @return
*/
public static IFile getActiveEditorFile()
{
return getEditorFile( EditorUtils.getActiveXtextEditor() );
}
public static IFile getEditorFile( XtextEditor editor )
{
if ( editor == null )
return null;
return ( IFile ) editor.getEditorInput( ).getAdapter( IFile.class );
}
}

View file

@ -31,10 +31,10 @@ import org.wesnoth.schema.Tag;
import org.wesnoth.schema.TagKey;
import org.wesnoth.templates.TemplateProvider;
import org.wesnoth.ui.WMLUiModule;
import org.wesnoth.ui.WMLUtil;
import org.wesnoth.ui.editor.WMLEditor;
import org.wesnoth.ui.labeling.WMLLabelProvider;
import org.wesnoth.utils.StringUtils;
import org.wesnoth.utils.WMLGrammarUtils;
import org.wesnoth.utils.WMLUtils;
import org.wesnoth.wml.WMLKey;
import org.wesnoth.wml.WMLTag;
import org.wesnoth.wml.core.WMLConfig;
@ -80,7 +80,7 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider
if ( projectCache_ != null )
return;
IFile file = WMLUtil.getActiveEditorFile();
IFile file = WMLEditor.getActiveEditorFile();
projectCache_ = ProjectUtils.getCacheForProject( file.getProject( ) );
// load the schema so we know what to suggest for autocomplete
@ -252,7 +252,7 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider
if (key.isRepeatable() == false)
{
// don't suggest already completed keys
for( WMLKey eKey: WMLGrammarUtils.getTagKeys( tag ) )
for( WMLKey eKey: WMLUtils.getTagKeys( tag ) )
if (eKey.getName().equals(key.getName())) {
found = true;
break;
@ -305,7 +305,7 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider
// check only non-repeatable tags
if (tag.isRepeatable() == false)
{
for( WMLTag wmlTag : WMLGrammarUtils.getTagTags( parentTag ) )
for( WMLTag wmlTag : WMLUtils.getTagTags( parentTag ) )
if (wmlTag.getName().equals(tag.getName()))
{
found = true;

View file

@ -50,6 +50,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.utils.EditorUtils;
import org.wesnoth.Logger;
import org.wesnoth.WesnothPlugin;
import org.wesnoth.ui.Messages;
@ -105,6 +106,28 @@ public class WMLEditor extends XtextEditor
return u1.toString().equals(u2.toString());
}
/**
* Gets current edited file.
* @return An IFile instance
*/
public static IFile getActiveEditorFile()
{
return getEditorFile( EditorUtils.getActiveXtextEditor() );
}
/**
* Gets the edited file from the specified editor
* @param editor The editor to get the file from
* @return An IFile instance
*/
public static IFile getEditorFile( XtextEditor editor )
{
if ( editor == null )
return null;
return ( IFile ) editor.getEditorInput( ).getAdapter( IFile.class );
}
/**
* Here it comes the part that handles external files
* (from outside the workspace)

View file

@ -23,7 +23,8 @@ import org.wesnoth.Logger;
import org.wesnoth.installs.WesnothInstallsUtils;
import org.wesnoth.preprocessor.Define;
import org.wesnoth.projects.ProjectUtils;
import org.wesnoth.ui.WMLUtil;
import org.wesnoth.ui.editor.WMLEditor;
import org.wesnoth.utils.WMLUtils;
import org.wesnoth.wml.WMLMacroCall;
import org.wesnoth.wml.WMLTag;
@ -37,7 +38,7 @@ public class WMLDocHandler extends AbstractHandler
try
{
final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
final IFile editedFile = WMLUtil.getEditorFile( editor );
final IFile editedFile = WMLEditor.getEditorFile( editor );
final String installName = WesnothInstallsUtils.getInstallNameForResource( editedFile );
editor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>()
@ -51,7 +52,7 @@ public class WMLDocHandler extends AbstractHandler
Point positionAbsolute = editor.getInternalSourceViewer().getTextWidget().toDisplay(positionRelative);
positionAbsolute.y += 20;
EObject grammarElement = WMLUtil.EObjectUtils( ).resolveElementAt( resource, selection.getOffset( ) );
EObject grammarElement = WMLUtils.EObjectUtils( ).resolveElementAt( resource, selection.getOffset( ) );
if ( grammarElement == null )
return;

View file

@ -23,7 +23,8 @@ 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.WMLUtil;
import org.wesnoth.ui.editor.WMLEditor;
import org.wesnoth.utils.WMLUtils;
import org.wesnoth.wml.WMLMacroCall;
public class WMLHyperlinkHelper extends HyperlinkHelper
@ -34,12 +35,12 @@ public class WMLHyperlinkHelper extends HyperlinkHelper
{
super.createHyperlinksByOffset(resource, offset, acceptor);
EObject object = WMLUtil.EObjectUtils( ).resolveElementAt( resource, offset );
EObject object = WMLUtils.EObjectUtils( ).resolveElementAt( resource, offset );
if ( object == null )
return;
IFile file = WMLUtil.getActiveEditorFile();
IFile file = WMLEditor.getActiveEditorFile();
if ( file == null ){
Logger.getInstance().logError( "FATAL! file is null (and it shouldn't) ");
return;

View file

@ -19,7 +19,7 @@ import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.model.XtextDocument;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
import org.wesnoth.ui.WMLSyntaxColoringAdapter;
import org.wesnoth.ui.WMLUtil;
import org.wesnoth.utils.WMLUtils;
import org.wesnoth.wml.WMLTag;
public class WMLCharacterPairMatcher extends DefaultCharacterPairMatcher
@ -63,7 +63,7 @@ public class WMLCharacterPairMatcher extends DefaultCharacterPairMatcher
public IRegion computeMatchingRegion(XtextResource state, int offset)
{
EObject object = WMLUtil.EObjectUtils( ).resolveElementAt( state, offset );
EObject object = WMLUtils.EObjectUtils( ).resolveElementAt( state, offset );
// do nothing if we clicked the same tag
if ( currentTag_ == object )

View file

@ -12,20 +12,26 @@ import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.INode;
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.WMLMacroCall;
import org.wesnoth.wml.WMLMacroCallParameter;
import org.wesnoth.wml.WMLTag;
import org.wesnoth.wml.impl.WMLKeyValueImpl;
public class WMLGrammarUtils
public class WMLUtils
{
private static EObjectAtOffsetHelper eObjectAtOffsetHelper_;
public static EObjectAtOffsetHelper EObjectUtils(){
if ( eObjectAtOffsetHelper_ == null ) {
eObjectAtOffsetHelper_ = new EObjectAtOffsetHelper( );
}
return eObjectAtOffsetHelper_;
}
/**
* Returns the list of child keys for this tag
* @param tag The tag to process
@ -68,63 +74,26 @@ public class WMLGrammarUtils
StringBuilder result = new StringBuilder( );
for ( WMLKeyValue value : values ) {
if ( value.getClass( ).equals( WMLKeyValueImpl.class ) ) {
result.append( toStringWMLKeyValue( value ) );
} else if ( value instanceof WMLMacroCall ) {
result.append( toStringWMLMacroCall( (WMLMacroCall) value ) );
}
result.append( toString( value ) );
}
return result.toString( );
}
/**
* Returns the string representation of the specified WMLKeyValue
* @param keyValue The key value instance
* Returns the string representation of the specified WML object
* with the preceeding space/new lines cleaned
* @param object A WML EObject
* @return A string representation
*/
public static String toStringWMLKeyValue( WMLKeyValue keyValue )
public static String toString( EObject object )
{
ICompositeNode node = NodeModelUtils.getNode( keyValue );
if ( node == null )
return "";
StringBuilder result = new StringBuilder( );
for ( INode tmpNode : node.getChildren( ) ) {
for ( ILeafNode leafNode : tmpNode.getLeafNodes( ) ) {
if ( leafNode.getLength( ) == 0 )
continue;
result.append( leafNode.getText( ) );
}
}
return result.toString( );
return toCleanedUpText( object );
}
/**
* Returns the string representation of the specified WMLMacroCall
* @param macro The macro call instance
* @return A string representation
*/
public static String toStringWMLMacroCall( WMLMacroCall macro )
private static String toCleanedUpText( EObject obj )
{
StringBuilder result = new StringBuilder( );
result.append( "{" );
result.append( macro.getPoint( ) );
result.append( macro.getRelative( ) );
result.append( macro.getName( ) );
for ( WMLMacroCallParameter param : macro.getParameters( ) ) {
if ( param instanceof WMLMacroCall )
result.append( toStringWMLMacroCall( ( WMLMacroCall ) param ) );
else {
//
}
}
result.append( "}" );
return result.toString( );
return NodeModelUtils.getNode( obj ).getText( )
.replaceFirst( "(\\n|\\r| )+", "" );
}
}