Skip to content

Commit

Permalink
Bug 421544 - When searching for the target file for Toggle Source/Hea…
Browse files Browse the repository at this point in the history
…der,

prefer files closer to the origin file in the directory structure

Change-Id: I82a3c1dc3f09cecb69e07511dd5b8bed62676b6a
Signed-off-by: Nathan Ridge <[email protected]>
  • Loading branch information
HighCommander4 authored and sprigogin committed Jul 19, 2015
1 parent 91f4a98 commit 5623c39
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.cdt.ui.tests.buildconsole.BuildConsoleTests;
import org.eclipse.cdt.ui.tests.callhierarchy.CallHierarchyTestSuite;
import org.eclipse.cdt.ui.tests.chelp.CHelpTest;
import org.eclipse.cdt.ui.tests.editor.EditorTestSuite;
import org.eclipse.cdt.ui.tests.includebrowser.IncludeBrowserTestSuite;
import org.eclipse.cdt.ui.tests.misc.MiscTestSuite;
import org.eclipse.cdt.ui.tests.outline.OutlineTestSuite;
Expand Down Expand Up @@ -102,6 +103,9 @@ public AutomatedSuite() throws Exception {

// tests from package org.eclipse.cdt.ui.tests.misc
addTest(MiscTestSuite.suite());

// tests from package org.eclipse.cdt.ui.tests.editor
addTest(EditorTestSuite.suite());

addTest(AllTemplateEngineTests.suite());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************************
* Copyright (c) 2015 Nathan Ridge.
* 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:
* Nathan Ridge - initial implementation
*******************************************************************************/
package org.eclipse.cdt.ui.tests.editor;

import junit.framework.TestSuite;

/**
* Tests for functionality in the package org.eclipse.cdt.internal.ui.editor.
*/
public class EditorTestSuite extends TestSuite {

public static TestSuite suite() {
return new EditorTestSuite();
}

public EditorTestSuite() {
super(EditorTestSuite.class.getName());
addTest(SourceHeaderPartnerFinderTest.suite());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*******************************************************************************
* Copyright (c) 2015 Nathan Ridge.
* 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:
* Nathan Ridge - initial implementation
*******************************************************************************/
package org.eclipse.cdt.ui.tests.editor;

import junit.framework.TestSuite;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.model.CoreModel;
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.ui.tests.BaseUITestCase;

import org.eclipse.cdt.internal.ui.editor.SourceHeaderPartnerFinder;

/**
* Tests for org.eclipse.cdt.internal.ui.editor.SourceHeaderPartnerFinder.
*/
public class SourceHeaderPartnerFinderTest extends BaseUITestCase {

public static TestSuite suite() {
return suite(SourceHeaderPartnerFinderTest.class);
}

protected static IProgressMonitor NPM= new NullProgressMonitor();

private ICProject fCProject;

public SourceHeaderPartnerFinderTest(String name) {
super(name);
}

@Override
protected void setUp() throws Exception {
super.setUp();
fCProject= CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin",
IPDOMManager.ID_FAST_INDEXER);
}

@Override
protected void tearDown() throws Exception {
if (fCProject != null) {
CProjectHelper.delete(fCProject);
}
super.tearDown();
}

public void testFilesWithSameNameInSubdirectory_421544() throws Exception {
IProject project = fCProject.getProject();
IFile originFile = createFile(project, "code.cc", "");
IFile expectedTargetFile = createFile(project, "code.h", "");
createFile(project, "sub/code.cc", "");
createFile(project, "sub/code.h", "");
ITranslationUnit originTU = (ITranslationUnit) CoreModel.getDefault().create(originFile);
ITranslationUnit targetTU = SourceHeaderPartnerFinder.getPartnerTranslationUnit(originTU);
IFile targetFile = (IFile) targetTU.getResource();
assertEquals(expectedTargetFile, targetFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,19 @@ private static IFile findInContainer(IContainer container, final String basename
IResourceProxyVisitor visitor= new IResourceProxyVisitor() {
@Override
public boolean visit(IResourceProxy proxy) throws CoreException {
if (result[0] != null) {
return false;
}
if (!proxy.isAccessible()) {
return false;
}
if (proxy.getType() == IResource.FILE && proxy.getName().equals(basename)) {
result[0]= (IFile)proxy.requestResource();
IFile candidate = (IFile) proxy.requestResource();
if (result[0] == null) {
result[0] = candidate;
} else {
// Prefer files closer to the root of the container.
if (candidate.getFullPath().segmentCount() < result[0].getFullPath().segmentCount()) {
result[0] = candidate;
}
}
return false;
}
return true;
Expand Down Expand Up @@ -278,13 +283,10 @@ private static ITranslationUnit getPartnerFileFromFilename(ITranslationUnit tu)
}
IPath partnerBasePath= sourceFileLocation.removeFileExtension();
IContentType[] contentTypes= getPartnerContentTypes(tu.getContentTypeId());
HashSet<String> extensionsTried= new HashSet<String>();
for (int j = 0; j < contentTypes.length; j++) {
IContentType contentType= contentTypes[j];
String[] partnerExtensions;
partnerExtensions= contentType.getFileSpecs(IContentType.FILE_EXTENSION_SPEC);
for (int i= 0; i < partnerExtensions.length; i++) {
String ext= partnerExtensions[i];
HashSet<String> extensionsTried= new HashSet<>();
for (IContentType contentType : contentTypes) {
String[] partnerExtensions = contentType.getFileSpecs(IContentType.FILE_EXTENSION_SPEC);
for (String ext : partnerExtensions) {
if (extensionsTried.add(ext)) {
String partnerFileBasename= partnerBasePath.addFileExtension(ext).lastSegment();

Expand Down

0 comments on commit 5623c39

Please sign in to comment.