From d6a9b8779720c8aeaaf85176b217e8eb34d71730 Mon Sep 17 00:00:00 2001 From: Timotei Dolean Date: Mon, 2 Aug 2010 19:04:17 +0000 Subject: [PATCH] eclipse plugin:add completion for WMLTag rule --- .../ui/contentassist/WMLProposalProvider.java | 71 ++++++++++++------- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java b/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java index e7052dce2c0..91cdd8f9124 100644 --- a/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java +++ b/utils/java/org.wesnoth.wml.ui/src/org/wesnoth/ui/contentassist/WMLProposalProvider.java @@ -11,6 +11,7 @@ package org.wesnoth.ui.contentassist; import org.eclipse.emf.ecore.EObject; import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.xtext.Assignment; +import org.eclipse.xtext.RuleCall; import org.eclipse.xtext.parsetree.AbstractNode; import org.eclipse.xtext.parsetree.LeafNode; import org.eclipse.xtext.parsetree.NodeUtil; @@ -31,61 +32,77 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider SchemaParser.getInstance().parseSchema(false); } + @Override + public void complete_WMLTag(EObject model, RuleCall ruleCall, + ContentAssistContext context, ICompletionProposalAcceptor acceptor) + { + super.complete_WMLTag(model, ruleCall, context, acceptor); + addTagProposals(model, true, context, acceptor); + } + @Override public void completeWMLTag_Name(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { super.completeWMLTag_Name(model, assignment, context, acceptor); dbg("completing wmltagname"); + addTagProposals(model, false, context, acceptor); + } + + private void addTagProposals(EObject model, boolean firstBracket, + ContentAssistContext context, ICompletionProposalAcceptor acceptor) + { if (context.getCurrentNode().eContainer() != null && - context.getCurrentNode().eContainer() instanceof AbstractNode) + context.getCurrentNode().eContainer() instanceof AbstractNode && + ((AbstractNode)context.getCurrentNode().eContainer()).getParent() != null && + ((AbstractNode)context.getCurrentNode().eContainer()).getParent().eContainer() != null) { AbstractNode node = (AbstractNode)context.getCurrentNode().eContainer(); - if (node.getParent().eContainer() != null) // we are not at the root + LeafNode parent = (LeafNode)NodeUtil.findLeafNodeAtOffset(node.getParent(), + node.getParent().getOffset() + 2); + String parentIndent = ((LeafNode)NodeUtil.findLeafNodeAtOffset(node.getParent(), + node.getOffset())).getText(); + + // remove ugly new lines that break indentation + parentIndent = parentIndent.replace("\r", "").replace("\n", ""); + + Tag tagChildren = SchemaParser.getInstance().getTags().get(parent.getText()); + if (tagChildren != null) { - LeafNode parent = (LeafNode)NodeUtil.findLeafNodeAtOffset(node.getParent(), - node.getParent().getOffset() + 2); - String parentIndent = ((LeafNode)NodeUtil.findLeafNodeAtOffset(node.getParent(), - node.getOffset())).getText(); -// dbg("parent text: " + parent.getText()); - parentIndent = parentIndent.replace("\r", "").replace("\n", ""); - Tag tagChildren = SchemaParser.getInstance().getTags().get(parent.getText()); - if (tagChildren != null) + for(Tag tag : tagChildren.getTagChildren()) { - for(Tag tag : tagChildren.getTagChildren()) - { - acceptor.accept(tagProposal(tag, parentIndent, context)); - } + acceptor.accept(tagProposal(tag, parentIndent, false, context)); } - else - dbg("!!! no tag found with that name:" + parent.getText()); } - else // we are at the root + else + dbg("!!! no tag found with that name:" + parent.getText()); + } + else // we are at the root + { + Tag rootTag = SchemaParser.getInstance().getTags().get("root"); + dbg("root node. adding tags: "+ rootTag.getTagChildren().size()); + for(Tag tag : rootTag.getTagChildren()) { - Tag rootTag = SchemaParser.getInstance().getTags().get("root"); - dbg("root node. adding tags: "+ rootTag.getTagChildren().size()); - for(Tag tag : rootTag.getTagChildren()) - { - acceptor.accept(tagProposal(tag, "", context)); - } + acceptor.accept(tagProposal(tag, "", firstBracket, context)); } } - else - dbg("current container is null or not an abstract node."); } - /** * Returns the proposal for the specified tag, usign the specified indent * @param tag The tag from which to construct the proposal * @param indent The indent used to indent the tag and subsequent keys + * @param firstBracket Whether to add or not the '[' to the autcompletion + * @param context * @return */ - private ICompletionProposal tagProposal(Tag tag, String indent, + private ICompletionProposal tagProposal(Tag tag, String indent, boolean firstBracket, ContentAssistContext context) { // dbg("indent:[" + indent +"]"); StringBuilder proposal = new StringBuilder(); + if (firstBracket) + proposal.append("["); proposal.append(tag.getName()); proposal.append("]\n"); for(TagKey key : tag.getKeyChildren())