eclipse plugin:add completion for WMLTag rule

This commit is contained in:
Timotei Dolean 2010-08-02 19:04:17 +00:00
parent 61133fe6fd
commit d6a9b87797

View File

@ -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,31 +32,47 @@ 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();
// dbg("parent text: " + parent.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)
{
for(Tag tag : tagChildren.getTagChildren())
{
acceptor.accept(tagProposal(tag, parentIndent, context));
acceptor.accept(tagProposal(tag, parentIndent, false, context));
}
}
else
@ -67,25 +84,25 @@ public class WMLProposalProvider extends AbstractWMLProposalProvider
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())