1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-08 11:03:28 +02:00

2004-12-03 Chris Wiebe

fix for 74098
	* src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCodeGenerator.java
	* src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCreationWizardPage.java
This commit is contained in:
Chris Wiebe 2004-12-04 01:59:31 +00:00
parent fd68874052
commit 0feec5d746
3 changed files with 169 additions and 27 deletions

View file

@ -1,3 +1,8 @@
2004-12-03 Chris Wiebe
fix for 74098
* src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCodeGenerator.java
* src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCreationWizardPage.java
2004-12-03 Alain Magloire
Implement comment blocks(Code take from JDT Editor)
* src/org/eclipse/cdt/internal/ui/action/AddBlockCommentAction.java

View file

@ -162,7 +162,7 @@ public class NewClassCodeGenerator {
// create a working copy with a new owner
sourceWorkingCopy = sourceTU.getWorkingCopy();
String sourceContent = constructSourceFileContent(sourceTU, headerTU, publicMethods, protectedMethods, privateMethods, new SubProgressMonitor(monitor, 100));
String sourceContent = constructSourceFileContent(sourceTU, headerTU, publicMethods, protectedMethods, privateMethods, sourceWorkingCopy.getBuffer().getContents(), new SubProgressMonitor(monitor, 100));
sourceWorkingCopy.getBuffer().setContents(sourceContent);
if (monitor.isCanceled()) {
@ -190,15 +190,17 @@ public class NewClassCodeGenerator {
}
public String constructHeaderFileContent(ITranslationUnit headerTU, List publicMethods, List protectedMethods, List privateMethods, String oldContents, IProgressMonitor monitor) throws CodeGeneratorException {
monitor.beginTask(NewClassWizardMessages.getString("NewClassCodeGeneration.createType.task.header"), 100); //$NON-NLS-1$
if (oldContents != null && oldContents.length() == 0)
oldContents = null;
//TODO should use code templates
StringBuffer text = new StringBuffer();
int appendFirstCharPos = -1;
if (oldContents != null) {
int insertionPos = getInsertionPos(oldContents);
int insertionPos = getClassDefInsertionPos(oldContents);
if (insertionPos == -1) {
text.append(oldContents);
} else {
@ -267,7 +269,10 @@ public class NewClassCodeGenerator {
return newContents;
}
private int getInsertionPos(String contents) {
private int getClassDefInsertionPos(String contents) {
if (contents.length() == 0) {
return -1;
}
//TODO temporary hack
int insertPos = contents.lastIndexOf("#endif"); //$NON-NLS-1$
if (insertPos != -1) {
@ -569,15 +574,52 @@ public class NewClassCodeGenerator {
return list;
}
public String constructSourceFileContent(ITranslationUnit sourceTU, ITranslationUnit headerTU, List publicMethods, List protectedMethods, List privateMethods, IProgressMonitor monitor) {
public String constructSourceFileContent(ITranslationUnit sourceTU, ITranslationUnit headerTU, List publicMethods, List protectedMethods, List privateMethods, String oldContents, IProgressMonitor monitor) {
monitor.beginTask(NewClassWizardMessages.getString("NewClassCodeGeneration.createType.task.source"), 150); //$NON-NLS-1$
if (oldContents != null && oldContents.length() == 0)
oldContents = null;
//TODO should use code templates
StringBuffer text = new StringBuffer();
String includeString = null;
if (headerTU != null) {
addHeaderInclude(sourceTU, headerTU, text, new SubProgressMonitor(monitor, 50));
includeString = getHeaderIncludeString(sourceTU, headerTU, text, new SubProgressMonitor(monitor, 50));
if (includeString != null) {
// check if file already has the include
if (oldContents != null && hasInclude(oldContents, includeString)) {
// don't bother to add it
includeString = null;
}
}
}
if (includeString != null) {
if (oldContents != null) {
int insertionPos = getIncludeInsertionPos(oldContents);
if (insertionPos == -1) {
text.append(oldContents);
text.append(fLineDelimiter);
text.append(includeString);
text.append(fLineDelimiter);
} else {
text.append(oldContents.substring(0, insertionPos));
text.append(includeString);
text.append(fLineDelimiter);
text.append(oldContents.substring(insertionPos));
}
} else {
text.append(includeString);
text.append(fLineDelimiter);
}
// add a blank line
text.append(fLineDelimiter);
} else if (oldContents != null) {
text.append(oldContents);
// add a blank line
text.append(fLineDelimiter);
}
@ -602,8 +644,8 @@ public class NewClassCodeGenerator {
monitor.done();
return newContents;
}
private void addHeaderInclude(ITranslationUnit sourceTU, ITranslationUnit headerTU, StringBuffer text, IProgressMonitor monitor) {
private String getHeaderIncludeString(ITranslationUnit sourceTU, ITranslationUnit headerTU, StringBuffer text, IProgressMonitor monitor) {
IProject project = headerTU.getCProject().getProject();
IPath projectLocation = project.getLocation();
IPath headerLocation = headerTU.getResource().getLocation();
@ -620,9 +662,68 @@ public class NewClassCodeGenerator {
if (includePath == null)
includePath = headerLocation;
String include = getIncludeString(includePath.toString(), isSystemIncludePath);
text.append(include);
text.append(fLineDelimiter);
return getIncludeString(includePath.toString(), isSystemIncludePath);
}
private boolean hasInclude(String contents, String include) {
int maxStartPos = contents.length() - include.length() - 1;
if (maxStartPos < 0) {
return false;
}
int startPos = 0;
while (startPos <= maxStartPos) {
int includePos = contents.indexOf(include, startPos);
if (includePos == -1) {
return false;
} else {
if (includePos == startPos) {
return true;
}
// TODO detect if it's commented out
// make sure it's on a line by itself
int linePos = findFirstLineChar(contents, includePos);
if (linePos == -1 || linePos == includePos) {
return true;
}
boolean badLine = false;
for (int pos = linePos; pos < includePos; ++pos) {
char c = contents.charAt(pos);
if (!Character.isWhitespace(c)) {
badLine = true;
break;
}
}
if (!badLine) {
return true;
}
// keep searching
startPos = includePos + include.length();
}
}
return false;
}
private int getIncludeInsertionPos(String contents) {
if (contents.length() == 0) {
return -1;
}
//TODO temporary hack
int includePos = contents.lastIndexOf("#include "); //$NON-NLS-1$
if (includePos != -1) {
// find the end of line
int startPos = includePos + "#include ".length(); //$NON-NLS-1$
int eolPos = findLastLineChar(contents, startPos);
if (eolPos != -1) {
int insertPos = eolPos + 1;
if (insertPos < (contents.length() - 1)) {
return insertPos;
}
}
}
return -1;
}
private void addMethodBodies(List publicMethods, List protectedMethods, List privateMethods, StringBuffer text, IProgressMonitor monitor) {
@ -660,7 +761,7 @@ public class NewClassCodeGenerator {
}
}
private static String getIncludeString(String fileName, boolean isSystemInclude) {
private String getIncludeString(String fileName, boolean isSystemInclude) {
StringBuffer buf = new StringBuffer();
buf.append("#include "); //$NON-NLS-1$
if (isSystemInclude)
@ -674,4 +775,41 @@ public class NewClassCodeGenerator {
buf.append('\"'); //$NON-NLS-1$
return buf.toString();
}
}
private int findLastLineChar(String contents, int startPos) {
int endPos = contents.length() - 1;
int linePos = startPos;
while (linePos <= endPos) {
char c = contents.charAt(linePos);
if (c == '\r') {
// could be '\r\n' as one delimiter
if (linePos < endPos && contents.charAt(linePos + 1) == '\n') {
return linePos + 1;
}
return linePos;
} else if (c == '\n') {
return linePos;
}
++linePos;
}
return -1;
}
private int findFirstLineChar(String contents, int startPos) {
int linePos = startPos;
while (linePos >= 0) {
char c = contents.charAt(linePos);
if (c == '\n' || c == '\r') {
if (linePos + 1 < startPos) {
return linePos + 1;
} else {
return -1;
}
}
--linePos;
}
return -1;
}
}

View file

@ -93,8 +93,8 @@ import org.eclipse.ui.views.contentoutline.ContentOutline;
public class NewClassCreationWizardPage extends NewElementWizardPage {
private static final int NAMESPACE_INDEX = 0;
private static final int CLASS_INDEX = 1;
// private static final int NAMESPACE_INDEX = 0;
// private static final int CLASS_INDEX = 1;
private final static String PAGE_NAME = "NewClassWizardPage"; //$NON-NLS-1$
private static final int MAX_FIELD_CHARS = 50;
@ -294,11 +294,12 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
fEnclosingTypeSelection.doFillIntoGrid(tabGroup, 1);
Text text= fEnclosingTypeDialogField.getTextControl(composite);
Text textControl= fEnclosingTypeDialogField.getTextControl(composite);
GridData gd= new GridData(GridData.FILL_HORIZONTAL);
gd.widthHint= getMaxFieldWidth();
gd.horizontalSpan= 2;
text.setLayoutData(gd);
textControl.setLayoutData(gd);
textControl.addFocusListener(new StatusFocusListener(ENCLOSING_TYPE_ID));
Button button= fEnclosingTypeDialogField.getChangeControl(composite);
gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
@ -700,7 +701,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
// enclosingType = chooseEnclosingClass();
// }
if (enclosingType != null) {
int changedFields = ENCLOSING_TYPE_ID;
int changedFields = ENCLOSING_TYPE_ID|CLASS_NAME_ID;
IPath oldFolderPath = getSourceFolderFullPath();
if (oldFolderPath == null) {
IPath headerPath = getHeaderFileFullPath();
@ -735,7 +736,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
if (field == fEnclosingTypeSelection) {
updateEnclosingTypeEnableState();
}
handleFieldChanged(ENCLOSING_TYPE_ID);
handleFieldChanged(ENCLOSING_TYPE_ID|CLASS_NAME_ID);
}
}
@ -976,6 +977,8 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
if (val.getSeverity() == IStatus.ERROR) {
status.setError(NewClassWizardMessages.getFormattedString("NewClassCreationWizardPage.error.InvalidNamespace", val.getMessage())); //$NON-NLS-1$
return status;
} else if (val.getSeverity() == IStatus.WARNING) {
status.setWarning(NewClassWizardMessages.getFormattedString("NewClassCreationWizardPage.warning.NamespaceDiscouraged", val.getMessage())); //$NON-NLS-1$
}
IProject project = getCurrentProject();
@ -1029,11 +1032,10 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
}
}
if (exactMatch) {
status.setError(NewClassWizardMessages.getString("NewClassCreationWizardPage.error.TypeMatchingNamespaceExists")); //$NON-NLS-1$
status.setWarning(NewClassWizardMessages.getString("NewClassCreationWizardPage.error.TypeMatchingNamespaceExists")); //$NON-NLS-1$
} else {
status.setError(NewClassWizardMessages.getString("NewClassCreationWizardPage.error.TypeMatchingNamespaceExistsDifferentCase")); //$NON-NLS-1$
status.setWarning(NewClassWizardMessages.getString("NewClassCreationWizardPage.error.TypeMatchingNamespaceExistsDifferentCase")); //$NON-NLS-1$
}
return status;
}
} else {
status.setWarning(NewClassWizardMessages.getString("NewClassCreationWizardPage.warning.NamespaceNotExists")); //$NON-NLS-1$
@ -1044,8 +1046,6 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
if (val.getSeverity() == IStatus.ERROR) {
status.setError(NewClassWizardMessages.getFormattedString("NewClassCreationWizardPage.error.InvalidNamespace", val.getMessage())); //$NON-NLS-1$
return status;
} else if (val.getSeverity() == IStatus.WARNING) {
status.setWarning(NewClassWizardMessages.getFormattedString("NewClassCreationWizardPage.warning.NamespaceDiscouraged", val.getMessage())); //$NON-NLS-1$
}
return status;
@ -2131,8 +2131,7 @@ public class NewClassCreationWizardPage extends NewElementWizardPage {
getHeaderFileFullPath(),
getSourceFileFullPath(),
getClassTypeName(),
// isNamespaceButtonSelected() ? getEnclosingTypeName() : null,
getEnclosingTypeName(),
isEnclosingTypeSelected() ? getEnclosingTypeName() : null,
getBaseClasses(),
getCheckedMethodStubs());
fCodeGenerator.createClass(monitor);