[5] | 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;
|
---|
[18] | 7 | import org.eclipse.emf.ecore.EObject;
|
---|
[5] | 8 | import org.eclipse.jface.text.BadLocationException;
|
---|
| 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.AbstractNode;
|
---|
| 21 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
[18] | 22 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
[5] | 23 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
| 24 | import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
---|
| 25 |
|
---|
[18] | 26 | import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry;
|
---|
[5] | 27 |
|
---|
| 28 | public class AutoRenumberRefactoringProcessor extends RefactoringProcessor {
|
---|
| 29 | IFile file;
|
---|
| 30 | private CompositeNode rootNode;
|
---|
| 31 | private XtextDocument document;
|
---|
| 32 |
|
---|
| 33 | // ----------------------------------------------------------------------------------------------------
|
---|
| 34 |
|
---|
| 35 | public AutoRenumberRefactoringProcessor(XtextEditor editor, CompositeNode root, XtextDocument doc) {
|
---|
| 36 | file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
|
---|
| 37 | this.rootNode = root;
|
---|
| 38 | this.document = doc;
|
---|
| 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 "Autorenumbering Processor";
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | // ----------------------------------------------------------------------------------------------------
|
---|
| 56 |
|
---|
| 57 | @Override
|
---|
| 58 | public String getProcessorName() {
|
---|
| 59 | return "Autorenumbering 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 | * Create a change, initialise the IProgressMonitor with 2 task:
|
---|
| 100 | * 1. renaming the reference with the rule's right side
|
---|
| 101 | * 2. renaming the whole rule with an empty string
|
---|
| 102 | */
|
---|
| 103 | CompositeChange compositeChange = new CompositeChange("Autorenumbering");
|
---|
| 104 | pm.beginTask("Autorenumbering Refactoring", 2);
|
---|
| 105 |
|
---|
| 106 | //initialising the edit
|
---|
| 107 | MultiTextEdit multiEdit = new MultiTextEdit();
|
---|
| 108 | TextFileChange fileChange = new TextFileChange("Autorenumbering", file);
|
---|
| 109 | fileChange.setEdit(multiEdit);
|
---|
| 110 | fileChange.setTextType("bnf");
|
---|
| 111 | compositeChange.add(fileChange);
|
---|
| 112 |
|
---|
| 113 | int counter = 1;
|
---|
| 114 | // renumber the rules
|
---|
| 115 | for (int i=0; i < rootNode.getChildren().size(); i++) {
|
---|
| 116 | AbstractNode child = rootNode.getChildren().get(i);
|
---|
[18] | 117 | EObject element = child.getElement();
|
---|
| 118 | if (!(element instanceof BnfEntry))
|
---|
[5] | 119 | continue;
|
---|
[18] | 120 | BnfEntry bnfEntry = (BnfEntry) element;
|
---|
| 121 | if (bnfEntry.getRule() == null)
|
---|
| 122 | continue;
|
---|
| 123 |
|
---|
| 124 | child = NodeUtil.getNodeAdapter(bnfEntry.getRule()).getParserNode();
|
---|
| 125 |
|
---|
[5] | 126 | String ruleText;
|
---|
| 127 | try {
|
---|
| 128 | ruleText = document.get(child.getOffset(), child.getLength());
|
---|
| 129 | int numberSeperatorPosition = ruleText.indexOf(".");
|
---|
| 130 | if (numberSeperatorPosition >= 0) {
|
---|
| 131 | int startOffset = child.getOffset();
|
---|
| 132 | int length = numberSeperatorPosition;
|
---|
| 133 |
|
---|
| 134 | ReplaceEdit replaceEdit = new ReplaceEdit(startOffset, length, Integer.toString(counter));
|
---|
| 135 | multiEdit.addChild(replaceEdit);
|
---|
| 136 | TextEditGroup editGroup2 = new TextEditGroup("renumbering", replaceEdit);
|
---|
| 137 | fileChange.addTextEditGroup(editGroup2);
|
---|
| 138 | pm.worked(1);
|
---|
| 139 | }
|
---|
| 140 | counter++;
|
---|
| 141 | } catch (BadLocationException e) {
|
---|
| 142 | e.printStackTrace();
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | return compositeChange;
|
---|
| 147 |
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | // ----------------------------------------------------------------------------------------------------
|
---|
| 151 |
|
---|
| 152 | @Override
|
---|
| 153 | public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
|
---|
| 154 | SharableParticipants sharedParticipants) throws CoreException {
|
---|
| 155 | return null;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | } |
---|