[5] | 1 | package de.ugoe.cs.swe.bnftools.ui.refactoring.replacepassthroughrules;
|
---|
| 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 |
|
---|
| 24 | import de.ugoe.cs.swe.bnftools.analysis.EbnfAnalysisUtils;
|
---|
[18] | 25 | import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry;
|
---|
[5] | 26 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
| 27 | import de.ugoe.cs.swe.bnftools.ui.quickfix.processors.ReplacePassthroughRuleProcessor;
|
---|
| 28 | import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoring;
|
---|
| 29 | import de.ugoe.cs.swe.bnftools.ui.refactoring.generic.GenericRefactoringWizard;
|
---|
| 30 | import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
|
---|
| 31 | import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
|
---|
| 32 | import de.ugoe.cs.swe.bnftools.utils.Utils;
|
---|
| 33 |
|
---|
| 34 | public class ReplaceAllPassthroughRulesRefactoringHandler extends
|
---|
| 35 | AbstractHandler {
|
---|
| 36 |
|
---|
| 37 | @Inject
|
---|
| 38 | IResourceDescriptions resourceDescriptions;
|
---|
| 39 |
|
---|
| 40 | // ----------------------------------------------------------------------------------------------------
|
---|
| 41 |
|
---|
| 42 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
| 43 | XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
|
---|
| 44 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
---|
| 45 |
|
---|
| 46 | //TODO: better error handling
|
---|
| 47 | if (!(editor.getDocument() instanceof XtextDocument)) {
|
---|
| 48 | return null;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | XtextDocument doc = (XtextDocument) editor.getDocument();
|
---|
| 52 | EObject root = doc.readOnly(new RootEObjectFinder());
|
---|
| 53 | NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
|
---|
| 54 |
|
---|
| 55 | List<DeclarationReferencesPair> nodePairs = new ArrayList<DeclarationReferencesPair>();
|
---|
| 56 | for (int i=0; i < rootNode.getParserNode().getChildren().size(); i++) {
|
---|
| 57 | AbstractNode currentNode = rootNode.getParserNode().getChildren().get(i);
|
---|
[18] | 58 |
|
---|
| 59 | EObject element = currentNode.getElement();
|
---|
| 60 | if (!(element instanceof BnfEntry))
|
---|
| 61 | continue;
|
---|
| 62 | BnfEntry bnfEntry = (BnfEntry) element;
|
---|
| 63 | if (bnfEntry.getRule() == null)
|
---|
| 64 | continue;
|
---|
| 65 |
|
---|
| 66 | currentNode = NodeUtil.getNodeAdapter(bnfEntry.getRule()).getParserNode();
|
---|
| 67 |
|
---|
[5] | 68 | if (currentNode.getElement() instanceof Rule) {
|
---|
| 69 | Rule rule = (Rule) currentNode.getElement();
|
---|
| 70 | NodeAdapter declarationNode = NodeUtil.getNodeAdapter(rule);
|
---|
| 71 | if (EbnfAnalysisUtils.isPassthroughRule(rule)) {
|
---|
| 72 | List<CompositeNode> references = Utils.findReferences(rule,
|
---|
| 73 | resourceDescriptions, editor);
|
---|
| 74 |
|
---|
| 75 | DeclarationReferencesPair nodePair = new DeclarationReferencesPair(declarationNode.getParserNode(), references);
|
---|
| 76 | nodePairs.add(nodePair);
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | ReplacePassthroughRuleProcessor processor = new ReplacePassthroughRuleProcessor(
|
---|
| 82 | editor, editor.getDocument(), nodePairs, false);
|
---|
| 83 |
|
---|
| 84 | GenericRefactoring refactoring = new GenericRefactoring(processor);
|
---|
| 85 |
|
---|
| 86 | GenericRefactoringWizard wizard = new GenericRefactoringWizard(
|
---|
| 87 | refactoring, RefactoringWizard.WIZARD_BASED_USER_INTERFACE);
|
---|
| 88 |
|
---|
| 89 | RefactoringWizardOpenOperation openOperation = new RefactoringWizardOpenOperation(
|
---|
| 90 | wizard);
|
---|
| 91 |
|
---|
| 92 | try {
|
---|
| 93 | openOperation.run(Display.getCurrent().getActiveShell(),
|
---|
| 94 | "Refactoring not possible!");
|
---|
| 95 | } catch (InterruptedException e1) {
|
---|
| 96 | e1.printStackTrace();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | return null;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | }
|
---|