diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java index 8a7217d77c7..7aa4ddee40d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/CDocumentProvider.java @@ -29,6 +29,7 @@ import org.eclipse.jface.text.DefaultLineTracker; import org.eclipse.jface.text.IDocument; 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 +923,34 @@ public class CDocumentProvider extends TextFileDocumentProvider { * @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 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 end of file when saving + if (PreferenceConstants.getPreferenceStore().getBoolean( + PreferenceConstants.ENSURE_NEWLINE_AT_EOF)) { + // even if the document is empty, there will be at least 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() { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CEditorPreferencePage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CEditorPreferencePage.java index 019f068a6a4..9d4f977eeb2 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CEditorPreferencePage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/CEditorPreferencePage.java @@ -132,6 +132,7 @@ public class CEditorPreferencePage extends AbstractPreferencePage implements IWo overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PreferenceConstants.EDITOR_TASK_INDICATION_COLOR)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_TASK_INDICATION)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER)); + overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.ENSURE_NEWLINE_AT_EOF)); OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys.size()]; overlayKeys.toArray(keys); @@ -352,6 +353,9 @@ public class CEditorPreferencePage extends AbstractPreferencePage implements IWo label = PreferencesMessages.getString("CEditorPreferencePage.behaviorPage.tabSpace"); //$NON-NLS-1$ addCheckBox(behaviorComposite, label, CEditor.SPACES_FOR_TABS, 0); + label = PreferencesMessages.getString("CEditorPreferencePage.behaviorPage.ensureNewline"); //$NON-NLS-1$ + addCheckBox(behaviorComposite, label, PreferenceConstants.ENSURE_NEWLINE_AT_EOF, 0); + Label l = new Label(behaviorComposite, SWT.LEFT); GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); gd.horizontalSpan = 2; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties index d109638c9ad..352a8ad9053 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.properties @@ -107,6 +107,7 @@ CEditorPreferencePage.colorPage.color=C&olor: CEditorPreferencePage.colorPage.bold=&Bold CEditorPreferencePage.colorPage.preview=Preview: CEditorPreferencePage.behaviorPage.tabSpace=&Insert space for tabs +CEditorPreferencePage.behaviorPage.ensureNewline=Ensure &newline at end of file when saving CEditorPreferencePage.behaviorPage.matchingBrackets=Highlight &matching brackets CEditorPreferencePage.behaviorPage.subWordNavigation=Smart &caret positioning in identifiers CEditorPreferencePage.behaviorPage.inactiveCode=Highlight &inactive code diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/PreferenceConstants.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/PreferenceConstants.java index 460cd545453..bc7bfc0c19f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/PreferenceConstants.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/PreferenceConstants.java @@ -408,6 +408,14 @@ public class PreferenceConstants { */ public static final String TEMPLATES_USE_CODEFORMATTER= "org.eclipse.cdt.ui.text.templates.format"; //$NON-NLS-1$ + + /** + * Preference key for whether to ensure a newline at the end of files when saving. + * + * @since 4.0 + */ + public final static String ENSURE_NEWLINE_AT_EOF = "ensureNewlineAtEOF"; //$NON-NLS-1$ + /** * Returns the CDT-UI preference store. * @@ -462,5 +470,7 @@ public class PreferenceConstants { store.setDefault(PreferenceConstants.EDITOR_CLOSE_BRACES, true); store.setDefault(PreferenceConstants.EDITOR_WRAP_STRINGS, true); store.setDefault(PreferenceConstants.EDITOR_ESCAPE_STRINGS, false); + + store.setDefault(PreferenceConstants.ENSURE_NEWLINE_AT_EOF, false); } }