1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-13 11:15:38 +02:00

Bug 249359. An option to remove trailing whitespace in either edited or all lines. End-of-file newline doesn't depend on changed lines. Separate preference page for save actions.

This commit is contained in:
Sergey Prigogin 2009-04-25 22:46:49 +00:00
parent 02d1f6533a
commit 5c017b1daf
11 changed files with 198 additions and 56 deletions

View file

@ -182,7 +182,8 @@ SmartTypingPreferencePage.name=Typing
ColoringPreferencePage.name=Syntax Coloring
FoldingPreferencePage.name=Folding
HoverPreferencePage.name=Hovers
markOccurrencesPreferencePage.name= Mark Occurrences
markOccurrencesPreferencePage.name=Mark Occurrences
SaveActionsPreferencePage.name=Save Actions
ScalabilityPreferencePage.name=Scalability
DefaultBinaryFileEditor.name = Default Binary File Editor
@ -528,6 +529,7 @@ preferenceKeywords.indexer=index skip references type macro search build configu
preferenceKeywords.ceditor=editor appearance navigation colors smart caret positioning highlight matching bracket inactive code foreground background save trailing whitespace newline doxygen
preferenceKeywords.contentassist=editor content code assist complete completion insert overwrite single proposal common prefix automatically auto activation trigger category categories separate specific word hippie template
preferenceKeywords.saveactions=editor save trailing whitespace white space end file newline
preferenceKeywords.scalability=editor mode large file lines disable performance
preferenceKeywords.hover=editor hover annotation key modifier combined variable problem string source documentation
preferenceKeywords.syntaxcoloring=editor colors semantic coloring highlighting multi line single line comment task tag class struct union function method static field constant keywords local variable operators brackets strings type variable inherited method declaration

View file

@ -709,6 +709,14 @@
<keywordReference id="org.eclipse.cdt.ui.markoccurrences"/>
<keywordReference id="org.eclipse.cdt.ui.common"/>
</page>
<page
name="%SaveActionsPreferencePage.name"
category="org.eclipse.cdt.ui.preferences.CEditorPreferencePage"
class="org.eclipse.cdt.internal.ui.preferences.SaveActionsPreferencePage"
id="org.eclipse.cdt.ui.preferences.SaveActionsPreferencePage">
<keywordReference id="org.eclipse.cdt.ui.saveactions"/>
<keywordReference id="org.eclipse.cdt.ui.common"/>
</page>
<page
name="%ScalabilityPreferencePage.name"
category="org.eclipse.cdt.ui.preferences.CEditorPreferencePage"
@ -755,6 +763,9 @@
<keyword
label="%preferenceKeywords.hover"
id="org.eclipse.cdt.ui.hover"/>
<keyword
label="%preferenceKeywords.saveactions"
id="org.eclipse.cdt.ui.saveactions"/>
<keyword
label="%preferenceKeywords.scalability"
id="org.eclipse.cdt.ui.scalability"/>

View file

@ -167,6 +167,6 @@ public interface ICHelpContextIds {
public static final String PATHENTRY_VARIABLES_PREFERENCE_PAGE= PREFIX + "pathentry_variables_preference_page_context"; //$NON-NLS-1$
public static final String SAVE_ACTIONS_PREFERENCE_PAGE = PREFIX + "save_actions_preference_page_context"; //$NON-NLS-1$
public static final String SCALABILITY_PREFERENCE_PAGE = PREFIX + "scalability_preference_page_context"; //$NON-NLS-1$
}

View file

