package org.autoplot.jythonsupport.ui; import java.util.Collections; import java.util.List; import javax.swing.text.Element; import org.autoplot.jythonsupport.StaticCodeAnalysis; import org.python.parser.SimpleNode; /** * See also https://github.com/autoplot/dev/blob/master/rfe/sf/394/renameSymbol.jy * @author jbf */ public class RefactorRenameVariable extends javax.swing.JPanel { EditorTextPane editor; /** * Creates new form RefactorRenameVariable * @param editor */ public RefactorRenameVariable( EditorTextPane editor ) { initComponents(); this.editor = editor; String var= editor.getSelectedText(); if ( var==null || var.length()==0 ) { var= EditorAnnotationsSupport.getSymbolAt( editor, editor.getCaretPosition() ); } oldNameTF.setText(var); oldNameTF.setEditable(false); } public String refactorVarRename( String script, int i, String oldName, String newName ) { int oldLen = oldName.length(); List usages = StaticCodeAnalysis.showUsage( script,oldName ); Collections.reverse(usages); for ( SimpleNode n : usages ) { Element root = editor.getDocument().getDefaultRootElement(); int i0; if ( n.beginLine > 0 ) { i0 = root.getElement(n.beginLine-1).getStartOffset(); } else { i0 = 0; } i0 = i0 + (n.beginColumn-1); System.err.println("replace at "+i0); script= script.substring(0,i0) + newName + script.substring(i0+oldLen); } return script; } public void refactorVarRename( ) { String script = this.editor.getText(); String oldName= oldNameTF.getText(); String newName= newNameTF.getText(); String newScript= refactorVarRename( script, 0, oldName, newName ); this.editor.setText(newScript); } /** * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of * this method is always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); oldNameTF = new javax.swing.JTextField(); newNameTF = new javax.swing.JTextField(); jLabel1.setText("Replace each instance where the name is used with the name. Global/Local context is not supported."); jLabel2.setText("Old Symbol:"); jLabel3.setText("New Symbol:"); oldNameTF.setText(" "); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addComponent(jLabel3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(oldNameTF, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE) .addComponent(newNameTF)))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {newNameTF, oldNameTF}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(oldNameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(newNameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(34, Short.MAX_VALUE)) ); }// //GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JTextField newNameTF; private javax.swing.JTextField oldNameTF; // End of variables declaration//GEN-END:variables }