[5] | 1 | package de.ugoe.cs.swe.bnftools.ui.refactoring.removeunusedrules;
|
---|
| 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.RemoveUnusedRulesProcessor;
|
---|
| 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.RootEObjectFinder;
|
---|
| 30 | import de.ugoe.cs.swe.bnftools.utils.Utils;
|
---|
| 31 |
|
---|
| 32 | public class RemoveUnusedRulesRefactoringHandler extends AbstractHandler {
|
---|
| 33 |
|
---|
| 34 | @Inject
|
---|
| 35 | IResourceDescriptions resourceDescriptions;
|
---|
| 36 |
|
---|
| 37 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
| 38 | XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
|
---|
| 39 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
---|
| 40 |
|
---|
| 41 | //TODO: better error handling
|
---|
| 42 | if (!(editor.getDocument() instanceof XtextDocument)) {
|
---|
| 43 | return null;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | XtextDocument doc = (XtextDocument) editor.getDocument();
|
---|
| 47 | EObject root = doc.readOnly(new RootEObjectFinder());
|
---|
| 48 | NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
|
---|
| 49 |
|
---|
| 50 | List<CompositeNode> unusedRules = new ArrayList<CompositeNode>();
|
---|
| 51 |
|
---|
| 52 | int counter = 0;
|
---|
| 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() == 0) && (counter > 0)){
|
---|
| 72 | unusedRules.add(declarationNode.getParserNode());
|
---|
| 73 | }
|
---|
| 74 | counter++;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | RemoveUnusedRulesProcessor processor = new RemoveUnusedRulesProcessor(
|
---|
| 79 | editor, editor.getDocument(), unusedRules);
|
---|
| 80 |
|
---|
| 81 | GenericRefactoring refactoring = new GenericRefactoring(processor);
|
---|
| 82 |
|
---|
| 83 | GenericRefactoringWizard wizard = new GenericRefactoringWizard(
|
---|
| 84 | refactoring, RefactoringWizard.WIZARD_BASED_USER_INTERFACE);
|
---|
| 85 |
|
---|
| 86 | RefactoringWizardOpenOperation openOperation = new RefactoringWizardOpenOperation(
|
---|
| 87 | wizard);
|
---|
| 88 |
|
---|
| 89 | try {
|
---|
| 90 | openOperation.run(Display.getCurrent().getActiveShell(),
|
---|
| 91 | "Refactoring not possible!");
|
---|
| 92 | } catch (InterruptedException e1) {
|
---|
| 93 | e1.printStackTrace();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 | return null;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | }
|
---|