1 | package de.ugoe.cs.swe.bnftools.ui.quickfix.processors;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import org.eclipse.core.resources.IFile;
|
---|
6 | import org.eclipse.core.runtime.CoreException;
|
---|
7 | import org.eclipse.core.runtime.IProgressMonitor;
|
---|
8 | import org.eclipse.core.runtime.OperationCanceledException;
|
---|
9 | import org.eclipse.jface.text.BadLocationException;
|
---|
10 | import org.eclipse.ltk.core.refactoring.Change;
|
---|
11 | import org.eclipse.ltk.core.refactoring.CompositeChange;
|
---|
12 | import org.eclipse.ltk.core.refactoring.RefactoringStatus;
|
---|
13 | import org.eclipse.ltk.core.refactoring.TextFileChange;
|
---|
14 | import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
|
---|
15 | import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
|
---|
16 | import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
|
---|
17 | import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
|
---|
18 | import org.eclipse.text.edits.DeleteEdit;
|
---|
19 | import org.eclipse.text.edits.MultiTextEdit;
|
---|
20 | import org.eclipse.text.edits.ReplaceEdit;
|
---|
21 | import org.eclipse.text.edits.TextEditGroup;
|
---|
22 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
23 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
24 | import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
---|
25 |
|
---|
26 | import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
|
---|
27 | import de.ugoe.cs.swe.bnftools.utils.Utils;
|
---|
28 |
|
---|
29 | public class InlineRulesProcessor extends RefactoringProcessor {
|
---|
30 |
|
---|
31 | private IFile file;
|
---|
32 | private CompositeNode rootNode;
|
---|
33 | private XtextDocument document;
|
---|
34 | private List<DeclarationReferencesPair> nodePairs;
|
---|
35 | private boolean isRemoveLine;
|
---|
36 |
|
---|
37 | // ----------------------------------------------------------------------------------------------------
|
---|
38 |
|
---|
39 | public InlineRulesProcessor(XtextEditor editor, CompositeNode root, XtextDocument doc, List<DeclarationReferencesPair> nodePairs, boolean removal) {
|
---|
40 | file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
|
---|
41 | this.rootNode = root;
|
---|
42 | this.document = doc;
|
---|
43 | this.nodePairs = nodePairs;
|
---|
44 | this.isRemoveLine = removal;
|
---|
45 |
|
---|
46 | }
|
---|
47 |
|
---|
48 | // ----------------------------------------------------------------------------------------------------
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public Object[] getElements() {
|
---|
52 | return null;
|
---|
53 | }
|
---|
54 |
|
---|
55 | // ----------------------------------------------------------------------------------------------------
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public String getIdentifier() {
|
---|
59 | return "Inline Rules Processor";
|
---|
60 | }
|
---|
61 |
|
---|
62 | // ----------------------------------------------------------------------------------------------------
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public String getProcessorName() {
|
---|
66 | return "Inline Rules Processor";
|
---|
67 | }
|
---|
68 |
|
---|
69 | // ----------------------------------------------------------------------------------------------------
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public boolean isApplicable() throws CoreException {
|
---|
73 | return (rootNode != null);
|
---|
74 | }
|
---|
75 |
|
---|
76 | // ----------------------------------------------------------------------------------------------------
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
|
---|
80 | throws CoreException, OperationCanceledException {
|
---|
81 | RefactoringStatus status = new RefactoringStatus();
|
---|
82 |
|
---|
83 | if (rootNode == null)
|
---|
84 | status.addFatalError("Root node is null!");
|
---|
85 |
|
---|
86 | return status;
|
---|
87 | }
|
---|
88 |
|
---|
89 | // ----------------------------------------------------------------------------------------------------
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
|
---|
93 | CheckConditionsContext context) throws CoreException,
|
---|
94 | OperationCanceledException {
|
---|
95 |
|
---|
96 | return checkInitialConditions(pm);
|
---|
97 | }
|
---|
98 |
|
---|
99 | // ----------------------------------------------------------------------------------------------------
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public Change createChange(IProgressMonitor pm) throws CoreException,
|
---|
103 | OperationCanceledException {
|
---|
104 |
|
---|
105 | CompositeChange compositeChange = new CompositeChange("Inline Rule");
|
---|
106 | pm.beginTask("Inline Rules Refactoring", 2);
|
---|
107 |
|
---|
108 |
|
---|
109 | //initialising the edit
|
---|
110 | MultiTextEdit multiEdit = new MultiTextEdit();
|
---|
111 | TextFileChange fileChange = new TextFileChange("Inline Rules", file);
|
---|
112 | fileChange.setEdit(multiEdit);
|
---|
113 | fileChange.setTextType("bnf");
|
---|
114 | compositeChange.add(fileChange);
|
---|
115 |
|
---|
116 | // inline Rules
|
---|
117 | for (int i=0; i < nodePairs.size(); i++) {
|
---|
118 | CompositeNode declarationRule = nodePairs.get(i).getDeclarationNode();
|
---|
119 | List<CompositeNode> ruleReferences = nodePairs.get(i).getReferenceNodes();
|
---|
120 |
|
---|
121 | try {
|
---|
122 | String ruleText = document.get(declarationRule.getOffset(), declarationRule.getLength());
|
---|
123 | String ruleRightHandSide = ruleText.substring(ruleText.indexOf("::=")+3).trim();
|
---|
124 | if (ruleRightHandSide.length() == 0)
|
---|
125 | return compositeChange;
|
---|
126 |
|
---|
127 | ruleRightHandSide = "(" + ruleRightHandSide + ")";
|
---|
128 | //TODO: handle the parentheses more intelligently!
|
---|
129 |
|
---|
130 | // replace references with passthrough rule name
|
---|
131 | for (int j=0; j < ruleReferences.size(); j++) {
|
---|
132 | CompositeNode ruleReference = ruleReferences.get(j);
|
---|
133 | ReplaceEdit replaceEdit2 = new ReplaceEdit(ruleReference.getOffset(), ruleReference.getLength(), ruleRightHandSide);
|
---|
134 | multiEdit.addChild(replaceEdit2);
|
---|
135 | TextEditGroup editGroup2 = new TextEditGroup("reference renaming", replaceEdit2);
|
---|
136 | fileChange.addTextEditGroup(editGroup2);
|
---|
137 | pm.worked(1);
|
---|
138 | }
|
---|
139 |
|
---|
140 | // remove old line
|
---|
141 | if (isRemoveLine) {
|
---|
142 | int amount = Utils.stepBackNewlines(document, declarationRule.getOffset());
|
---|
143 | DeleteEdit deleteEdit = new DeleteEdit(declarationRule.getOffset() - amount, declarationRule.getLength() + amount);
|
---|
144 | multiEdit.addChild(deleteEdit);
|
---|
145 | TextEditGroup editGroup3 = new TextEditGroup("remove old line", deleteEdit);
|
---|
146 | fileChange.addTextEditGroup(editGroup3);
|
---|
147 | pm.worked(1);
|
---|
148 | }
|
---|
149 | } catch (BadLocationException e) {
|
---|
150 | // TODO Auto-generated catch block
|
---|
151 | e.printStackTrace();
|
---|
152 | }
|
---|
153 |
|
---|
154 | }
|
---|
155 |
|
---|
156 | return compositeChange;
|
---|
157 |
|
---|
158 | }
|
---|
159 |
|
---|
160 | // ----------------------------------------------------------------------------------------------------
|
---|
161 |
|
---|
162 | @Override
|
---|
163 | public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
|
---|
164 | SharableParticipants sharedParticipants) throws CoreException {
|
---|
165 | return null;
|
---|
166 | }
|
---|
167 |
|
---|
168 | } |
---|