@ -41,6 +41,7 @@ import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.ILineTracker;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationModel;
@ -1037,7 +1038,9 @@ public class CDocumentProvider extends TextFileDocumentProvider {
*/
private void performSaveActions(ITextFileBuffer buffer, IProgressMonitor monitor) throws CoreException {
if (shouldRemoveTrailingWhitespace() || shouldAddNewlineAtEof()) {
IRegion[] changedRegions= EditorUtility.calculateChangedLineRegions(buffer, getSubProgressMonitor(monitor, 20));
IRegion[] changedRegions= needsChangedRegions() ?
EditorUtility.calculateChangedLineRegions(buffer, getSubProgressMonitor(monitor, 20)) :
null;
IDocument document = buffer.getDocument();
TextEdit edit = createSaveActionEdit(document, changedRegions);
if (edit != null) {
@ -1071,14 +1074,28 @@ public class CDocumentProvider extends TextFileDocumentProvider {
PreferenceConstants.REMOVE_TRAILING_WHITESPACE);
}
private static boolean isLimitedRemoveTrailingWhitespace() {
return PreferenceConstants.getPreferenceStore().getBoolean(
PreferenceConstants.REMOVE_TRAILING_WHITESPACE_LIMIT_TO_EDITED_LINES);
}
private static boolean needsChangedRegions() {
return shouldRemoveTrailingWhitespace() && isLimitedRemoveTrailingWhitespace();
}
/**
* Creates a text edit for the save actions.
* @return a text edit, or <code>null</code> if the save actions leave the file intact.
*/
private TextEdit createSaveActionEdit(IDocument document, IRegion[] changedRegions) {
TextEdit rootEdit = null;
TextEdit lastWhitespaceEdit = null;
try {
if (shouldRemoveTrailingWhitespace()) {
if (!isLimitedRemoveTrailingWhitespace()) {
// Pretend that the whole document changed.
changedRegions = new IRegion[] { new Region(0, document.getLength()) };
}
// Remove trailing whitespace from changed lines.
for (IRegion region : changedRegions) {
int firstLine = document.getLineOfOffset(region.getOffset());
@ -1098,11 +1115,11 @@ public class CDocumentProvider extends TextFileDocumentProvider {
charPos++;
if (charPos < lineEnd) {
TextEdit edit= new DeleteEdit(charPos, lineEnd - charPos);
lastWhitespaceEdit= new DeleteEdit(charPos, lineEnd - charPos);
if (rootEdit == null) {
rootEdit = new MultiTextEdit();
}
rootEdit.addChild(edit);
rootEdit.addChild(lastWhitespaceEdit);
}
}
}
@ -1111,22 +1128,18 @@ public class CDocumentProvider extends TextFileDocumentProvider {
// Add newline at the end of the file.
int endOffset = document.getLength();
IRegion lastLineRegion = document.getLineInformationOfOffset(endOffset);
if (lastLineRegion.getLength() != 0) {
for (IRegion region : changedRegions) {
if (region.getOffset() + region.getLength() >= lastLineRegion.getOffset()) {
// Last line has changed
if (!shouldRemoveTrailingWhitespace() || !isWhitespaceRegion(document, lastLineRegion)) {
TextEdit edit = new InsertEdit(endOffset,
TextUtilities.getDefaultLineDelimiter(document));
if (rootEdit == null) {
rootEdit = edit;
} else {
rootEdit.addChild(edit);
}
}
break;
}
}
// Insert newline at the end of the document if the last line is not empty and
// will not become empty after removal of trailing whitespace.
if (lastLineRegion.getLength() != 0 &&
(lastWhitespaceEdit == null ||
lastWhitespaceEdit.getOffset() != lastLineRegion.getOffset() ||
lastWhitespaceEdit.getLength() != lastLineRegion.getLength())) {
TextEdit edit = new InsertEdit(endOffset, TextUtilities.getDefaultLineDelimiter(document));
if (rootEdit == null) {
rootEdit = edit;
} else {
rootEdit.addChild(edit);
}
}
}
} catch (BadLocationException e) {

View file

@ -120,7 +120,8 @@ public abstract class AbstractPreferencePage extends PreferencePage implements I
radioButton.setLayoutData(gd);
radioButton.addSelectionListener(fCheckBoxListener);
fCheckBoxes.put(radioButton, key);
if (key != null)
fCheckBoxes.put(radioButton, key);
return radioButton;
}

View file

@ -81,8 +81,6 @@ public class CEditorPreferencePage extends AbstractPreferencePage {
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, CEditor.MATCHING_BRACKETS));
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));
@ -228,22 +226,6 @@ public class CEditorPreferencePage extends AbstractPreferencePage {
return behaviorComposite;
}
private Control createSaveActionsBlock(Composite parent) {
Composite saveActionsComposite= ControlFactory.createGroup(parent, PreferencesMessages.CEditorPreferencePage_SaveActionsTitle, 1);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
saveActionsComposite.setLayout(layout);
String label = PreferencesMessages.CEditorPreferencePage_behaviorPage_removeTrailingWhitespace;
addCheckBox(saveActionsComposite, label, PreferenceConstants.REMOVE_TRAILING_WHITESPACE, 0);
label = PreferencesMessages.CEditorPreferencePage_behaviorPage_ensureNewline;
addCheckBox(saveActionsComposite, label, PreferenceConstants.ENSURE_NEWLINE_AT_EOF, 0);
return saveActionsComposite;
}
private void handleAppearanceColorListSelection() {
int i = fAppearanceColorList.getSelectionIndex();
String key = fAppearanceColorListModel[i][1];
@ -293,25 +275,25 @@ public class CEditorPreferencePage extends AbstractPreferencePage {
fOverlayStore.load();
fOverlayStore.start();
createHeader(parent);
Composite contents= ControlFactory.createComposite(parent, 1);
contents.setLayoutData(new GridData(GridData.FILL_BOTH));
ControlFactory.createEmptySpace(parent, 2);
createBehaviorBlock(parent);
createHeader(contents);
ControlFactory.createEmptySpace(parent, 2);
createSaveActionsBlock(parent);
ControlFactory.createEmptySpace(contents, 2);
createBehaviorBlock(contents);
ControlFactory.createEmptySpace(parent, 2);
ControlFactory.createEmptySpace(contents, 2);
String dsc= PreferencesMessages.CEditorPreferencePage_SelectDocToolDescription;
String msg= PreferencesMessages.CEditorPreferencePage_WorkspaceDefaultLabel;
IDocCommentOwner workspaceOwner= DocCommentOwnerManager.getInstance().getWorkspaceCommentOwner();
fDocCommentOwnerComposite= new DocCommentOwnerComposite(parent, workspaceOwner, dsc, msg);
fDocCommentOwnerComposite.setLayoutData(GridDataFactory.fillDefaults().grab(true,false).create());
fDocCommentOwnerComposite= new DocCommentOwnerComposite(contents, workspaceOwner, dsc, msg);
fDocCommentOwnerComposite.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
initialize();
return parent;
return contents;
}
private void initialize() {
@ -357,5 +339,4 @@ public class CEditorPreferencePage extends AbstractPreferencePage {
initializeDefaultColors();
handleAppearanceColorListSelection();
}
}

View file

@ -171,7 +171,7 @@ class MarkOccurrencesConfigurationBlock implements IPreferenceConfigurationBlock
private static void indent(Control control) {
GridData gridData= new GridData();
gridData.horizontalIndent= 10;
gridData.horizontalIndent= 20;
control.setLayoutData(gridData);
}

View file

@ -90,8 +90,6 @@ public final class PreferencesMessages extends NLS {
public static String CEditorColoringConfigurationBlock_underline;
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;
@ -186,6 +184,11 @@ public final class PreferencesMessages extends NLS {
public static String CEditorPreferencePage_typing_smartTab;
public static String CEditorPreferencePage_WorkspaceDefaultLabel;
public static String SaveActionsPreferencePage_removeTrailingWhitespace;
public static String SaveActionsPreferencePage_inEditedLines;
public static String SaveActionsPreferencePage_inAllLines;
public static String SaveActionsPreferencePage_ensureNewline;
public static String SmartTypingConfigurationBlock_autoclose_title;
public static String SmartTypingConfigurationBlock_autoindent_newlines;
public static String SmartTypingConfigurationBlock_autoindent_title;

View file

@ -29,7 +29,7 @@ CEditorPreferencePage_ContentAssistPage_showProposalsInAlphabeticalOrder=Present
CEditorPreferencePage_ContentAssistPage_autoActivationGroupTitle=Auto-Activation
CEditorPreferencePage_ContentAssistPage_autoActivationEnableDot=Enable "." as trigger
CEditorPreferencePage_ContentAssistPage_autoActivationEnableArrow=Enable "->" as trigger
CEditorPreferencePage_ContentAssistPage_autoActivationDelay=dela&y (ms)
CEditorPreferencePage_ContentAssistPage_autoActivationDelay=Dela&y (ms)
CEditorPreferencePage_ContentAssistPage_proposalFilterSelect=Completion Proposal Filter:
CEditorPreferencePage_ContentAssistPage_completionProposalBackgroundColor=Completion proposal background
CEditorPreferencePage_ContentAssistPage_completionProposalForegroundColor=Completion proposal foreground
@ -85,8 +85,6 @@ CEditorColoringConfigurationBlock_underline=&Underline
CEditorColoringConfigurationBlock_strikethrough=&Strikethrough
CEditorPreferencePage_colorPage_systemDefault=S&ystem Default
CEditorPreferencePage_behaviorPage_removeTrailingWhitespace=Remove trailing &whitespace in edited lines
CEditorPreferencePage_behaviorPage_ensureNewline=Ensure &newline at the end of file
CEditorPreferencePage_behaviorPage_matchingBrackets=Highlight &matching brackets
CEditorPreferencePage_behaviorPage_subWordNavigation=Smart &caret positioning in identifiers
CEditorPreferencePage_behaviorPage_inactiveCode=Highlight &inactive code
@ -95,6 +93,11 @@ CEditorPreferencePage_behaviorPage_matchingBracketColor=Matching brackets highli
CEditorPreferencePage_behaviorPage_inactiveCodeColor=Inactive code highlight
CEditorPreferencePage_behaviorPage_Color=Color:
SaveActionsPreferencePage_removeTrailingWhitespace=Remove trailing &whitespace
SaveActionsPreferencePage_inEditedLines=In &edited lines
SaveActionsPreferencePage_inAllLines=In a&ll lines
SaveActionsPreferencePage_ensureNewline=Ensure &newline at the end of file
TemplatePreferencePage_Viewer_preview=Preview:
CFileTypesPreferencePage_description=C/C++ File Types

View file

@ -0,0 +1,117 @@
/*******************************************************************************
* Copyright (c) 2009 Google, Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Sergey Prigogin (Google) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.preferences;
import java.util.ArrayList;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.PlatformUI;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.utils.ui.controls.ControlFactory;
import org.eclipse.cdt.internal.ui.ICHelpContextIds;
import org.eclipse.cdt.internal.ui.preferences.OverlayPreferenceStore.OverlayKey;
/*
* The page for configuring actions performed when a C/C++ file is saved.
*/
public class SaveActionsPreferencePage extends AbstractPreferencePage {
private Button fRadioEditedLines;
private Button fRadioAllLines;
public SaveActionsPreferencePage() {
super();
}
@Override
protected OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
ArrayList<OverlayKey> overlayKeys = new ArrayList<OverlayKey>();
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
PreferenceConstants.REMOVE_TRAILING_WHITESPACE));
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
PreferenceConstants.REMOVE_TRAILING_WHITESPACE_LIMIT_TO_EDITED_LINES));
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
PreferenceConstants.ENSURE_NEWLINE_AT_EOF));
OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
overlayKeys.toArray(keys);
return keys;
}
/*
* @see PreferencePage#createControl(Composite)
*/
@Override
public void createControl(Composite parent) {
super.createControl(parent);
PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
ICHelpContextIds.SAVE_ACTIONS_PREFERENCE_PAGE);
}
// sets enabled flag for a control and all its sub-tree
protected static void setEnabled(Control control, boolean enable) {
control.setEnabled(enable);
if (control instanceof Composite) {
Composite composite = (Composite) control;
Control[] children = composite.getChildren();
for (Control element : children)
setEnabled(element, enable);
}
}
private Control createConfigurationBlock(Composite parent) {
Composite composite= ControlFactory.createComposite(parent, 1);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
String label = PreferencesMessages.SaveActionsPreferencePage_removeTrailingWhitespace;
Button checkboxTrailingWhitespace = addCheckBox(composite, label,
PreferenceConstants.REMOVE_TRAILING_WHITESPACE, 0);
fRadioEditedLines = addRadioButton(composite, PreferencesMessages.SaveActionsPreferencePage_inEditedLines,
PreferenceConstants.REMOVE_TRAILING_WHITESPACE_LIMIT_TO_EDITED_LINES, 0);
fRadioAllLines = addRadioButton(composite, PreferencesMessages.SaveActionsPreferencePage_inAllLines,
null, 0);
createDependency(checkboxTrailingWhitespace,
PreferenceConstants.REMOVE_TRAILING_WHITESPACE, fRadioEditedLines);
createDependency(checkboxTrailingWhitespace,
PreferenceConstants.REMOVE_TRAILING_WHITESPACE, fRadioAllLines);
ControlFactory.createEmptySpace(composite, 1);
label = PreferencesMessages.SaveActionsPreferencePage_ensureNewline;
addCheckBox(composite, label, PreferenceConstants.ENSURE_NEWLINE_AT_EOF, 0);
return composite;
}
/*
* @see PreferencePage#createContents(Composite)
*/
@Override
protected Control createContents(Composite parent) {
fOverlayStore.load();
fOverlayStore.start();
createConfigurationBlock(parent);
initialize();
return parent;
}
private void initialize() {
initializeFields();
fRadioAllLines.setSelection(!fRadioEditedLines.getSelection());
}
}

