Commit 537ca7ee authored by Administrator's avatar Administrator
Browse files

Rename Minijava identifier as Refactoring

parent 67fc2add
......@@ -22,5 +22,27 @@
value="true">
</persistent>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
defaultHandler="mjplugin.commands.RenameSymbolHandler"
id="mjplugin.commands.RenameSymbol"
name="Rename MiniJava-Symbol ...">
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="true"
locationURI="popup:org.eclipse.ui.popup.any?before=additions">
<command commandId="mjplugin.commands.RenameSymbol" style="push">
</command>
</menuContribution>
</extension>
</plugin>
package mjplugin.commands;
import java_cup.runtime.ComplexSymbolFactory;
import minijava.Expr;
import minijava.IASTNode;
import minijava.MinijavaVisitor;
import minijava.Expr.Identifier;
import mjplugin.ast.MiniJavaAST;
import mjplugin.editors.MJEditor;
import org.eclipse.core.commands.AbstractHandler;
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.text.ITextSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
public class RenameSymbolHandler extends AbstractHandler {
MJEditor editor;
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
editor = (MJEditor)HandlerUtil.getActiveEditorChecked(event);
ITextSelection textSelection = (ITextSelection) editor.getSite().getSelectionProvider().getSelection();
int offset = textSelection.getOffset();
MiniJavaAST ast = editor.getModel().getAstModel();
IASTNode node = ast
.program
.findAtPosition(new ComplexSymbolFactory.Location(0, 0, offset));
System.out.println(node.getClass().toString()+": "+node);
if (node!=null && node instanceof Expr.Identifier){
Expr.Identifier oldID = (Expr.Identifier) node;
String oldname = oldID.i;
Shell activeShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
RenameDialog dialog = new RenameDialog(node.toString(), activeShell);
dialog.create();
if (dialog.open() == Window.OK) {
String newName = dialog.getNewName();
IDocument doc = editor.getDocument();
editor.beginCompoundChange();
ast.program.accept(new MinijavaVisitor(){
@Override
public void visit(Identifier d) {
if (d.i.equals(oldname)) {
int replaceOffset=d.left.getOffset();
try {
doc.replace(replaceOffset, oldname.length(), newName);
d.setName(newName);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
});
editor.endCompoundChange();
}
}
return null;
}
static class RenameDialog extends TitleAreaDialog {
private Text newNameControl;
private String oldName;
private String newName;
public String getNewName() {
return newName;
}
public RenameDialog(String oldName, Shell parentShell) {
super(parentShell);
this.oldName = oldName;
}
@Override
public void create() {
super.create();
setTitle("Rename Symbol");
setMessage("Rename Terminals and Non-Terminals ...",
IMessageProvider.INFORMATION);
}
@Override
protected void okPressed() {
newName = newNameControl.getText();
super.okPressed();
}
@Override
protected Control createDialogArea(Composite parent) {
Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(area, SWT.NONE);
container.setLayoutData(new GridData(GridData.FILL_BOTH));
GridLayout layout = new GridLayout(2, false);
container
.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
container.setLayout(layout);
Label labelOldName = new Label(container, SWT.NONE);
labelOldName.setText("Old name:");
GridData dataOldName = new GridData();
dataOldName.grabExcessHorizontalSpace = true;
dataOldName.horizontalAlignment = GridData.FILL;
Label dummy = new Label(container, SWT.BORDER);
dummy.setText(oldName);
dummy.setLayoutData(dataOldName);
Label labelNewName = new Label(container, SWT.NONE);
labelNewName.setText("New name:");
GridData dataNewName = new GridData();
dataNewName.grabExcessHorizontalSpace = true;
dataNewName.horizontalAlignment = GridData.FILL;
newNameControl = new Text(container, SWT.BORDER);
newNameControl.setLayoutData(dataNewName);
newNameControl.setText(oldName);
return area;
}
@Override
protected boolean isResizable() {
return true;
}
}
}
......@@ -60,7 +60,7 @@ public class GUIErrorReporter implements IFrontendObserver {
}
@Override
public void report(String message, Location left, Location right,List expected) {
public void report(String message, Location left, Location right, List expected) {
errors.add(new Message(message, left, right));
}
......
package mjplugin.editors;
import java_cup.runtime.ComplexSymbolFactory.Location;
import minijava.IASTNode;
import minijava.MinijavaVisitor;
public class LocationPatchVisitor extends MinijavaVisitor{
private int fixAfterOffset;
private int fixColumnUntilOffset;
private int diffLines;
private int diffColumns;
private int diffOffset;
public LocationPatchVisitor(int fixAfterOffset, int fixColumnUntilOffset,
int diffLines, int diffColumns, int diffOffset) {
this.fixAfterOffset = fixAfterOffset;
this.fixColumnUntilOffset = fixColumnUntilOffset;
this.diffLines = diffLines;
this.diffColumns = diffColumns;
this.diffOffset = diffOffset;
}
private void fixPosition(IASTNode n,Location pos) {
if (pos != null && pos.getOffset() >= fixAfterOffset) {
int cols = diffColumns;
if (fixColumnUntilOffset != -1 && pos.getOffset() >= fixColumnUntilOffset) cols = 0;
System.out.print("fixing (diffColumns: " + diffColumns + ", cols: " + cols + ") " + "... from " + pos);
pos.move(diffLines, cols, diffOffset);
System.out.println(" to " + pos);
}
}
// fix start and end locations of visited nodes
@Override
public void defaultPostVisit(IASTNode n) {
fixPosition(n,n.getLeft());
fixPosition(n,n.getRight());
}
// skip all nodes that do not span over the fixOffset
@Override
public boolean defaultPreVisit(IASTNode n) {
// if (n.getRight().getOffset()<fixAfterOffset) return false;
return true;
}
}
......@@ -52,6 +52,7 @@ public class MJDocumentListener implements IDocumentListener {
int removedLength = event.getLength();
int linesAdded = 0;
int addedLastLineColumns = 0;
int fixColumnUntilOffset = 0;
try {
int startLineNumber = document.getLineOfOffset(offset);
int endOffset = offset + textLength;
......@@ -59,7 +60,9 @@ public class MJDocumentListener implements IDocumentListener {
addedLastLineColumns = endOffset
- document.getLineOffset(endLineNumber);
linesAdded = endLineNumber - startLineNumber;
fixColumnUntilOffset = document.getLineInformationOfOffset(offset).getLength()+document.getLineInformationOfOffset(offset).getOffset();
System.out.println("PROCESSING DocumentEvent: ");
System.out.println(" Offset: " + offset);
System.out.println(" Replaced: " + event.getLength());
......@@ -73,17 +76,14 @@ public class MJDocumentListener implements IDocumentListener {
}
// NOTE: we do not try to fix the replaced part.
int fixColumnUntilOffset = removedLength;
int fixAfterOffset = offset + removedLength;
int diffOffset = textLength - removedLength;
int diffLines = linesAdded - linesRemoved;
int diffColumns = addedLastLineColumns - removedLastLineColumns;
//LocationPatchVisitor visitor = new LocationPatchVisitor(fixAfterOffset,
// fixColumnUntilOffset, diffLines, diffColumns, diffOffset);
//ast.accept(visitor, null);
LocationPatchVisitor visitor = new LocationPatchVisitor(fixAfterOffset,
fixColumnUntilOffset, diffLines, diffColumns, diffOffset);
ast.program.accept(visitor);
}
@Override
public void documentChanged(DocumentEvent event) {
......
......@@ -14,6 +14,7 @@ import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
......@@ -69,7 +70,13 @@ public class MJEditor extends TextEditor implements IMJParserChangeObserver,IReg
super();
setSourceViewerConfiguration(new MJSourceViewerConfiguration(this));
}
public void beginCompoundChange() {
((SourceViewer) getSourceViewer()).getUndoManager().beginCompoundChange();
}
public void endCompoundChange() {
((SourceViewer) getSourceViewer()).getUndoManager().endCompoundChange();
}
@Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
......
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