Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] CDT/UI Fix for OpenInclude Action and CEditor loading external file

Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/ChangeLog,v
retrieving revision 1.147
diff -u -r1.147 ChangeLog
--- ChangeLog	27 Aug 2003 13:17:58 -0000	1.147
+++ ChangeLog	28 Aug 2003 19:45:25 -0000
@@ -1,3 +1,18 @@
+2003-08-28 Alain Magloire
+
+	Changes to be able to see external file in the CEditor.  The main problem
+	was that the way the Core/Model ICElement and IWorkingCopy was designed
+	they always assume that files are inside the workspace .... So to always have
+	an IFile.  One of the problem was the CContentOutliner.   We provid and
+	extern WorkingCopy: CFileWorkingCopy.  But this should be revisited.
+
+	Changes aslo to the OpenIncludAction to use the IScannerInfo to search
+	for headers.
+
+	* src/org/eclipse/cdt/internal/ui/CFileElmentWorkingCopy.java
+	* src/org/eclipse/cdt/internal/ui/editor/CContentOutliner.java
+	* src/org/eclipse/cdt/internal/ui/editor/OpenIncludeAction.java
+
 2003-08-27 Thomas Fletcher
 	
 	Update code completion to include () for functions and methods and
Index: src/org/eclipse/cdt/internal/ui/CFileElementWorkingCopy.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CFileElementWorkingCopy.java,v
retrieving revision 1.5
diff -u -r1.5 CFileElementWorkingCopy.java
--- src/org/eclipse/cdt/internal/ui/CFileElementWorkingCopy.java	25 Jun 2003 22:47:43 -0000	1.5
+++ src/org/eclipse/cdt/internal/ui/CFileElementWorkingCopy.java	28 Aug 2003 19:45:25 -0000
@@ -4,32 +4,200 @@
  * (c) Copyright IBM Corp. 2000, 2001.
  * All Rights Reserved.
  */
- 
+
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
+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.core.model.ITranslationUnit;
 import org.eclipse.cdt.internal.core.model.WorkingCopy;
