162 lines
5.4 KiB
Java
162 lines
5.4 KiB
Java
|
package pyUML.refactoring;
|
||
|
|
||
|
import java.io.File;
|
||
|
|
||
|
|
||
|
import pyUML.backend.ParseHelpers;
|
||
|
import pyUML.pythonTree.PythonTreeFile;
|
||
|
|
||
|
/**
|
||
|
* Some convenience methods for modifying/writing files
|
||
|
*/
|
||
|
public class FileRefactoring {
|
||
|
|
||
|
|
||
|
/**
|
||
|
* replaces a string in a file from a given line/col
|
||
|
* deletes the file from that position unto
|
||
|
*
|
||
|
* @param pyFile the python file to edit
|
||
|
* @param startLine the line, from which the replace should begin
|
||
|
* @param startCol the column, from which the replace should begin
|
||
|
* @param newString the new String to replace the deleted with
|
||
|
* @param endMarker the char/string, unto the file content is replaces
|
||
|
* @param reInit if true, a reInit() is called on the python file after finishing
|
||
|
* @return
|
||
|
*/
|
||
|
public static boolean replaceFromCoordinate(PythonTreeFile pyFile, int startLine, int startCol, String newString, String endMarker, boolean reInit){
|
||
|
String content = pyFile.getFileContent();
|
||
|
int lineIndex = 0;
|
||
|
for (int i=1; i<startLine; i++) {
|
||
|
lineIndex = content.indexOf('\n' ,lineIndex)+1;
|
||
|
}
|
||
|
int startIndex = lineIndex + startCol -1;
|
||
|
int endIndex = content.indexOf(endMarker, startIndex);
|
||
|
String newContent = content.substring(0, startIndex) + newString + content.substring(endIndex);
|
||
|
ParseHelpers.stringToFile(new File(pyFile.getFilePath().toOSString()), newContent, pyFile.getProject());
|
||
|
pyFile.setFileContent(newContent);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* inserts the given string at the beginning of the given line
|
||
|
*
|
||
|
* @param pyFile
|
||
|
* @param insertLine
|
||
|
* @param newString
|
||
|
* @return success
|
||
|
*/
|
||
|
public static boolean insertAtLine(PythonTreeFile pyFile, int insertLine, String newString){
|
||
|
String content = pyFile.getFileContent();
|
||
|
int lineIndex = 0;
|
||
|
for (int i=1; i<insertLine; i++) {
|
||
|
lineIndex = content.indexOf('\n' ,lineIndex)+1;
|
||
|
}
|
||
|
String newContent = content.substring(0, lineIndex) + newString + content.substring(lineIndex);
|
||
|
ParseHelpers.stringToFile(new File(pyFile.getFilePath().toOSString()), newContent, pyFile.getProject());
|
||
|
pyFile.setFileContent(newContent);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* completely replace a given line in a python file
|
||
|
* @param pyFile
|
||
|
* @param lineNo
|
||
|
* @param newString
|
||
|
* @param writeToFile if true, the changes are directly written to the python file
|
||
|
* @return the String with the replaced line
|
||
|
*/
|
||
|
public static String replaceLine(PythonTreeFile pyFile, int lineNo, String newString, boolean writeToFile) {
|
||
|
String content = pyFile.getFileContent();
|
||
|
int lineIndex = 0;
|
||
|
for (int i=1; i<lineNo; i++) {
|
||
|
lineIndex = content.indexOf('\n' ,lineIndex)+1;
|
||
|
}
|
||
|
int lineIndexAfter = content.indexOf('\n' ,lineIndex);
|
||
|
|
||
|
String contentBefore = content.substring(0, lineIndex);
|
||
|
String newContent = contentBefore + newString;
|
||
|
if (lineIndexAfter > lineIndex) {
|
||
|
String contentAfter = content.substring(lineIndexAfter);
|
||
|
newContent += contentAfter;
|
||
|
}
|
||
|
if (writeToFile) {
|
||
|
ParseHelpers.stringToFile(new File(pyFile.getFilePath().toOSString()), newContent, pyFile.getProject());
|
||
|
pyFile.updateFileInEclipse();
|
||
|
}
|
||
|
pyFile.setFileContent(newContent);
|
||
|
|
||
|
return newContent;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* completely removes a given line in a python file
|
||
|
* @param pyFile
|
||
|
* @param lineNo The lineNo to deletes
|
||
|
* @param writeToFile if true, the changes are directly written to the python file
|
||
|
* @param onlyIfNotEmpty if true, only a non-empty-line is deleted (to preserve file design)
|
||
|
* @return the String with the replaced line
|
||
|
*/
|
||
|
public static String removeLine(PythonTreeFile pyFile, int lineNo, boolean writeToFile, boolean onlyIfNotEmpty) {
|
||
|
String content = pyFile.getFileContent();
|
||
|
int lineIndex = 0;
|
||
|
for (int i=1; i<lineNo+1; i++) {
|
||
|
lineIndex = content.indexOf('\n' ,lineIndex)+1;
|
||
|
}
|
||
|
int lineIndexAfter = content.indexOf('\n' ,lineIndex)+1;
|
||
|
|
||
|
String contentBefore = content.substring(0, lineIndex);
|
||
|
String newContent = contentBefore;
|
||
|
if (lineIndexAfter > lineIndex) {
|
||
|
String contentAfter = content.substring(lineIndexAfter);
|
||
|
newContent += contentAfter;
|
||
|
}
|
||
|
if (onlyIfNotEmpty) {
|
||
|
String lineToDelete = content.substring(lineIndex+1, lineIndexAfter );
|
||
|
if (lineToDelete.matches("[\\s]*"))
|
||
|
return content;
|
||
|
}
|
||
|
|
||
|
if (writeToFile) {
|
||
|
ParseHelpers.stringToFile(new File(pyFile.getFilePath().toOSString()), newContent, pyFile.getProject());
|
||
|
pyFile.updateFileInEclipse();
|
||
|
}
|
||
|
|
||
|
pyFile.setFileContent(newContent);
|
||
|
return newContent;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Appends a String to a PythonTreeFile and saves the file on disk
|
||
|
* @param pyFile
|
||
|
* @param stringToAppend
|
||
|
* @return
|
||
|
*/
|
||
|
public static boolean appendToFile(PythonTreeFile pyFile, String stringToAppend) {
|
||
|
String content = pyFile.getFileContent();
|
||
|
String newContent = content + stringToAppend;
|
||
|
ParseHelpers.stringToFile(new File(pyFile.getFilePath().toOSString()), newContent, pyFile.getProject());
|
||
|
pyFile.updateFileInEclipse();
|
||
|
pyFile.setFileContent(newContent);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the line with the given line number
|
||
|
* for this file. Line numbers start with 1, not with 0
|
||
|
* @param pyFile the PythonTreeFile which represents the file
|
||
|
* @param lineNo the lineNumber
|
||
|
* @return The searched line as a string, null if lineNo is
|
||
|
* not in index.
|
||
|
*/
|
||
|
public static String getLine(PythonTreeFile pyFile, int lineNo) {
|
||
|
String[] splittedFileContent = pyFile.getSplittedFileContent();
|
||
|
if (lineNo <= 0)
|
||
|
return null;
|
||
|
if (lineNo > splittedFileContent.length)
|
||
|
return null;
|
||
|
|
||
|
return splittedFileContent[lineNo - 1];
|
||
|
}
|
||
|
}
|