From 08278a1193e64316b3425edcc8617e756acc95a2 Mon Sep 17 00:00:00 2001 From: John Camelon Date: Mon, 18 Jul 2005 23:47:30 +0000 Subject: [PATCH] Patch for Devin Steffler. Fixed Bug 103697 - Open Definition/Declaration on files in linked folders open a new editor --- .../eclipse/cdt/core/parser/ParserUtil.java | 7 + .../BaseSelectionTestsIndexer.java | 38 ++++- .../CPPSelectionTestsCTagsIndexer.java | 24 +++ .../CPPSelectionTestsDOMIndexer.java | 26 ++- .../CPPSelectionTestsNoIndexer.java | 49 ++++++ .../CSelectionTestsCTagsIndexer.java | 26 ++- .../CSelectionTestsDOMIndexer.java | 24 +++ .../internal/ui/search/CSearchResultPage.java | 152 ++++++++++-------- .../search/actions/OpenDefinitionAction.java | 2 +- .../search/actions/SelectionParseAction.java | 10 ++ .../cdt/internal/ui/util/EditorUtility.java | 45 ++++-- 11 files changed, 316 insertions(+), 87 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/parser/ParserUtil.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/parser/ParserUtil.java index 20deb09f83f..59fde73e058 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/parser/ParserUtil.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/parser/ParserUtil.java @@ -128,6 +128,13 @@ public class ParserUtil resultingResource = root.getFileForLocation( path ); if( resultingResource != null && resultingResource.exists() ) return resultingResource; + + // check for linked resources + IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(path); + if( files != null && files.length > 0 && files[0] != null) { + return files[0]; + } + return null; } catch( IllegalArgumentException iae ) //thrown on invalid paths diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/BaseSelectionTestsIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/BaseSelectionTestsIndexer.java index eb264b4b411..9703ae2606d 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/BaseSelectionTestsIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/BaseSelectionTestsIndexer.java @@ -11,8 +11,11 @@ package org.eclipse.cdt.ui.tests.text.selectiontests; import java.io.ByteArrayInputStream; +import java.io.File; import java.io.InputStream; +import junit.framework.TestCase; + import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.ICDescriptor; import org.eclipse.cdt.core.ICDescriptorOperation; @@ -27,28 +30,24 @@ import org.eclipse.cdt.internal.ui.editor.ICEditorActionDefinitionIds; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; import org.eclipse.jface.action.IAction; import org.eclipse.jface.text.TextSelection; -import org.eclipse.jface.util.IPropertyChangeListener; -import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.jface.viewers.ISelection; -import org.eclipse.search2.internal.ui.SearchView; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.IViewPart; -import org.eclipse.ui.IViewReference; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.ui.texteditor.AbstractTextEditor; -import junit.framework.TestCase; - /** * Base test class for testing Ctrl_F3/F3 with the indexers. * @@ -113,6 +112,31 @@ public class BaseSelectionTestsIndexer extends TestCase { return file; } + + protected IFile importFileWithLink(String fileName, String contents) throws Exception{ + //Obtain file handle + IFile file = project.getProject().getFile(fileName); + + IPath location = new Path(project.getLocation().removeLastSegments(1).toOSString() + File.separator + fileName); //$NON-NLS-1$ + + File linkFile = new File(location.toOSString()); + if (!linkFile.exists()) { + linkFile.createNewFile(); + } + + file.createLink(location, IResource.ALLOW_MISSING_LOCAL, null); + + InputStream stream = new ByteArrayInputStream( contents.getBytes() ); + //Create file input stream + if( file.exists() ) + file.setContents( stream, false, false, monitor ); + else + file.create( stream, false, monitor ); + + fileManager.addFile(file); + + return file; + } protected IFolder importFolder(String folderName) throws Exception { IFolder folder = project.getProject().getFolder(folderName); diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsCTagsIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsCTagsIndexer.java index 102b7ea31d7..bf568be25d9 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsCTagsIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsCTagsIndexer.java @@ -127,6 +127,7 @@ public class CPPSelectionTestsCTagsIndexer extends BaseSelectionTestsIndexer suite.addTest(new CPPSelectionTestsCTagsIndexer("testNoDefinitions")); //$NON-NLS-1$ suite.addTest(new CPPSelectionTestsCTagsIndexer("testOpenFileDiffDir")); //$NON-NLS-1$ suite.addTest(new CPPSelectionTestsCTagsIndexer("testBug101287")); //$NON-NLS-1$ + suite.addTest(new CPPSelectionTestsCTagsIndexer("testBug103697")); //$NON-NLS-1$ return suite; } @@ -662,6 +663,29 @@ public class CPPSelectionTestsCTagsIndexer extends BaseSelectionTestsIndexer } } + public void testBug103697() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("int x;\n"); //$NON-NLS-1$ + buffer.append("int foo() {\n"); //$NON-NLS-1$ + buffer.append(" return x;\n"); //$NON-NLS-1$ + buffer.append("}\n"); //$NON-NLS-1$ + + String code = buffer.toString(); + IFile file = importFileWithLink("testBug103697.cpp", code); //$NON-NLS-1$ + + int offset = code.indexOf("return x;\n") + "return ".length(); //$NON-NLS-1$ //$NON-NLS-2$ + IASTNode def = testCtrl_F3(file, offset); + IASTNode decl = testF3(file, offset); + assertTrue(def instanceof IASTName); + assertEquals(((IASTName)def).toString(), "x"); //$NON-NLS-1$ + assertEquals(((ASTNode)def).getOffset(), 4); + assertEquals(((ASTNode)def).getLength(), 1); + assertTrue(decl instanceof IASTName); + assertEquals(((IASTName)decl).toString(), "x"); //$NON-NLS-1$ + assertEquals(((ASTNode)decl).getOffset(), 4); + assertEquals(((ASTNode)decl).getLength(), 1); + } + // REMINDER: see CSelectionTestsCTagsIndexer#suite() when appending new tests to this suite } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsDOMIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsDOMIndexer.java index 4e897d8ffdb..9c864799ba8 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsDOMIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsDOMIndexer.java @@ -127,7 +127,8 @@ public class CPPSelectionTestsDOMIndexer extends BaseSelectionTestsIndexer imple suite.addTest(new CPPSelectionTestsDOMIndexer("testBug101287")); //$NON-NLS-1$ suite.addTest(new CPPSelectionTestsDOMIndexer("testBug102258")); //$NON-NLS-1$ suite.addTest(new CPPSelectionTestsDOMIndexer("testBug103323")); //$NON-NLS-1$ - + suite.addTest(new CPPSelectionTestsDOMIndexer("testBug103697")); //$NON-NLS-1$ + return suite; } @@ -1069,5 +1070,28 @@ public class CPPSelectionTestsDOMIndexer extends BaseSelectionTestsIndexer imple } + public void testBug103697() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("int x;\n"); //$NON-NLS-1$ + buffer.append("int foo() {\n"); //$NON-NLS-1$ + buffer.append(" return x;\n"); //$NON-NLS-1$ + buffer.append("}\n"); //$NON-NLS-1$ + + String code = buffer.toString(); + IFile file = importFileWithLink("testBug103697.cpp", code); //$NON-NLS-1$ + + int offset = code.indexOf("return x;\n") + "return ".length(); //$NON-NLS-1$ //$NON-NLS-2$ + IASTNode def = testCtrl_F3(file, offset); + IASTNode decl = testF3(file, offset); + assertTrue(def instanceof IASTName); + assertEquals(((IASTName)def).toString(), "x"); //$NON-NLS-1$ + assertEquals(((ASTNode)def).getOffset(), 4); + assertEquals(((ASTNode)def).getLength(), 1); + assertTrue(decl instanceof IASTName); + assertEquals(((IASTName)decl).toString(), "x"); //$NON-NLS-1$ + assertEquals(((ASTNode)decl).getOffset(), 4); + assertEquals(((ASTNode)decl).getLength(), 1); + } + // REMINDER: see CPPSelectionTestsDomIndexer#suite() when appending new tests to this suite } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsNoIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsNoIndexer.java index 780f2d9e662..97b8e0e393d 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsNoIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CPPSelectionTestsNoIndexer.java @@ -37,6 +37,7 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; import org.eclipse.jface.action.IAction; import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.viewers.ISelection; @@ -146,6 +147,31 @@ public class CPPSelectionTestsNoIndexer extends TestCase { return file; } + protected IFile importFileWithLink(String fileName, String contents) throws Exception{ + //Obtain file handle + IFile file = project.getProject().getFile(fileName); + + IPath location = new Path(project.getLocation().removeLastSegments(1).toOSString() + File.separator + fileName); //$NON-NLS-1$ + + File linkFile = new File(location.toOSString()); + if (!linkFile.exists()) { + linkFile.createNewFile(); + } + + file.createLink(location, IResource.ALLOW_MISSING_LOCAL, null); + + InputStream stream = new ByteArrayInputStream( contents.getBytes() ); + //Create file input stream + if( file.exists() ) + file.setContents( stream, false, false, monitor ); + else + file.create( stream, false, monitor ); + + fileManager.addFile(file); + + return file; + } + protected IASTNode testF3(IFile file, int offset) throws ParserException { return testF3(file, offset, 0); } @@ -1078,4 +1104,27 @@ public class CPPSelectionTestsNoIndexer extends TestCase { assertEquals(((ASTNode)decl).getOffset(), 11); assertEquals(((ASTNode)decl).getLength(), 14); } + + public void testBug103697() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("int x;\n"); //$NON-NLS-1$ + buffer.append("int foo() {\n"); //$NON-NLS-1$ + buffer.append(" return x;\n"); //$NON-NLS-1$ + buffer.append("}\n"); //$NON-NLS-1$ + + String code = buffer.toString(); + IFile file = importFileWithLink("testBug103697.cpp", code); //$NON-NLS-1$ + + int offset = code.indexOf("return x;\n") + "return ".length(); //$NON-NLS-1$ //$NON-NLS-2$ + IASTNode def = testCtrl_F3(file, offset); + IASTNode decl = testF3(file, offset); + assertTrue(def instanceof IASTName); + assertEquals(((IASTName)def).toString(), "x"); //$NON-NLS-1$ + assertEquals(((ASTNode)def).getOffset(), 4); + assertEquals(((ASTNode)def).getLength(), 1); + assertTrue(decl instanceof IASTName); + assertEquals(((IASTName)decl).toString(), "x"); //$NON-NLS-1$ + assertEquals(((ASTNode)decl).getOffset(), 4); + assertEquals(((ASTNode)decl).getLength(), 1); + } } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CSelectionTestsCTagsIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CSelectionTestsCTagsIndexer.java index c208f23339b..7c078489b95 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CSelectionTestsCTagsIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CSelectionTestsCTagsIndexer.java @@ -120,7 +120,8 @@ public class CSelectionTestsCTagsIndexer extends BaseSelectionTestsIndexer suite.addTest(new CSelectionTestsCTagsIndexer("testNoDefinitions")); //$NON-NLS-1$ suite.addTest(new CSelectionTestsCTagsIndexer("testOpenFileDiffDir")); //$NON-NLS-1$ suite.addTest(new CSelectionTestsCTagsIndexer("testBug101287")); //$NON-NLS-1$ - + suite.addTest(new CSelectionTestsCTagsIndexer("testBug103697")); //$NON-NLS-1$ + return suite; } @@ -648,6 +649,29 @@ public class CSelectionTestsCTagsIndexer extends BaseSelectionTestsIndexer } } + public void testBug103697() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("int x;\n"); //$NON-NLS-1$ + buffer.append("int foo() {\n"); //$NON-NLS-1$ + buffer.append(" return x;\n"); //$NON-NLS-1$ + buffer.append("}\n"); //$NON-NLS-1$ + + String code = buffer.toString(); + IFile file = importFileWithLink("testBug103697.cpp", code); //$NON-NLS-1$ + + int offset = code.indexOf("return x;\n") + "return ".length(); //$NON-NLS-1$ //$NON-NLS-2$ + IASTNode def = testCtrl_F3(file, offset); + IASTNode decl = testF3(file, offset); + assertTrue(def instanceof IASTName); + assertEquals(((IASTName)def).toString(), "x"); //$NON-NLS-1$ + assertEquals(((ASTNode)def).getOffset(), 4); + assertEquals(((ASTNode)def).getLength(), 1); + assertTrue(decl instanceof IASTName); + assertEquals(((IASTName)decl).toString(), "x"); //$NON-NLS-1$ + assertEquals(((ASTNode)decl).getOffset(), 4); + assertEquals(((ASTNode)decl).getLength(), 1); + } + // REMINDER: see CSelectionTestsCTagsIndexer#suite() when appending new tests to this suite } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CSelectionTestsDOMIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CSelectionTestsDOMIndexer.java index 0a686d810b2..af3c26be118 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CSelectionTestsDOMIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selectiontests/CSelectionTestsDOMIndexer.java @@ -115,6 +115,7 @@ public class CSelectionTestsDOMIndexer extends BaseSelectionTestsIndexer impleme suite.addTest(new CSelectionTestsDOMIndexer("testNoDefinitions")); //$NON-NLS-1$ suite.addTest(new CSelectionTestsDOMIndexer("testOpenFileDiffDir")); //$NON-NLS-1$ suite.addTest(new CSelectionTestsDOMIndexer("testBug101287")); //$NON-NLS-1$ + suite.addTest(new CSelectionTestsDOMIndexer("testBug103697")); //$NON-NLS-1$ return suite; } @@ -644,5 +645,28 @@ public class CSelectionTestsDOMIndexer extends BaseSelectionTestsIndexer impleme assertEquals(((ASTNode)decl).getLength(), 3); } + public void testBug103697() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer.append("int x;\n"); //$NON-NLS-1$ + buffer.append("int foo() {\n"); //$NON-NLS-1$ + buffer.append(" return x;\n"); //$NON-NLS-1$ + buffer.append("}\n"); //$NON-NLS-1$ + + String code = buffer.toString(); + IFile file = importFileWithLink("testBug103697.cpp", code); //$NON-NLS-1$ + + int offset = code.indexOf("return x;\n") + "return ".length(); //$NON-NLS-1$ //$NON-NLS-2$ + IASTNode def = testCtrl_F3(file, offset); + IASTNode decl = testF3(file, offset); + assertTrue(def instanceof IASTName); + assertEquals(((IASTName)def).toString(), "x"); //$NON-NLS-1$ + assertEquals(((ASTNode)def).getOffset(), 4); + assertEquals(((ASTNode)def).getLength(), 1); + assertTrue(decl instanceof IASTName); + assertEquals(((IASTName)decl).toString(), "x"); //$NON-NLS-1$ + assertEquals(((ASTNode)decl).getOffset(), 4); + assertEquals(((ASTNode)decl).getLength(), 1); + } + // REMINDER: see CSelectionTestsDomIndexer#suite() when appending new tests to this suite } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResultPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResultPage.java index 8d228079bd2..dcc9023517e 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResultPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/CSearchResultPage.java @@ -30,6 +30,7 @@ import org.eclipse.cdt.ui.CSearchResultLabelProvider; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; @@ -119,82 +120,99 @@ public class CSearchResultPage extends AbstractTextSearchViewPage { editor = IDE.openEditor(CUIPlugin.getActivePage(), getCanonicalFile((IFile) searchMatch.getResource()), false); IMatchLocatable searchLocatable = searchMatch.getLocatable(); showWithMarker(editor, getCanonicalFile((IFile) searchMatch.getResource()), searchLocatable, currentOffset, currentLength); + return; } - else { - //Match is outside of the workspace - try { - IEditorInput input =EditorUtility.getEditorInput(new ExternalSearchFile(searchMatch.getPath(), searchMatch)); - IWorkbenchPage p= CUIPlugin.getActivePage(); - IEditorPart editorPart= p.openEditor(input, "org.eclipse.cdt.ui.editor.ExternalSearchEditor"); //$NON-NLS-1$ - if (editorPart instanceof ITextEditor) { - ITextEditor textEditor= (ITextEditor) editorPart; + + IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(searchMatch.getPath()); + if (files != null && files.length > 0){ + for (int i=0; i0 && tempendLine > tempstartLine){ + int endOffset; try { -// NOTE: Subtract 1 from the passed in line number because, even though the editor is 1 based, the line - //resolver doesn't take this into account and is still 0 based - startOffset = doc.getLineOffset(tempstartLine-1); - length=doc.getLineLength(tempstartLine-1); + //See NOTE above + endOffset = doc.getLineOffset(tempendLine-1); + length = endOffset - startOffset; } catch (BadLocationException e) {} - //See if an end line number has been provided - if so - //use it to calculate the length of the reveal... - //Make sure that an end offset exists that is greater than 0 - //and that is greater than the start line number - if (tempendLine>0 && tempendLine > tempstartLine){ - int endOffset; - try { - //See NOTE above - endOffset = doc.getLineOffset(tempendLine-1); - length = endOffset - startOffset; - } catch (BadLocationException e) {} - - } } - textEditor.selectAndReveal(startOffset,length); } - + textEditor.selectAndReveal(startOffset,length); + } + //TODO: Put in once we have marker support for External Translation Units /* //Get all the CProjects off the model - ICProject[] cprojects = CoreModel.getDefault().getCModel().getCProjects(); - - ICProject containingProject=null; - ICElement celem = null; - //Find the CProject that the element belongs to - for (int i=0; i 0) { + path = path.removeLastSegments(1); + IContainer[] containers = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocation(path); + + for(int i=0; i