[5] | 1 | package de.ugoe.cs.swe.bnftools.ui.quickfix.modifications;
|
---|
| 2 |
|
---|
| 3 | import java.util.ArrayList;
|
---|
| 4 | import java.util.List;
|
---|
| 5 |
|
---|
| 6 | import org.eclipse.core.runtime.NullProgressMonitor;
|
---|
| 7 | import org.eclipse.emf.ecore.EObject;
|
---|
| 8 | import org.eclipse.ltk.core.refactoring.Change;
|
---|
| 9 | import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
|
---|
| 10 | import org.eclipse.ltk.core.refactoring.RefactoringCore;
|
---|
| 11 | import org.eclipse.ui.PlatformUI;
|
---|
| 12 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
| 13 | import org.eclipse.xtext.parsetree.NodeAdapter;
|
---|
| 14 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
| 15 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
| 16 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
| 17 | import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
---|
| 18 | import org.eclipse.xtext.ui.editor.model.edit.IModificationContext;
|
---|
| 19 | import org.eclipse.xtext.ui.editor.model.edit.ISemanticModification;
|
---|
| 20 |
|
---|
| 21 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
| 22 | import de.ugoe.cs.swe.bnftools.ui.quickfix.processors.InlineRulesProcessor;
|
---|
| 23 | import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
|
---|
| 24 | import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
|
---|
| 25 | import de.ugoe.cs.swe.bnftools.utils.Utils;
|
---|
| 26 |
|
---|
| 27 | public class InlineRuleModification implements ISemanticModification {
|
---|
| 28 |
|
---|
| 29 | IResourceDescriptions resourceDescriptions;
|
---|
| 30 |
|
---|
| 31 | // ----------------------------------------------------------------------------------------------------
|
---|
| 32 |
|
---|
| 33 | public InlineRuleModification(IResourceDescriptions resourceDescriptions) {
|
---|
| 34 | this.resourceDescriptions = resourceDescriptions;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | // ----------------------------------------------------------------------------------------------------
|
---|
| 38 |
|
---|
| 39 | public void apply(EObject element, IModificationContext context)
|
---|
| 40 | throws Exception {
|
---|
| 41 | XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
|
---|
| 42 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
---|
| 43 |
|
---|
| 44 | //TODO: better error handling
|
---|
| 45 | if (!(editor.getDocument() instanceof XtextDocument)) {
|
---|
| 46 | return;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | if (!(element instanceof Rule))
|
---|
| 50 | return;
|
---|
| 51 |
|
---|
| 52 | Rule rule = (Rule) element;
|
---|
| 53 |
|
---|
| 54 | XtextDocument doc = (XtextDocument) editor.getDocument();
|
---|
| 55 | EObject root = doc.readOnly(new RootEObjectFinder());
|
---|
| 56 | NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
|
---|
| 57 |
|
---|
| 58 | List<DeclarationReferencesPair> nodePairs = new ArrayList<DeclarationReferencesPair>();
|
---|
| 59 |
|
---|
| 60 | List<CompositeNode> references = Utils.findReferences(rule,
|
---|
| 61 | resourceDescriptions, editor);
|
---|
| 62 |
|
---|
| 63 | nodePairs.add(new DeclarationReferencesPair(NodeUtil.getNodeAdapter(rule).getParserNode(), references));
|
---|
| 64 |
|
---|
| 65 | InlineRulesProcessor processor = new InlineRulesProcessor(editor, rootNode.getParserNode(), doc, nodePairs, true);
|
---|
| 66 |
|
---|
| 67 | NullProgressMonitor pm = new NullProgressMonitor();
|
---|
| 68 |
|
---|
| 69 | Change change;
|
---|
| 70 | try {
|
---|
| 71 | change = processor.createChange(pm);
|
---|
| 72 | change.initializeValidationData(pm);
|
---|
| 73 | change.setEnabled(true);
|
---|
| 74 | PerformChangeOperation operation = new PerformChangeOperation(
|
---|
| 75 | change);
|
---|
| 76 | operation.setUndoManager(RefactoringCore.getUndoManager(),
|
---|
| 77 | processor.getProcessorName());
|
---|
| 78 | operation.run(pm);
|
---|
| 79 | } catch (Exception e) {
|
---|
| 80 | e.printStackTrace();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | }
|
---|