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 36f20575cc9..9f8efe8bd03 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 @@ -72,6 +72,7 @@ import org.eclipse.cdt.ui.PreferenceConstants; import org.eclipse.cdt.ui.text.ICPartitions; import org.eclipse.cdt.internal.core.model.IBufferFactory; +import org.eclipse.cdt.internal.core.model.TranslationUnit; import org.eclipse.cdt.internal.ui.text.IProblemRequestorExtension; import org.eclipse.cdt.internal.ui.text.spelling.CoreSpellingProblem; @@ -111,7 +112,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { SpellingAnnotation.TYPE : INDEXER_ANNOTATION_TYPE); if (problem instanceof IPersistableProblem) fMarkerType= ((IPersistableProblem) problem).getMarkerType(); - else + else fMarkerType= null; } @@ -195,7 +196,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { } /** - * Internal structure for mapping positions to some value. + * Internal structure for mapping positions to some value. * The reason for this specific structure is that positions can * change over time. Thus a lookup is based on value and not * on hash value. @@ -336,7 +337,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { private boolean fIsActive= false; private ReverseMap fReverseMap= new ReverseMap(); - private List fPreviouslyOverlaid= null; + private List fPreviouslyOverlaid= null; private List fCurrentlyOverlaid= new ArrayList(); @@ -422,7 +423,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { public void beginReporting() { ProblemRequestorState state= fProblemRequestorState.get(); if (state == null) - internalBeginReporting(false); + internalBeginReporting(false); } /* @@ -509,7 +510,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { fCurrentlyOverlaid= new ArrayList(); if (fGeneratedAnnotations.size() > 0) { - temporaryProblemsChanged= true; + temporaryProblemsChanged= true; removeAnnotations(fGeneratedAnnotations, false, true); fGeneratedAnnotations.clear(); } @@ -529,7 +530,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { try { ProblemAnnotation annotation= new ProblemAnnotation(problem, fTranslationUnit); - overlayMarkers(position, annotation); + overlayMarkers(position, annotation); addAnnotation(annotation, position, false); fGeneratedAnnotations.add(annotation); @@ -558,7 +559,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { CMarkerAnnotation annotation= e.next(); annotation.setOverlay(null); } - } + } } /** @@ -592,7 +593,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { * Tells this annotation model to collect temporary problems from now on. */ private void startCollectingProblems() { - fGeneratedAnnotations= new ArrayList(); + fGeneratedAnnotations= new ArrayList(); } /** @@ -642,7 +643,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { */ @Override @SuppressWarnings("unchecked") - protected void addAnnotation(Annotation annotation, Position position, boolean fireModelChanged) throws BadLocationException { + protected void addAnnotation(Annotation annotation, Position position, boolean fireModelChanged) throws BadLocationException { super.addAnnotation(annotation, position, fireModelChanged); synchronized (getLockObject()) { @@ -731,8 +732,8 @@ public class CDocumentProvider extends TextFileDocumentProvider { public void removeListener(IAnnotationModelListener listener) { fListenerList.remove(listener); - } - } + } + } /** Preference key for temporary problems */ private final static String HANDLE_TEMPORARY_PROBLEMS= PreferenceConstants.EDITOR_EVALUATE_TEMPORARY_PROBLEMS; @@ -740,10 +741,10 @@ public class CDocumentProvider extends TextFileDocumentProvider { /** Internal property changed listener */ private IPropertyChangeListener fPropertyListener; /** Annotation model listener added to all created CU annotation models */ - private GlobalAnnotationModelListener fGlobalAnnotationModelListener; + private GlobalAnnotationModelListener fGlobalAnnotationModelListener; /** - * + * */ public CDocumentProvider() { super(); @@ -787,12 +788,10 @@ public class CDocumentProvider extends TextFileDocumentProvider { } if (element == null) { // not in a source folder? - final IPath location= file.getLocation(); - if (location != null) { - ICProject cproject= CoreModel.getDefault().create(file.getProject()); - if (cproject != null) { - return CoreModel.getDefault().createTranslationUnitFrom(cproject, location); - } + ICProject cproject= CoreModel.getDefault().create(file.getProject()); + if (cproject != null) { + String contentTypeId= CoreModel.getRegistedContentTypeId(file.getProject(), file.getName()); + return new TranslationUnit(cproject, file, contentTypeId); } } return null; @@ -937,7 +936,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { //----------------------------------------------------------------------- //for people who do not want auto-modification of their files, //this flag will prevent addition of a newline unless the user - //explicitly sets the preference thru Window -> Preferences -> C/C++ -> Editor + //explicitly sets the preference thru Window -> Preferences -> C/C++ -> Editor // -> Appearance Tab -> Ensure newline end of file when saving if (PreferenceConstants.getPreferenceStore().getBoolean( PreferenceConstants.ENSURE_NEWLINE_AT_EOF)) { @@ -948,8 +947,8 @@ public class CDocumentProvider extends TextFileDocumentProvider { try { // we have to ensure that the length of the last line is 0. // this will also take care of empty files. empty files have - // only one line in them and the length of this one and only - // line is 0. + // only one line in them and the length of this one and only + // line is 0. // Thus we do not need to append an extra line separator to // empty files. int lastLineLength = document.getLineLength(lastLineIndex); @@ -959,10 +958,10 @@ public class CDocumentProvider extends TextFileDocumentProvider { } } catch (BadLocationException e) { } - } + } - // Remove trailing whitespace when saving. Triggered by the flag - // in Preferences -> C/C++ -> Editor + // Remove trailing whitespace when saving. Triggered by the flag + // in Preferences -> C/C++ -> Editor if (PreferenceConstants.getPreferenceStore().getBoolean( PreferenceConstants.REMOVE_TRAILING_WHITESPACE)) { try { @@ -989,7 +988,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { } } catch (BadLocationException e) { } - } + } final FileInfo info= getFileInfo(element); if (info instanceof TranslationUnitInfo) { @@ -1026,7 +1025,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { protected boolean isHandlingTemporaryProblems() { IPreferenceStore store= CUIPlugin.getDefault().getPreferenceStore(); return store.getBoolean(HANDLE_TEMPORARY_PROBLEMS); - } + } /** * Switches the state of problem acceptance according to the value in the preference store. diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java index 5cc11aceb1a..3189aea262e 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java @@ -195,7 +195,6 @@ import org.eclipse.cdt.internal.ui.actions.GoToNextPreviousMemberAction; import org.eclipse.cdt.internal.ui.actions.GotoNextBookmarkAction; import org.eclipse.cdt.internal.ui.actions.IndentAction; import org.eclipse.cdt.internal.ui.actions.RemoveBlockCommentAction; -import org.eclipse.cdt.internal.ui.actions.SelectionConverter; import org.eclipse.cdt.internal.ui.dnd.TextEditorDropAdapter; import org.eclipse.cdt.internal.ui.dnd.TextViewerDragAdapter; import org.eclipse.cdt.internal.ui.search.OccurrencesFinder; @@ -1405,12 +1404,9 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC } else if (required == IShowInSource.class) { ICElement ce= null; - try { - ce= SelectionConverter.getElementAtOffset(this); - if (ce instanceof ITranslationUnit) { - ce = null; - } - } catch (CModelException ex) { + ce= getElementAt(getSourceViewer().getSelectedRange().x, false); + if (ce instanceof ITranslationUnit) { + ce = null; } final ISelection selection= ce != null ? new StructuredSelection(ce) : null; return new IShowInSource() { 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 a1d80c93aab..2c848e7aa89 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2007 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 @@ -12,14 +12,18 @@ package org.eclipse.cdt.internal.ui.editor; +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; import org.eclipse.cdt.core.model.IWorkingCopy; +import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.internal.core.model.IBufferFactory; @@ -54,7 +58,17 @@ public class CustomBufferFactory implements IBufferFactory { if (location != null) { return new DocumentAdapter(owner, location); } - + + // URI + URI locationUri= original.getLocationURI(); + if (locationUri != null) { + try { + return new DocumentAdapter(owner, locationUri); + } catch (CoreException exc) { + CUIPlugin.log(exc); + } + } + } return DocumentAdapter.NULL; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DocumentAdapter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DocumentAdapter.java index 8f2aa357fdb..31c6f4ec032 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DocumentAdapter.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DocumentAdapter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2007 IBM Corporation and others. + * Copyright (c) 2002, 2008 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 @@ -12,25 +12,23 @@ package org.eclipse.cdt.internal.ui.editor; +import java.net.URI; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; -import org.eclipse.cdt.core.model.BufferChangedEvent; -import org.eclipse.cdt.core.model.CModelException; -import org.eclipse.cdt.core.model.IBuffer; -import org.eclipse.cdt.core.model.IBufferChangedListener; -import org.eclipse.cdt.core.model.IOpenable; -import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.core.filebuffers.FileBuffers; import org.eclipse.core.filebuffers.ITextFileBuffer; import org.eclipse.core.filebuffers.ITextFileBufferManager; import org.eclipse.core.filebuffers.LocationKind; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourceAttributes; +import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; @@ -42,13 +40,19 @@ import org.eclipse.jface.text.DocumentEvent; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocumentListener; import org.eclipse.jface.text.ISynchronizable; -import org.eclipse.core.runtime.Assert; import org.eclipse.swt.widgets.Display; +import org.eclipse.cdt.core.model.BufferChangedEvent; +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.IBuffer; +import org.eclipse.cdt.core.model.IBufferChangedListener; +import org.eclipse.cdt.core.model.IOpenable; +import org.eclipse.cdt.ui.CUIPlugin; + /** * Adapts IDocument to IBuffer. Uses the - * same algorithm as the text widget to determine the buffer's line delimiter. + * same algorithm as the text widget to determine the buffer's line delimiter. * All text inserted into the buffer is converted to this line delimiter. * This class is public for test purposes only. * @@ -186,6 +190,8 @@ public class DocumentAdapter implements IBuffer, IDocumentListener { final private IPath fLocation; final private LocationKind fLocationKind; + private IFileStore fFileStore; + public DocumentAdapter(IOpenable owner, IFile file) { fOwner= owner; @@ -193,7 +199,7 @@ public class DocumentAdapter implements IBuffer, IDocumentListener { fLocation= file.getFullPath(); fLocationKind= LocationKind.IFILE; - initialize(); + initialize(); } public DocumentAdapter(IOpenable owner, IPath location) { @@ -201,20 +207,38 @@ public class DocumentAdapter implements IBuffer, IDocumentListener { fLocation= location; fLocationKind= LocationKind.LOCATION; - initialize(); + initialize(); + } + + public DocumentAdapter(IOpenable owner, URI locationUri) throws CoreException { + fOwner= owner; + fFileStore= EFS.getStore(locationUri); + + fLocation= null; + fLocationKind= null; + + initialize(); } private void initialize() { ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); try { - manager.connect(fLocation, fLocationKind, new NullProgressMonitor()); - fTextFileBuffer= manager.getTextFileBuffer(fLocation, fLocationKind); + if (fLocation != null) { + manager.connect(fLocation, fLocationKind, new NullProgressMonitor()); + fTextFileBuffer= manager.getTextFileBuffer(fLocation, fLocationKind); + } else { + manager.connectFileStore(fFileStore, new NullProgressMonitor()); + fTextFileBuffer= manager.getFileStoreTextFileBuffer(fFileStore); + } fDocument= fTextFileBuffer.getDocument(); } catch (CoreException x) { fStatus= x.getStatus(); - fDocument= manager.createEmptyDocument(fLocation, fLocationKind); - if (fDocument instanceof ISynchronizable) + if (fLocation != null) { + fDocument= manager.createEmptyDocument(fLocation, fLocationKind); + } + if (fDocument instanceof ISynchronizable) { ((ISynchronizable)fDocument).setLockObject(new Object()); + } } fDocument.addPrenotifiedDocumentListener(this); } @@ -261,7 +285,7 @@ public class DocumentAdapter implements IBuffer, IDocumentListener { * @see org.eclipse.cdt.core.model.IBuffer#append(char[]) */ public void append(char[] text) { - append(new String(text)); + append(new String(text)); } /** @@ -271,7 +295,7 @@ public class DocumentAdapter implements IBuffer, IDocumentListener { if (DEBUG_LINE_DELIMITERS) { validateLineDelimiters(text); } - fReplaceCmd.replace(fDocument.getLength(), 0, text); + fReplaceCmd.replace(fDocument.getLength(), 0, text); } @@ -290,7 +314,11 @@ public class DocumentAdapter implements IBuffer, IDocumentListener { if (fTextFileBuffer != null) { ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); try { - manager.disconnect(fLocation, fLocationKind, new NullProgressMonitor()); + if (fLocation != null) { + manager.disconnect(fLocation, fLocationKind, new NullProgressMonitor()); + } else { + manager.disconnectFileStore(fFileStore, new NullProgressMonitor()); + } } catch (CoreException x) { // ignore } @@ -384,14 +412,14 @@ public class DocumentAdapter implements IBuffer, IDocumentListener { return attributes.isReadOnly(); } } - return false; + return false; } /** * @see org.eclipse.cdt.core.model.IBuffer#replace(int, int, char[]) */ public void replace(int position, int length, char[] text) { - replace(position, length, new String(text)); + replace(position, length, new String(text)); } /** @@ -420,7 +448,7 @@ public class DocumentAdapter implements IBuffer, IDocumentListener { * @see org.eclipse.cdt.core.model.IBuffer#setContents(char[]) */ public void setContents(char[] contents) { - setContents(new String(contents)); + setContents(new String(contents)); } /** diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorLinkHelper.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorLinkHelper.java index 4a563116bab..fa5935de17b 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorLinkHelper.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/navigator/CNavigatorLinkHelper.java @@ -57,14 +57,23 @@ public class CNavigatorLinkHelper implements ILinkHelper { */ public IStructuredSelection findSelection(IEditorInput input) { IWorkingCopyManager mgr= CUIPlugin.getDefault().getWorkingCopyManager(); - ICElement element= mgr.getWorkingCopy(input); + Object element= mgr.getWorkingCopy(input); if (element == null) { IFile file = ResourceUtil.getFile(input); if (file != null && CoreModel.hasCNature(file.getProject())) { element= CoreModel.getDefault().create(file); } } else { - element= ((IWorkingCopy) element).getOriginalElement(); + ITranslationUnit tUnit= ((IWorkingCopy) element).getOriginalElement(); + IFile file= (IFile) tUnit.getResource(); + if (file != null) { + element= CoreModel.getDefault().create(file); + if (element == null) { + element= file; + } + } else { + element= tUnit; + } } return (element != null) ? new StructuredSelection(element) : StructuredSelection.EMPTY; }