1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 23:05:47 +02:00

Fix for Bug 171796 - Make external translation units editable

This commit is contained in:
Anton Leherbauer 2007-01-29 15:19:17 +00:00
parent f405ef7559
commit b2b9d11098
11 changed files with 179 additions and 223 deletions

View file

@ -257,10 +257,8 @@ public class CModelBuilder2 implements IContributedModelBuilder {
((ASTHolderTUInfo)elementInfo).fAST= ast; ((ASTHolderTUInfo)elementInfo).fAST= ast;
} }
if (ast == null) {
checkCanceled(); checkCanceled();
// fallback to old model builder if (ast == null) {
new CModelBuilder(fTranslationUnit, new HashMap()).parse(true);
return; return;
} }
startTime= System.currentTimeMillis(); startTime= System.currentTimeMillis();
@ -556,9 +554,11 @@ public class CModelBuilder2 implements IContributedModelBuilder {
IASTDeclaration[] declarations= linkageDeclaration.getDeclarations(); IASTDeclaration[] declarations= linkageDeclaration.getDeclarations();
for (int i= 0; i < declarations.length; i++) { for (int i= 0; i < declarations.length; i++) {
IASTDeclaration declaration= declarations[i]; IASTDeclaration declaration= declarations[i];
if (linkageDeclaration.getFileLocation() != null || isLocalToFile(declaration)) {
createDeclaration(parent, declaration); createDeclaration(parent, declaration);
} }
} }
}
private CElement[] createSimpleDeclarations(Parent parent, IASTSimpleDeclaration declaration, boolean isTemplate) throws CModelException, DOMException { private CElement[] createSimpleDeclarations(Parent parent, IASTSimpleDeclaration declaration, boolean isTemplate) throws CModelException, DOMException {
final IASTDeclSpecifier declSpecifier= declaration.getDeclSpecifier(); final IASTDeclSpecifier declSpecifier= declaration.getDeclSpecifier();
@ -661,9 +661,11 @@ public class CModelBuilder2 implements IContributedModelBuilder {
IASTDeclaration[] nsDeclarations= declaration.getDeclarations(); IASTDeclaration[] nsDeclarations= declaration.getDeclarations();
for (int i= 0; i < nsDeclarations.length; i++) { for (int i= 0; i < nsDeclarations.length; i++) {
IASTDeclaration nsDeclaration= nsDeclarations[i]; IASTDeclaration nsDeclaration= nsDeclarations[i];
if (declaration.getFileLocation() != null || isLocalToFile(nsDeclaration)) {
createDeclaration(element, nsDeclaration); createDeclaration(element, nsDeclaration);
} }
} }
}
private StructureDeclaration createElaboratedTypeDeclaration(Parent parent, IASTElaboratedTypeSpecifier elaboratedTypeSpecifier, boolean isTemplate) throws CModelException{ private StructureDeclaration createElaboratedTypeDeclaration(Parent parent, IASTElaboratedTypeSpecifier elaboratedTypeSpecifier, boolean isTemplate) throws CModelException{
// create element // create element
@ -852,8 +854,10 @@ public class CModelBuilder2 implements IContributedModelBuilder {
final IASTDeclaration[] memberDeclarations= compositeTypeSpecifier.getMembers(); final IASTDeclaration[] memberDeclarations= compositeTypeSpecifier.getMembers();
for (int i= 0; i < memberDeclarations.length; i++) { for (int i= 0; i < memberDeclarations.length; i++) {
IASTDeclaration member= memberDeclarations[i]; IASTDeclaration member= memberDeclarations[i];
if (compositeTypeSpecifier.getFileLocation() != null || isLocalToFile(member)) {
createDeclaration(element, member); createDeclaration(element, member);
} }
}
} finally { } finally {
popDefaultVisibility(); popDefaultVisibility();
} }
@ -1219,6 +1223,7 @@ public class CModelBuilder2 implements IContributedModelBuilder {
private void setIdentifierPosition(SourceManipulation element, IASTNode astName) { private void setIdentifierPosition(SourceManipulation element, IASTNode astName) {
final IASTFileLocation location= astName.getFileLocation(); final IASTFileLocation location= astName.getFileLocation();
if (location != null) { if (location != null) {
assert fTranslationUnitFileName.equals(location.getFileName());
element.setIdPos(location.getNodeOffset(), location.getNodeLength()); element.setIdPos(location.getNodeOffset(), location.getNodeLength());
} else { } else {
final IASTNodeLocation[] locations= astName.getNodeLocations(); final IASTNodeLocation[] locations= astName.getNodeLocations();

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2005 QNX Software Systems and others. * Copyright (c) 2000, 2007 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* QNX Software Systems - Initial API and implementation * QNX Software Systems - Initial API and implementation
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.model; package org.eclipse.cdt.internal.core.model;
@ -41,7 +42,13 @@ public class CreateWorkingCopyOperation extends CModelOperation {
protected void executeOperation() throws CModelException { protected void executeOperation() throws CModelException {
ITranslationUnit tu = getTranslationUnit(); ITranslationUnit tu = getTranslationUnit();
WorkingCopy workingCopy = new WorkingCopy(tu.getParent(), (IFile)tu.getResource(), tu.getContentTypeId(), this.factory, this.problemRequestor); WorkingCopy workingCopy;
if (tu.getResource() != null) {
workingCopy= new WorkingCopy(tu.getParent(), (IFile)tu.getResource(), tu.getContentTypeId(), this.factory, this.problemRequestor);
} else {
workingCopy= new WorkingCopy(tu.getParent(), tu.getLocation(), tu.getContentTypeId(), this.factory);
}
// open the working copy now to ensure contents are that of the current state of this element // open the working copy now to ensure contents are that of the current state of this element
// Alain: Actually no, delay the parsing 'till it is really needed. Doing the parsing here // Alain: Actually no, delay the parsing 'till it is really needed. Doing the parsing here
// really slows down the opening of the CEditor. // really slows down the opening of the CEditor.

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2005 QNX Software Systems and others. * Copyright (c) 2000, 2007 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -7,79 +7,24 @@
* *
* Contributors: * Contributors:
* QNX Software Systems - Initial API and implementation * QNX Software Systems - Initial API and implementation
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.model; package org.eclipse.cdt.internal.core.model;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.IBuffer;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
/** /**
* ExternalTranslationUnit * ExternalTranslationUnit
*/ */
public class ExternalTranslationUnit extends TranslationUnit { public class ExternalTranslationUnit extends TranslationUnit {
IPath fPath;
/** /**
* @param parent * @param parent
* @param path * @param path
*/ */
public ExternalTranslationUnit(ICElement parent, IPath path, String contentTypeID) { public ExternalTranslationUnit(ICElement parent, IPath path, String contentTypeID) {
super(parent, (IResource)null, path.toString(), contentTypeID); super(parent, path, contentTypeID);
fPath = path;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.model.Openable#openBuffer(org.eclipse.core.runtime.IProgressMonitor)
*/
protected IBuffer openBuffer(IProgressMonitor pm) throws CModelException {
// create buffer - translation units only use default buffer factory
BufferManager bufManager = getBufferManager();
IBuffer buffer = getBufferFactory().createBuffer(this);
if (buffer == null)
return null;
// set the buffer source
if (buffer.getCharacters() == null){
IPath path = this.getPath();
File file = path.toFile();
if (file != null && file.isFile()) {
try {
InputStream stream = new FileInputStream(file);
buffer.setContents(Util.getInputStreamAsCharArray(stream, (int)file.length(), null));
} catch (IOException e) {
buffer.setContents(new char[0]);
}
} else {
buffer.setContents(new char[0]);
}
}
// add buffer to buffer cache
bufManager.addBuffer(buffer);
// listen to buffer changes
buffer.addBufferChangedListener(this);
return buffer;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.model.ICElement#getPath()
*/
public IPath getPath() {
return fPath;
} }
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2006 QNX Software Systems and others. * Copyright (c) 2000, 2007 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -9,10 +9,14 @@
* QNX Software Systems - Initial API and implementation * QNX Software Systems - Initial API and implementation
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
* IBM Corporation * IBM Corporation
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.model; package org.eclipse.cdt.internal.core.model;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -81,13 +85,9 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
} }
public TranslationUnit(ICElement parent, IPath path, String idType) { public TranslationUnit(ICElement parent, IPath path, String idType) {
super(parent, path, ICElement.C_UNIT); super(parent, (IResource)null, path.toString(), ICElement.C_UNIT);
setContentTypeID(idType);
}
public TranslationUnit(ICElement parent, IResource res, String name, String idType) {
super(parent, res, name, ICElement.C_UNIT);
setContentTypeID(idType); setContentTypeID(idType);
setLocation(path);
} }
public ITranslationUnit getTranslationUnit() { public ITranslationUnit getTranslationUnit() {
@ -280,7 +280,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
return (INamespace[]) aList.toArray(new INamespace[0]); return (INamespace[]) aList.toArray(new INamespace[0]);
} }
public void setLocation(IPath loc) { protected void setLocation(IPath loc) {
location = loc; location = loc;
} }
@ -296,6 +296,10 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
return location; return location;
} }
public IPath getPath() {
return getLocation();
}
public IFile getFile() { public IFile getFile() {
IResource res = getResource(); IResource res = getResource();
if (res instanceof IFile) { if (res instanceof IFile) {
@ -454,7 +458,13 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
} }
public IWorkingCopy getWorkingCopy(IProgressMonitor monitor, IBufferFactory factory)throws CModelException{ public IWorkingCopy getWorkingCopy(IProgressMonitor monitor, IBufferFactory factory)throws CModelException{
WorkingCopy workingCopy = new WorkingCopy(getParent(), getFile(), getContentTypeId(), factory); WorkingCopy workingCopy;
IFile file= getFile();
if (file != null) {
workingCopy= new WorkingCopy(getParent(), file, getContentTypeId(), factory);
} else {
workingCopy= new WorkingCopy(getParent(), getLocation(), getContentTypeId(), factory);
}
// open the working copy now to ensure contents are that of the current state of this element // open the working copy now to ensure contents are that of the current state of this element
workingCopy.open(monitor); workingCopy.open(monitor);
return workingCopy; return workingCopy;
@ -549,9 +559,22 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
// set the buffer source // set the buffer source
if (buffer.getCharacters() == null) { if (buffer.getCharacters() == null) {
IResource file = this.getResource(); IResource resource = this.getResource();
if (file != null && file.getType() == IResource.FILE) { if (resource != null && resource.getType() == IResource.FILE) {
buffer.setContents(Util.getResourceContentsAsCharArray((IFile)file)); buffer.setContents(Util.getResourceContentsAsCharArray((IFile)resource));
} else {
IPath path = this.getLocation();
java.io.File file = path.toFile();
if (file != null && file.isFile()) {
try {
InputStream stream = new FileInputStream(file);
buffer.setContents(Util.getInputStreamAsCharArray(stream, (int)file.length(), null));
} catch (IOException e) {
buffer.setContents(new char[0]);
}
} else {
buffer.setContents(new char[0]);
}
} }
} }
@ -657,7 +680,10 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
IResource res = getResource(); IResource res = getResource();
if (res != null) if (res != null)
return res.exists(); return res.exists();
return super.exists(); if (location != null) {
return location.toFile().exists();
}
return false;
} }
public ILanguage getLanguage() throws CoreException { public ILanguage getLanguage() throws CoreException {

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2002, 2006 IBM Corporation and others. * Copyright (c) 2002, 2007 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -8,6 +8,7 @@
* Contributors: * Contributors:
* Rational Software - Initial API and implementation * Rational Software - Initial API and implementation
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.model; package org.eclipse.cdt.internal.core.model;
@ -157,7 +158,11 @@ public class Util implements ICLogConstants {
*/ */
public static char[] getResourceContentsAsCharArray(IFile file) public static char[] getResourceContentsAsCharArray(IFile file)
throws CModelException { throws CModelException {
return getResourceContentsAsCharArray(file, null); try {
return getResourceContentsAsCharArray(file, file.getCharset());
} catch (CoreException exc) {
throw new CModelException(exc, ICModelStatusConstants.CORE_EXCEPTION);
}
} }
public static char[] getResourceContentsAsCharArray(IFile file, public static char[] getResourceContentsAsCharArray(IFile file,

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2002, 2006 IBM Corporation and others. * Copyright (c) 2002, 2007 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -8,6 +8,7 @@
* Contributors: * Contributors:
* Rational Software - Initial API and implementation * Rational Software - Initial API and implementation
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.model; package org.eclipse.cdt.internal.core.model;
@ -219,8 +220,12 @@ public class WorkingCopy extends TranslationUnit implements IWorkingCopy {
* @see org.eclipse.cdt.core.model.IWorkingCopy#getOriginalElement() * @see org.eclipse.cdt.core.model.IWorkingCopy#getOriginalElement()
*/ */
public ITranslationUnit getOriginalElement() { public ITranslationUnit getOriginalElement() {
IFile file= getFile();
if (file != null) {
return new TranslationUnit(getParent(), getFile(), getContentTypeId()); return new TranslationUnit(getParent(), getFile(), getContentTypeId());
} }
return new ExternalTranslationUnit(getParent(), getLocation(), getContentTypeId());
}
/** /**
* @see org.eclipse.cdt.core.model.ITranslationUnit#getSharedWorkingCopy(IProgressMonitor, IBufferFactory) * @see org.eclipse.cdt.core.model.ITranslationUnit#getSharedWorkingCopy(IProgressMonitor, IBufferFactory)
@ -288,8 +293,9 @@ public class WorkingCopy extends TranslationUnit implements IWorkingCopy {
// this.problemRequestor.endReporting(); // this.problemRequestor.endReporting();
//} //}
} }
/**
* @see org.eclipse.cdt.internal.core.model.CFile#openBuffer(IProgressMonitor) /*
* @see org.eclipse.cdt.internal.core.model.TranslationUnit#openBuffer(org.eclipse.core.runtime.IProgressMonitor)
*/ */
protected IBuffer openBuffer(IProgressMonitor pm) throws CModelException { protected IBuffer openBuffer(IProgressMonitor pm) throws CModelException {
@ -301,7 +307,7 @@ public class WorkingCopy extends TranslationUnit implements IWorkingCopy {
return null; return null;
// set the buffer source if needed // set the buffer source if needed
if (buffer.getCharacters() == null){ if (buffer.getContents() == null){
ITranslationUnit original= this.getOriginalElement(); ITranslationUnit original= this.getOriginalElement();
IBuffer originalBuffer = null; IBuffer originalBuffer = null;
try { try {

View file

@ -11,11 +11,15 @@
package org.eclipse.cdt.ui.tests.text; package org.eclipse.cdt.ui.tests.text;
import java.io.File;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.text.TextSelection;
@ -24,11 +28,18 @@ import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.PartInitException; import org.eclipse.ui.PartInitException;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.testplugin.CProjectHelper; import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.ui.tests.BaseUITestCase; import org.eclipse.cdt.ui.tests.BaseUITestCase;
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.util.EditorUtility;
/** /**
* Basic CEditor tests. * Basic CEditor tests.
@ -74,9 +85,19 @@ public class BasicCEditorTest extends BaseUITestCase {
assertNotNull(fDocument); assertNotNull(fDocument);
} }
private void setUpEditor(File file) throws PartInitException, CModelException {
fEditor= (CEditor) EditorUtility.openInEditor(new ExternalTranslationUnit(fCProject, Path.fromOSString(file.toString()), CCorePlugin.CONTENT_TYPE_CXXSOURCE));
assertNotNull(fEditor);
fTextWidget= fEditor.getViewer().getTextWidget();
assertNotNull(fTextWidget);
fAccessor= new Accessor(fTextWidget, StyledText.class);
fDocument= fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
assertNotNull(fDocument);
}
public void testEditInNonCProject() throws Exception { public void testEditInNonCProject() throws Exception {
final String file= "/ceditor/src/main.cpp"; final String file= "/ceditor/src/main.cpp";
fNonCProject = EditorTestHelper.createNonCProject("ceditor", "resources/ceditor", false); fNonCProject= EditorTestHelper.createNonCProject("ceditor", "resources/ceditor", false);
setUpEditor(file); setUpEditor(file);
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor); fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100)); assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100));
@ -96,6 +117,46 @@ public class BasicCEditorTest extends BaseUITestCase {
assertEquals("Save failed", newContent, content); assertEquals("Save failed", newContent, content);
} }
public void testEditExternalTranslationUnit() throws Exception {
final String file= "/ceditor/src/main.cpp";
fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false);
IFile mainFile= ResourceTestHelper.findFile(file);
assertNotNull(mainFile);
File tmpFile= File.createTempFile("tmp", ".cpp");
tmpFile.deleteOnExit();
FileTool.copy(mainFile.getLocation().toFile(), tmpFile);
setUpEditor(tmpFile);
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100));
String content= fDocument.get();
setCaret(0);
String newtext= "/* "+getName()+" */";
type(newtext);
type('\n');
String newContent= fDocument.get();
assertEquals("Edit failed", newtext, newContent.substring(0, newtext.length()));
// save
fEditor.doSave(new NullProgressMonitor());
// close and reopen
EditorTestHelper.closeEditor(fEditor);
setUpEditor(tmpFile);
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100));
content= fDocument.get();
assertEquals("Save failed", newContent, content);
// check reconciler
ITranslationUnit tUnit= (ITranslationUnit)fEditor.getInputCElement();
ICElement[] children= tUnit.getChildren();
assertEquals(2, children.length);
setCaret(content.length());
type('\n');
type("void func() {}\n");
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100));
children= tUnit.getChildren();
assertEquals(3, children.length);
tmpFile.delete();
}
/** /**
* Type characters into the styled text. * Type characters into the styled text.
* *

View file

@ -1,47 +0,0 @@
/*******************************************************************************
* Copyright (c) 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* QNX Software System
*******************************************************************************/
package org.eclipse.cdt.internal.ui;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.IBuffer;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.core.model.WorkingCopy;
import org.eclipse.core.runtime.CoreException;
public class CFileElementWorkingCopy extends WorkingCopy {
ITranslationUnit unit;
/**
* Creates a working copy of this element
*/
public CFileElementWorkingCopy(ITranslationUnit unit) throws CoreException {
super(unit.getParent(), unit.getPath(), unit.getContentTypeId(), null);
this.unit = unit;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.model.IOpenable#getBuffer()
*/
public IBuffer getBuffer() throws CModelException {
return unit.getBuffer();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.model.IWorkingCopy#getOriginalElement()
*/
public ITranslationUnit getOriginalElement() {
return unit;
}
}

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2002, 2006 QNX Software Systems and others. * Copyright (c) 2002, 2007 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -71,7 +71,6 @@ import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.cdt.internal.core.model.IBufferFactory; import org.eclipse.cdt.internal.core.model.IBufferFactory;
import org.eclipse.cdt.internal.ui.CFileElementWorkingCopy;
import org.eclipse.cdt.internal.ui.CPluginImages; import org.eclipse.cdt.internal.ui.CPluginImages;
import org.eclipse.cdt.internal.ui.text.IProblemRequestorExtension; import org.eclipse.cdt.internal.ui.text.IProblemRequestorExtension;
@ -240,7 +239,7 @@ public class CDocumentProvider extends TextFileDocumentProvider {
} }
/* /*
* @see org.eclipse.cdt.internal.ui.javaeditor.IJavaAnnotation#getOverlay() * @see org.eclipse.cdt.internal.ui.editor.IJavaAnnotation#getOverlay()
*/ */
public ICAnnotation getOverlay() { public ICAnnotation getOverlay() {
return null; return null;
@ -466,7 +465,7 @@ public class CDocumentProvider extends TextFileDocumentProvider {
} }
/* /*
* @see org.eclipse.jdt.internal.ui.text.java.IProblemRequestorExtension#beginReportingSequence() * @see org.eclipse.cdt.internal.ui.text.java.IProblemRequestorExtension#beginReportingSequence()
*/ */
public void beginReportingSequence() { public void beginReportingSequence() {
ProblemRequestorState state= (ProblemRequestorState) fProblemRequestorState.get(); ProblemRequestorState state= (ProblemRequestorState) fProblemRequestorState.get();
@ -513,7 +512,7 @@ public class CDocumentProvider extends TextFileDocumentProvider {
} }
/* /*
* @see org.eclipse.jdt.internal.ui.text.java.IProblemRequestorExtension#endReportingSequence() * @see org.eclipse.cdt.internal.ui.text.java.IProblemRequestorExtension#endReportingSequence()
*/ */
public void endReportingSequence() { public void endReportingSequence() {
ProblemRequestorState state= (ProblemRequestorState) fProblemRequestorState.get(); ProblemRequestorState state= (ProblemRequestorState) fProblemRequestorState.get();
@ -773,10 +772,6 @@ public class CDocumentProvider extends TextFileDocumentProvider {
/** Annotation model listener added to all created CU annotation models */ /** Annotation model listener added to all created CU annotation models */
private GlobalAnnotationModelListener fGlobalAnnotationModelListener; private GlobalAnnotationModelListener fGlobalAnnotationModelListener;
/** The save policy used by this provider */
//private ISavePolicy fSavePolicy;
/** /**
* *
*/ */
@ -860,14 +855,8 @@ public class CDocumentProvider extends TextFileDocumentProvider {
setUpSynchronization(tuInfo); setUpSynchronization(tuInfo);
IProblemRequestor requestor= tuInfo.fModel instanceof IProblemRequestor ? (IProblemRequestor) tuInfo.fModel : null; IProblemRequestor requestor= tuInfo.fModel instanceof IProblemRequestor ? (IProblemRequestor) tuInfo.fModel : null;
IWorkingCopy copy = null;
if (element instanceof IFileEditorInput) {
IBufferFactory factory = CUIPlugin.getDefault().getBufferFactory(); IBufferFactory factory = CUIPlugin.getDefault().getBufferFactory();
copy = original.getSharedWorkingCopy(getProgressMonitor(), factory, requestor); tuInfo.fCopy = original.getSharedWorkingCopy(getProgressMonitor(), factory, requestor);
} else if (element instanceof ITranslationUnitEditorInput) {
copy = new CFileElementWorkingCopy(original);
}
tuInfo.fCopy = copy;
if (tuInfo.fModel == null && element instanceof IStorageEditorInput) { if (tuInfo.fModel == null && element instanceof IStorageEditorInput) {
IStorage storage= ((IStorageEditorInput)element).getStorage(); IStorage storage= ((IStorageEditorInput)element).getStorage();
@ -892,38 +881,6 @@ public class CDocumentProvider extends TextFileDocumentProvider {
return tuInfo; return tuInfo;
} }
/*
* @see org.eclipse.ui.editors.text.TextFileDocumentProvider#isReadOnly(java.lang.Object)
*/
public boolean isReadOnly(Object element) {
// external translation unit must not be modified
// because of missing functionality in CFileElementWorkingCopy
FileInfo info= getFileInfo(element);
if (info instanceof TranslationUnitInfo) {
TranslationUnitInfo tuInfo= (TranslationUnitInfo)info;
if (tuInfo.fCopy instanceof CFileElementWorkingCopy) {
return true;
}
}
return super.isReadOnly(element);
}
/*
* @see org.eclipse.ui.editors.text.TextFileDocumentProvider#isModifiable(java.lang.Object)
*/
public boolean isModifiable(Object element) {
// external translation unit must not be modified
// because of missing functionality in CFileElementWorkingCopy
FileInfo info= getFileInfo(element);
if (info instanceof TranslationUnitInfo) {
TranslationUnitInfo tuInfo= (TranslationUnitInfo)info;
if (tuInfo.fCopy instanceof CFileElementWorkingCopy) {
return false;
}
}
return super.isModifiable(element);
}
/* /*
* @see org.eclipse.ui.editors.text.TextFileDocumentProvider#disposeFileInfo(java.lang.Object, * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#disposeFileInfo(java.lang.Object,
* org.eclipse.ui.editors.text.TextFileDocumentProvider.FileInfo) * org.eclipse.ui.editors.text.TextFileDocumentProvider.FileInfo)
@ -941,25 +898,16 @@ public class CDocumentProvider extends TextFileDocumentProvider {
protected void commitWorkingCopy(IProgressMonitor monitor, Object element, TranslationUnitInfo info, boolean overwrite) protected void commitWorkingCopy(IProgressMonitor monitor, Object element, TranslationUnitInfo info, boolean overwrite)
throws CoreException { throws CoreException {
synchronized (info.fCopy) {
info.fCopy.reconcile();
}
IDocument document= info.fTextFileBuffer.getDocument(); IDocument document= info.fTextFileBuffer.getDocument();
IResource resource= info.fCopy.getResource(); IResource resource= info.fCopy.getResource();
//Assert.isTrue(resource instanceof IFile);
if (resource instanceof IFile && !resource.exists()) { if (resource instanceof IFile && !resource.exists()) {
// underlying resource has been deleted, just recreate file, ignore the rest // underlying resource has been deleted, just recreate file, ignore the rest
createFileFromDocument(monitor, (IFile) resource, document); createFileFromDocument(monitor, (IFile) resource, document);
return; return;
} }
//if (fSavePolicy != null)
// fSavePolicy.preSave(info.fCopy);
try { try {
//info.fCopy.commit(overwrite, monitor);
commitFileBuffer(monitor, info, overwrite); commitFileBuffer(monitor, info, overwrite);
} catch (CoreException x) { } catch (CoreException x) {
// inform about the failure // inform about the failure
@ -969,17 +917,7 @@ public class CDocumentProvider extends TextFileDocumentProvider {
// inform about the failure // inform about the failure
fireElementStateChangeFailed(element); fireElementStateChangeFailed(element);
throw x; throw x;
} finally {
} }
// If here, the dirty state of the editor will change to "not dirty".
// Thus, the state changing flag will be reset.
// NOTE: this is done in commitFileBuffer() if we use info.fCopy.comit(...) reenable code
//if (info.fModel instanceof AbstractMarkerAnnotationModel) {
// AbstractMarkerAnnotationModel model= (AbstractMarkerAnnotationModel) info.fModel;
// model.updateMarkers(document);
//}
} }
/* /*
@ -1038,7 +976,7 @@ public class CDocumentProvider extends TextFileDocumentProvider {
} }
}; };
} }
return null; return super.createSaveOperation(element, document, overwrite);
} }
/** /**
@ -1063,24 +1001,14 @@ public class CDocumentProvider extends TextFileDocumentProvider {
} }
} }
/*
* @see org.eclipse.jdt.internal.ui.javaeditor.ITranlationUnitDocumentProvider#addGlobalAnnotationModelListener(org.eclipse.jface.text.source.IAnnotationModelListener)
*/
public void addGlobalAnnotationModelListener(IAnnotationModelListener listener) { public void addGlobalAnnotationModelListener(IAnnotationModelListener listener) {
fGlobalAnnotationModelListener.addListener(listener); fGlobalAnnotationModelListener.addListener(listener);
} }
/*
* @see org.eclipse.jdt.internal.ui.javaeditor.ITranslationUnitDocumentProvider#removeGlobalAnnotationModelListener(org.eclipse.jface.text.source.IAnnotationModelListener)
*/
public void removeGlobalAnnotationModelListener(IAnnotationModelListener listener) { public void removeGlobalAnnotationModelListener(IAnnotationModelListener listener) {
fGlobalAnnotationModelListener.removeListener(listener); fGlobalAnnotationModelListener.removeListener(listener);
} }
/*
* @see org.eclipse.jdt.internal.ui.javaeditor.ICompilationUnitDocumentProvider#getWorkingCopy(java.lang.Object)
*/
public IWorkingCopy getWorkingCopy(Object element) { public IWorkingCopy getWorkingCopy(Object element) {
FileInfo fileInfo = getFileInfo(element); FileInfo fileInfo = getFileInfo(element);
if (fileInfo instanceof TranslationUnitInfo) { if (fileInfo instanceof TranslationUnitInfo) {
@ -1090,11 +1018,8 @@ public class CDocumentProvider extends TextFileDocumentProvider {
return null; return null;
} }
/*
* @see org.eclipse.jdt.internal.ui.javaeditor.ICompilationUnitDocumentProvider#shutdown()
*/
public void shutdown() { public void shutdown() {
//CUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(fPropertyListener); // CUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(fPropertyListener);
Iterator e = getConnectedElementsIterator(); Iterator e = getConnectedElementsIterator();
while (e.hasNext()) while (e.hasNext())
disconnect(e.next()); disconnect(e.next());

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2002, 2006 QNX Software Systems and others. * Copyright (c) 2002, 2007 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -7,17 +7,21 @@
* *
* Contributors: * Contributors:
* QNX Software Systems - Initial API and implementation * QNX Software Systems - Initial API and implementation
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.editor; package org.eclipse.cdt.internal.ui.editor;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.cdt.core.model.IBuffer; import org.eclipse.cdt.core.model.IBuffer;
import org.eclipse.cdt.core.model.IOpenable; import org.eclipse.cdt.core.model.IOpenable;
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.cdt.internal.core.model.IBufferFactory; import org.eclipse.cdt.internal.core.model.IBufferFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
/** /**
* CustomBufferFactory * CustomBufferFactory
@ -45,6 +49,12 @@ public class CustomBufferFactory implements IBufferFactory {
return adapter; return adapter;
} }
// external file
IPath location= original.getLocation();
if (location != null) {
return new DocumentAdapter(owner, location);
}
} }
return DocumentAdapter.NULL; return DocumentAdapter.NULL;
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2002, 2006 IBM Corporation and others. * Copyright (c) 2002, 2007 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -7,11 +7,11 @@
* *
* Contributors: * Contributors:
* Rational Software - Initial API and implementation * Rational Software - Initial API and implementation
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.editor; package org.eclipse.cdt.internal.ui.editor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -40,6 +40,7 @@ import org.eclipse.jface.text.DefaultLineTracker;
import org.eclipse.jface.text.DocumentEvent; import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener; import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.ISynchronizable;
import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Display;
@ -181,17 +182,27 @@ public class DocumentAdapter implements IBuffer, IDocumentListener {
private List fBufferListeners= new ArrayList(3); private List fBufferListeners= new ArrayList(3);
private IStatus fStatus; private IStatus fStatus;
private IPath fLocation;
public DocumentAdapter(IOpenable owner, IFile file) { public DocumentAdapter(IOpenable owner, IFile file) {
fOwner= owner; fOwner= owner;
fFile= file; fFile= file;
fLocation= file.getFullPath();
initialize();
}
public DocumentAdapter(IOpenable owner, IPath location) {
fOwner= owner;
fLocation= location;
initialize(); initialize();
} }
private void initialize() { private void initialize() {
ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
IPath location= fFile.getFullPath(); IPath location= fLocation;
try { try {
manager.connect(location, new NullProgressMonitor()); manager.connect(location, new NullProgressMonitor());
fTextFileBuffer= manager.getTextFileBuffer(location); fTextFileBuffer= manager.getTextFileBuffer(location);
@ -199,6 +210,8 @@ public class DocumentAdapter implements IBuffer, IDocumentListener {
} catch (CoreException x) { } catch (CoreException x) {
fStatus= x.getStatus(); fStatus= x.getStatus();
fDocument= manager.createEmptyDocument(location); fDocument= manager.createEmptyDocument(location);
if (fDocument instanceof ISynchronizable)
((ISynchronizable)fDocument).setLockObject(new Object());
} }
fDocument.addPrenotifiedDocumentListener(this); fDocument.addPrenotifiedDocumentListener(this);
} }