diff options
author | Chris Wiebe | 2004-12-04 01:55:51 +0000 |
---|---|---|
committer | Chris Wiebe | 2004-12-04 01:55:51 +0000 |
commit | 07d5d890117da344d2e7ed88fe5c133909e29e7e (patch) | |
tree | e4f371f6a130d0c387632b8c10772b12fd1a2f04 | |
parent | 1ace2429aed4e1c6dcbd2b065cd4b229cb703549 (diff) | |
download | org.eclipse.cdt-07d5d890117da344d2e7ed88fe5c133909e29e7e.tar.gz org.eclipse.cdt-07d5d890117da344d2e7ed88fe5c133909e29e7e.tar.xz org.eclipse.cdt-07d5d890117da344d2e7ed88fe5c133909e29e7e.zip |
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
3 files changed, 169 insertions, 27 deletions
diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog index abb3992d52b..2939a0a8830 100644 --- a/core/org.eclipse.cdt.ui/ChangeLog +++ b/core/org.eclipse.cdt.ui/ChangeLog @@ -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) This seems to be of importance for some ISV(including QNX) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCodeGenerator.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCodeGenerator.java index f79a271ef8d..c6a39618f25 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCodeGenerator.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCodeGenerator.java @@ -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(); } - }
\ No newline at end of file + + 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; + } + +} + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCreationWizardPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCreationWizardPage.java index 61719693e29..9c1555ffc2d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCreationWizardPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/classwizard/NewClassCreationWizardPage.java @@ -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); |