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:
parent
bb9fe47b10
commit
151a4c05e5
4 changed files with 74 additions and 12 deletions
|
@ -313,6 +313,38 @@ public class IncludeOrganizerTest extends IncludesTestBase {
|
|||
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
|
||||
//class A {};
|
||||
|
||||
|
|
|
@ -33,9 +33,11 @@ import org.eclipse.cdt.ui.PreferenceConstants;
|
|||
import org.eclipse.cdt.internal.corext.codemanipulation.IncludeInfo;
|
||||
|
||||
public class HeaderSubstitutor {
|
||||
private static int HEURISTIC_SCORE_NAME_MATCH = 1;
|
||||
private static int HEURISTIC_SCORE_NO_EXTENSION = 2;
|
||||
private static int HEURISTIC_SCORE_MAX = HEURISTIC_SCORE_NAME_MATCH + HEURISTIC_SCORE_NO_EXTENSION;
|
||||
private static int HEURISTIC_SCORE_NOT_AUTO_EXPORTED_HEADER = 0x1;
|
||||
private static int HEURISTIC_SCORE_NAME_MATCH = 0x2;
|
||||
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 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);
|
||||
}
|
||||
|
||||
|
@ -248,8 +254,10 @@ public class HeaderSubstitutor {
|
|||
return preferredHeader;
|
||||
}
|
||||
|
||||
private static int getScore(String path, String symbolName) {
|
||||
private int getScore(String path, String symbolName) {
|
||||
int score = 0;
|
||||
if (fContext.isHeaderFile(path) && !fContext.isAutoExportedFile(path))
|
||||
score += HEURISTIC_SCORE_NOT_AUTO_EXPORTED_HEADER;
|
||||
if (getFilename(path).equalsIgnoreCase(symbolName))
|
||||
score += HEURISTIC_SCORE_NAME_MATCH;
|
||||
if (!hasExtension(path))
|
||||
|
|
|
@ -96,12 +96,15 @@ public final class IncludeCreationContext extends InclusionContext {
|
|||
fHeadersToInclude.removeAll(exportedHeaders);
|
||||
}
|
||||
|
||||
private boolean isIncludedFileExported(IIndexInclude include) throws CoreException {
|
||||
private final boolean isIncludedFileExported(IIndexInclude include) throws CoreException {
|
||||
if (include.isIncludedFileExported())
|
||||
return true;
|
||||
String name = include.getName();
|
||||
int index = name.lastIndexOf('.');
|
||||
String extension = index >= 0 ? name.substring(index + 1) : ""; //$NON-NLS-1$
|
||||
return isAutoExportedFile(include.getName());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -161,4 +164,8 @@ public final class IncludeCreationContext extends InclusionContext {
|
|||
return !IncludeUtil.isSource(indexFile, getProject()) ||
|
||||
fIndex.findIncludedBy(indexFile, 0).length != 0;
|
||||
}
|
||||
|
||||
public final boolean isHeaderFile(String filename) {
|
||||
return IncludeUtil.isHeader(filename, getProject());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,16 +57,15 @@ public class IncludeUtil {
|
|||
/**
|
||||
* Checks if a file is a source file (.c, .cpp, .cc, etc). Header files are not considered
|
||||
* 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 {
|
||||
return isSource(getPath(file), project);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a file is a source file (.c, .cpp, .cc, etc). Header files are not considered
|
||||
* source files.
|
||||
* @return Returns {@code true} if the the file is a source file.
|
||||
* Checks if a file is a source file (.c, .cpp, .cc, etc). Header files are not considered source files.
|
||||
* @return {@code true} if the the file is a source file.
|
||||
*/
|
||||
public static boolean isSource(String filename, IProject project) {
|
||||
IContentType ct= CCorePlugin.getContentType(project, filename);
|
||||
|
@ -79,6 +78,22 @@ public class IncludeUtil {
|
|||
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.
|
||||
* @param file The index file.
|
||||
|
|
Loading…
Add table
Reference in a new issue