mail[Wesnoth-commits] r44146 - in /trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui: ./ syntax/bracketmatching/


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by timotei_cluj on July 13, 2010 - 19:59:
Author: timotei
Date: Tue Jul 13 19:59:00 2010
New Revision: 44146

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44146&view=rev
Log:
eclipse plugin: begin+end tag highlighting - part2 - match corresponding tag

Added:
    trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/WMLEditor.java
    trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/WMLUtil.java
    
trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/syntax/bracketmatching/
    
trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/syntax/bracketmatching/WMLBracketMatching.java

Added: trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/WMLEditor.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/WMLEditor.java?rev=44146&view=auto
==============================================================================
--- trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/WMLEditor.java 
(added)
+++ trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/WMLEditor.java Tue 
Jul 13 19:59:00 2010
@@ -1,0 +1,26 @@
+/**
+ * @author Timotei Dolean
+ *
+ */
+package org.wesnoth.ui;
+
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.xtext.ui.editor.XtextEditor;
+
+public class WMLEditor extends XtextEditor
+{
+       @Override
+       public void createPartControl(Composite parent)
+       {
+               super.createPartControl(parent);
+               //              listener_ = new 
WMLSelectionChangeListener(DefaultUiModule.class.getTypeParameters());
+               //              listener_.install(getSelectionProvider());
+       }
+
+       @Override
+       public void dispose()
+       {
+               //listener_.uninstall(getSelectionProvider());
+               super.dispose();
+       }
+}

Added: trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/WMLUtil.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/WMLUtil.java?rev=44146&view=auto
==============================================================================
--- trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/WMLUtil.java 
(added)
+++ trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/WMLUtil.java Tue 
Jul 13 19:59:00 2010
@@ -1,0 +1,30 @@
+/**
+ * @author Timotei Dolean
+ *
+ */
+package org.wesnoth.ui;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.xtext.parsetree.AbstractNode;
+import org.eclipse.xtext.parsetree.CompositeNode;
+import org.eclipse.xtext.parsetree.LeafNode;
+import org.eclipse.xtext.parsetree.NodeUtil;
+
+public class WMLUtil
+{
+       public static String debug(EObject root)
+       {
+               CompositeNode node = NodeUtil.getNode(root);
+               Iterable<AbstractNode> contents = 
NodeUtil.getAllContents(node);
+               StringBuffer text = new StringBuffer();
+               for (AbstractNode abstractNode : contents)
+               {
+                       if (abstractNode instanceof LeafNode)
+                       {
+                               System.out.println((((LeafNode) 
abstractNode).getText()));
+                               text.append(((LeafNode) 
abstractNode).getText());
+                       }
+               }
+               return text.toString();
+       }
+}

Added: 
trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/syntax/bracketmatching/WMLBracketMatching.java
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/syntax/bracketmatching/WMLBracketMatching.java?rev=44146&view=auto
==============================================================================
--- 
trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/syntax/bracketmatching/WMLBracketMatching.java
 (added)
+++ 
trunk/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/syntax/bracketmatching/WMLBracketMatching.java
 Tue Jul 13 19:59:00 2010
