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.edit.IModificationContext;
|
---|
18 | import org.eclipse.xtext.ui.editor.model.edit.ISemanticModification;
|
---|
19 |
|
---|
20 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
21 | import de.ugoe.cs.swe.bnftools.ui.quickfix.processors.ReplacePassthroughRuleProcessor;
|
---|
22 | import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
|
---|
23 | import de.ugoe.cs.swe.bnftools.utils.Utils;
|
---|
24 |
|
---|
25 | public class RemovePassthroughRuleModification implements ISemanticModification {
|
---|
26 |
|
---|
27 | IResourceDescriptions resourceDescriptions;
|
---|
28 |
|
---|
29 | // ----------------------------------------------------------------------------------------------------
|
---|
30 |
|
---|
31 | public RemovePassthroughRuleModification(
|
---|
32 | IResourceDescriptions resourceDescriptions) {
|
---|
33 | this.resourceDescriptions = resourceDescriptions;
|
---|
34 | }
|
---|
35 |
|
---|
36 | // ----------------------------------------------------------------------------------------------------
|
---|
37 |
|
---|
38 | public void apply(EObject element, IModificationContext context)
|
---|
39 | throws Exception {
|
---|
40 | // get current editor
|
---|
41 | XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
|
---|
42 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
---|
43 | Rule rule = (Rule) element;
|
---|
44 |
|
---|
45 | NodeAdapter declarationNode = NodeUtil.getNodeAdapter(rule);
|
---|
46 |
|
---|
47 | List<CompositeNode> references = Utils.findReferences(element,
|
---|
48 | resourceDescriptions, editor);
|
---|
49 |
|
---|
50 | List<DeclarationReferencesPair> nodePairs = new ArrayList<DeclarationReferencesPair>();
|
---|
51 | nodePairs.add(new DeclarationReferencesPair((CompositeNode) declarationNode.getParserNode(), references));
|
---|
52 |
|
---|
53 | ReplacePassthroughRuleProcessor processor = new ReplacePassthroughRuleProcessor(
|
---|
54 | editor, editor.getDocument(), nodePairs, true);
|
---|
55 |
|
---|
56 | NullProgressMonitor pm = new NullProgressMonitor();
|
---|
57 | Change change;
|
---|
58 | try {
|
---|
59 | change = processor.createChange(pm);
|
---|
60 | change.initializeValidationData(pm);
|
---|
61 | change.setEnabled(true);
|
---|
62 | PerformChangeOperation operation = new PerformChangeOperation(
|
---|
63 | change);
|
---|
64 | operation.setUndoManager(RefactoringCore.getUndoManager(),
|
---|
65 | processor.getProcessorName());
|
---|
66 | operation.run(pm);
|
---|
67 | change.dispose();
|
---|
68 | } catch (Exception e) {
|
---|
69 | e.printStackTrace();
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | }
|
---|