use of org.antlr.works.grammar.element.ElementGroup in project antlrworks by antlr.
the class EditorRules method rebuildTree.
public void rebuildTree() {
saveExpandedNodes();
rememberSelectedTreeItem();
rulesTreeRootNode.removeAllChildren();
List<ElementRule> rules = getGrammarEngine().getRules();
List<ElementGroup> groups = getGrammarEngine().getGroups();
if (rules == null || groups == null)
return;
if (groups.isEmpty()) {
buildTree(rulesTreeRootNode, rules, 0, rules.size() - 1);
} else {
Stack<DefaultMutableTreeNode> parentStack = new Stack<DefaultMutableTreeNode>();
parentStack.add(rulesTreeRootNode);
int ruleIndex = 0;
for (ElementGroup group : groups) {
DefaultMutableTreeNode parentNode = parentStack.peek();
if (group.ruleIndex >= 0) {
buildTree(parentNode, rules, ruleIndex, group.ruleIndex);
ruleIndex = group.ruleIndex + 1;
}
if (group.openGroup) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new RuleTreeUserObject(group));
parentNode.add(node);
parentStack.push(node);
} else {
if (parentStack.size() > 1)
parentStack.pop();
}
}
if (ruleIndex < rules.size()) {
DefaultMutableTreeNode parentNode = parentStack.peek();
buildTree(parentNode, rules, ruleIndex, rules.size() - 1);
}
}
rulesTreeModel.reload();
restoreSelectedTreeItem();
restoreExpandedNodes();
}
use of org.antlr.works.grammar.element.ElementGroup in project antlrworks by antlr.
the class GrammarMenu method ungroup.
public void ungroup() {
StatisticsAW.shared().recordEvent(StatisticsAW.EVENT_RULE_UNGROUP);
ElementGroup openGroup = window.editorRules.getSelectedGroup();
if (openGroup == null) {
// No open group selected in the tree. Try to find the closest open group
// by moving backward
openGroup = window.editorRules.findOpenGroupClosestToLocation(window.getTextPane().getSelectionStart());
if (openGroup == null) {
// Still no open group ? Give up
XJAlert.display(window.getJavaContainer(), "Ungroup", "Cannot ungroup because no enclosing group has been found.");
return;
}
}
ElementGroup closingGroup = window.editorRules.findClosingGroupForGroup(openGroup);
window.beginGroupChange("Ungroup");
if (closingGroup != null) {
// End of file is considered as a closing group but no group really exists
// for that purpose
ATEToken t = closingGroup.token;
window.replaceText(t.getStartIndex() - 1, t.getEndIndex(), "");
}
ATEToken t = openGroup.token;
window.replaceText(t.getStartIndex() - 1, t.getEndIndex(), "");
window.endGroupChange();
}
use of org.antlr.works.grammar.element.ElementGroup in project antlrworks by antlr.
the class EditorRules method findOpenGroupClosestToLocation.
public ElementGroup findOpenGroupClosestToLocation(int location) {
// Look backward into the list of groups
List<ElementGroup> groups = getGrammarEngine().getGroups();
if (groups == null || groups.isEmpty())
return null;
ElementGroup previous = null;
for (ElementGroup group : groups) {
if (!group.openGroup)
continue;
ATEToken t = group.token;
if (t.getStartIndex() > location)
break;
previous = group;
}
return previous;
}
use of org.antlr.works.grammar.element.ElementGroup in project antlrworks by antlr.
the class EditorRules method findClosingGroupForGroup.
public ElementGroup findClosingGroupForGroup(ElementGroup group) {
List<ElementGroup> groups = getGrammarEngine().getGroups();
if (groups == null || groups.isEmpty())
return null;
int index = groups.indexOf(group) + 1;
if (index == -1)
return null;
int open = 0;
while (index < groups.size()) {
ElementGroup g = groups.get(index);
if (g.openGroup)
open++;
else if (open == 0)
return g;
else
open--;
index++;
}
return null;
}
Aggregations