[5] | 1 | package de.ugoe.cs.swe.bnftools.ui.refactoring.inlinesinglereferencedrules;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.List;
|
---|
| 5 |
|
---|
| 6 | import org.eclipse.core.commands.AbstractHandler;
|
---|
| 7 | import org.eclipse.core.commands.ExecutionEvent;
|
---|
| 8 | import org.eclipse.core.commands.ExecutionException;
|
---|
| 9 | import org.eclipse.emf.ecore.EObject;
|
---|
| 10 | import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
|
---|
| 11 | import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
|
---|
| 12 | import org.eclipse.swt.widgets.Display;
|
---|
| 13 | import org.eclipse.ui.PlatformUI;
|
---|
| 14 | import org.eclipse.xtext.parsetree.AbstractNode;
|
---|
| 15 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
| 16 | import org.eclipse.xtext.parsetree.NodeAdapter;
|
---|
| 17 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
| 18 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
| 19 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
| 20 | import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
---|
| 21 |
|
---|
| 22 | import com.google.inject.Inject;
|
---|
| 23 |
|
---|
[18] | 24 | import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry;
|
---|
[5] | 25 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
| 26 | import de.ugoe.cs.swe.bnftools.ui.quickfix.processors.InlineRulesProcessor;
|
---|
| 27 | import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoring;
|
---|
| 28 | import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoringWizard;
|
---|
| 29 | import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
|
---|
| 30 | import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
|
---|
| 31 | import de.ugoe.cs.swe.bnftools.utils.Utils;
|
---|
| 32 |
|
---|
| 33 | public class InlineSingleReferencedRulesRefactoringHandler extends
|
---|
| 34 | AbstractHandler {
|
---|
| 35 |
|
---|
| 36 | @Inject
|
---|
| 37 | IResourceDescriptions resourceDescriptions;
|
---|
| 38 |
|
---|
| 39 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
| 40 | XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
|
---|
| 41 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
---|
| 42 |
|
---|
| 43 | //TODO: better error handling
|
---|
| 44 | if (!(editor.getDocument() instanceof XtextDocument)) {
|
---|
| 45 | return null;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | XtextDocument doc = (XtextDocument) editor.getDocument();
|
---|
| 49 | EObject root = doc.readOnly(new RootEObjectFinder());
|
---|
| 50 | NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
|
---|
| 51 |
|
---|
| 52 | List<DeclarationReferencesPair> nodePairs = new ArrayList<DeclarationReferencesPair>();
|
---|
| 53 | for (int i=0; i < rootNode.getParserNode().getChildren().size(); i++) {
|
---|
| 54 | AbstractNode currentNode = rootNode.getParserNode().getChildren().get(i);
|
---|
[18] | 55 |
|
---|
| 56 | EObject element = currentNode.getElement();
|
---|
| 57 | if (!(element instanceof BnfEntry))
|
---|
| 58 | continue;
|
---|
| 59 | BnfEntry bnfEntry = (BnfEntry) element;
|
---|
| 60 | if (bnfEntry.getRule() == null)
|
---|
| 61 | continue;
|
---|
| 62 |
|
---|
| 63 | currentNode = NodeUtil.getNodeAdapter(bnfEntry.getRule()).getParserNode();
|
---|
| 64 |
|
---|
[5] | 65 | if (currentNode.getElement() instanceof Rule) {
|
---|
| 66 | Rule rule = (Rule) currentNode.getElement();
|
---|
| 67 | NodeAdapter declarationNode = NodeUtil.getNodeAdapter(rule);
|
---|
| 68 | List<CompositeNode> references = Utils.findReferences(rule,
|
---|
| 69 | resourceDescriptions, editor);
|
---|
| 70 |
|
---|
| 71 | if (references.size() == 1) {
|
---|
| 72 | DeclarationReferencesPair nodePair = new DeclarationReferencesPair(declarationNode.getParserNode(), references);
|
---|
| 73 | nodePairs.add(nodePair);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | InlineRulesProcessor processor = new InlineRulesProcessor(editor, rootNode.getParserNode(), doc, nodePairs, false);
|
---|
| 79 |
|
---|
| 80 | GenericRefactoring refactoring = new GenericRefactoring(processor);
|
---|
| 81 |
|
---|
| 82 | GenericRefactoringWizard wizard = new GenericRefactoringWizard(
|
---|
| 83 | refactoring, RefactoringWizard.WIZARD_BASED_USER_INTERFACE);
|
---|
| 84 |
|
---|
| 85 | RefactoringWizardOpenOperation openOperation = new RefactoringWizardOpenOperation(
|
---|
| 86 | wizard);
|
---|
| 87 |
|
---|
| 88 | try {
|
---|
| 89 | openOperation.run(Display.getCurrent().getActiveShell(),
|
---|
| 90 | "Refactoring not possible!");
|
---|
| 91 | } catch (InterruptedException e1) {
|
---|
| 92 | e1.printStackTrace();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | return null;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | }
|
---|