In progress: [Issue 89] Proto buffer editor needs to have options to
remove whitespace at end of lines
Code cleanup.
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Editors.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/Editors.java
similarity index 68%
rename from com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Editors.java
rename to com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/Editors.java
index c9f70de..c26d91b 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Editors.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/Editors.java
@@ -7,10 +7,12 @@
*
* http://www.eclipse.org/legal/epl-v10.html
*/
-package com.google.eclipse.protobuf.ui.util;
+package com.google.eclipse.protobuf.ui.util.editor;
import static com.google.eclipse.protobuf.ui.ProtobufUiModule.PLUGIN_ID;
+import static com.google.eclipse.protobuf.ui.util.editor.Messages.*;
import static org.eclipse.compare.rangedifferencer.RangeDifferencer.findDifferences;
+import static org.eclipse.core.filebuffers.FileBuffers.createTextFileBufferManager;
import static org.eclipse.core.runtime.IStatus.ERROR;
import static org.eclipse.core.runtime.Status.OK_STATUS;
import static org.eclipse.core.runtime.SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK;
@@ -44,21 +46,19 @@
SafeRunner.run(new ISafeRunnable() {
public void handleException(Throwable exception) {
logger.error(exception.getMessage(), exception);
- String msg = "An error occurred while calculating the changed regions. See error log for details.";
- errorStatus[0] = new Status(ERROR, PLUGIN_ID, 0, msg, exception);
+ errorStatus[0] = new Status(ERROR, PLUGIN_ID, 0, errorCalculatingChangedRegions, exception);
result[0] = null;
}
public void run() throws Exception {
- monitor.beginTask("Calculating changed regions", 20);
+ monitor.beginTask(calculatingChangedRegions, 20);
IFileStore fileStore = buffer.getFileStore();
- ITextFileBufferManager fileBufferManager = FileBuffers.createTextFileBufferManager();
+ ITextFileBufferManager fileBufferManager = createTextFileBufferManager();
fileBufferManager.connectFileStore(fileStore, getSubProgressMonitor(monitor, 15));
try {
- IDocument currentDocument = buffer.getDocument();
- IDocument oldDocument =
- ((ITextFileBuffer) fileBufferManager.getFileStoreFileBuffer(fileStore)).getDocument();
- result[0] = getChangedLineRegions(oldDocument, currentDocument);
+ IDocument current = buffer.getDocument();
+ IDocument old = ((ITextFileBuffer) fileBufferManager.getFileStoreFileBuffer(fileStore)).getDocument();
+ result[0] = getChangedLineRegions(old, current);
} finally {
fileBufferManager.disconnectFileStore(fileStore, getSubProgressMonitor(monitor, 5));
monitor.done();
@@ -66,31 +66,30 @@
}
/*
- * Returns regions of all lines which differ comparing {@code oldDocument}s content with
- * {@code currentDocument}s content. Successive lines are merged into one region.
+ * Returns regions of all lines which differ comparing {@code old}s content with {@code current}s content.
+ * Successive lines are merged into one region.
*/
- private IRegion[] getChangedLineRegions(IDocument oldDocument, IDocument currentDocument) {
- RangeDifference[] differences =
- findDifferences(new LineComparator(oldDocument), new LineComparator(currentDocument));
+ private IRegion[] getChangedLineRegions(IDocument old, IDocument current) {
+ RangeDifference[] differences = differencesBetween(old, current);
List<IRegion> regions = new ArrayList<IRegion>();
- final int numberOfLines = currentDocument.getNumberOfLines();
- for (RangeDifference current : differences) {
- if (current.kind() == RangeDifference.CHANGE) {
- int startLine = Math.min(current.rightStart(), numberOfLines - 1);
- int endLine = current.rightEnd() - 1;
+ int numberOfLines = current.getNumberOfLines();
+ for (RangeDifference difference : differences) {
+ if (difference.kind() == RangeDifference.CHANGE) {
+ int startLine = Math.min(difference.rightStart(), numberOfLines - 1);
+ int endLine = difference.rightEnd() - 1;
IRegion startLineRegion;
try {
- startLineRegion = currentDocument.getLineInformation(startLine);
+ startLineRegion = current.getLineInformation(startLine);
if (startLine >= endLine) {
// startLine > endLine indicates a deletion of one or more lines.
// Deletions are ignored except at the end of the document.
if (startLine == endLine
- || startLineRegion.getOffset() + startLineRegion.getLength() == currentDocument.getLength()) {
+ || startLineRegion.getOffset() + startLineRegion.getLength() == current.getLength()) {
regions.add(startLineRegion);
}
continue;
}
- IRegion endLineRegion = currentDocument.getLineInformation(endLine);
+ IRegion endLineRegion = current.getLineInformation(endLine);
int startOffset = startLineRegion.getOffset();
int endOffset = endLineRegion.getOffset() + endLineRegion.getLength();
regions.add(new Region(startOffset, endOffset - startOffset));
@@ -101,6 +100,10 @@
}
return regions.toArray(new IRegion[regions.size()]);
}
+
+ private RangeDifference[] differencesBetween(IDocument old, IDocument current) {
+ return findDifferences(new LineComparator(old), new LineComparator(current));
+ }
});
} finally {
if (!errorStatus[0].isOK()) throw new CoreException(errorStatus[0]);
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/LineComparator.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/LineComparator.java
similarity index 96%
rename from com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/LineComparator.java
rename to com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/LineComparator.java
index 0515724..96553c8 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/LineComparator.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/LineComparator.java
@@ -7,7 +7,7 @@
*
* http://www.eclipse.org/legal/epl-v10.html
*/
-package com.google.eclipse.protobuf.ui.util;
+package com.google.eclipse.protobuf.ui.util.editor;
import org.apache.log4j.Logger;
import org.eclipse.compare.rangedifferencer.IRangeComparator;
@@ -20,6 +20,8 @@
*
* This implementation of <code>IRangeComparator</code> compares lines of a document. The lines are compared using a DJB
* hash function.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
*/
class LineComparator implements IRangeComparator {
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/Messages.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/Messages.java
new file mode 100644
index 0000000..0d62f2c
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/Messages.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2011 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.util.editor;
+
+import org.eclipse.osgi.util.NLS;
+
+/**
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class Messages extends NLS {
+
+ public static String calculatingChangedRegions;
+ public static String errorCalculatingChangedRegions;
+
+ static {
+ Class<Messages> targetType = Messages.class;
+ NLS.initializeMessages(targetType.getName(), targetType);
+ }
+
+ private Messages() {}
+}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/Messages.properties b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/Messages.properties
new file mode 100644
index 0000000..e14ce2b
--- /dev/null
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/editor/Messages.properties
@@ -0,0 +1,2 @@
+calculatingChangedRegions=Calculating changed regions
+errorCalculatingChangedRegions=An error occurred while calculating the changed regions. See error log for details.