From 54534b7ef08288305fab9279a9a3aa6aebdbf351 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Fri, 11 Apr 2008 04:59:41 +0000 Subject: [PATCH] =?UTF-8?q?Patch=20for=20bug=20203382=20provided=20by=20To?= =?UTF-8?q?r=20Arne=20Vestb=C3=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/ui/editor/CDocumentProvider.java | 45 +++++++++++++++---- .../ui/preferences/CEditorPreferencePage.java | 6 ++- .../ui/preferences/PreferencesMessages.java | 1 + .../PreferencesMessages.properties | 1 + .../eclipse/cdt/ui/PreferenceConstants.java | 8 ++++ 5 files changed, 52 insertions(+), 9 deletions(-) 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 f2457491d3e..2ed692b67cd 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 @@ -45,6 +45,7 @@ import org.eclipse.jface.text.source.IAnnotationModelListener; import org.eclipse.jface.text.source.IAnnotationModelListenerExtension; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; +import org.eclipse.text.edits.DeleteEdit; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.IStorageEditorInput; @@ -316,7 +317,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { /** * Annotation model dealing with c marker annotations and temporary problems. - * Also acts as problem requestor for its translation unit. Initialiy inactive. Must explicitly be + * Also acts as a problem requestor for its translation unit. Initially inactive. Must be explicitly * activated. */ protected static class TranslationUnitAnnotationModel extends ResourceMarkerAnnotationModel implements IProblemRequestor, IProblemRequestorExtension { @@ -502,7 +503,6 @@ public class CDocumentProvider extends TextFileDocumentProvider { boolean temporaryProblemsChanged= false; synchronized (getLockObject()) { - boolean isCanceled= false; fPreviouslyOverlaid= fCurrentlyOverlaid; @@ -515,7 +515,6 @@ public class CDocumentProvider extends TextFileDocumentProvider { } if (reportedProblems != null && reportedProblems.size() > 0) { - Iterator e= reportedProblems.iterator(); while (e.hasNext()) { @@ -578,7 +577,7 @@ public class CDocumentProvider extends TextFileDocumentProvider { } } - private void overlayMarkers(Position position, ProblemAnnotation problemAnnotation) { + private void overlayMarkers(Position position, ProblemAnnotation problemAnnotation) { Object value= getAnnotations(position); if (value instanceof List) { List list= (List) value; @@ -648,9 +647,9 @@ public class CDocumentProvider extends TextFileDocumentProvider { synchronized (getLockObject()) { Object cached= fReverseMap.get(position); - if (cached == null) + if (cached == null) { fReverseMap.put(position, annotation); - else if (cached instanceof List) { + } else if (cached instanceof List) { List list= (List) cached; list.add(annotation); } else if (cached instanceof Annotation) { @@ -956,8 +955,38 @@ public class CDocumentProvider extends TextFileDocumentProvider { // empty files. int lastLineLength = document.getLineLength(lastLineIndex); if (lastLineLength != 0) { - document.replace(document.getLength(), 0, TextUtilities - .getDefaultLineDelimiter(document)); + document.replace(document.getLength(), 0, + TextUtilities.getDefaultLineDelimiter(document)); + } + } catch (BadLocationException e) { + } + } + + // Remove trailing whitespace when saving. Triggered by the flag + // in Preferences -> C/C++ -> Editor + if (PreferenceConstants.getPreferenceStore().getBoolean( + PreferenceConstants.REMOVE_TRAILING_WHITESPACE)) { + try { + int lineCount= document.getNumberOfLines(); + for (int i= 0; i < lineCount; i++) { + + IRegion region= document.getLineInformation(i); + if (region.getLength() == 0) + continue; + + int lineStart= region.getOffset(); + int lineExclusiveEnd= lineStart + region.getLength(); + + // Find the rightmost none-whitespace character + int charPos= lineExclusiveEnd - 1; + while (charPos >= lineStart && Character.isWhitespace(document.getChar(charPos))) + charPos--; + + charPos++; + if (charPos < lineExclusiveEnd) { + DeleteEdit edit= new DeleteEdit(charPos, lineExclusiveEnd - charPos); + edit.apply(document); + } } } catch (BadLocationException e) { } 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 a2b4759ba7c..b0f0210dec7 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 @@ -82,6 +82,7 @@ public class CEditorPreferencePage extends AbstractPreferencePage { 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, PreferenceConstants.ENSURE_NEWLINE_AT_EOF)); + overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.REMOVE_TRAILING_WHITESPACE)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, ContentAssistPreference.PROPOSALS_BACKGROUND)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, ContentAssistPreference.PROPOSALS_FOREGROUND)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, ContentAssistPreference.PARAMETERS_BACKGROUND)); @@ -142,7 +143,10 @@ public class CEditorPreferencePage extends AbstractPreferencePage { label = PreferencesMessages.CEditorPreferencePage_behaviorPage_ensureNewline; addCheckBox(behaviorComposite, label, PreferenceConstants.ENSURE_NEWLINE_AT_EOF, 0); - + + label = PreferencesMessages.CEditorPreferencePage_behaviorPage_removeTrailingWhitespace; + addCheckBox(behaviorComposite, label, PreferenceConstants.REMOVE_TRAILING_WHITESPACE, 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.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.java index d7864ca419a..962774d7b8c 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/preferences/PreferencesMessages.java @@ -91,6 +91,7 @@ public final class PreferencesMessages extends NLS { public static String CEditorColoringConfigurationBlock_strikethrough; public static String CEditorPreferencePage_colorPage_systemDefault; public static String CEditorPreferencePage_behaviorPage_ensureNewline; + public static String CEditorPreferencePage_behaviorPage_removeTrailingWhitespace; public static String CEditorPreferencePage_behaviorPage_matchingBrackets; public static String CEditorPreferencePage_behaviorPage_subWordNavigation; public static String CEditorPreferencePage_behaviorPage_inactiveCode; 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 68398bdc37e..34c64b612c7 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 @@ -86,6 +86,7 @@ CEditorColoringConfigurationBlock_strikethrough=&Strikethrough CEditorPreferencePage_colorPage_systemDefault=S&ystem Default CEditorPreferencePage_behaviorPage_ensureNewline=Ensure &newline at end of file when saving +CEditorPreferencePage_behaviorPage_removeTrailingWhitespace=Remove trailing &whitespace 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 ac0f63130ce..0b2fa6d1c2d 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 @@ -927,6 +927,13 @@ public class PreferenceConstants { */ public final static String ENSURE_NEWLINE_AT_EOF = "ensureNewlineAtEOF"; //$NON-NLS-1$ + /** + * Preference key for whether to remove trailing whitespace when saving. + * + * @since 5.0 + */ + public final static String REMOVE_TRAILING_WHITESPACE = "removeTrailingWhitespace"; //$NON-NLS-1$ + /** * A named preference that defines whether the hint to make hover sticky should be shown. * @@ -1410,6 +1417,7 @@ public class PreferenceConstants { store.setDefault(PreferenceConstants.EDITOR_AUTO_INDENT, true); store.setDefault(PreferenceConstants.ENSURE_NEWLINE_AT_EOF, false); + store.setDefault(PreferenceConstants.REMOVE_TRAILING_WHITESPACE, false); // formatter profile store.setDefault(PreferenceConstants.FORMATTER_PROFILE, FormatterProfileManager.DEFAULT_PROFILE);