View file

@ -946,6 +946,16 @@ public class PreferenceConstants {
*/
public final static String REMOVE_TRAILING_WHITESPACE = "removeTrailingWhitespace"; //$NON-NLS-1$
/**
* Preference key controlling how REMOVE_TRAILING_WHITESPACE option is applied.
* If REMOVE_TRAILING_WHITESPACE is enabled, this option limits the scope of
* the removal to edited lines only. This option has no effect if
* REMOVE_TRAILING_WHITESPACE is disabled.
*
* @since 5.1
*/
public final static String REMOVE_TRAILING_WHITESPACE_LIMIT_TO_EDITED_LINES = "removeTrailingWhitespaceEditedLines"; //$NON-NLS-1$
/**
* A named preference that defines whether the hint to make hover sticky should be shown.
*
@ -1536,6 +1546,7 @@ public class PreferenceConstants {
store.setDefault(PreferenceConstants.EDITOR_AUTO_INDENT, true);
store.setDefault(PreferenceConstants.REMOVE_TRAILING_WHITESPACE, true);
store.setDefault(PreferenceConstants.REMOVE_TRAILING_WHITESPACE_LIMIT_TO_EDITED_LINES, true);
store.setDefault(PreferenceConstants.ENSURE_NEWLINE_AT_EOF, true);
// formatter profile