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 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 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]; } }