@@ -1,0 +1,132 @@
+/**
+ * @author Timotei Dolean
+ *
+ */
+package org.wesnoth.ui.syntax.bracketmatching;
+
+import org.wesnoth.services.WMLGrammarAccess;
+import org.wesnoth.wML.WMLTag;
+
+import com.google.inject.Inject;
+
+import java.util.List;
+
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.Region;
+import org.eclipse.xtext.AbstractElement;
+import org.eclipse.xtext.Keyword;
+import org.eclipse.xtext.parsetree.AbstractNode;
+import org.eclipse.xtext.parsetree.CompositeNode;
+import org.eclipse.xtext.parsetree.LeafNode;
+import org.eclipse.xtext.parsetree.NodeUtil;
+import org.eclipse.xtext.resource.XtextResource;
+import org.eclipse.xtext.ui.editor.bracketmatching.DefaultBracketMatcher;
+import org.eclipse.xtext.util.Pair;
+
+public class WMLBracketMatching extends DefaultBracketMatcher
+{
+       @Inject
+       private WMLGrammarAccess        grammarAccess;
+
+       @Override
+       public void configure(IBracketPairAcceptor acceptor)
+       {
+               //TODO: fix highlighting multiple characters
+               // look into: MatchingCharacterPainter : private void 
handleDrawRequest(GC gc)  -> length
+               List<Pair<Keyword, Keyword>> pairs2 = 
grammarAccess.findKeywordPairs("[/", "]");
+               for (Pair<Keyword, Keyword> pair : pairs2)
+               {
+                       acceptor.accept(pair.getFirst(), pair.getSecond());
+               }
+               super.configure(acceptor);
+       }
+
+       @Override
+       public IRegion computeMatchingRegion(XtextResource state, int offset)
+       {
+               if (state == null || state.getContents() == null || 
state.getContents().isEmpty())
+                       return null;
+               CompositeNode rootNode = 
NodeUtil.getRootNode(state.getContents().get(0));
+               if (rootNode == null)
+                       return null;
+               AbstractNode node = NodeUtil.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)
+               {
+                       AbstractNode correspondingNode = 
findMatchingNode(node, element, forwardSearch);
+                       if (correspondingNode != null)
+                       {
+                               return new 
Region(correspondingNode.getOffset(), correspondingNode.getLength());
+                       }
+               }
+
+               /* -- WML Related -- */
+               // search for opened/closed tag
+
+               // find opened tag at this offset
+               LeafNode wmlNode = findWMLLeafNodeAtOffset(rootNode, offset, 
false);
+               if (wmlNode == null)
+               {
+                       wmlNode = findWMLLeafNodeAtOffset(rootNode, offset, 
true);
+               }
+               if (wmlNode != null)
+               {
+                       AbstractNode correspondingTag = null;
+                       for (AbstractNode tmpNode : 
wmlNode.getParent().getChildren())
+                       {
+                               if (!(tmpNode instanceof LeafNode))
+                                       continue;
+                               if (((LeafNode) 
tmpNode).getText().equals(wmlNode.getText()) && tmpNode != wmlNode)
+                               {
+                                       correspondingTag = tmpNode;
+                               }
+                       }
+                       if (correspondingTag != null)
+                       {
+                               return new 
Region(correspondingTag.getOffset(), correspondingTag.getLength());
+                       }
+               }
+
+               return null;
+       }
+
+       public LeafNode findWMLLeafNodeAtOffset(CompositeNode 
parseTreeRootNode, int offset, boolean findClosed)
+       {
+               boolean isClosed = false;
+               for (AbstractNode node : parseTreeRootNode.getChildren())
+               {
+                       if (node.getTotalOffset() <= offset)
+                       {
+                               if (node instanceof LeafNode && ((LeafNode) 
node).getText().equals("[/"))
+                               {
+                                       isClosed = true;
+                               }
+                               if (node.getTotalOffset() + 
node.getTotalLength() >= offset)
+                               {
+                                       if (node instanceof LeafNode && 
isWMLTag(node) && (isClosed == findClosed))
+                                               return (LeafNode) node;
+
+                                       else if (node instanceof 
CompositeNode)
+                                               return 
findWMLLeafNodeAtOffset((CompositeNode) node, offset, findClosed);
+                               }
+                       }
+               }
+               return null;
+       }
+
+       private boolean isWMLTag(AbstractNode node)
+       {
+               return (node.eContainer() != null &&
+                               node.eContainer() instanceof CompositeNode && 
((CompositeNode) node.eContainer()).getElement() instanceof WMLTag);
+       }
+}




Related Messages


Powered by MHonArc, Updated Tue Jul 13 20:40:12 2010