eclipse plugin: Rework the entire Highlight...
...start/end tag strategy. It now uses a non-hacky method of achieving it.
This commit is contained in:
parent
9698d5bf39
commit
1a9e13b1f3
11 changed files with 196 additions and 403 deletions
|
@ -0,0 +1,35 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 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.emf.common.notify.impl.AdapterImpl;
|
||||
import org.wesnoth.ui.syntax.WMLHighlightingConfiguration;
|
||||
|
||||
/**
|
||||
* A simple WML adapter that holds a coloring id
|
||||
*/
|
||||
public class WMLSyntaxColoringAdapter extends AdapterImpl
|
||||
{
|
||||
/**
|
||||
* A color id from the {@link WMLHighlightingConfiguration}
|
||||
*/
|
||||
public String ColorId;
|
||||
|
||||
/**
|
||||
* True whether this coloring applies to the start
|
||||
* of the tag or to the end tag
|
||||
*/
|
||||
public boolean PaintStart;
|
||||
|
||||
public WMLSyntaxColoringAdapter( String id, boolean paintStart)
|
||||
{
|
||||
ColorId = id;
|
||||
PaintStart = paintStart;
|
||||
}
|
||||
}
|
|
@ -22,9 +22,7 @@ import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalComparator;
|
|||
import org.eclipse.xtext.ui.editor.folding.DefaultFoldingRegionProvider;
|
||||
import org.eclipse.xtext.ui.editor.hyperlinking.HyperlinkHelper;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.AbstractAntlrTokenToAttributeIdMapper;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingReconciler;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfiguration;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingHelper;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;
|
||||
import org.wesnoth.ui.autoedit.WMLAutoEditStrategy;
|
||||
import org.wesnoth.ui.contentassist.WMLContentAssistContext;
|
||||
|
@ -38,8 +36,6 @@ import org.wesnoth.ui.resource.WMLLocationInFileProvider;
|
|||
import org.wesnoth.ui.syntax.WMLAntlrTokenToAttributeIdMapper;
|
||||
import org.wesnoth.ui.syntax.WMLCharacterPairMatcher;
|
||||
import org.wesnoth.ui.syntax.WMLHighlightingConfiguration;
|
||||
import org.wesnoth.ui.syntax.WMLHighlightingHelper;
|
||||
import org.wesnoth.ui.syntax.WMLHighlightingReconciler;
|
||||
import org.wesnoth.ui.syntax.WMLSemanticHighlightingCalculator;
|
||||
|
||||
import com.google.inject.Binder;
|
||||
|
@ -89,16 +85,6 @@ public class WMLUiModule extends org.wesnoth.ui.AbstractWMLUiModule
|
|||
return WMLEditor.class;
|
||||
}
|
||||
|
||||
public Class<? extends IHighlightingHelper> bindIHighlightingHelper()
|
||||
{
|
||||
return WMLHighlightingHelper.class;
|
||||
}
|
||||
|
||||
public Class<? extends HighlightingReconciler> bindHighlightingReconciler()
|
||||
{
|
||||
return WMLHighlightingReconciler.class;
|
||||
}
|
||||
|
||||
public Class<? extends ILocationInFileProvider> bindILocationInFileProvider()
|
||||
{
|
||||
return WMLLocationInFileProvider.class;
|
||||
|
|
|
@ -49,14 +49,11 @@ import org.eclipse.ui.PlatformUI;
|
|||
import org.eclipse.ui.ide.FileStoreEditorInput;
|
||||
import org.eclipse.ui.part.FileEditorInput;
|
||||
import org.eclipse.ui.texteditor.IDocumentProvider;
|
||||
import org.eclipse.xtext.nodemodel.ILeafNode;
|
||||
import org.eclipse.xtext.ui.editor.XtextEditor;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingHelper;
|
||||
import org.wesnoth.Logger;
|
||||
import org.wesnoth.WesnothPlugin;
|
||||
import org.wesnoth.ui.Messages;
|
||||
import org.wesnoth.ui.WMLUiModule;
|
||||
import org.wesnoth.ui.syntax.WMLHighlightingHelper;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
|
@ -65,9 +62,6 @@ public class WMLEditor extends XtextEditor
|
|||
protected static final String AUTOLINK_PROJECT_NAME = "_AutoLinked_CFGExternalFiles_"; //$NON-NLS-1$
|
||||
protected static final String ENCODING_UTF8 = "utf-8"; //$NON-NLS-1$
|
||||
|
||||
protected IHighlightingHelper highlightingHelper_;
|
||||
protected ILeafNode currentHighlightedNode_;
|
||||
|
||||
@Inject
|
||||
private IExtXtextEditorCustomizer editorCustomizer;
|
||||
|
||||
|
@ -96,26 +90,6 @@ public class WMLEditor extends XtextEditor
|
|||
return getSourceViewer();
|
||||
}
|
||||
|
||||
public WMLHighlightingHelper getHighlightingHelper()
|
||||
{
|
||||
return (WMLHighlightingHelper) highlightingHelper_;
|
||||
}
|
||||
|
||||
public void setHighlightHelper(IHighlightingHelper helper)
|
||||
{
|
||||
highlightingHelper_ = helper;
|
||||
}
|
||||
|
||||
public ILeafNode getCurrentHighlightedNode()
|
||||
{
|
||||
return currentHighlightedNode_;
|
||||
}
|
||||
|
||||
public void setCurrentHighlightedNode(ILeafNode leaf)
|
||||
{
|
||||
currentHighlightedNode_ = leaf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
|
|
|
@ -9,7 +9,7 @@ WMLEditor_2=Problems During Save As...
|
|||
WMLEditor_3=Save could not be completed.
|
||||
WMLEditor_4=Problems During Save As...
|
||||
WMLEditor_5=Save could not be completed.
|
||||
WMLHighlightingConfiguration_10=WML Start/End Tag Highlight
|
||||
WMLHighlightingConfiguration_10=WML Matching Tag Highlight
|
||||
WMLHighlightingConfiguration_11=WML Macro Define
|
||||
WMLHighlightingConfiguration_12=WML IF
|
||||
WMLHighlightingConfiguration_13=WML Textdomain
|
||||
|
|
|
@ -8,45 +8,54 @@
|
|||
*******************************************************************************/
|
||||
package org.wesnoth.ui.syntax;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.eclipse.emf.common.notify.Adapter;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.jface.text.IRegion;
|
||||
import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
|
||||
import org.eclipse.xtext.nodemodel.ICompositeNode;
|
||||
import org.eclipse.xtext.nodemodel.ILeafNode;
|
||||
import org.eclipse.xtext.nodemodel.INode;
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
||||
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
|
||||
import org.wesnoth.services.WMLGrammarAccess;
|
||||
import org.wesnoth.ui.WMLSyntaxColoringAdapter;
|
||||
import org.wesnoth.ui.WMLUtil;
|
||||
import org.wesnoth.wml.WMLTag;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class WMLCharacterPairMatcher extends DefaultCharacterPairMatcher
|
||||
{
|
||||
@Inject
|
||||
private WMLGrammarAccess grammarAccess;
|
||||
private WMLSyntaxColoringAdapter currentAdapter_;
|
||||
private WMLTag currentTag_;
|
||||
|
||||
private int matchCnt = 0;
|
||||
|
||||
public WMLCharacterPairMatcher( char[] chars )
|
||||
{
|
||||
super( chars );
|
||||
currentAdapter_ = null;
|
||||
currentTag_ = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IRegion match( IDocument doc, final int offset )
|
||||
{
|
||||
++ matchCnt;
|
||||
IRegion region = super.match( doc, offset );
|
||||
|
||||
if ( region == null && doc instanceof XtextDocument ) {
|
||||
( ( XtextDocument ) doc ).modify( new IUnitOfWork<IRegion, XtextResource>(){
|
||||
if ( matchCnt == 2 ) {
|
||||
matchCnt = 0;
|
||||
} else {
|
||||
( ( XtextDocument ) doc ).modify( new IUnitOfWork<IRegion, XtextResource>(){
|
||||
|
||||
@Override
|
||||
public IRegion exec( XtextResource state ) throws Exception
|
||||
{
|
||||
return computeMatchingRegion( state, offset );
|
||||
}
|
||||
@Override
|
||||
public IRegion exec( XtextResource state ) throws Exception
|
||||
{
|
||||
return computeMatchingRegion( state, offset );
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return region;
|
||||
|
@ -54,129 +63,36 @@ public class WMLCharacterPairMatcher extends DefaultCharacterPairMatcher
|
|||
|
||||
public IRegion computeMatchingRegion(XtextResource state, int offset)
|
||||
{
|
||||
//TODO: rework this.
|
||||
if ( true )
|
||||
EObject object = WMLUtil.EObjectUtils( ).resolveElementAt( state, offset );
|
||||
|
||||
// do nothing if we clicked the same tag
|
||||
if ( currentTag_ == object )
|
||||
return null;
|
||||
// if (state == null || state.getContents() == null || state.getContents().isEmpty())
|
||||
// return null;
|
||||
// ICompositeNode rootNode = state.getParseResult( ).getRootNode( );
|
||||
// if (rootNode == null)
|
||||
// return null;
|
||||
// ILeafNode node = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset);
|
||||
// if (node == null)
|
||||
// return null;
|
||||
//
|
||||
// /* -- AbstractBracketMatcher.class -- */
|
||||
// AbstractElement element = findElement(node, getPairs());
|
||||
// boolean forwardSearch = true;
|
||||
// if (element == null)
|
||||
// {
|
||||
// forwardSearch = false;
|
||||
// element = findElement(node, getPairs().inverse());
|
||||
// }
|
||||
// if (element != null)
|
||||
// {
|
||||
// INode correspondingNode = findMatchingNode(node, element, forwardSearch);
|
||||
// if (correspondingNode != null)
|
||||
// {
|
||||
// return new Region(correspondingNode.getOffset(), correspondingNode.getLength());
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// WMLEditor editor = (WMLEditor) EditorUtils.getActiveXtextEditor();
|
||||
// if (editor == null || editor.getHighlightingHelper() == null)
|
||||
// return null;
|
||||
// HighlightingReconciler reconcilier = editor.getHighlightingHelper().getReconciler();
|
||||
// if (reconcilier == null)
|
||||
// return null;
|
||||
// editor.setCurrentHighlightedNode(null);
|
||||
// // clear any highlighted nodes
|
||||
// reconcilier.refresh();
|
||||
// /* -- WML Related -- */
|
||||
// // search for opened/closed tag
|
||||
//
|
||||
// // find opened tag at this offset
|
||||
// ILeafNode wmlNode = findWMLLeafNodeAtOffset(rootNode, offset, false);
|
||||
// if (wmlNode == null)
|
||||
// {
|
||||
// wmlNode = findWMLLeafNodeAtOffset(rootNode, offset, true);
|
||||
// }
|
||||
// if (wmlNode != null)
|
||||
// {
|
||||
// ILeafNode tmp = null;
|
||||
// ILeafNode correspondingTag = null;
|
||||
// boolean correspondingIsClosed = false;
|
||||
// for (int i = 0; i < wmlNode.getParent().getChildren().size(); i++)
|
||||
// {
|
||||
// if (!(wmlNode.getParent().getChildren().get(i) instanceof ILeafNode))
|
||||
// continue;
|
||||
//
|
||||
// if (i > 0 && wmlNode.getParent().getChildren().get(i-1) instanceof ILeafNode)
|
||||
// tmp = (ILeafNode)wmlNode.getParent().getChildren().get(i-1);
|
||||
//
|
||||
// ILeafNode tmpNode = (ILeafNode)wmlNode.getParent().getChildren().get(i);
|
||||
//
|
||||
// if ((tmpNode).getText().equals(wmlNode.getText()) &&
|
||||
// tmpNode != wmlNode && !tmpNode.isHidden())
|
||||
// {
|
||||
// correspondingTag = tmpNode;
|
||||
// if (tmp != null && tmp.getText().equals("[/")) //$NON-NLS-1$
|
||||
// correspondingIsClosed = true;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (correspondingTag != null)
|
||||
// {
|
||||
// int tagOffset = correspondingTag.getTotalOffset();
|
||||
// int length = correspondingTag.getLength();
|
||||
// tagOffset--; // we need to color the '['
|
||||
// length += 2;
|
||||
//
|
||||
// // we need to color the auxilliary '/'
|
||||
// if (correspondingIsClosed)
|
||||
// {
|
||||
// tagOffset--;
|
||||
// length++;
|
||||
// }
|
||||
//
|
||||
// editor.setCurrentHighlightedNode(correspondingTag);
|
||||
// reconcilier.addPosition(tagOffset, length,
|
||||
// WMLHighlightingConfiguration.RULE_START_END_TAG);
|
||||
// reconcilier.refresh();
|
||||
// }
|
||||
// }
|
||||
|
||||
return null;
|
||||
}
|
||||
// remove current colored tag ( if any )
|
||||
if ( currentTag_ != null ) {
|
||||
synchronized ( currentTag_ ) {
|
||||
|
||||
public ILeafNode findWMLLeafNodeAtOffset(ICompositeNode parseTreeRootNode, int offset, boolean findClosed)
|
||||
{
|
||||
boolean isClosed = false;
|
||||
for (INode node : parseTreeRootNode.getChildren())
|
||||
{
|
||||
if (node.getTotalOffset() <= offset)
|
||||
{
|
||||
if (node instanceof ILeafNode && ((ILeafNode) node).getText().equals("[/")) //$NON-NLS-1$
|
||||
{
|
||||
isClosed = true;
|
||||
Iterator<Adapter> itor = currentTag_.eAdapters( ).iterator( );
|
||||
while ( itor.hasNext( ) ) {
|
||||
if ( itor.next( ) instanceof WMLSyntaxColoringAdapter ) {
|
||||
itor.remove( );
|
||||
}
|
||||
}
|
||||
if (node.getTotalOffset() + node.getTotalLength() >= offset)
|
||||
{
|
||||
if (node instanceof ILeafNode && isWMLTag(node) && (isClosed == findClosed))
|
||||
return (ILeafNode) node;
|
||||
|
||||
else if (node instanceof ICompositeNode)
|
||||
return findWMLLeafNodeAtOffset((ICompositeNode) node, offset, findClosed);
|
||||
}
|
||||
currentAdapter_ = null;
|
||||
currentTag_ = null;
|
||||
}
|
||||
}
|
||||
|
||||
if ( object instanceof WMLTag ) {
|
||||
WMLTag tag = ( WMLTag ) object;
|
||||
|
||||
currentAdapter_ = new WMLSyntaxColoringAdapter( WMLHighlightingConfiguration.RULE_MATCH_TAG, true );
|
||||
currentTag_ = tag;
|
||||
tag.eAdapters( ).add( currentAdapter_ );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isWMLTag(INode node)
|
||||
{
|
||||
return ( node != null &&
|
||||
node.getGrammarElement( ) instanceof WMLTag );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ public class WMLHighlightingConfiguration extends DefaultHighlightingConfigurati
|
|||
public static final String RULE_WML_IF = "wmlIF"; //$NON-NLS-1$
|
||||
public static final String RULE_WML_TEXTDOMAIN = "wmlTextdomain"; //$NON-NLS-1$
|
||||
|
||||
public static final String RULE_START_END_TAG = "wmlStartEnd"; //$NON-NLS-1$
|
||||
public static final String RULE_MATCH_TAG = "wmlMatchTag"; //$NON-NLS-1$
|
||||
|
||||
@Override
|
||||
public void configure(IHighlightingConfigurationAcceptor acceptor)
|
||||
|
@ -35,7 +35,7 @@ public class WMLHighlightingConfiguration extends DefaultHighlightingConfigurati
|
|||
acceptor.acceptDefaultHighlighting(RULE_WML_KEY, Messages.WMLHighlightingConfiguration_8, keyTextStyle());
|
||||
acceptor.acceptDefaultHighlighting(RULE_WML_MACRO_CALL, Messages.WMLHighlightingConfiguration_9, macroTextStyle());
|
||||
|
||||
acceptor.acceptDefaultHighlighting(RULE_START_END_TAG,
|
||||
acceptor.acceptDefaultHighlighting(RULE_MATCH_TAG,
|
||||
Messages.WMLHighlightingConfiguration_10, startEndTextStyle());
|
||||
|
||||
// preproc
|
||||
|
|
|
@ -1,30 +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.syntax;
|
||||
|
||||
import org.eclipse.xtext.ui.editor.XtextEditor;
|
||||
import org.eclipse.xtext.ui.editor.XtextSourceViewer;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingHelper;
|
||||
import org.wesnoth.ui.editor.WMLEditor;
|
||||
|
||||
/**
|
||||
* Highlighting helper.
|
||||
* Initially copied from org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlightingManager
|
||||
*
|
||||
* @author Sebastian Zarnekow
|
||||
*/
|
||||
public class WMLHighlightingHelper extends HighlightingHelper
|
||||
{
|
||||
@Override
|
||||
public void install(XtextEditor editor, XtextSourceViewer sourceViewer)
|
||||
{
|
||||
super.install(editor, sourceViewer);
|
||||
((WMLEditor) editor).setHighlightHelper(this);
|
||||
}
|
||||
}
|
|
@ -1,43 +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.syntax;
|
||||
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.ui.editor.XtextEditor;
|
||||
import org.eclipse.xtext.ui.editor.XtextSourceViewer;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingPresenter;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingReconciler;
|
||||
import org.wesnoth.ui.editor.WMLEditor;
|
||||
|
||||
public class WMLHighlightingReconciler extends HighlightingReconciler
|
||||
{
|
||||
protected WMLEditor editor;
|
||||
|
||||
@Override
|
||||
public void install(XtextEditor editor, XtextSourceViewer sourceViewer, HighlightingPresenter presenter)
|
||||
{
|
||||
super.install(editor, sourceViewer, presenter);
|
||||
this.editor = (WMLEditor) editor;
|
||||
}
|
||||
|
||||
public WMLEditor getEditor()
|
||||
{
|
||||
return editor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modelChanged(XtextResource resource)
|
||||
{
|
||||
super.modelChanged(resource);
|
||||
// add our own acceptor
|
||||
WMLMergingHighlightedPositionAcceptor acceptor =
|
||||
new WMLMergingHighlightedPositionAcceptor(getCalculator());
|
||||
acceptor.provideHighlightingFor(resource, this);
|
||||
}
|
||||
}
|
|
@ -1,39 +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.syntax;
|
||||
|
||||
import org.eclipse.xtext.resource.XtextResource;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.HighlightingReconciler;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightedPositionAcceptor;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.MergingHighlightedPositionAcceptor;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class WMLMergingHighlightedPositionAcceptor extends MergingHighlightedPositionAcceptor
|
||||
{
|
||||
protected HighlightingReconciler reconciler_;
|
||||
|
||||
@Inject
|
||||
public WMLMergingHighlightedPositionAcceptor(ISemanticHighlightingCalculator delegate) {
|
||||
super(delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor)
|
||||
{
|
||||
reconciler_ = (HighlightingReconciler) acceptor;
|
||||
super.provideHighlightingFor(resource, acceptor);
|
||||
}
|
||||
|
||||
public HighlightingReconciler getHighlightingReconciler()
|
||||
{
|
||||
return reconciler_;
|
||||
}
|
||||
}
|
|
@ -8,20 +8,22 @@
|
|||
*******************************************************************************/
|
||||
package org.wesnoth.ui.syntax;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.emf.common.notify.Adapter;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EStructuralFeature;
|
||||
import org.eclipse.emf.ecore.util.EcoreUtil;
|
||||
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.syntaxcoloring.IHighlightedPositionAcceptor;
|
||||
import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;
|
||||
import org.wesnoth.ui.editor.WMLEditor;
|
||||
import org.wesnoth.ui.WMLSyntaxColoringAdapter;
|
||||
import org.wesnoth.utils.Pair;
|
||||
import org.wesnoth.wml.WMLKey;
|
||||
import org.wesnoth.wml.WMLMacroCall;
|
||||
import org.wesnoth.wml.WMLMacroDefine;
|
||||
|
@ -32,131 +34,113 @@ import org.wesnoth.wml.WmlPackage;
|
|||
|
||||
public class WMLSemanticHighlightingCalculator implements ISemanticHighlightingCalculator
|
||||
{
|
||||
@Override
|
||||
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor)
|
||||
{
|
||||
if (resource == null)
|
||||
return;
|
||||
@Override
|
||||
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor)
|
||||
{
|
||||
if (resource == null)
|
||||
return;
|
||||
|
||||
// we skip xtext's default acceptor since we want only ours
|
||||
if (!(acceptor instanceof WMLMergingHighlightedPositionAcceptor))
|
||||
return;
|
||||
Iterator<EObject> iter = EcoreUtil.getAllContents(resource, true);
|
||||
while (iter.hasNext()) {
|
||||
EObject current = iter.next();
|
||||
|
||||
Iterator<EObject> iter = EcoreUtil.getAllContents(resource, true);
|
||||
while (iter.hasNext())
|
||||
{
|
||||
EObject current = iter.next();
|
||||
if (skipNode(acceptor, current))
|
||||
continue;
|
||||
List< Pair< INode, String > > toColor = new ArrayList< Pair< INode,String > >();
|
||||
|
||||
INode begin=null, end=null;
|
||||
String beginId = null, endId = null;
|
||||
if (current instanceof WMLTag)
|
||||
{
|
||||
begin = getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME );
|
||||
beginId = WMLHighlightingConfiguration.RULE_WML_TAG;
|
||||
if (current instanceof WMLTag) {
|
||||
toColor.add( Pair.create(
|
||||
getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME ),
|
||||
WMLHighlightingConfiguration.RULE_WML_TAG ) );
|
||||
|
||||
end = getFirstFeatureNode( current, WmlPackage.Literals.WML_TAG__END_NAME );
|
||||
endId = WMLHighlightingConfiguration.RULE_WML_TAG;
|
||||
}
|
||||
else if (current instanceof WMLKey)
|
||||
{
|
||||
begin = getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME );
|
||||
beginId = WMLHighlightingConfiguration.RULE_WML_KEY;
|
||||
}
|
||||
else if (current instanceof WMLMacroCall)
|
||||
{
|
||||
begin = getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME );
|
||||
beginId = WMLHighlightingConfiguration.RULE_WML_MACRO_CALL;
|
||||
}
|
||||
else if (current instanceof WMLTextdomain)
|
||||
{
|
||||
begin = getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME );
|
||||
beginId = WMLHighlightingConfiguration.RULE_WML_TEXTDOMAIN;
|
||||
}
|
||||
else if (current instanceof WMLPreprocIF)
|
||||
{
|
||||
begin = getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME );
|
||||
beginId = WMLHighlightingConfiguration.RULE_WML_IF;
|
||||
toColor.add( Pair.create(
|
||||
getFirstFeatureNode( current, WmlPackage.Literals.WML_TAG__END_NAME ),
|
||||
WMLHighlightingConfiguration.RULE_WML_TAG ) );
|
||||
}
|
||||
else if (current instanceof WMLKey) {
|
||||
toColor.add( Pair.create(
|
||||
getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME ),
|
||||
WMLHighlightingConfiguration.RULE_WML_KEY ) );
|
||||
}
|
||||
else if (current instanceof WMLMacroCall) {
|
||||
toColor.add( Pair.create(
|
||||
getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME ),
|
||||
WMLHighlightingConfiguration.RULE_WML_MACRO_CALL ) );
|
||||
}
|
||||
else if (current instanceof WMLTextdomain) {
|
||||
toColor.add( Pair.create(
|
||||
getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME ),
|
||||
WMLHighlightingConfiguration.RULE_WML_TEXTDOMAIN ) );
|
||||
}
|
||||
else if (current instanceof WMLPreprocIF) {
|
||||
toColor.add( Pair.create(
|
||||
getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME ),
|
||||
WMLHighlightingConfiguration.RULE_WML_IF ) );
|
||||
|
||||
end = getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME );
|
||||
endId = WMLHighlightingConfiguration.RULE_WML_IF;
|
||||
}
|
||||
else if (current instanceof WMLMacroDefine)
|
||||
{
|
||||
begin = getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME );
|
||||
beginId = WMLHighlightingConfiguration.RULE_WML_MACRO_DEFINE;
|
||||
toColor.add( Pair.create(
|
||||
getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME ),
|
||||
WMLHighlightingConfiguration.RULE_WML_IF ) );
|
||||
}
|
||||
else if (current instanceof WMLMacroDefine) {
|
||||
toColor.add( Pair.create(
|
||||
getFirstFeatureNode( current, WmlPackage.Literals.WML_EXPRESSION__NAME ),
|
||||
WMLHighlightingConfiguration.RULE_WML_MACRO_DEFINE ) );
|
||||
|
||||
end = getFirstFeatureNode( current, WmlPackage.Literals.WML_MACRO_DEFINE__END_NAME );
|
||||
endId = WMLHighlightingConfiguration.RULE_WML_MACRO_DEFINE;
|
||||
}
|
||||
toColor.add( Pair.create(
|
||||
getFirstFeatureNode( current, WmlPackage.Literals.WML_MACRO_DEFINE__END_NAME ),
|
||||
WMLHighlightingConfiguration.RULE_WML_MACRO_DEFINE ) );
|
||||
}
|
||||
|
||||
highlightNode(begin, beginId, acceptor);
|
||||
highlightNode(end, endId, acceptor);
|
||||
}
|
||||
}
|
||||
// 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;
|
||||
|
||||
/**
|
||||
* We have this auxilliar function to know when to skip a node from being highlighted
|
||||
* This is usually case of "highlighting start/end tags" and we don't want
|
||||
* highlighting going over.
|
||||
*
|
||||
* @param acceptor
|
||||
* @param node
|
||||
* @return
|
||||
*/
|
||||
private boolean skipNode(IHighlightedPositionAcceptor acceptor, EObject node)
|
||||
{
|
||||
if (!(node instanceof WMLTag))
|
||||
return false;
|
||||
for ( Pair<INode, String> pair : toColor ) {
|
||||
pair.Second = wmlAdapter.ColorId;
|
||||
}
|
||||
|
||||
WMLTag tag = (WMLTag) node;
|
||||
WMLEditor editor = ((WMLHighlightingReconciler)
|
||||
((WMLMergingHighlightedPositionAcceptor) acceptor).
|
||||
getHighlightingReconciler()).getEditor();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (editor == null || editor.getCurrentHighlightedNode() == null)
|
||||
return false;
|
||||
return (tag.getName().equals(editor.getCurrentHighlightedNode().getText()));
|
||||
}
|
||||
for ( Pair<INode, String> pair : toColor ) {
|
||||
highlightNode( pair.First, pair.Second, acceptor );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void highlightNode(INode node, String id, IHighlightedPositionAcceptor acceptor)
|
||||
{
|
||||
if (node == null || id == null)
|
||||
return;
|
||||
/**
|
||||
* Copied from org.eclipse.xtext.xtext.ui.editor.syntaxcoloring.SemanticHighlightingCalculator
|
||||
* @param node The node to highlight
|
||||
* @param id The id of the coloring
|
||||
* @param acceptor The acceptor to add the node to
|
||||
*/
|
||||
private void highlightNode(INode node, String id, IHighlightedPositionAcceptor acceptor) {
|
||||
if (node == null)
|
||||
return;
|
||||
if (node instanceof ILeafNode) {
|
||||
acceptor.addPosition(node.getOffset(), node.getLength(), id);
|
||||
} else {
|
||||
for(ILeafNode leaf: node.getLeafNodes()) {
|
||||
if (!leaf.isHidden()) {
|
||||
acceptor.addPosition(leaf.getOffset(), leaf.getLength(), id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (node instanceof ILeafNode)
|
||||
{
|
||||
acceptor.addPosition(node.getOffset(), node.getLength(), id);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (ILeafNode leaf : node.getLeafNodes())
|
||||
{
|
||||
if (!leaf.isHidden())
|
||||
{
|
||||
acceptor.addPosition(leaf.getOffset(), leaf.getLength(), id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public INode getFirstFeatureNode(EObject semantic, EStructuralFeature feature)
|
||||
{
|
||||
ICompositeNode node = NodeModelUtils.findActualNodeFor( semantic );
|
||||
if (node == null)
|
||||
return null;
|
||||
|
||||
for (INode child : node.getChildren())
|
||||
{
|
||||
if ( child instanceof ILeafNode == false)
|
||||
continue;
|
||||
|
||||
List<INode> features = NodeModelUtils.findNodesForFeature(
|
||||
child.getGrammarElement( ), feature );
|
||||
if ( !features.isEmpty( ) )
|
||||
return features.get( 0 );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Copied from org.eclipse.xtext.xtext.ui.editor.syntaxcoloring.SemanticHighlightingCalculator
|
||||
* @param semantic The object to search in
|
||||
* @param feature The feature to search for
|
||||
* @return Null if there is no feature, or an {@link INode} node
|
||||
*/
|
||||
public INode getFirstFeatureNode(EObject semantic, EStructuralFeature feature) {
|
||||
if (feature == null)
|
||||
return NodeModelUtils.findActualNodeFor(semantic);
|
||||
List<INode> nodes = NodeModelUtils.findNodesForFeature(semantic, feature);
|
||||
if (!nodes.isEmpty())
|
||||
return nodes.get(0);
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -17,4 +17,14 @@ public class Pair<T, K>
|
|||
First = first;
|
||||
Second = second;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Pair
|
||||
* @param first The first item
|
||||
* @param second The second item
|
||||
* @return A new pair
|
||||
*/
|
||||
public static <U,V> Pair<U,V> create( U first, V second) {
|
||||
return new Pair<U, V> ( first, second );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue