[5] | 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.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.DeleteEdit;
|
---|
| 18 | import org.eclipse.text.edits.MultiTextEdit;
|
---|
| 19 | import org.eclipse.text.edits.TextEditGroup;
|
---|
| 20 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
| 21 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
| 22 | import org.eclipse.xtext.ui.editor.model.IXtextDocument;
|
---|
| 23 |
|
---|
| 24 | import de.ugoe.cs.swe.bnftools.utils.Utils;
|
---|
| 25 |
|
---|
| 26 | public class RemoveUnusedRulesProcessor extends RefactoringProcessor {
|
---|
| 27 | IFile file;
|
---|
| 28 | private List<CompositeNode> unusedRuleNodes;
|
---|
| 29 | private IXtextDocument document;
|
---|
| 30 |
|
---|
| 31 | // ----------------------------------------------------------------------------------------------------
|
---|
| 32 |
|
---|
| 33 | public RemoveUnusedRulesProcessor(XtextEditor editor, IXtextDocument doc, List<CompositeNode> unusedRuleNodes) {
|
---|
| 34 | file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
|
---|
| 35 | this.unusedRuleNodes = unusedRuleNodes;
|
---|
| 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 "Remove Unused Rules Processor";
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | // ----------------------------------------------------------------------------------------------------
|
---|
| 54 |
|
---|
| 55 | @Override
|
---|
| 56 | public String getProcessorName() {
|
---|
| 57 | return "Remove Unused Rules Processor";
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | // ----------------------------------------------------------------------------------------------------
|
---|
| 61 |
|
---|
| 62 | @Override
|
---|
| 63 | public boolean isApplicable() throws CoreException {
|
---|
| 64 | return (unusedRuleNodes != null) && (unusedRuleNodes.size() > 0);
|
---|
| 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 (unusedRuleNodes == null) {
|
---|
| 76 | status.addFatalError("No unused rule node!");
|
---|
| 77 | } else if (unusedRuleNodes.size() == 0) {
|
---|
| 78 | status.addFatalError("No unused rule node!");
|
---|
| 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("Remove Unused Rules");
|
---|
| 104 | pm.beginTask("Remove Unused Rules", unusedRuleNodes.size());
|
---|
| 105 |
|
---|
| 106 | //initialising the edit
|
---|
| 107 | MultiTextEdit multiEdit = new MultiTextEdit();
|
---|
| 108 | TextFileChange fileChange = new TextFileChange("Remove Unused Rule", file);
|
---|
| 109 | fileChange.setEdit(multiEdit);
|
---|
| 110 | fileChange.setTextType("bnf");
|
---|
| 111 | compositeChange.add(fileChange);
|
---|
| 112 |
|
---|
| 113 | for (int i=0; i < unusedRuleNodes.size(); i++) {
|
---|
| 114 | CompositeNode declarationRule = unusedRuleNodes.get(i);
|
---|
| 115 |
|
---|
| 116 | // delete the declaration
|
---|
| 117 | int offset = declarationRule.getOffset();
|
---|
| 118 | int amount = Utils.stepBackNewlines(document, offset);
|
---|
| 119 |
|
---|
| 120 | DeleteEdit deleteEdit = new DeleteEdit(offset-amount, declarationRule.getLength()+amount);
|
---|
| 121 | multiEdit.addChild(deleteEdit);
|
---|
| 122 | TextEditGroup editGroup = new TextEditGroup("declaration removal", deleteEdit);
|
---|
| 123 | fileChange.addTextEditGroup(editGroup);
|
---|
| 124 | pm.worked(1);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | return compositeChange;
|
---|
| 128 |
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | // ----------------------------------------------------------------------------------------------------
|
---|
| 132 |
|
---|
| 133 | @Override
|
---|
| 134 | public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
|
---|
| 135 | SharableParticipants sharedParticipants) throws CoreException {
|
---|
| 136 | return null;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | // ----------------------------------------------------------------------------------------------------
|
---|
| 140 |
|
---|
| 141 |
|
---|
| 142 | } |
---|