diff --git a/core/org.eclipse.cdt.ui.tests/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.ui.tests/META-INF/MANIFEST.MF index 2fe1e1e20b4..4aff9b7610d 100644 --- a/core/org.eclipse.cdt.ui.tests/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.ui.tests/META-INF/MANIFEST.MF @@ -35,7 +35,8 @@ Require-Bundle: org.eclipse.jface.text, org.eclipse.core.expressions, org.eclipse.cdt.make.core, com.ibm.icu, - org.eclipse.ltk.core.refactoring;bundle-version="3.4.0" + org.eclipse.ltk.core.refactoring;bundle-version="3.4.0", + org.eclipse.core.filesystem;bundle-version="1.2.0" Bundle-ActivationPolicy: lazy Bundle-Vendor: Eclipse.org Bundle-RequiredExecutionEnvironment: J2SE-1.5 diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java index cf916745fb1..fb27d72a16a 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/BasicCEditorTest.java @@ -16,8 +16,11 @@ import java.io.File; import junit.framework.Test; import junit.framework.TestSuite; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.URIUtil; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jface.text.DocumentEvent; import org.eclipse.jface.text.IDocument; @@ -31,7 +34,10 @@ import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Event; import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.ide.IDE; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.model.CModelException; @@ -124,6 +130,19 @@ public class BasicCEditorTest extends BaseUITestCase { assertNotNull(fDocument); } + private void setUpEditorUsingFileStore(File file) throws CoreException { + final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + IEditorPart editor= IDE.openEditorOnFileStore(activePage, EFS.getStore(URIUtil.toURI(file.getAbsolutePath()))); + assertNotNull(editor); + assertTrue(editor instanceof CEditor); + fEditor= (CEditor) editor; + fTextWidget= fEditor.getViewer().getTextWidget(); + assertNotNull(fTextWidget); + fAccessor= new Accessor(fTextWidget, StyledText.class); + fDocument= fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); + assertNotNull(fDocument); + } + public void testEditTranslationUnit() throws Exception { final String file= "/ceditor/src/main.cpp"; fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false, false); @@ -302,6 +321,47 @@ public class BasicCEditorTest extends BaseUITestCase { tmpFile.delete(); } + public void testEditExternalTranslationUnitUsingFileStore() throws Exception { + final String file= "/ceditor/src/main.cpp"; + fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false, false); + IFile mainFile= ResourceTestHelper.findFile(file); + assertNotNull(mainFile); + File tmpFile= File.createTempFile("tmp", ".cpp"); + tmpFile.deleteOnExit(); + FileTool.copy(mainFile.getLocation().toFile(), tmpFile); + setUpEditorUsingFileStore(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()); + assertFalse("Editor is still dirty", fEditor.isDirty()); + // 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(); + } + public void testSyntaxHighlighting_Bug180433() throws Exception { CColorManager colorMgr= CUIPlugin.getDefault().getTextTools().getColorManager(); colorMgr.unbindColor(ICColorConstants.PP_DIRECTIVE); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java index 682f0780ea1..c2753c3b6a7 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java @@ -12,6 +12,7 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.editor; +import java.net.URI; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -51,6 +52,7 @@ import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.IStorageEditorInput; import org.eclipse.ui.editors.text.ForwardingDocumentProvider; import org.eclipse.ui.editors.text.ILocationProvider; +import org.eclipse.ui.editors.text.ILocationProviderExtension; import org.eclipse.ui.editors.text.TextFileDocumentProvider; import org.eclipse.ui.texteditor.IDocumentProvider; import org.eclipse.ui.texteditor.IMarkerUpdater; @@ -830,7 +832,11 @@ public class CDocumentProvider extends TextFileDocumentProvider { } else if (element instanceof IAdaptable) { IAdaptable adaptable= (IAdaptable)element; ILocationProvider locationProvider= (ILocationProvider)adaptable.getAdapter(ILocationProvider.class); - if (locationProvider != null) { + if (locationProvider instanceof ILocationProviderExtension) { + URI uri= ((ILocationProviderExtension)locationProvider).getURI(element); + original= createTranslationUnit(uri); + } + if (original == null && locationProvider != null) { IPath location= locationProvider.getPath(element); original= createTranslationUnit(location); } @@ -889,6 +895,22 @@ public class CDocumentProvider extends TextFileDocumentProvider { return null; } + /** + * Try to synthesize an ITranslationUnit out of thin air. + * @param uri the URU of the file in question + * @return a translation unit or null + */ + private ITranslationUnit createTranslationUnit(URI uri) { + if (uri == null) { + return null; + } + IEditorInput input= EditorUtility.getEditorInputForLocation(uri, null); + if (input instanceof ITranslationUnitEditorInput) { + return ((ITranslationUnitEditorInput)input).getTranslationUnit(); + } + return null; + } + /* * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#disposeFileInfo(java.lang.Object, * org.eclipse.ui.editors.text.TextFileDocumentProvider.FileInfo) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CustomBufferFactory.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CustomBufferFactory.java index 2c848e7aa89..c59d2834e2f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CustomBufferFactory.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CustomBufferFactory.java @@ -17,8 +17,6 @@ import java.net.URI; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; - import org.eclipse.cdt.core.model.IBuffer; import org.eclipse.cdt.core.model.IOpenable; import org.eclipse.cdt.core.model.ITranslationUnit; @@ -53,12 +51,6 @@ public class CustomBufferFactory implements IBufferFactory { return adapter; } - // external file - IPath location= original.getLocation(); - if (location != null) { - return new DocumentAdapter(owner, location); - } - // URI URI locationUri= original.getLocationURI(); if (locationUri != null) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ITranslationUnitEditorInput.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ITranslationUnitEditorInput.java index 5968e9ee989..27dff9d4523 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ITranslationUnitEditorInput.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/ITranslationUnitEditorInput.java @@ -1,24 +1,26 @@ /******************************************************************************* - * Copyright (c) 2002, 2006 QNX Software Systems and others. + * Copyright (c) 2002, 2008 QNX Software Systems 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: - * QNX Software Systems - Initial API and implementation + * QNX Software Systems - Initial API and implementation *******************************************************************************/ package org.eclipse.cdt.internal.ui.editor; -import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.ui.IStorageEditorInput; import org.eclipse.ui.editors.text.ILocationProvider; +import org.eclipse.ui.editors.text.ILocationProviderExtension; + +import org.eclipse.cdt.core.model.ITranslationUnit; /** * ITranslationUnitEditorInput */ -public interface ITranslationUnitEditorInput extends IStorageEditorInput, ILocationProvider { +public interface ITranslationUnitEditorInput extends IStorageEditorInput, ILocationProvider, ILocationProviderExtension { ITranslationUnit getTranslationUnit(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/ExternalEditorInput.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/ExternalEditorInput.java index a85bab9f0ac..f20afe18b4b 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/ExternalEditorInput.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/ExternalEditorInput.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2007 QNX Software Systems and others. + * Copyright (c) 2000, 2008 QNX Software Systems 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 @@ -12,6 +12,9 @@ package org.eclipse.cdt.internal.ui.util; +import java.net.URI; + +import org.eclipse.core.filesystem.URIUtil; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IStorage; import org.eclipse.core.runtime.CoreException; @@ -26,6 +29,7 @@ import org.eclipse.ui.PlatformUI; import org.eclipse.ui.editors.text.ILocationProvider; import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.core.resources.EFSFileStorage; import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput; @@ -172,5 +176,19 @@ public class ExternalEditorInput implements ITranslationUnitEditorInput, IPersis public void saveState(IMemento memento) { ExternalEditorInputFactory.saveState(memento, this); } + + /* + * @see org.eclipse.ui.editors.text.ILocationProviderExtension#getURI(java.lang.Object) + */ + public URI getURI(Object element) { + if (externalFile instanceof EFSFileStorage) { + return ((EFSFileStorage) externalFile).getLocationURI(); + } + IPath location = getPath(element); + if (location != null) { + return URIUtil.toURI(location); + } + return null; + } }