mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-11 18:25:40 +02:00
Include guard generation option based on file path relative to the source folder. Bug 235586.
This commit is contained in:
parent
66e0db829f
commit
0bb3286605
2 changed files with 50 additions and 42 deletions
|
@ -20,8 +20,10 @@ import java.util.UUID;
|
||||||
|
|
||||||
import org.eclipse.core.resources.IFile;
|
import org.eclipse.core.resources.IFile;
|
||||||
import org.eclipse.core.resources.IProject;
|
import org.eclipse.core.resources.IProject;
|
||||||
|
import org.eclipse.core.resources.IResource;
|
||||||
import org.eclipse.core.resources.ProjectScope;
|
import org.eclipse.core.resources.ProjectScope;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
|
import org.eclipse.core.runtime.IPath;
|
||||||
import org.eclipse.core.runtime.Path;
|
import org.eclipse.core.runtime.Path;
|
||||||
import org.eclipse.core.runtime.Platform;
|
import org.eclipse.core.runtime.Platform;
|
||||||
import org.eclipse.core.runtime.Status;
|
import org.eclipse.core.runtime.Status;
|
||||||
|
@ -50,9 +52,11 @@ import org.eclipse.cdt.core.model.CoreModel;
|
||||||
import org.eclipse.cdt.core.model.IBuffer;
|
import org.eclipse.cdt.core.model.IBuffer;
|
||||||
import org.eclipse.cdt.core.model.ICElement;
|
import org.eclipse.cdt.core.model.ICElement;
|
||||||
import org.eclipse.cdt.core.model.ICProject;
|
import org.eclipse.cdt.core.model.ICProject;
|
||||||
|
import org.eclipse.cdt.core.model.ISourceRoot;
|
||||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
import org.eclipse.cdt.ui.CUIPlugin;
|
import org.eclipse.cdt.ui.CUIPlugin;
|
||||||
import org.eclipse.cdt.ui.PreferenceConstants;
|
import org.eclipse.cdt.ui.PreferenceConstants;
|
||||||
|
import org.eclipse.cdt.utils.PathUtil;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.corext.template.c.CodeTemplateContext;
|
import org.eclipse.cdt.internal.corext.template.c.CodeTemplateContext;
|
||||||
import org.eclipse.cdt.internal.corext.template.c.CodeTemplateContextType;
|
import org.eclipse.cdt.internal.corext.template.c.CodeTemplateContextType;
|
||||||
|
@ -63,7 +67,6 @@ import org.eclipse.cdt.internal.corext.util.Strings;
|
||||||
import org.eclipse.cdt.internal.ui.viewsupport.ProjectTemplateStore;
|
import org.eclipse.cdt.internal.ui.viewsupport.ProjectTemplateStore;
|
||||||
|
|
||||||
public class StubUtility {
|
public class StubUtility {
|
||||||
|
|
||||||
private static final String[] EMPTY= new String[0];
|
private static final String[] EMPTY= new String[0];
|
||||||
|
|
||||||
private StubUtility() {
|
private StubUtility() {
|
||||||
|
@ -92,7 +95,7 @@ public class StubUtility {
|
||||||
context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : ""); //$NON-NLS-1$
|
context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : ""); //$NON-NLS-1$
|
||||||
context.setVariable(CodeTemplateContextType.DECLARATIONS, declarations != null ? declarations : ""); //$NON-NLS-1$
|
context.setVariable(CodeTemplateContextType.DECLARATIONS, declarations != null ? declarations : ""); //$NON-NLS-1$
|
||||||
context.setVariable(CodeTemplateContextType.TYPENAME, new Path(tu.getElementName()).removeFileExtension().toString());
|
context.setVariable(CodeTemplateContextType.TYPENAME, new Path(tu.getElementName()).removeFileExtension().toString());
|
||||||
String includeGuardSymbol= generateIncludeGuardSymbol(tu.getElementName(), project);
|
String includeGuardSymbol= generateIncludeGuardSymbol(tu.getResource(), project);
|
||||||
context.setVariable(CodeTemplateContextType.INCLUDE_GUARD_SYMBOL, includeGuardSymbol != null ? includeGuardSymbol : ""); //$NON-NLS-1$
|
context.setVariable(CodeTemplateContextType.INCLUDE_GUARD_SYMBOL, includeGuardSymbol != null ? includeGuardSymbol : ""); //$NON-NLS-1$
|
||||||
|
|
||||||
String[] fullLine= { CodeTemplateContextType.FILE_COMMENT, CodeTemplateContextType.TYPE_COMMENT, CodeTemplateContextType.DECLARATIONS };
|
String[] fullLine= { CodeTemplateContextType.FILE_COMMENT, CodeTemplateContextType.TYPE_COMMENT, CodeTemplateContextType.DECLARATIONS };
|
||||||
|
@ -131,7 +134,7 @@ public class StubUtility {
|
||||||
String fileComment= getFileComment(file, lineDelimiter);
|
String fileComment= getFileComment(file, lineDelimiter);
|
||||||
context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : ""); //$NON-NLS-1$
|
context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : ""); //$NON-NLS-1$
|
||||||
ICProject cproject = CoreModel.getDefault().create(file.getProject());
|
ICProject cproject = CoreModel.getDefault().create(file.getProject());
|
||||||
String includeGuardSymbol= generateIncludeGuardSymbol(file.getName(), cproject);
|
String includeGuardSymbol= generateIncludeGuardSymbol(file, cproject);
|
||||||
context.setVariable(CodeTemplateContextType.INCLUDE_GUARD_SYMBOL, includeGuardSymbol != null ? includeGuardSymbol : ""); //$NON-NLS-1$
|
context.setVariable(CodeTemplateContextType.INCLUDE_GUARD_SYMBOL, includeGuardSymbol != null ? includeGuardSymbol : ""); //$NON-NLS-1$
|
||||||
context.setResourceVariables(file);
|
context.setResourceVariables(file);
|
||||||
String[] fullLine= { CodeTemplateContextType.FILE_COMMENT };
|
String[] fullLine= { CodeTemplateContextType.FILE_COMMENT };
|
||||||
|
@ -318,7 +321,7 @@ public class StubUtility {
|
||||||
edit.addChild(new InsertEdit(lineOffset, prefix));
|
edit.addChild(new InsertEdit(lineOffset, prefix));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (BadLocationException exc) {
|
} catch (BadLocationException e) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -341,7 +344,6 @@ public class StubUtility {
|
||||||
return doc.get();
|
return doc.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static TemplateVariable findVariable(TemplateBuffer buffer, String variable) {
|
private static TemplateVariable findVariable(TemplateBuffer buffer, String variable) {
|
||||||
TemplateVariable[] positions= buffer.getVariables();
|
TemplateVariable[] positions= buffer.getVariables();
|
||||||
for (int i= 0; i < positions.length; i++) {
|
for (int i= 0; i < positions.length; i++) {
|
||||||
|
@ -453,7 +455,6 @@ public class StubUtility {
|
||||||
return getProjectLineDelimiter(elem.getCProject());
|
return getProjectLineDelimiter(elem.getCProject());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the default task tag for the given project.
|
* Get the default task tag for the given project.
|
||||||
*
|
*
|
||||||
|
@ -505,6 +506,7 @@ public class StubUtility {
|
||||||
private static Template getCodeTemplate(String id, ICProject cProject) {
|
private static Template getCodeTemplate(String id, ICProject cProject) {
|
||||||
return getCodeTemplate(id, cProject != null ? cProject.getProject() : null);
|
return getCodeTemplate(id, cProject != null ? cProject.getProject() : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Template getCodeTemplate(String id, IProject project) {
|
private static Template getCodeTemplate(String id, IProject project) {
|
||||||
if (project == null)
|
if (project == null)
|
||||||
return CUIPlugin.getDefault().getCodeTemplateStore().findTemplateById(id);
|
return CUIPlugin.getDefault().getCodeTemplateStore().findTemplateById(id);
|
||||||
|
@ -517,46 +519,44 @@ public class StubUtility {
|
||||||
return projectStore.findTemplateById(id);
|
return projectStore.findTemplateById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getIncludeGuardScheme(ICProject cproject) {
|
private static String generateIncludeGuardSymbol(IResource file, ICProject cproject) {
|
||||||
return PreferenceConstants.getPreference(
|
int scheme = PreferenceConstants.getPreference(
|
||||||
PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME,
|
PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME, cproject,
|
||||||
cproject,
|
|
||||||
PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_FILE_NAME);
|
PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_FILE_NAME);
|
||||||
}
|
|
||||||
|
|
||||||
private static String generateIncludeGuardSymbol(String fileName, ICProject cproject) {
|
switch (scheme) {
|
||||||
|
case PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_FILE_PATH:
|
||||||
int preferenceValue = getIncludeGuardScheme(cproject);
|
if (file == null)
|
||||||
|
|
||||||
switch (preferenceValue) {
|
|
||||||
case PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_FILE_NAME:
|
|
||||||
if (fileName != null) {
|
|
||||||
return generateIncludeGuardSymbolFromFileName(fileName);
|
|
||||||
} else {
|
|
||||||
return null;
|
return null;
|
||||||
|
IPath path = file.getFullPath();
|
||||||
|
ISourceRoot root = cproject.findSourceRoot(file);
|
||||||
|
if (root != null) {
|
||||||
|
path = PathUtil.makeRelativePath(path, root.getPath());
|
||||||
}
|
}
|
||||||
|
return generateIncludeGuardSymbolFromFilePath(path.toString());
|
||||||
|
|
||||||
|
default:
|
||||||
|
CUIPlugin.log("Unknown preference value " + scheme + " for include guard scheme.", null); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
//$FALL-THROUGH$
|
||||||
|
case PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_FILE_NAME:
|
||||||
|
if (file == null)
|
||||||
|
return null;
|
||||||
|
return generateIncludeGuardSymbolFromFilePath(file.getName());
|
||||||
|
|
||||||
case PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_UUID:
|
case PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_UUID:
|
||||||
return generateIncludeGuardSymbolFromUUID();
|
return generateIncludeGuardSymbolFromUUID();
|
||||||
|
|
||||||
default:
|
|
||||||
CUIPlugin.log("Unknown preference value " + preferenceValue + "for include guard scheme", null); //$NON-NLS-1$ //$NON-NLS-2$
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String generateIncludeGuardSymbolFromFileName(String fileName) {
|
private static String generateIncludeGuardSymbolFromFilePath(String filename) {
|
||||||
String name = fileName;
|
// Convert to upper case and replace invalid characters with underscores,
|
||||||
//convert to upper case and remove invalid characters
|
// e.g. convert some/directory/foo-bar.h to SOME_DIRECTORY_FOO_BAR_H_
|
||||||
//eg convert foo.h --> FOO_H_
|
StringBuilder buf = new StringBuilder(filename.length() + 1);
|
||||||
StringBuffer buf = new StringBuffer();
|
for (int i = 0; i < filename.length(); ++i) {
|
||||||
// Do not do this, leading underscores are discouraged by the std.
|
char ch = filename.charAt(i);
|
||||||
//buf.append('_');
|
|
||||||
for (int i = 0; i < name.length(); ++i) {
|
|
||||||
char ch = name.charAt(i);
|
|
||||||
if (Character.isLetterOrDigit(ch)) {
|
if (Character.isLetterOrDigit(ch)) {
|
||||||
buf.append(Character.toUpperCase(ch));
|
buf.append(Character.toUpperCase(ch));
|
||||||
} else if (ch == '.' || ch == '_') {
|
} else if (buf.length() > 0){
|
||||||
buf.append('_');
|
buf.append('_');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -573,7 +573,7 @@ public class StubUtility {
|
||||||
// e.g. convert
|
// e.g. convert
|
||||||
// 067e6162-3b6f-4ae2-a171-2470b63dff00 to
|
// 067e6162-3b6f-4ae2-a171-2470b63dff00 to
|
||||||
// H067E6162-3b6F-4AE2-A171-2470B63DFF00
|
// H067E6162-3b6F-4AE2-A171-2470B63DFF00
|
||||||
StringBuffer buf = new StringBuffer();
|
StringBuilder buf = new StringBuilder();
|
||||||
|
|
||||||
buf.append('H');
|
buf.append('H');
|
||||||
|
|
||||||
|
@ -624,5 +624,4 @@ public class StubUtility {
|
||||||
}
|
}
|
||||||
return result.toArray(new Template[result.size()]);
|
return result.toArray(new Template[result.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1433,8 +1433,8 @@ public class PreferenceConstants {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value of <code>CODE_TEMPLATES_INCLUDE_GUARD_GENERATION_SCHEME</code>
|
* The value of <code>CODE_TEMPLATES_INCLUDE_GUARD_GENERATION_SCHEME</code>
|
||||||
* specifying that the include guard symbol is to be derived from the
|
* specifying that the include guard symbol is to be derived from
|
||||||
* include file's name.
|
* the include file's name.
|
||||||
*
|
*
|
||||||
* @since 5.1
|
* @since 5.1
|
||||||
*/
|
*/
|
||||||
|
@ -1448,6 +1448,15 @@ public class PreferenceConstants {
|
||||||
*/
|
*/
|
||||||
public static final int CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_UUID = 1;
|
public static final int CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_UUID = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The value of <code>CODE_TEMPLATES_INCLUDE_GUARD_GENERATION_SCHEME</code>
|
||||||
|
* specifying that the include guard symbol is to be derived from
|
||||||
|
* the include file's path relative to the source folder.
|
||||||
|
*
|
||||||
|
* @since 5.2
|
||||||
|
*/
|
||||||
|
public static final int CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_FILE_PATH = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the CDT-UI preference store.
|
* Returns the CDT-UI preference store.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue