source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/quickfix/processors/ReplacePassthroughRuleProcessor.java @ 5

Last change on this file since 5 was 5, checked in by zeiss, 14 years ago
  • Property svn:mime-type set to text/plain
File size: 6.6 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.quickfix.processors;
2
3import java.util.List;
4
5import org.eclipse.core.resources.IFile;
6import org.eclipse.core.runtime.CoreException;
7import org.eclipse.core.runtime.IProgressMonitor;
8import org.eclipse.core.runtime.OperationCanceledException;
9import org.eclipse.ltk.core.refactoring.Change;
10import org.eclipse.ltk.core.refactoring.CompositeChange;
11import org.eclipse.ltk.core.refactoring.RefactoringStatus;
12import org.eclipse.ltk.core.refactoring.TextFileChange;
13import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
14import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
15import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
16import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
17import org.eclipse.text.edits.DeleteEdit;
18import org.eclipse.text.edits.MultiTextEdit;
19import org.eclipse.text.edits.ReplaceEdit;
20import org.eclipse.text.edits.TextEditGroup;
21import org.eclipse.xtext.parsetree.CompositeNode;
22import org.eclipse.xtext.ui.editor.XtextEditor;
23import org.eclipse.xtext.ui.editor.model.IXtextDocument;
24
25import de.ugoe.cs.swe.bnftools.analysis.EbnfAnalysisUtils;
26import de.ugoe.cs.swe.bnftools.ebnf.Rule;
27import de.ugoe.cs.swe.bnftools.ebnf.RuleReference;
28import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
29import de.ugoe.cs.swe.bnftools.utils.Utils;
30
31public class ReplacePassthroughRuleProcessor extends RefactoringProcessor {
32        IFile file;
33        private List<DeclarationReferencesPair> nodePairs;
34        private boolean isRemoveLine;
35        private IXtextDocument document;
36       
37        // ----------------------------------------------------------------------------------------------------
38
39        public ReplacePassthroughRuleProcessor(XtextEditor editor, IXtextDocument doc, List<DeclarationReferencesPair> nodePairs, boolean removal) {
40                file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
41                this.nodePairs = nodePairs;
42                this.isRemoveLine = removal;
43                this.document = doc;
44        }
45       
46        // ----------------------------------------------------------------------------------------------------
47
48        @Override
49        public Object[] getElements() {
50                return null;
51        }
52
53        // ----------------------------------------------------------------------------------------------------
54
55        @Override
56        public String getIdentifier() {
57                return "Replace Passthrough Rule Processor";
58        }
59
60        // ----------------------------------------------------------------------------------------------------
61
62        @Override
63        public String getProcessorName() {
64                return "Replace Passthrough Rule Processor";
65        }
66
67        // ----------------------------------------------------------------------------------------------------
68
69        @Override
70        public boolean isApplicable() throws CoreException {
71                if ((nodePairs != null) && (nodePairs.size() > 0))
72                        return false;
73               
74               
75                return true;
76        }
77
78        // ----------------------------------------------------------------------------------------------------
79
80        @Override
81        public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
82                        throws CoreException, OperationCanceledException {
83               
84                RefactoringStatus status = new RefactoringStatus();
85
86                if (nodePairs == null) {
87                        status.addFatalError("No passthrough rules to replace!");
88                } else if (nodePairs.size() == 0) {
89                        status.addFatalError("No passthrough rules to replace!");
90                }
91               
92                boolean isNodeWithReferences = false;
93                for (int i=0; i < nodePairs.size(); i++) {
94                        if (nodePairs.get(i).getReferenceNodes().size() > 0) {
95                                isNodeWithReferences = true;
96                        }
97                }
98               
99                if (!isNodeWithReferences)
100                        status.addFatalError("No passthrough rules to replace!");
101               
102                return status;
103        }
104
105        // ----------------------------------------------------------------------------------------------------
106
107        @Override
108        public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
109                        CheckConditionsContext context) throws CoreException,
110                        OperationCanceledException {
111               
112                return checkInitialConditions(pm);
113        }
114
115        // ----------------------------------------------------------------------------------------------------
116
117        @Override
118        public Change createChange(IProgressMonitor pm) throws CoreException,
119                        OperationCanceledException {
120                /*
121                 * Create a change, initialise the IProgressMonitor with 2 task:
122                 * 1. renaming the reference with the rule's right side
123                 * 2. renaming the whole rule with an empty string
124                 */
125                CompositeChange compositeChange = new CompositeChange("Replace Passthrough Rule");
126                pm.beginTask("Replace Passthrough Rule Refactoring", nodePairs.size());
127               
128                //initialising the edit
129                MultiTextEdit multiEdit = new MultiTextEdit();
130                TextFileChange fileChange = new TextFileChange("Replace Passthrough Rule", file);
131                fileChange.setEdit(multiEdit);
132                fileChange.setTextType("bnf");
133                compositeChange.add(fileChange);
134               
135                for (int i=0; i < nodePairs.size(); i++) {
136                        CompositeNode declarationRule = nodePairs.get(i).getDeclarationNode();
137                        List<CompositeNode> ruleReferences = nodePairs.get(i).getReferenceNodes();
138                        // get the replacement name
139                        Rule rule = (Rule) declarationRule.getElement();
140                        RuleReference rightSideRule = EbnfAnalysisUtils.getPassthroughRuleReference(rule);
141                        String replaceRuleName = rightSideRule.getRuleref().getName();
142
143                        // replace references with passthrough rule name
144                        for (int j=0; j < ruleReferences.size(); j++) {
145                                CompositeNode ruleReference = ruleReferences.get(j);
146                                ReplaceEdit replaceEdit = new ReplaceEdit(ruleReference.getOffset(), ruleReference.getLength(), replaceRuleName);
147                                multiEdit.addChild(replaceEdit);
148                                TextEditGroup editGroup2 = new TextEditGroup("reference renaming", replaceEdit);
149                                fileChange.addTextEditGroup(editGroup2);
150                                pm.worked(1);
151                        }
152
153                        // remove old line
154                        if (isRemoveLine) {
155                                int amount = Utils.stepBackNewlines(document, declarationRule.getOffset());
156                                DeleteEdit deleteEdit = new DeleteEdit(declarationRule.getOffset() - amount, declarationRule.getLength() + amount);
157                                multiEdit.addChild(deleteEdit);
158                                TextEditGroup editGroup3 = new TextEditGroup("remove old line", deleteEdit);
159                                fileChange.addTextEditGroup(editGroup3);
160                                pm.worked(1);
161                        }
162                }
163               
164                return compositeChange;
165               
166        }
167
168        // ----------------------------------------------------------------------------------------------------
169
170        @Override
171        public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
172                        SharableParticipants sharedParticipants) throws CoreException {
173                return null;
174        }
175
176        // ----------------------------------------------------------------------------------------------------
177
178       
179}
Note: See TracBrowser for help on using the repository browser.