+import org.eclipse.cdt.internal.ui.editor.CDocumentProvider;
+import org.eclipse.cdt.internal.ui.editor.DocumentAdapter;
+import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IStorage;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.DefaultLineTracker;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.ui.IEditorInput;
-import org.eclipse.ui.IFileEditorInput;
 import org.eclipse.ui.IStorageEditorInput;
 import org.eclipse.ui.texteditor.IDocumentProvider;
 
 public class CFileElementWorkingCopy extends WorkingCopy {
-	
-	private IDocumentProvider fProvider;
-	//private IFileEditorInput input;
-	private IEditorInput input;
-	
+
+	IDocumentProvider fProvider;
+	IEditorInput input;
+	IBuffer buffer;
+
 	/**
-	 * Creates a working copy of this element
+	 * Internal IBuffer implementation very simple, must cases will use DocumentAdapter.
+	 * 
 	 */
-	public CFileElementWorkingCopy(IFileEditorInput fileInput, IDocumentProvider provider) throws CoreException {
-		super(null, fileInput.getFile(), null);
-		input= fileInput;
-		fProvider= provider;
+	class Buffer implements IBuffer {
+
+		CFileElementWorkingCopy owner;
+
+		public Buffer(CFileElementWorkingCopy o) {
+			owner = o;
+		}
+		/* (non-Javadoc)
+		* @see org.eclipse.cdt.core.model.IBuffer#addBufferChangedListener(org.eclipse.cdt.core.model.IBufferChangedListener)
+		*/
+		public void addBufferChangedListener(IBufferChangedListener listener) {
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#append(char[])
+		 */
+		public void append(char[] text) {
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#append(java.lang.String)
+		 */
+		public void append(String text) {
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#close()
+		 */
+		public void close() {
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#getChar(int)
+		 */
+		public char getChar(int position) {
+			IDocument doc = fProvider.getDocument(input);
+			if (doc != null) {
+				try {
+					return doc.getChar(position);
+				} catch (BadLocationException e) {
+				}
+			}
+			return 0;
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#getCharacters()
+		 */
+		public char[] getCharacters() {
+			return getContents().toCharArray();
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#getContents()
+		 */
+		public String getContents() {
+			IDocument doc = fProvider.getDocument(input);
+			if (doc != null) {
+				return doc.get();
+			}
+			return new String();
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#getLength()
+		 */
+		public int getLength() {
+			IDocument doc = fProvider.getDocument(input);
+			if (doc != null) {
+				return doc.getLength();
+			}
+			return 0;
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#getOwner()
+		 */
+		public IOpenable getOwner() {
+			return owner;
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#getText(int, int)
+		 */
+		public String getText(int offset, int length) {
+			IDocument doc = fProvider.getDocument(input);
+			if (doc != null) {
+				try {
+					return doc.get(offset, length);
+				} catch (BadLocationException e) {
+				}
+			}
+			return new String();
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#getUnderlyingResource()
+		 */
+		public IResource getUnderlyingResource() {
+			return null;
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#hasUnsavedChanges()
+		 */
+		public boolean hasUnsavedChanges() {
+			return false;
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#isClosed()
+		 */
+		public boolean isClosed() {
+			return false;
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#isReadOnly()
+		 */
+		public boolean isReadOnly() {
+			return true;
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#removeBufferChangedListener(org.eclipse.cdt.core.model.IBufferChangedListener)
+		 */
+		public void removeBufferChangedListener(IBufferChangedListener listener) {
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#replace(int, int, char[])
+		 */
+		public void replace(int position, int length, char[] text) {
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#replace(int, int, java.lang.String)
+		 */
+		public void replace(int position, int length, String text) {
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#save(org.eclipse.core.runtime.IProgressMonitor, boolean)
+		 */
+		public void save(IProgressMonitor progress, boolean force) throws CModelException {
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#setContents(char[])
+		 */
+		public void setContents(char[] contents) {
+		}
+
+		/* (non-Javadoc)
+		 * @see org.eclipse.cdt.core.model.IBuffer#setContents(java.lang.String)
+		 */
+		public void setContents(String contents) {
+		}
+
 	}
 
 	/**
@@ -43,18 +211,44 @@
 		super.setLocation(storage.getFullPath());
 	}
 
-	/**
-	 * @see CFileElement#update
+	/* (non-Javadoc)
+	 * @see org.eclipse.cdt.core.model.ITranslationUnit#parse()
 	 */
-	public void update() throws CoreException {
-		IDocument doc= fProvider.getDocument(input);
+	public Map parse() {
+		IDocument doc = fProvider.getDocument(input);
 		if (doc != null) {
-			DocumentInputStream dis= new DocumentInputStream(doc);
+			DocumentInputStream dis = new DocumentInputStream(doc);
 			try {
-				parse(dis); 
+				return parse(dis);
 			} finally {
-				try { dis.close(); } catch (IOException e) {}
+				try {
+					dis.close();
+				} catch (IOException e) {
+				}
 			}
 		}
+		return new HashMap();
 	}
+
+	/* (non-Javadoc)
+	 * @see org.eclipse.cdt.core.model.IOpenable#getBuffer()
+	 */
+	public IBuffer getBuffer() throws CModelException {
+		if (buffer == null) {
+			if (fProvider instanceof CDocumentProvider) {
+				buffer = new DocumentAdapter(this, fProvider.getDocument(input), new DefaultLineTracker(), (CDocumentProvider)fProvider, input);
+			} else {
+				buffer = new Buffer(this);
+			}
+		}
+		return buffer;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.eclipse.cdt.internal.core.model.IWorkingCopy#getOriginalElement()
+	 */
+	public ITranslationUnit getOriginalElement() {
+		return this;
+	}
+
 }
Index: src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java,v
retrieving revision 1.13
diff -u -r1.13 CContentOutlinePage.java
--- src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java	20 Aug 2003 20:53:42 -0000	1.13
+++ src/org/eclipse/cdt/internal/ui/editor/CContentOutlinePage.java	28 Aug 2003 19:45:25 -0000
@@ -185,24 +185,23 @@
 		
 		registerToolbarActions();
 		
-		//IFileEditorInput editorInput= (IFileEditorInput)fEditor.getEditorInput();
 		IEditorInput editorInput= (IEditorInput)fEditor.getEditorInput();
 		IDocumentProvider provider= fEditor.getDocumentProvider();
 		try {
 			if (editorInput instanceof IFileEditorInput){				
-				//fInput = new CFileElementWorkingCopy((IFileEditorInput)editorInput, provider);
 				IWorkingCopyManager wcManager = CUIPlugin.getDefault().getWorkingCopyManager();
 				fInput = (WorkingCopy)wcManager.getWorkingCopy(editorInput);
 				if (fInput == null) {
+					// XXX This should never happen.  Put an assert.
 					fInput = new CFileElementWorkingCopy((IFileEditorInput)editorInput, provider);
 				}
 			} else if (editorInput instanceof IStorageEditorInput){
 				// CHECKPOINT: do we create a CFileElementWorkingCopy or just a working copy for the IStorageEditorInput?
-				//fInput = ((CUIPlugin.ElementFactory)plugin.getCCore()).createWorkingCopy((IStorageEditorInput)editorInput, provider);
+				// If it is an IStorage it means that there is no underlying IFile.
 				fInput = new CFileElementWorkingCopy((IStorageEditorInput)editorInput, provider);
-			} else
+			} else {
 				throw new CoreException(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0, "no Editor Input", null));
-
+			}
 			treeViewer.setInput(fInput);
 		} catch (CoreException e) {
 			CUIPlugin.getDefault().log(e.getStatus());
Index: src/org/eclipse/cdt/internal/ui/editor/OpenIncludeAction.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/OpenIncludeAction.java,v
retrieving revision 1.6
diff -u -r1.6 OpenIncludeAction.java
--- src/org/eclipse/cdt/internal/ui/editor/OpenIncludeAction.java	14 Feb 2003 01:17:11 -0000	1.6
+++ src/org/eclipse/cdt/internal/ui/editor/OpenIncludeAction.java	28 Aug 2003 19:45:25 -0000
@@ -5,18 +5,25 @@
  * All Rights Reserved.
  */
  
+import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 
+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.parser.IScannerInfo;
+import org.eclipse.cdt.core.parser.IScannerInfoProvider;
+import org.eclipse.cdt.core.resources.FileStorage;
 import org.eclipse.cdt.internal.ui.CPluginImages;
 import org.eclipse.cdt.internal.ui.dialogs.ElementListSelectionDialog;
 import org.eclipse.cdt.internal.ui.util.EditorUtility;
 import org.eclipse.cdt.ui.CUIPlugin;
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
@@ -63,74 +70,81 @@
 			IResource res = include.getUnderlyingResource();
 			ArrayList filesFound= new ArrayList(4);
 			if (res != null) {
-				findFile(res.getProject(), new Path(include.getElementName()), filesFound);
+				IProject proj = res.getProject();
+				String includeName = include.getElementName();
+				// Search in the scannerInfo information
+				IScannerInfoProvider provider =  CCorePlugin.getDefault().getScannerInfoProvider(proj);
+				if (provider != null) {
+					IScannerInfo info = provider.getScannerInformation(res);
+					// XXXX this should fall back to project by itself
+					if (info == null) {
+						info = provider.getScannerInformation(proj);
+					}
+					if (info != null) {
+						String[] includePaths = info.getIncludePaths();
+						findFile(includePaths, includeName, filesFound);
+					} else {
+						// Fall back and search the project
+						findFile(proj, new Path(includeName), filesFound);
+					}
+				}
 			}
-			IFile fileToOpen;
+			IPath fileToOpen;
 			int nElementsFound= filesFound.size();
 			if (nElementsFound == 0) {
 				fileToOpen= null;
 			} else if (nElementsFound == 1) {
-				fileToOpen= (IFile) filesFound.get(0);
+				fileToOpen= (IPath) filesFound.get(0);
 			} else {
 				fileToOpen= chooseFile(filesFound);
 			}
 			
 			if (fileToOpen != null) {
-				EditorUtility.openInEditor(fileToOpen);
-			} else { // Try to get via the include path.
-
-// This code is for getting the include paths from the builder.
-//				ICBuilder[] builders = CCorePlugin.getDefault().getBuilders(res.getProject());
-//				
-//				IPath includePath = null;
-//				for( int j = 0; includePath == null && j < builders.length; j++ ) {				
-//					IPath[] paths = builders[j].getIncludePaths();
-//
-//					for (int i = 0; i < paths.length; i++) {
-//						if (res != null) {
-//							// We've already scan the project.
-//							if (paths[i].isPrefixOf(res.getProject().getLocation()))
-//								continue;
-//						}
-//						IPath path = paths[i].append(include.getElementName());
-//						if (path.toFile().exists()) {
-//							includePath = path;
-//							break;
-//						}
-//					}
-//				}
-//
-//				if (includePath != null) {
-//					EditorUtility.openInEditor(includePath);
-//				}
-			}
+				IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(fileToOpen);
+				if (file != null) {
+					EditorUtility.openInEditor(file);
+				}  else {
+					FileStorage storage = new FileStorage(null, fileToOpen);
+					EditorUtility.openInEditor(storage);
+				}
+			} 
 		} catch (CModelException e) {
 			CUIPlugin.getDefault().log(e.getStatus());
 		} catch (CoreException e) {
 			CUIPlugin.getDefault().log(e.getStatus());
 		}
 	}
-	
-	private void findFile(IContainer parent, IPath name, ArrayList res) throws CoreException {
+
+	private void findFile(String[] includePaths,  String name, ArrayList list)  throws CoreException {
+		for (int i = 0; i < includePaths.length; i++) {
+			IPath path = new Path(includePaths[i] + "/" + name);
+			File file = path.toFile();
+			if (file.exists()) {
+				list.add(path);
+			} 
+		}
+	}
+
+	private void findFile(IContainer parent, IPath name, ArrayList list) throws CoreException {
 		IResource found= parent.findMember(name);
 		if (found != null && found.getType() == IResource.FILE) {
-			res.add(found);
+			list.add(found.getLocation());
 		}
 		IResource[] children= parent.members();
 		for (int i= 0; i < children.length; i++) {
 			if (children[i] instanceof IContainer) {
-				findFile((IContainer)children[i], name, res);
+				findFile((IContainer)children[i], name, list);
 			}
 		}		
 	}
 
 
-	private IFile chooseFile(ArrayList filesFound) {
+	private IPath chooseFile(ArrayList filesFound) {
 		ILabelProvider renderer= new LabelProvider() {
 			public String getText(Object element) {
-				if (element instanceof IFile) {
-					IFile file= (IFile)element;
-					return file.getName() + " - " + file.getParent().getFullPath().toString();
+				if (element instanceof IPath) {
+					IPath file= (IPath)element;
+					return file.lastSegment() + " - "  + file.toString();
 				}
 				return super.getText(element);
 			}
@@ -142,7 +156,7 @@
 		dialog.setElements(filesFound);
 		
 		if (dialog.open() == Window.OK) {
-			return (IFile) dialog.getSelectedElement();
+			return (IPath) dialog.getSelectedElement();
 		}
 		return null;
 	}
@@ -162,7 +176,14 @@
 	}
 	
 	public static boolean canActionBeAdded(ISelection selection) {
-		return getIncludeStatement(selection) != null;
+		ICElement include = getIncludeStatement(selection);
+		if (include != null) {
+			IResource res = include.getUnderlyingResource();
+			if (res != null) {
+				return true; 
+			}
+		}
+		return false;
 	}	
 
 



Back to the top