Commit 7e1cff33 authored by Johannes Roith's avatar Johannes Roith

Implemented structured renaming of Terminals and Non-Terminals.

parent 60149f01
......@@ -5,6 +5,7 @@ import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
......@@ -20,15 +21,79 @@ import org.eclipse.ui.handlers.HandlerUtil;
import de.in.tum.www2.cup.Range;
import de.in.tum.www2.cup.analysis.FindAtPositionVisitor;
import de.in.tum.www2.cup.analysis.Visitor;
import de.in.tum.www2.cup.ast.AbstractNode;
import de.in.tum.www2.cup.ast.IWithName;
import de.in.tum.www2.cup.ast.Name;
import de.in.tum.www2.cup.ast.NonTerminal;
import de.in.tum.www2.cup.ast.ParserResult;
import de.in.tum.www2.cup.ast.Production;
import de.in.tum.www2.cup.ast.ProductionRight;
import de.in.tum.www2.cup.ast.ProductionSymbolRef;
import de.in.tum.www2.cup.ast.Terminal;
import de.tum.in.www2.cupplugin.PluginUtility;
import de.tum.in.www2.cupplugin.editors.CupTextEditor;
import de.tum.in.www2.cupplugin.editors.MultiPageEditor;
import de.tum.in.www2.cupplugin.editors.RevisionManager;
import de.tum.in.www2.cupplugin.model.Model;
public class RenameSymbolHandler extends AbstractHandler {
static class RenamingVisitor extends Visitor<Object>
{
private IDocument doc;
private String oldName;
private String newName;
private int oldNameLength;
public RenamingVisitor(IDocument doc, String oldName, String newName) {
this.doc = doc;
this.oldName = oldName;
this.newName = newName;
this.oldNameLength = oldName.length();
}
private void patch(IWithName withName) {
if (withName == null || withName.getNameStringOrNull() == null)
return;
if (withName.getNameStringOrNull().equals(oldName)) {
// We do not need to track how the offset shifts,
// because the Location patcher will take care of that
// anyway when we edit the document.
AbstractNode node = (AbstractNode) withName;
int offset = node.getBegin().getOffsetFromStart();
try {
doc.replace(offset, oldNameLength, newName);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
@Override
public void postVisit(ProductionSymbolRef ref, Object obj) {
patch(ref);
}
@Override
public void postVisit(NonTerminal nt, Object obj) {
patch(nt);
}
@Override
public void postVisit(Production p, Object obj) {
patch(p);
}
@Override
public void postVisit(Terminal t, Object obj) {
patch(t);
}
}
static class RenameDialog extends TitleAreaDialog {
private Text newNameControl;
private String oldName;
......@@ -67,26 +132,27 @@ public class RenameSymbolHandler extends AbstractHandler {
.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
container.setLayout(layout);
Label lbtLastName = new Label(container, SWT.NONE);
lbtLastName.setText("Old name:");
Label labelOldName = new Label(container, SWT.NONE);
labelOldName.setText("Old name:");
GridData dataLastName = new GridData();
dataLastName.grabExcessHorizontalSpace = true;
dataLastName.horizontalAlignment = GridData.FILL;
GridData dataOldName = new GridData();
dataOldName.grabExcessHorizontalSpace = true;
dataOldName.horizontalAlignment = GridData.FILL;
Label dummy = new Label(container, SWT.BORDER);
dummy.setText(oldName);
dummy.setLayoutData(dataLastName);
dummy.setLayoutData(dataOldName);
Label lbtFirstName = new Label(container, SWT.NONE);
lbtFirstName.setText("New name:");
Label labelNewName = new Label(container, SWT.NONE);
labelNewName.setText("New name:");
GridData dataFirstName = new GridData();
dataFirstName.grabExcessHorizontalSpace = true;
dataFirstName.horizontalAlignment = GridData.FILL;
GridData dataNewName = new GridData();
dataNewName.grabExcessHorizontalSpace = true;
dataNewName.horizontalAlignment = GridData.FILL;
newNameControl = new Text(container, SWT.BORDER);
newNameControl.setLayoutData(dataFirstName);
newNameControl.setLayoutData(dataNewName);
newNameControl.setText(oldName);
return area;
}
......@@ -120,12 +186,17 @@ public class RenameSymbolHandler extends AbstractHandler {
RenameDialog renameDialog = new RenameDialog(oldName, activeShell);
renameDialog.create();
if (renameDialog.open() == Window.OK) {
// visitor: make editings while traversing tree.
IDocument doc = editor.getDocument();
doc.set(renameDialog.getNewName());
Model model = Model.getInstanceForDocument(doc);
ParserResult ast = model.getAstModel();
if (ast == null || RevisionManager.get(doc) != model.getAstModelRevisionNumber()) {
PluginUtility.showMessage("Can't rename symbols, because a valid, " +
"updated AST is not available.");
return null;
}
RenamingVisitor visitor = new RenamingVisitor(doc, oldName, renameDialog.getNewName());
ast.accept(visitor, null);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment