1 | package de.ugoe.cs.swe.bnftools.ui.refactoring.uppercasetokenrules;
|
---|
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.ltk.core.refactoring.Change;
|
---|
10 | import org.eclipse.ltk.core.refactoring.CompositeChange;
|
---|
11 | import org.eclipse.ltk.core.refactoring.RefactoringStatus;
|
---|
12 | import org.eclipse.ltk.core.refactoring.TextFileChange;
|
---|
13 | import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
|
---|
14 | import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
|
---|
15 | import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
|
---|
16 | import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
|
---|
17 | import org.eclipse.text.edits.MultiTextEdit;
|
---|
18 | import org.eclipse.text.edits.ReplaceEdit;
|
---|
19 | import org.eclipse.text.edits.TextEditGroup;
|
---|
20 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
21 | import org.eclipse.xtext.parsetree.LeafNode;
|
---|
22 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
23 | import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
---|
24 |
|
---|
25 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
26 | import de.ugoe.cs.swe.bnftools.utils.DeclarationReferencesPair;
|
---|
27 |
|
---|
28 | public class UppercaseTokenRulesRefactoringProcessor extends RefactoringProcessor {
|
---|
29 | IFile file;
|
---|
30 | private CompositeNode rootNode;
|
---|
31 | private List<DeclarationReferencesPair> nodePairs;
|
---|
32 |
|
---|
33 | // ----------------------------------------------------------------------------------------------------
|
---|
34 |
|
---|
35 | public UppercaseTokenRulesRefactoringProcessor(XtextEditor editor, CompositeNode root, XtextDocument doc, List<DeclarationReferencesPair> nodePairs) {
|
---|
36 | file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
|
---|
37 | this.rootNode = root;
|
---|
38 | this.nodePairs = nodePairs;
|
---|
39 | }
|
---|
40 |
|
---|
41 | // ----------------------------------------------------------------------------------------------------
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public Object[] getElements() {
|
---|
45 | return null;
|
---|
46 | }
|
---|
47 |
|
---|
48 | // ----------------------------------------------------------------------------------------------------
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public String getIdentifier() {
|
---|
52 | return "Uppercase Token Rules Processor";
|
---|
53 | }
|
---|
54 |
|
---|
55 | // ----------------------------------------------------------------------------------------------------
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public String getProcessorName() {
|
---|
59 | return "Uppercase Token Rules Processor";
|
---|
60 | }
|
---|
61 |
|
---|
62 | // ----------------------------------------------------------------------------------------------------
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public boolean isApplicable() throws CoreException {
|
---|
66 | return (rootNode != null);
|
---|
67 | }
|
---|
68 |
|
---|
69 | // ----------------------------------------------------------------------------------------------------
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
|
---|
73 | throws CoreException, OperationCanceledException {
|
---|
74 |
|
---|
75 | RefactoringStatus status = new RefactoringStatus();
|
---|
76 |
|
---|
77 | if (rootNode == null)
|
---|
78 | status.addFatalError("Root node is null!");
|
---|
79 |
|
---|
80 | return status;
|
---|
81 | }
|
---|
82 |
|
---|
83 | // ----------------------------------------------------------------------------------------------------
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
|
---|
87 | CheckConditionsContext context) throws CoreException,
|
---|
88 | OperationCanceledException {
|
---|
89 |
|
---|
90 | return checkInitialConditions(pm);
|
---|
91 | }
|
---|
92 |
|
---|
93 | // ----------------------------------------------------------------------------------------------------
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public Change createChange(IProgressMonitor pm) throws CoreException,
|
---|
97 | OperationCanceledException {
|
---|
98 |
|
---|
99 | CompositeChange compositeChange = new CompositeChange("Uppercase Token Rule");
|
---|
100 | pm.beginTask("Uppercase Token Rules Refactoring", 2);
|
---|
101 |
|
---|
102 | //initialising the edit
|
---|
103 | MultiTextEdit multiEdit = new MultiTextEdit();
|
---|
104 | TextFileChange fileChange = new TextFileChange("Uppercase Token Rules", file);
|
---|
105 | fileChange.setEdit(multiEdit);
|
---|
106 | fileChange.setTextType("bnf");
|
---|
107 | compositeChange.add(fileChange);
|
---|
108 |
|
---|
109 | // uppercase Token Rules
|
---|
110 | for (int i=0; i < nodePairs.size(); i++) {
|
---|
111 | CompositeNode declarationRule = nodePairs.get(i).getDeclarationNode();
|
---|
112 | List<CompositeNode> ruleReferences = nodePairs.get(i).getReferenceNodes();
|
---|
113 |
|
---|
114 | // get the replacement name
|
---|
115 | Rule rule = (Rule) declarationRule.getElement();
|
---|
116 | String replaceRuleName = rule.getName().toUpperCase();
|
---|
117 |
|
---|
118 | LeafNode nameNode = null;
|
---|
119 | for (int j=0; j < declarationRule.getLeafNodes().size(); j++) {
|
---|
120 | if (declarationRule.getLeafNodes().get(j).getText().equals(rule.getName()))
|
---|
121 | nameNode = declarationRule.getLeafNodes().get(j);
|
---|
122 | }
|
---|
123 |
|
---|
124 | ReplaceEdit replaceEdit = new ReplaceEdit(nameNode.getOffset(), nameNode.getLength(), replaceRuleName);
|
---|
125 | multiEdit.addChild(replaceEdit);
|
---|
126 | TextEditGroup editGroup = new TextEditGroup("reference renaming", replaceEdit);
|
---|
127 | fileChange.addTextEditGroup(editGroup);
|
---|
128 | pm.worked(1);
|
---|
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(), replaceRuleName);
|
---|
134 | multiEdit.addChild(replaceEdit2);
|
---|
135 | TextEditGroup editGroup2 = new TextEditGroup("reference renaming", replaceEdit);
|
---|
136 | fileChange.addTextEditGroup(editGroup2);
|
---|
137 | pm.worked(1);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | return compositeChange;
|
---|
142 |
|
---|
143 | }
|
---|
144 |
|
---|
145 | // ----------------------------------------------------------------------------------------------------
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
|
---|
149 | SharableParticipants sharedParticipants) throws CoreException {
|
---|
150 | return null;
|
---|
151 | }
|
---|
152 |
|
---|
153 | } |
---|