mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-08 16:55:38 +02:00
Patch from Abeer Bagul for bug 26639 - option to ensure newline at end of file
This commit is contained in:
parent
b87f4ffff2
commit
0c3a0662c4
4 changed files with 44 additions and 0 deletions
|
@ -29,6 +29,7 @@ import org.eclipse.jface.text.DefaultLineTracker;
|
||||||
import org.eclipse.jface.text.IDocument;
|
import org.eclipse.jface.text.IDocument;
|
||||||
import org.eclipse.jface.text.ILineTracker;
|
import org.eclipse.jface.text.ILineTracker;
|
||||||
import org.eclipse.jface.text.Position;
|
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.Annotation;
|
||||||
import org.eclipse.jface.text.source.AnnotationModelEvent;
|
import org.eclipse.jface.text.source.AnnotationModelEvent;
|
||||||
import org.eclipse.jface.text.source.IAnnotationAccessExtension;
|
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)
|
* @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 {
|
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);
|
final FileInfo info= getFileInfo(element);
|
||||||
if (info instanceof TranslationUnitInfo) {
|
if (info instanceof TranslationUnitInfo) {
|
||||||
return new DocumentProviderOperation() {
|
return new DocumentProviderOperation() {
|
||||||
|
|
|
@ -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.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));
|
||||||
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER));
|
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()];
|
OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
|
||||||
overlayKeys.toArray(keys);
|
overlayKeys.toArray(keys);
|
||||||
|
@ -352,6 +353,9 @@ public class CEditorPreferencePage extends AbstractPreferencePage implements IWo
|
||||||
label = PreferencesMessages.getString("CEditorPreferencePage.behaviorPage.tabSpace"); //$NON-NLS-1$
|
label = PreferencesMessages.getString("CEditorPreferencePage.behaviorPage.tabSpace"); //$NON-NLS-1$
|
||||||
addCheckBox(behaviorComposite, label, CEditor.SPACES_FOR_TABS, 0);
|
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);
|
Label l = new Label(behaviorComposite, SWT.LEFT);
|
||||||
GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
|
GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
|
||||||
gd.horizontalSpan = 2;
|
gd.horizontalSpan = 2;
|
||||||
|
|
|
@ -107,6 +107,7 @@ CEditorPreferencePage.colorPage.color=C&olor:
|
||||||
CEditorPreferencePage.colorPage.bold=&Bold
|
CEditorPreferencePage.colorPage.bold=&Bold
|
||||||
CEditorPreferencePage.colorPage.preview=Preview:
|
CEditorPreferencePage.colorPage.preview=Preview:
|
||||||
CEditorPreferencePage.behaviorPage.tabSpace=&Insert space for tabs
|
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.matchingBrackets=Highlight &matching brackets
|
||||||
CEditorPreferencePage.behaviorPage.subWordNavigation=Smart &caret positioning in identifiers
|
CEditorPreferencePage.behaviorPage.subWordNavigation=Smart &caret positioning in identifiers
|
||||||
CEditorPreferencePage.behaviorPage.inactiveCode=Highlight &inactive code
|
CEditorPreferencePage.behaviorPage.inactiveCode=Highlight &inactive code
|
||||||
|
|
|
@ -408,6 +408,14 @@ public class PreferenceConstants {
|
||||||
*/
|
*/
|
||||||
public static final String TEMPLATES_USE_CODEFORMATTER= "org.eclipse.cdt.ui.text.templates.format"; //$NON-NLS-1$
|
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.
|
* Returns the CDT-UI preference store.
|
||||||
*
|
*
|
||||||
|
@ -462,5 +470,7 @@ public class PreferenceConstants {
|
||||||
store.setDefault(PreferenceConstants.EDITOR_CLOSE_BRACES, true);
|
store.setDefault(PreferenceConstants.EDITOR_CLOSE_BRACES, true);
|
||||||
store.setDefault(PreferenceConstants.EDITOR_WRAP_STRINGS, true);
|
store.setDefault(PreferenceConstants.EDITOR_WRAP_STRINGS, true);
|
||||||
store.setDefault(PreferenceConstants.EDITOR_ESCAPE_STRINGS, false);
|
store.setDefault(PreferenceConstants.EDITOR_ESCAPE_STRINGS, false);
|
||||||
|
|
||||||
|
store.setDefault(PreferenceConstants.ENSURE_NEWLINE_AT_EOF, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue