1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 512260 - Organize Includes should prefer .h and .hpp files to files

with other extensions

Change-Id: I115a3441d98d220f0d2b985b7d595971a146e8b6
This commit is contained in:
Sergey Prigogin 2017-02-15 20:02:20 -08:00
parent bb9fe47b10
commit 151a4c05e5
4 changed files with 74 additions and 12 deletions

View file

@ -313,6 +313,38 @@ public class IncludeOrganizerTest extends IncludesTestBase {
assertExpectedResults(); assertExpectedResults();
} }
//a.impl
//class A {};
//a.h
//#include "a.impl" // Non-header file extension
//b.inc
//class B {};
//b.h
//#include "b.inc" // Auto-exported header file extension
//c.inc
//class C {};
//test.cpp
//A a;
//B b;
//C c;
//====================
//#include "a.h"
//#include "b.h"
//#include "c.inc"
//
//A a;
//B b;
//C c;
public void testNonStandardFileExtensions() throws Exception {
assertExpectedResults();
}
//a.h //a.h
//class A {}; //class A {};

View file

@ -33,9 +33,11 @@ import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.internal.corext.codemanipulation.IncludeInfo; import org.eclipse.cdt.internal.corext.codemanipulation.IncludeInfo;
public class HeaderSubstitutor { public class HeaderSubstitutor {
private static int HEURISTIC_SCORE_NAME_MATCH = 1; private static int HEURISTIC_SCORE_NOT_AUTO_EXPORTED_HEADER = 0x1;
private static int HEURISTIC_SCORE_NO_EXTENSION = 2; private static int HEURISTIC_SCORE_NAME_MATCH = 0x2;
private static int HEURISTIC_SCORE_MAX = HEURISTIC_SCORE_NAME_MATCH + HEURISTIC_SCORE_NO_EXTENSION; private static int HEURISTIC_SCORE_NO_EXTENSION = 0x4;
private static int HEURISTIC_SCORE_MAX =
HEURISTIC_SCORE_NOT_AUTO_EXPORTED_HEADER + HEURISTIC_SCORE_NAME_MATCH + HEURISTIC_SCORE_NO_EXTENSION;
private final IncludeCreationContext fContext; private final IncludeCreationContext fContext;
private IncludeMap[] fIncludeMaps; private IncludeMap[] fIncludeMaps;
@ -150,6 +152,10 @@ public class HeaderSubstitutor {
} }
} }
} }
String filename = includeInfo.getName();
if (!fContext.isHeaderFile(filename) || fContext.isAutoExportedFile(filename)) {
return null;
}
return fContext.resolveInclude(includeInfo); return fContext.resolveInclude(includeInfo);
} }
@ -248,8 +254,10 @@ public class HeaderSubstitutor {
return preferredHeader; return preferredHeader;
} }
private static int getScore(String path, String symbolName) { private int getScore(String path, String symbolName) {
int score = 0; int score = 0;
if (fContext.isHeaderFile(path) && !fContext.isAutoExportedFile(path))
score += HEURISTIC_SCORE_NOT_AUTO_EXPORTED_HEADER;
if (getFilename(path).equalsIgnoreCase(symbolName)) if (getFilename(path).equalsIgnoreCase(symbolName))
score += HEURISTIC_SCORE_NAME_MATCH; score += HEURISTIC_SCORE_NAME_MATCH;
if (!hasExtension(path)) if (!hasExtension(path))

View file

@ -96,12 +96,15 @@ public final class IncludeCreationContext extends InclusionContext {
fHeadersToInclude.removeAll(exportedHeaders); fHeadersToInclude.removeAll(exportedHeaders);
} }
private boolean isIncludedFileExported(IIndexInclude include) throws CoreException { private final boolean isIncludedFileExported(IIndexInclude include) throws CoreException {
if (include.isIncludedFileExported()) if (include.isIncludedFileExported())
return true; return true;
String name = include.getName(); return isAutoExportedFile(include.getName());
int index = name.lastIndexOf('.'); }
String extension = index >= 0 ? name.substring(index + 1) : ""; //$NON-NLS-1$
public final boolean isAutoExportedFile(String filename) {
int index = filename.lastIndexOf('.');
String extension = index >= 0 ? filename.substring(index + 1) : ""; //$NON-NLS-1$
return ArrayUtil.containsEqual(getPreferences().extensionsOfAutoExportedFiles, extension); return ArrayUtil.containsEqual(getPreferences().extensionsOfAutoExportedFiles, extension);
} }
@ -161,4 +164,8 @@ public final class IncludeCreationContext extends InclusionContext {
return !IncludeUtil.isSource(indexFile, getProject()) || return !IncludeUtil.isSource(indexFile, getProject()) ||
fIndex.findIncludedBy(indexFile, 0).length != 0; fIndex.findIncludedBy(indexFile, 0).length != 0;
} }
public final boolean isHeaderFile(String filename) {
return IncludeUtil.isHeader(filename, getProject());
}
} }

View file

@ -57,16 +57,15 @@ public class IncludeUtil {
/** /**
* Checks if a file is a source file (.c, .cpp, .cc, etc). Header files are not considered * Checks if a file is a source file (.c, .cpp, .cc, etc). Header files are not considered
* source files. * source files.
* @return Returns {@code true} if the the file is a source file. * @return {@code true} if the the file is a source file.
*/ */
public static boolean isSource(IIndexFile file, IProject project) throws CoreException { public static boolean isSource(IIndexFile file, IProject project) throws CoreException {
return isSource(getPath(file), project); return isSource(getPath(file), project);
} }
/** /**
* Checks if a file is a source file (.c, .cpp, .cc, etc). Header files are not considered * Checks if a file is a source file (.c, .cpp, .cc, etc). Header files are not considered source files.
* source files. * @return {@code true} if the the file is a source file.
* @return Returns {@code true} if the the file is a source file.
*/ */
public static boolean isSource(String filename, IProject project) { public static boolean isSource(String filename, IProject project) {
IContentType ct= CCorePlugin.getContentType(project, filename); IContentType ct= CCorePlugin.getContentType(project, filename);
@ -79,6 +78,22 @@ public class IncludeUtil {
return false; return false;
} }
/**
* Checks if a file is a header file (.h, .hpp, C++ header without extension, etc).
*
* @return {@code true} if the the file is a header file.
*/
public static boolean isHeader(String filename, IProject project) {
IContentType ct= CCorePlugin.getContentType(project, filename);
if (ct != null) {
String id = ct.getId();
if (CCorePlugin.CONTENT_TYPE_CHEADER.equals(id) || CCorePlugin.CONTENT_TYPE_CXXHEADER.equals(id)) {
return true;
}
}
return false;
}
/** /**
* Returns the path of the given index file. * Returns the path of the given index file.
* @param file The index file. * @param file The index file.