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