1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 10:16:03 +02:00

Fixes bug 112366; adds CModel notifications during rename refactoring.

This commit is contained in:
Markus Schorn 2006-07-06 09:20:05 +00:00
parent 7af727a38e
commit af6000c0e0

View file

@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Rational Software - Initial API and implementation * Rational Software - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.model; package org.eclipse.cdt.internal.core.model;
@ -18,8 +19,11 @@ import org.eclipse.cdt.core.model.ICModelStatus;
import org.eclipse.cdt.core.model.ICModelStatusConstants; import org.eclipse.cdt.core.model.ICModelStatusConstants;
import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.jobs.ISchedulingRule; import org.eclipse.core.runtime.jobs.ISchedulingRule;
/** /**
@ -72,38 +76,72 @@ public class CommitWorkingCopyOperation extends CModelOperation {
protected void executeOperation() throws CModelException { protected void executeOperation() throws CModelException {
try { try {
beginTask("workingCopy.commit", 2); //$NON-NLS-1$ beginTask("workingCopy.commit", 2); //$NON-NLS-1$
WorkingCopy copy = (WorkingCopy)getElementToProcess(); WorkingCopy wc = (WorkingCopy)getElementToProcess();
ITranslationUnit original = copy.getOriginalElement(); ITranslationUnit tu = wc.getOriginalElement();
// creates the delta builder (this remembers the content of the cu) // creates the delta builder (this remembers the content of the cu)
if (!original.isOpen()) { if (!tu.isOpen()) {
// force opening so that the delta builder can get the old info // force opening so that the delta builder can get the old info
original.open(null); tu.open(null);
} }
CElementDeltaBuilder deltaBuilder = new CElementDeltaBuilder(original); CElementDeltaBuilder deltaBuilder = new CElementDeltaBuilder(tu);
// save the cu // save the translation unit
IBuffer originalBuffer = original.getBuffer();
if (originalBuffer == null) return;
char[] originalContents = originalBuffer.getCharacters();
boolean hasSaved = false; boolean hasSaved = false;
try { IBuffer tuBuffer = tu.getBuffer();
IBuffer copyBuffer = copy.getBuffer(); IBuffer wcBuffer = wc.getBuffer();
if (copyBuffer == null) return; if (wcBuffer == null || tuBuffer == null) {
originalBuffer.setContents(copyBuffer.getCharacters()); return;
original.save(fMonitor, fForce); }
this.hasModifiedResource = true; ITextFileBuffer tuFileBuffer= null;
ITextFileBuffer wcFileBuffer= null;
if (tuBuffer instanceof IAdaptable) {
tuFileBuffer= (ITextFileBuffer) ((IAdaptable) tuBuffer).getAdapter(ITextFileBuffer.class);
}
if (wcBuffer instanceof IAdaptable) {
wcFileBuffer= (ITextFileBuffer) ((IAdaptable) wcBuffer).getAdapter(ITextFileBuffer.class);
}
if (wcFileBuffer != null) {
if (wcFileBuffer.equals(tuFileBuffer)) {
// working on the same buffer, saving the translation unit does the trick.
tu.save(fMonitor, fForce);
hasSaved= true; hasSaved= true;
} finally { }
else {
if (wcFileBuffer.getLocation().equals(tu.getPath())) {
char[] originalContents = tuBuffer.getCharacters();
try {
// save the file buffer of the working copy.
wcFileBuffer.commit(fMonitor, fForce);
// change the buffer of the translation unit.
tuBuffer.setContents(wcBuffer.getCharacters());
tu.makeConsistent(null);
hasSaved= true;
} catch (CoreException e) {
tuBuffer.setContents(originalContents);
throw new CModelException(e);
}
}
}
}
if (!hasSaved) { if (!hasSaved) {
// restore original buffer contents since something went wrong char[] originalContents = tuBuffer.getCharacters();
originalBuffer.setContents(originalContents); try {
tuBuffer.setContents(wcBuffer.getCharacters());
tu.save(fMonitor, fForce);
} catch (CModelException e) {
tuBuffer.setContents(originalContents);
throw e;
} }
} }
this.hasModifiedResource = true;
// make sure working copy is in sync // make sure working copy is in sync
copy.updateTimeStamp((TranslationUnit)original); wc.updateTimeStamp((TranslationUnit)tu);
copy.makeConsistent(this); wc.makeConsistent(this);
worked(1); worked(1);
if (deltaBuilder != null) { if (deltaBuilder != null) {