Added functional tests for:
1. "Save Actions" preference page
2. Execution of "Save Actions" when an editor is saved.
diff --git a/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/hyperlinking/ImportHyperlinking_Test.java b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/hyperlinking/ImportHyperlinking_Test.java
index 032d708..61636dd 100644
--- a/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/hyperlinking/ImportHyperlinking_Test.java
+++ b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/hyperlinking/ImportHyperlinking_Test.java
@@ -24,9 +24,6 @@
* @author alruiz@google.com (Alex Ruiz)
*/
public class ImportHyperlinking_Test {
- @Rule public CommentReaderRule commentReader = new CommentReaderRule();
- @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
-
private static ProtobufBot robot;
@BeforeClass public static void setUpOnce() throws CoreException {
@@ -35,6 +32,9 @@
robot.createGeneralProject("ImportHyperlinkingTest");
}
+ @Rule public CommentReaderRule commentReader = new CommentReaderRule();
+ @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
// import 'google/protobuf/descriptor.proto';
@Test public void should_open_file_in_plugIn() throws InterruptedException {
String text = commentReader.comments().get(0);
diff --git a/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/model/ProtobufDocumentProvider_Test.java b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/model/ProtobufDocumentProvider_Test.java
new file mode 100644
index 0000000..7f17b53
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/model/ProtobufDocumentProvider_Test.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2012 Google Inc.
+ *
+ * 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
+ */
+package com.google.eclipse.protobuf.ui.editor.model;
+
+import static com.google.eclipse.protobuf.junit.core.XtextRule.createWith;
+import static com.google.eclipse.protobuf.ui.preferences.editor.save.SaveActionsTestPreferences.RemoveTrailingWhitespace.*;
+import static org.junit.Assert.assertEquals;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor;
+import org.junit.*;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+import com.google.eclipse.protobuf.junit.util.MultiLineTextBuilder;
+import com.google.eclipse.protobuf.ui.plugin.ProtobufEditorPlugIn;
+import com.google.eclipse.protobuf.ui.preferences.editor.save.SaveActionsTestPreferences;
+import com.google.eclipse.protobuf.ui.swtbot.ProtobufBot;
+import com.google.inject.Inject;
+
+/**
+ * Tests for <code>{@link ProtobufDocumentProvider}</code>
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class ProtobufDocumentProvider_Test {
+ private static ProtobufBot robot;
+ private static SWTBotEclipseEditor editor;
+
+ @BeforeClass public static void setUpOnce() throws CoreException {
+ robot = new ProtobufBot();
+ robot.resetAll();
+ robot.createGeneralProject("ProtobufDocumentProvider");
+ editor = robot.createFile("test.proto");
+ }
+
+ @Rule public XtextRule xtext = createWith(ProtobufEditorPlugIn.injector());
+
+ @Inject private SaveActionsTestPreferences preferences;
+
+ @Test public void should_remove_trailing_whitespace_in_edited_lines_only() {
+ initEditor();
+ preferences.removeTrailingWhitespace(EDITED_LINES);
+ editor.typeText("option optimize_for = SPEED; ");
+ editor.save();
+ MultiLineTextBuilder expected = new MultiLineTextBuilder();
+ expected.append("syntax = 'proto2'; ")
+ .append("import 'google/protobuf/descriptor.proto'; ")
+ .append("option optimize_for = SPEED;");
+ assertEquals(expected.toString(), editor.getText());
+ }
+
+ @Test public void should_remove_trailing_whitespace_in_all_lines() {
+ initEditor();
+ preferences.removeTrailingWhitespace(ALL_LINES);
+ editor.typeText("option optimize_for = SPEED; ");
+ editor.save();
+ MultiLineTextBuilder expected = new MultiLineTextBuilder();
+ expected.append("syntax = 'proto2';")
+ .append("import 'google/protobuf/descriptor.proto';")
+ .append("option optimize_for = SPEED;");
+ assertEquals(expected.toString(), editor.getText());
+ }
+
+ private void initEditor() {
+ preferences.removeTrailingWhitespace(NONE);
+ MultiLineTextBuilder text = new MultiLineTextBuilder();
+ text.append("syntax = 'proto2'; ")
+ .append("import 'google/protobuf/descriptor.proto'; ")
+ .append("");
+ editor.setText(text.toString());
+ editor.save();
+ editor.navigateTo(2, 0);
+ }
+}
diff --git a/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/preferences/AbsractPreferencePageTestCase.java b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/preferences/AbsractPreferencePageTestCase.java
new file mode 100644
index 0000000..fe0952f
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/preferences/AbsractPreferencePageTestCase.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2012 Google Inc.
+ *
+ * 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
+ */
+package com.google.eclipse.protobuf.ui.preferences;
+
+import static com.google.eclipse.protobuf.junit.core.XtextRule.createWith;
+
+import org.eclipse.core.runtime.CoreException;
+import org.junit.*;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+import com.google.eclipse.protobuf.ui.plugin.ProtobufEditorPlugIn;
+import com.google.eclipse.protobuf.ui.swtbot.ProtobufBot;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public abstract class AbsractPreferencePageTestCase {
+ protected static ProtobufBot robot;
+
+ @BeforeClass public static void resetWorkbench() throws CoreException {
+ robot = new ProtobufBot();
+ robot.resetAll();
+ }
+
+ @Rule public XtextRule xtext = createWith(ProtobufEditorPlugIn.injector());
+
+ @Before public void restoreDefaults() {
+ robot.button("Restore Defaults").click();
+ applyChanges();
+ }
+
+ protected void applyChanges() {
+ robot.button("Apply").click();
+ }
+}
diff --git a/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsPreferencePage_Test.java b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsPreferencePage_Test.java
new file mode 100644
index 0000000..a31982a
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsPreferencePage_Test.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2012 Google Inc.
+ *
+ * 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
+ */
+package com.google.eclipse.protobuf.ui.preferences.editor.save;
+
+import static org.junit.Assert.*;
+
+import org.junit.*;
+
+import com.google.eclipse.protobuf.ui.preferences.AbsractPreferencePageTestCase;
+import com.google.inject.Inject;
+
+/**
+ * Tests for <code>{@link SaveActionsPreferences}</code>.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class SaveActionsPreferencePage_Test extends AbsractPreferencePageTestCase {
+ @BeforeClass public static void setUpOnce() {
+ robot.openPreferencePage("com.google.eclipse.protobuf.ui.preferences.editor.save.SaveActionsPreferencePage");
+ }
+
+ @Inject private SaveActionsPreferences preferences;
+
+ @Before public void setUp() {
+ assertTrue(preferences.shouldRemoveTrailingWhitespace());
+ assertTrue(preferences.shouldRemoveTrailingWhitespaceInEditedLines());
+ }
+
+ @Test public void should_update_property_for_removing_trailing_whitespace() {
+ robot.checkBox("Remove trailing whitespace").deselect();
+ applyChanges();
+ assertFalse(preferences.shouldRemoveTrailingWhitespace());
+ }
+
+ @Test public void should_update_property_for_removing_trailing_whitespace_in_edited_lines() {
+ robot.radio("In all lines").click();
+ applyChanges();
+ assertFalse(preferences.shouldRemoveTrailingWhitespaceInEditedLines());
+ }
+}
diff --git a/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsTestPreferences.java b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsTestPreferences.java
new file mode 100644
index 0000000..4c1c34d
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsTestPreferences.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2012 Google Inc.
+ *
+ * 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
+ */
+package com.google.eclipse.protobuf.ui.preferences.editor.save;
+
+import static com.google.eclipse.protobuf.ui.preferences.editor.save.PreferenceNames.*;
+
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess;
+
+import com.google.inject.Inject;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class SaveActionsTestPreferences {
+ private final IPreferenceStore store;
+
+ @Inject public SaveActionsTestPreferences(IPreferenceStoreAccess storeAccess) {
+ store = storeAccess.getWritablePreferenceStore();
+ }
+
+ public void removeTrailingWhitespace(RemoveTrailingWhitespace value) {
+ if (value == RemoveTrailingWhitespace.NONE) {
+ store.setValue(REMOVE_TRAILING_WHITESPACE, false);
+ return;
+ }
+ store.setValue(REMOVE_TRAILING_WHITESPACE, true);
+ boolean inEditedLines = (value == RemoveTrailingWhitespace.EDITED_LINES);
+ store.setValue(IN_ALL_LINES, !inEditedLines);
+ store.setValue(IN_EDITED_LINES, inEditedLines);
+ }
+
+ public static enum RemoveTrailingWhitespace {
+ NONE, EDITED_LINES, ALL_LINES;
+ }
+}
diff --git a/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/swtbot/ProtobufBot.java b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/swtbot/ProtobufBot.java
index 6c9532c..c25b192 100644
--- a/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/swtbot/ProtobufBot.java
+++ b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/swtbot/ProtobufBot.java
@@ -9,9 +9,11 @@
package com.google.eclipse.protobuf.ui.swtbot;
import static com.google.eclipse.protobuf.ui.util.Workspaces.workspaceRoot;
+import static org.eclipse.ui.dialogs.PreferencesUtil.createPreferenceDialogOn;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.*;
+import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.*;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
@@ -72,4 +74,18 @@
editor.saveAndClose();
}
}
+
+ public void openPreferencePage(final String preferencePageId) {
+ runInUiThread(new Runnable() {
+ @Override public void run() {
+ PreferenceDialog dialog = createPreferenceDialogOn(activeShell().widget, preferencePageId, null, null);
+ dialog.setBlockOnOpen(false);
+ dialog.open();
+ }
+ });
+ }
+
+ public void runInUiThread(Runnable r) {
+ activeShell().display.syncExec(r);
+ }
}
diff --git a/com.google.eclipse.protobuf.ui/plugin.xml b/com.google.eclipse.protobuf.ui/plugin.xml
index 54a8999..ffd462d 100644
--- a/com.google.eclipse.protobuf.ui/plugin.xml
+++ b/com.google.eclipse.protobuf.ui/plugin.xml
@@ -53,13 +53,13 @@
</extension>
<extension point="org.eclipse.ui.preferencePages">
<page
- class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.general.page.GeneralPreferencePage"
+ class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.general.GeneralPreferencePage"
id="com.google.eclipse.protobuf.Protobuf"
name="%page.name">
<keywordReference id="com.google.eclipse.protobuf.ui.keyword_Protobuf" />
</page>
<page
- category="com.google.eclipse.protobuf.ui.preferences.editor.general.page.EditorPreferencePage"
+ category="com.google.eclipse.protobuf.ui.preferences.editor.general.EditorPreferencePage"
class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:org.eclipse.xtext.ui.editor.syntaxcoloring.SyntaxColoringPreferencePage"
id="com.google.eclipse.protobuf.Protobuf.coloring"
name="%page.name.0">
@@ -67,29 +67,29 @@
</page>
<page
category="com.google.eclipse.protobuf.Protobuf"
- class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.compiler.page.CompilerPreferencePage"
- id="com.google.eclipse.protobuf.ui.preferences.compiler.page.CompilerPreferencePage"
+ class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.compiler.CompilerPreferencePage"
+ id="com.google.eclipse.protobuf.ui.preferences.compiler.CompilerPreferencePage"
name="%page.name.2">
<keywordReference id="com.google.eclipse.protobuf.ui.keyword_Protobuf" />
</page>
<page
category="com.google.eclipse.protobuf.Protobuf"
- class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.editor.general.page.EditorPreferencePage"
- id="com.google.eclipse.protobuf.ui.preferences.editor.general.page.EditorPreferencePage"
+ class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.editor.general.EditorPreferencePage"
+ id="com.google.eclipse.protobuf.ui.preferences.editor.general.EditorPreferencePage"
name="%page.name.4">
<keywordReference id="com.google.eclipse.protobuf.ui.keyword_Protobuf" />
</page>
<page
- category="com.google.eclipse.protobuf.ui.preferences.editor.general.page.EditorPreferencePage"
- class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.editor.save.page.SaveActionsPreferencePage"
- id="com.google.eclipse.protobuf.ui.preferences.editor.save.page.SaveActionsPreferencePage"
+ category="com.google.eclipse.protobuf.ui.preferences.editor.general.EditorPreferencePage"
+ class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.editor.save.SaveActionsPreferencePage"
+ id="com.google.eclipse.protobuf.ui.preferences.editor.save.SaveActionsPreferencePage"
name="%page.name.5">
<keywordReference id="com.google.eclipse.protobuf.ui.keyword_Protobuf" />
</page>
<page
- category="com.google.eclipse.protobuf.ui.preferences.editor.general.page.EditorPreferencePage"
- class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.editor.numerictag.page.NumericTagPreferencePage"
- id="com.google.eclipse.protobuf.ui.preferences.editor.numerictag.page.NumericTagPreferencePage"
+ category="com.google.eclipse.protobuf.ui.preferences.editor.general.EditorPreferencePage"
+ class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.editor.numerictag.NumericTagPreferencePage"
+ id="com.google.eclipse.protobuf.ui.preferences.editor.numerictag.NumericTagPreferencePage"
name="%page.name.6">
<keywordReference id="com.google.eclipse.protobuf.ui.keyword_Protobuf" />
</page>
@@ -216,7 +216,7 @@
</extension>
<extension point="org.eclipse.ui.propertyPages">
<page
- class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.general.page.GeneralPreferencePage"
+ class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.general.GeneralPreferencePage"
id="com.google.eclipse.protobuf.Protobuf"
name="%page.name"
selectionFilter="single">
@@ -227,7 +227,7 @@
</page>
<page
category="com.google.eclipse.protobuf.Protobuf"
- class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.compiler.page.CompilerPreferencePage"
+ class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.compiler.CompilerPreferencePage"
id="com.google.eclipse.protobuf.ui.properties.compiler.PropertyPage"
name="%page.name.2"
selectionFilter="single">
@@ -237,7 +237,7 @@
</page>
<page
category="com.google.eclipse.protobuf.Protobuf"
- class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.paths.page.PathsPreferencePage"
+ class="com.google.eclipse.protobuf.ui.ProtobufExecutableExtensionFactory:com.google.eclipse.protobuf.ui.preferences.paths.PathsPreferencePage"
id="com.google.eclipse.protobuf.ui.properties.paths.PropertyPage"
name="%page.name.3"
selectionFilter="single">
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/ProtobufDocumentProvider.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/ProtobufDocumentProvider.java
index a826144..9461248 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/ProtobufDocumentProvider.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/ProtobufDocumentProvider.java
@@ -26,11 +26,10 @@
import org.eclipse.text.undo.IDocumentUndoManager;
import org.eclipse.ui.*;
import org.eclipse.xtext.ui.editor.model.*;
-import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess;
import com.google.eclipse.protobuf.ui.preferences.editor.save.SaveActionsPreferences;
import com.google.eclipse.protobuf.ui.util.editor.ChangedLineRegionCalculator;
-import com.google.inject.Inject;
+import com.google.inject.*;
/**
* @author alruiz@google.com (Alex Ruiz)
@@ -39,7 +38,7 @@
private static final IRegion[] NO_CHANGE = new IRegion[0];
@Inject private ChangedLineRegionCalculator calculator;
- @Inject private IPreferenceStoreAccess storeAccess;
+ @Inject private Provider<SaveActionsPreferences> preferencesProvider;
@Inject private SaveActions saveActions;
private final List<DocumentContentsFactory> documentFactories;
@@ -126,14 +125,14 @@
private IRegion[] changedRegions(IProgressMonitor monitor, IFileEditorInput editorInput, IDocument document)
throws CoreException {
- SaveActionsPreferences preferences = new SaveActionsPreferences(storeAccess);
+ SaveActionsPreferences preferences = preferencesProvider.get();
if (!preferences.shouldRemoveTrailingWhitespace()) {
return NO_CHANGE;
}
- if (preferences.shouldRemoveTrailingWhitespaceInAllLines()) {
- return new IRegion[] { new Region(0, document.getLength()) };
+ if (preferences.shouldRemoveTrailingWhitespaceInEditedLines()) {
+ return calculator.calculateChangedLineRegions(textFileBuffer(monitor, editorInput), document, monitor);
}
- return calculator.calculateChangedLineRegions(textFileBuffer(monitor, editorInput), document, monitor);
+ return new IRegion[] { new Region(0, document.getLength()) };
}
private ITextFileBuffer textFileBuffer(IProgressMonitor monitor, IFileEditorInput editorInput) throws CoreException {
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/SaveActions.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/SaveActions.java
index 16ea991..76d2199 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/SaveActions.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/SaveActions.java
@@ -26,35 +26,40 @@
private static Logger logger = Logger.getLogger(SaveActions.class);
TextEdit createSaveAction(IDocument document, IRegion[] changedRegions) {
- TextEdit rootEdit = null;
try {
- for (IRegion region : changedRegions) {
- int lastLine = document.getLineOfOffset(region.getOffset() + region.getLength());
- for (int line = firstLine(region, document); line <= lastLine; line++) {
- IRegion lineRegion = document.getLineInformation(line);
- if (lineRegion.getLength() == 0) {
- continue;
- }
- int lineStart = lineRegion.getOffset();
- int lineEnd = lineStart + lineRegion.getLength();
- int charPos = rightMostNonWhitespaceChar(document, lineStart, lineEnd);
- if (charPos >= lineEnd) {
- continue;
- }
- // check partition - don't remove whitespace inside strings
- ITypedRegion partition = getPartition(document, DEFAULT_PARTITIONING, charPos, false);
- if ("__string".equals(partition.getType())) {
- continue;
- }
- if (rootEdit == null) {
- rootEdit = new MultiTextEdit();
- }
- rootEdit.addChild(new DeleteEdit(charPos, lineEnd - charPos));
- }
- }
+ return doCreateSaveAction(document, changedRegions);
} catch (BadLocationException e) {
logger.error("Unable to create save actions", e);
}
+ return null;
+ }
+
+ private TextEdit doCreateSaveAction(IDocument document, IRegion[] changedRegions) throws BadLocationException {
+ TextEdit rootEdit = null;
+ for (IRegion region : changedRegions) {
+ int lastLine = document.getLineOfOffset(region.getOffset() + region.getLength());
+ for (int line = firstLine(region, document); line <= lastLine; line++) {
+ IRegion lineRegion = document.getLineInformation(line);
+ if (lineRegion.getLength() == 0) {
+ continue;
+ }
+ int lineStart = lineRegion.getOffset();
+ int lineEnd = lineStart + lineRegion.getLength();
+ int charPos = rightMostNonWhitespaceChar(document, lineStart, lineEnd);
+ if (charPos >= lineEnd) {
+ continue;
+ }
+ // check partition - don't remove whitespace inside strings
+ ITypedRegion partition = getPartition(document, DEFAULT_PARTITIONING, charPos, false);
+ if ("__string".equals(partition.getType())) {
+ continue;
+ }
+ if (rootEdit == null) {
+ rootEdit = new MultiTextEdit();
+ }
+ rootEdit.addChild(new DeleteEdit(charPos, lineEnd - charPos));
+ }
+ }
return rootEdit;
}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/compiler/CompilerPreferencePage.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/compiler/CompilerPreferencePage.java
index 5ddd0a8..64df708 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/compiler/CompilerPreferencePage.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/compiler/CompilerPreferencePage.java
@@ -403,6 +403,7 @@
String fileName = file.getName();
return expectedFileName.equals(fileName);
}
+
@Override protected String preferencePageId() {
return PREFERENCE_PAGE_ID;
}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/Messages.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/Messages.java
index 6bf5ef8..0c1c314 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/Messages.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/Messages.java
@@ -14,6 +14,8 @@
* @author alruiz@google.com (Alex Ruiz)
*/
public class Messages extends NLS {
+ public static String inAllLines;
+ public static String inEditedLines;
public static String removeTrailingWhitespace;
static {
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/Messages.properties b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/Messages.properties
index 2161910..780eb9b 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/Messages.properties
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/Messages.properties
@@ -1 +1,3 @@
+inAllLines=In all lines
+inEditedLines=In edited lines
removeTrailingWhitespace=Remove trailing &whitespace
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsPreferencePage.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsPreferencePage.java
index 69d6013..84046dd 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsPreferencePage.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsPreferencePage.java
@@ -9,7 +9,7 @@
*/
package com.google.eclipse.protobuf.ui.preferences.editor.save;
-import static com.google.eclipse.protobuf.ui.preferences.editor.save.Messages.removeTrailingWhitespace;
+import static com.google.eclipse.protobuf.ui.preferences.editor.save.Messages.*;
import static com.google.eclipse.protobuf.ui.preferences.editor.save.PreferenceNames.*;
import static com.google.eclipse.protobuf.ui.preferences.pages.binding.BindingToButtonSelection.bindSelectionOf;
@@ -34,7 +34,7 @@
private final PreferenceBinder preferenceBinder = new PreferenceBinder();
- private Button btnRemoveTrailingwhitespace;
+ private Button btnRemoveTrailingWhitespace;
private Button btnInEditedLines;
private Button btnInAllLines;
@@ -43,19 +43,19 @@
@Override protected Control createContents(Composite parent) {
Composite contents = new Composite(parent, NONE);
contents.setLayout(new GridLayout(1, false));
- btnRemoveTrailingwhitespace = new Button(contents, SWT.CHECK);
- btnRemoveTrailingwhitespace.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false, 1, 1));
- btnRemoveTrailingwhitespace.setText(removeTrailingWhitespace);
+ btnRemoveTrailingWhitespace = new Button(contents, SWT.CHECK);
+ btnRemoveTrailingWhitespace.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false, 1, 1));
+ btnRemoveTrailingWhitespace.setText(removeTrailingWhitespace);
Composite composite = new Composite(contents, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
composite.setLayout(new GridLayout(1, false));
btnInEditedLines = new Button(composite, SWT.RADIO);
- btnInEditedLines.setText("In edited lines");
+ btnInEditedLines.setText(inEditedLines);
btnInAllLines = new Button(composite, SWT.RADIO);
- btnInAllLines.setText("In all lines");
+ btnInAllLines.setText(inAllLines);
setUpBinding();
preferenceBinder.applyValues();
@@ -67,14 +67,14 @@
private void setUpBinding() {
PreferenceFactory factory = new PreferenceFactory(getPreferenceStore());
preferenceBinder.addAll(
- bindSelectionOf(btnRemoveTrailingwhitespace).to(factory.newBooleanPreference(REMOVE_TRAILING_WHITESPACE)),
+ bindSelectionOf(btnRemoveTrailingWhitespace).to(factory.newBooleanPreference(REMOVE_TRAILING_WHITESPACE)),
bindSelectionOf(btnInAllLines).to(factory.newBooleanPreference(IN_ALL_LINES)),
bindSelectionOf(btnInEditedLines).to(factory.newBooleanPreference(IN_EDITED_LINES))
);
}
private void addEventListeners() {
- btnRemoveTrailingwhitespace.addSelectionListener(new SelectionAdapter() {
+ btnRemoveTrailingWhitespace.addSelectionListener(new SelectionAdapter() {
@Override public void widgetSelected(SelectionEvent e) {
updateContents();
}
@@ -82,7 +82,7 @@
}
private void updateContents() {
- boolean enabled = btnRemoveTrailingwhitespace.getSelection();
+ boolean enabled = btnRemoveTrailingWhitespace.getSelection();
btnInAllLines.setEnabled(enabled);
btnInEditedLines.setEnabled(enabled);
}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsPreferences.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsPreferences.java
index d714606..c16c10f 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsPreferences.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/editor/save/SaveActionsPreferences.java
@@ -13,13 +13,15 @@
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.xtext.ui.editor.preferences.*;
+import com.google.inject.Inject;
+
/**
* @author alruiz@google.com (Alex Ruiz)
*/
public class SaveActionsPreferences {
private final IPreferenceStore store;
- public SaveActionsPreferences(IPreferenceStoreAccess storeAccess) {
+ @Inject public SaveActionsPreferences(IPreferenceStoreAccess storeAccess) {
this.store = storeAccess.getWritablePreferenceStore();
}
@@ -27,8 +29,8 @@
return store.getBoolean(REMOVE_TRAILING_WHITESPACE);
}
- public boolean shouldRemoveTrailingWhitespaceInAllLines() {
- return store.getBoolean(IN_ALL_LINES);
+ public boolean shouldRemoveTrailingWhitespaceInEditedLines() {
+ return store.getBoolean(IN_EDITED_LINES);
}
public static class Initializer implements IPreferenceStoreInitializer {