Skip to content

Commit

Permalink
Text file change changes.
Browse files Browse the repository at this point in the history
- Don't use documents.
- Workaround #359.
  • Loading branch information
khatchad committed Jun 10, 2024
1 parent c6b7e51 commit 3515e9d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.NullChange;
Expand Down Expand Up @@ -524,26 +523,23 @@ public Change createChange(IProgressMonitor pm) throws CoreException, OperationC
if (optimizableFunctions.isEmpty())
return new NullChange(Messages.NoFunctionsToOptimize);

Map<IDocument, Queue<TextEdit>> documentToEdits = new HashMap<>();
Map<IFile, Queue<TextEdit>> fileToEdits = new HashMap<>();

// for each optimizable function (i.e., those with transformations.
for (Function function : optimizableFunctions) {
// get the containing file.
IFile file = function.getContainingActualFile();

TextChange change = new TextFileChange(Messages.Name, function.getContainingActualFile());
change.setKeepPreviewEdits(true);

// get the edits for that document.
Queue<TextEdit> docEdits = documentToEdits.get(doc);
// get the edits for that file.
Queue<TextEdit> fileEdits = fileToEdits.get(file);

// if we don't have one yet.
if (docEdits == null) {
if (fileEdits == null) {
// create a new one.
docEdits = new PriorityQueue<>((x, y) -> x.getExclusiveEnd() - y.getExclusiveEnd());
fileEdits = new PriorityQueue<>((x, y) -> x.getExclusiveEnd() - y.getExclusiveEnd());

// store it in the map.
documentToEdits.put(doc, docEdits);
fileToEdits.put(file, fileEdits);
}

// transform the function.
Expand All @@ -556,16 +552,16 @@ public Change createChange(IProgressMonitor pm) throws CoreException, OperationC
}

// add the edit to the edits for the document.
docEdits.addAll(funcEdits);
fileEdits.addAll(funcEdits);
}

CompositeChange ret = new CompositeChange("Hybridize");

for (IDocument document : documentToEdits.keySet()) {
Queue<TextEdit> edits = documentToEdits.get(document);
for (IFile file : fileToEdits.keySet()) {
Queue<TextEdit> edits = fileToEdits.get(file);

if (!edits.isEmpty()) {
TextChange change = new DocumentChange(Messages.Name, document);
TextChange change = new TextFileChange(Messages.Name, file);
change.setKeepPreviewEdits(true);
change.setTextType("py");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringCore;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
Expand Down Expand Up @@ -108,7 +108,6 @@
import org.python.pydev.plugin.nature.PythonNature;
import org.python.pydev.refactoring.ast.PythonModuleManager;
import org.python.pydev.shared_core.io.FileUtils;
import org.python.pydev.shared_core.io.PyUnsupportedEncodingException;
import org.python.pydev.shared_core.parsing.BaseParser.ParseOutput;
import org.python.pydev.shared_core.preferences.InMemoryEclipsePreferences;
import org.python.pydev.shared_core.string.CoreTextSelection;
Expand Down Expand Up @@ -186,6 +185,8 @@ public int getInterpreterType() throws CoreException {

private static final String RUN_INPUT_TEST_FILE_KEY = "edu.cuny.hunter.hybridize.tests.runInput";

private static final String COMPARE_OUTPUT_TEST_FILE_KEY = "edu.cuny.hunter.hybridize.tests.compareOutput";

/**
* Add a module to the given {@link IPythonNature}.
*
Expand Down Expand Up @@ -482,6 +483,11 @@ public static void tearDown() {
*/
protected boolean runInputTestFile = Boolean.getBoolean(RUN_INPUT_TEST_FILE_KEY);

/**
* True iff the output test Python file should be compared.
*/
protected boolean compareOutputTestFile = Boolean.getBoolean(COMPARE_OUTPUT_TEST_FILE_KEY);

private Entry<SimpleNode, IDocument> createPythonNodeFromTestFile(String fileNameWithoutExtension)
throws IOException, MisconfigurationException {
return this.createPythonNodeFromTestFile(fileNameWithoutExtension, true);
Expand All @@ -507,6 +513,13 @@ public boolean shouldRunInputTestFile() {
return this.runInputTestFile;
}

/**
* True iff the output test Python file should be compared.
*/
public boolean shouldCompareOutputTestFile() {
return this.compareOutputTestFile;
}

@Override
public void genericafter() throws Exception {
// Do nothing.
Expand Down Expand Up @@ -613,16 +626,13 @@ public String getFileExtension() {

@Override
public IPath getFullPath() {
return new org.eclipse.core.runtime.Path(inputTestFile.getAbsolutePath());
// NOTE: This is incorrect when implemenng https://github.com/ponder-lab/Hybridize-Functions-Refactoring/issues/359.
return org.eclipse.core.runtime.Path.fromOSString(inputTestFile.getAbsolutePath());
}

@Override
public String getCharset(boolean checkImplicit) throws CoreException {
try {
return FileUtils.getPythonFileEncoding(document, this.getFullPath().toOSString());
} catch (PyUnsupportedEncodingException e) {
throw new CoreException(Status.error("Can't determine encoding.", e));
}
return System.getProperty("file.encoding");
}

@Override
Expand All @@ -646,20 +656,26 @@ public ResourceAttributes getResourceAttributes() {

ProcessorBasedRefactoring refactoring = new ProcessorBasedRefactoring(processor);

RefactoringStatus status = this.performRefactoringWithStatus(refactoring);
RefactoringStatus status = refactoring.checkAllConditions(new NullProgressMonitor());

if (processor.getFunctions().stream().map(Function::getStatus).allMatch(RefactoringStatus::isOK))
assertTrue(status.isOK());
else
assertFalse(status.isOK());

// check if there's an expected output.
File outputTestFile = this.getOutputTestFile(fileNameWithoutExtension);
// NOTE: Fix https://github.com/ponder-lab/Hybridize-Functions-Refactoring/issues/359 first.
if (this.shouldCompareOutputTestFile()) {
// check if there's an expected output.
File outputTestFile = this.getOutputTestFile(fileNameWithoutExtension);

if (outputTestFile.exists()) {
String expected = this.getFileContents(this.getOutputTestFileName(fileNameWithoutExtension));
String actual = document.get();
assertEqualLines(expected, actual);
if (outputTestFile.exists()) {
Change change = refactoring.createChange(new NullProgressMonitor());
this.performChange(change);

String expected = this.getFileContents(this.getOutputTestFileName(fileNameWithoutExtension));
String actual = document.get();
assertEqualLines(expected, actual);
}
}

return processor.getFunctions();
Expand Down

0 comments on commit 3515e9d

Please sign in to comment.