eclipse plugin: Rework the closing end tag
autoedit Strategy
This commit is contained in:
parent
1a9e13b1f3
commit
ae3f8ca4bf
2 changed files with 40 additions and 75 deletions
|
@ -8,99 +8,65 @@
|
|||
*******************************************************************************/
|
||||
package org.wesnoth.ui.autoedit;
|
||||
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.DocumentCommand;
|
||||
import org.eclipse.jface.text.IAutoEditStrategy;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.xtext.nodemodel.BidiIterator;
|
||||
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.parser.IParseResult;
|
||||
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.utils.StringUtils;
|
||||
import org.wesnoth.wml.WMLTag;
|
||||
|
||||
/**
|
||||
* Strategy that tries to close the nearest unclosed tag
|
||||
*/
|
||||
public class ClosingEndTagAutoEditStrategy implements IAutoEditStrategy
|
||||
{
|
||||
public void customizeDocumentCommand(final IDocument document,
|
||||
final DocumentCommand command)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (command.text.equals("/") && document.get(command.offset-1, 1).equals("[")) //$NON-NLS-1$ //$NON-NLS-2$
|
||||
{
|
||||
XtextEditor editor = EditorUtils.getActiveXtextEditor();
|
||||
if (editor == null)
|
||||
return;
|
||||
editor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>(){
|
||||
@Override
|
||||
public void process(XtextResource state) throws Exception
|
||||
{
|
||||
IParseResult parseResult = state.getParseResult();
|
||||
if(parseResult == null)
|
||||
return;
|
||||
ICompositeNode rootNode = parseResult.getRootNode();
|
||||
ILeafNode currentNode = NodeModelUtils.findLeafNodeAtOffset(rootNode, command.offset);
|
||||
public void customizeDocumentCommand(final IDocument document,
|
||||
final DocumentCommand command)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (command.text.equals("/") && document.get(command.offset-1, 1).equals("[")) //$NON-NLS-1$ //$NON-NLS-2$
|
||||
{
|
||||
XtextEditor editor = EditorUtils.getActiveXtextEditor();
|
||||
if (editor == null)
|
||||
return;
|
||||
|
||||
String tagName = ""; //$NON-NLS-1$
|
||||
BidiIterator<INode> itor = currentNode.getParent( ).getChildren( ).iterator( );
|
||||
editor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>(){
|
||||
@Override
|
||||
public void process(XtextResource state) throws Exception
|
||||
{
|
||||
ILeafNode currentNode = NodeModelUtils.findLeafNodeAtOffset(
|
||||
state.getParseResult( ).getRootNode( ), command.offset);
|
||||
|
||||
//TODO test the new closing tag strategy
|
||||
if ( currentNode == null )
|
||||
return;
|
||||
|
||||
INode prevNode = null;
|
||||
while ( itor.hasNext( ) ) {
|
||||
INode node = itor.next( );
|
||||
EObject semanticElement = currentNode.getSemanticElement( );
|
||||
if ( semanticElement == null )
|
||||
return;
|
||||
|
||||
if ( node instanceof ILeafNode == false)
|
||||
continue;
|
||||
String tagName = ""; //$NON-NLS-1$
|
||||
EObject container = semanticElement.eContainer( );
|
||||
if ( container instanceof WMLTag ){
|
||||
tagName = ( ( WMLTag ) container ).getName( );
|
||||
}
|
||||
|
||||
if ( itor.hasNext( ) )
|
||||
|
||||
prevNode = node;
|
||||
}
|
||||
|
||||
// EList<INode> children = currentNode.getParent().getParent().getChildren();
|
||||
// for(int i = 0 ;i < children.size(); ++i)
|
||||
// {
|
||||
// if ((children.get(i) instanceof ILeafNode) == false)
|
||||
// continue;
|
||||
// // we need one more child
|
||||
// if (i+1 >= children.size())
|
||||
// continue;
|
||||
// if (((ILeafNode)children.get(i)).getText().equals("[")) //$NON-NLS-1$
|
||||
// {
|
||||
// if (children.get(i+1) instanceof ILeafNode)
|
||||
// {
|
||||
// // in case we have [+
|
||||
// if (((ILeafNode)children.get(i+1)).getText().equals("+")) //$NON-NLS-1$
|
||||
// {
|
||||
// if (i+2 >= children.size() ||
|
||||
// (children.get(i+2) instanceof ILeafNode) == false)
|
||||
// continue;
|
||||
// tagName = ((ILeafNode)children.get(i+2)).getText();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// tagName = ((ILeafNode)children.get(i+1)).getText();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if (tagName.isEmpty() == false)
|
||||
// {
|
||||
// command.shiftsCaret = true;
|
||||
// command.text = ("/" + tagName + "]"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
// }
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (BadLocationException e) {
|
||||
}
|
||||
}
|
||||
if ( !StringUtils.isNullOrEmpty( tagName ) )
|
||||
{
|
||||
command.shiftsCaret = true;
|
||||
command.text = ( "/" + tagName ); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (BadLocationException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,7 +92,6 @@ public class WMLSemanticHighlightingCalculator implements ISemanticHighlightingC
|
|||
// check if we have any information specific information for highlighting
|
||||
for ( Adapter adapter : current.eAdapters( ) ) {
|
||||
if ( adapter instanceof WMLSyntaxColoringAdapter ) {
|
||||
System.out.println( current );
|
||||
WMLSyntaxColoringAdapter wmlAdapter = ( WMLSyntaxColoringAdapter ) adapter;
|
||||
|
||||
for ( Pair<INode, String> pair : toColor ) {
|
||||
|
|
Loading…
Add table
Reference in a new issue