1 | package de.ugoe.cs.swe.bnftools.ui.refactoring.autorenumber;
|
---|
2 |
|
---|
3 | import org.eclipse.core.resources.IFile;
|
---|
4 | import org.eclipse.core.runtime.CoreException;
|
---|
5 | import org.eclipse.core.runtime.IProgressMonitor;
|
---|
6 | import org.eclipse.core.runtime.OperationCanceledException;
|
---|
7 | import org.eclipse.jface.text.BadLocationException;
|
---|
8 | import org.eclipse.ltk.core.refactoring.Change;
|
---|
9 | import org.eclipse.ltk.core.refactoring.CompositeChange;
|
---|
10 | import org.eclipse.ltk.core.refactoring.RefactoringStatus;
|
---|
11 | import org.eclipse.ltk.core.refactoring.TextFileChange;
|
---|
12 | import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
|
---|
13 | import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
|
---|
14 | import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
|
---|
15 | import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
|
---|
16 | import org.eclipse.text.edits.MultiTextEdit;
|
---|
17 | import org.eclipse.text.edits.ReplaceEdit;
|
---|
18 | import org.eclipse.text.edits.TextEditGroup;
|
---|
19 | import org.eclipse.xtext.parsetree.AbstractNode;
|
---|
20 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
21 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
22 | import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
---|
23 |
|
---|
24 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
25 |
|
---|
26 | public class AutoRenumberRefactoringProcessor extends RefactoringProcessor {
|
---|
27 | IFile file;
|
---|
28 | private CompositeNode rootNode;
|
---|
29 | private XtextDocument document;
|
---|
30 |
|
---|
31 | // ----------------------------------------------------------------------------------------------------
|
---|
32 |
|
---|
33 | public AutoRenumberRefactoringProcessor(XtextEditor editor, CompositeNode root, XtextDocument doc) {
|
---|
34 | file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
|
---|
35 | this.rootNode = root;
|
---|
36 | this.document = doc;
|
---|
37 | }
|
---|
38 |
|
---|
39 | // ----------------------------------------------------------------------------------------------------
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public Object[] getElements() {
|
---|
43 | return null;
|
---|
44 | }
|
---|
45 |
|
---|
46 | // ----------------------------------------------------------------------------------------------------
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public String getIdentifier() {
|
---|
50 | return "Autorenumbering Processor";
|
---|
51 | }
|
---|
52 |
|
---|
53 | // ----------------------------------------------------------------------------------------------------
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public String getProcessorName() {
|
---|
57 | return "Autorenumbering Processor";
|
---|
58 | }
|
---|
59 |
|
---|
60 | // ----------------------------------------------------------------------------------------------------
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public boolean isApplicable() throws CoreException {
|
---|
64 | return (rootNode != null);
|
---|
65 | }
|
---|
66 |
|
---|
67 | // ----------------------------------------------------------------------------------------------------
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
|
---|
71 | throws CoreException, OperationCanceledException {
|
---|
72 |
|
---|
73 | RefactoringStatus status = new RefactoringStatus();
|
---|
74 |
|
---|
75 | if (rootNode == null)
|
---|
76 | status.addFatalError("Root node is null!");
|
---|
77 |
|
---|
78 | return status;
|
---|
79 | }
|
---|
80 |
|
---|
81 | // ----------------------------------------------------------------------------------------------------
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
|
---|
85 | CheckConditionsContext context) throws CoreException,
|
---|
86 | OperationCanceledException {
|
---|
87 |
|
---|
88 | return checkInitialConditions(pm);
|
---|
89 | }
|
---|
90 |
|
---|
91 | // ----------------------------------------------------------------------------------------------------
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public Change createChange(IProgressMonitor pm) throws CoreException,
|
---|
95 | OperationCanceledException {
|
---|
96 | /*
|
---|
97 | * Create a change, initialise the IProgressMonitor with 2 task:
|
---|
98 | * 1. renaming the reference with the rule's right side
|
---|
99 | * 2. renaming the whole rule with an empty string
|
---|
100 | */
|
---|
101 | CompositeChange compositeChange = new CompositeChange("Autorenumbering");
|
---|
102 | pm.beginTask("Autorenumbering Refactoring", 2);
|
---|
103 |
|
---|
104 | //initialising the edit
|
---|
105 | MultiTextEdit multiEdit = new MultiTextEdit();
|
---|
106 | TextFileChange fileChange = new TextFileChange("Autorenumbering", file);
|
---|
107 | fileChange.setEdit(multiEdit);
|
---|
108 | fileChange.setTextType("bnf");
|
---|
109 | compositeChange.add(fileChange);
|
---|
110 |
|
---|
111 | int counter = 1;
|
---|
112 | // renumber the rules
|
---|
113 | for (int i=0; i < rootNode.getChildren().size(); i++) {
|
---|
114 | AbstractNode child = rootNode.getChildren().get(i);
|
---|
115 | if (!(child.getElement() instanceof Rule))
|
---|
116 | continue;
|
---|
117 | String ruleText;
|
---|
118 | try {
|
---|
119 | ruleText = document.get(child.getOffset(), child.getLength());
|
---|
120 | int numberSeperatorPosition = ruleText.indexOf(".");
|
---|
121 | if (numberSeperatorPosition >= 0) {
|
---|
122 | int startOffset = child.getOffset();
|
---|
123 | int length = numberSeperatorPosition;
|
---|
124 |
|
---|
125 | ReplaceEdit replaceEdit = new ReplaceEdit(startOffset, length, Integer.toString(counter));
|
---|
126 | multiEdit.addChild(replaceEdit);
|
---|
127 | TextEditGroup editGroup2 = new TextEditGroup("renumbering", replaceEdit);
|
---|
128 | fileChange.addTextEditGroup(editGroup2);
|
---|
129 | pm.worked(1);
|
---|
130 | }
|
---|
131 | counter++;
|
---|
132 | } catch (BadLocationException e) {
|
---|
133 | e.printStackTrace();
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | return compositeChange;
|
---|
138 |
|
---|
139 | }
|
---|
140 |
|
---|
141 | // ----------------------------------------------------------------------------------------------------
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
|
---|
145 | SharableParticipants sharedParticipants) throws CoreException {
|
---|
146 | return null;
|
---|
147 | }
|
---|
148 |
|
---|
149 | } |
---|