Index: src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties,v retrieving revision 1.45 diff -u -r1.45 PreferencesMessages.properties --- src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties 17 Jul 2006 14:26:49 -0000 1.45 +++ src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties 20 Jul 2006 13:24:05 -0000 @@ -107,6 +107,7 @@ CEditorPreferencePage.colorPage.bold=&Bold CEditorPreferencePage.colorPage.preview=Preview: CEditorPreferencePage.behaviorPage.tabSpace=&Insert space for tabs +CEditorPreferencePage.behaviorPage.ensureNewline=Add missing newline at the end of the file when saving. CEditorPreferencePage.behaviorPage.matchingBrackets=Highlight &matching brackets CEditorPreferencePage.behaviorPage.subWordNavigation=Smart &caret positioning in identifiers CEditorPreferencePage.behaviorPage.inactiveCode=Highlight &inactive code Index: src/org/eclipse/cdt/internal/ui/preferences/CEditorPreferencePage.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CEditorPreferencePage.java,v retrieving revision 1.58 diff -u -r1.58 CEditorPreferencePage.java --- src/org/eclipse/cdt/internal/ui/preferences/CEditorPreferencePage.java 17 Jul 2006 14:26:49 -0000 1.58 +++ src/org/eclipse/cdt/internal/ui/preferences/CEditorPreferencePage.java 20 Jul 2006 13:24:05 -0000 @@ -126,6 +126,7 @@ overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, CEditor.INACTIVE_CODE_COLOR)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, CEditor.INACTIVE_CODE_ENABLE)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, CEditor.SPACES_FOR_TABS)); + overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, CEditor.ENSURE_NEWLINE_AT_EOF)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PreferenceConstants.EDITOR_TASK_TAG_COLOR)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_TASK_TAG_BOLD)); @@ -152,6 +153,8 @@ store.setDefault(CEditor.SUB_WORD_NAVIGATION, true); + store.setDefault(CEditor.ENSURE_NEWLINE_AT_EOF, false); + store.setDefault(CEditor.MATCHING_BRACKETS, true); PreferenceConverter.setDefault(store, CEditor.MATCHING_BRACKETS_COLOR, new RGB(170,170,170)); Index: src/org/eclipse/cdt/internal/ui/editor/CEditor.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CEditor.java,v retrieving revision 1.107 diff -u -r1.107 CEditor.java --- src/org/eclipse/cdt/internal/ui/editor/CEditor.java 19 Jul 2006 14:31:57 -0000 1.107 +++ src/org/eclipse/cdt/internal/ui/editor/CEditor.java 20 Jul 2006 13:24:04 -0000 @@ -534,6 +534,8 @@ public static final String INACTIVE_CODE_COLOR = "inactiveCodeColor"; //$NON-NLS-1$ /** Preference key for inserting spaces rather than tabs */ public final static String SPACES_FOR_TABS = "spacesForTabs"; //$NON-NLS-1$ + /** Preference key for ensuring a newline at the end of header files and c files */ + public final static String ENSURE_NEWLINE_AT_EOF = "ensureNewlineAtEOF"; //$NON-NLS-1$ /** Preference key for compiler task tags */ private final static String TRANSLATION_TASK_TAGS= CCorePreferenceConstants.TRANSLATION_TASK_TAGS; Index: src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java,v retrieving revision 1.33 diff -u -r1.33 CDocumentProvider.java --- src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java 6 Jul 2006 14:52:55 -0000 1.33 +++ src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java 20 Jul 2006 13:24:03 -0000 @@ -27,8 +27,10 @@ import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.DefaultLineTracker; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IDocumentExtension4; import org.eclipse.jface.text.ILineTracker; import org.eclipse.jface.text.Position; +import org.eclipse.jface.text.TextUtilities; import org.eclipse.jface.text.source.Annotation; import org.eclipse.jface.text.source.AnnotationModelEvent; import org.eclipse.jface.text.source.IAnnotationAccessExtension; @@ -922,6 +924,31 @@ * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createSaveOperation(java.lang.Object, org.eclipse.jface.text.IDocument, boolean) */ protected DocumentProviderOperation createSaveOperation(final Object element, final IDocument document, final boolean overwrite) throws CoreException { + //add a newline to the end of the document (if it is not already present) + //----------------------------------------------------------------------- + //for people who do not want auto-modification of their header files and .c files, + //this flag will prevent addition of a newline unless the user + //explicitly sets the preference thru Window -> Preferences -> C/C++ -> Editor -> Appearance Tab -> Ensure newline at the end of header files and .c files + if (PreferenceConstants.getPreferenceStore().getBoolean(CEditor.ENSURE_NEWLINE_AT_EOF)) + { + //even if the document is empty, there will be atleast one line in it (the 0th one) + int lastLineIndex = document.getNumberOfLines() - 1; + + try + { + //we have to ensure that the length of the last line is 0. + //this will also take care of empty files. empty files have only one line + //in them and the length of this one and only line is 0. + //Thus we do not need to append an extra line separator to empty files + int lastLineLength = document.getLineLength(lastLineIndex); + if (lastLineLength != 0) + { + document.replace(document.getLength(), 0, TextUtilities.getDefaultLineDelimiter(document)); + } + } catch (BadLocationException e) { + } + } + final FileInfo info= getFileInfo(element); if (info instanceof TranslationUnitInfo) { return new DocumentProviderOperation() {