In progress: [Issue 13] Implement a formatter
* Fixed line-wrapping for syntax, package and import elements.
* Added more tests.
diff --git a/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/Formatter_Test.java b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/Formatter_Test.java
index eadabcf..934e31b 100644
--- a/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/Formatter_Test.java
+++ b/com.google.eclipse.protobuf.ui.functional.test/src/com/google/eclipse/protobuf/ui/editor/Formatter_Test.java
@@ -13,6 +13,7 @@
import java.util.List;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.SWT;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor;
import org.junit.*;
@@ -31,7 +32,7 @@
public @Rule CommentReaderRule commentReader = new CommentReaderRule();
- @BeforeClass public static void setUpOnce() throws Exception {
+ @BeforeClass public static void setUpOnce() throws CoreException {
robot = new ProtobufBot();
robot.resetAll();
robot.createGeneralProject("FormatterTest");
@@ -40,11 +41,37 @@
editor.saveAndClose();
}
+ // syntax = 'proto2';import 'google/protobuf/descriptor.proto';
+
+ // syntax = 'proto2';
+ //
+ // import 'google/protobuf/descriptor.proto';
+ @Test public void should_format_syntax() {
+ SWTBotEclipseEditor editor = robot.createFile("formatSyntax.proto");
+ Comments comments = commentsAbove();
+ editor.setText(comments.beforeFormatting);
+ formatAndSave(editor);
+ assertThat(editor.getText(), equalTo(comments.expected));
+ }
+
+ // package com.google.proto.test;import 'google/protobuf/descriptor.proto';
+
+ // package com.google.proto.test;
+ //
+ // import 'google/protobuf/descriptor.proto';
+ @Test public void should_format_package() {
+ SWTBotEclipseEditor editor = robot.createFile("formatPackage.proto");
+ Comments comments = commentsAbove();
+ editor.setText(comments.beforeFormatting);
+ formatAndSave(editor);
+ assertThat(editor.getText(), equalTo(comments.expected));
+ }
+
// import 'dummy.proto';import 'google/protobuf/descriptor.proto';
// import 'dummy.proto';
// import 'google/protobuf/descriptor.proto';
- @Test public void should_add_line_wrap_after_normal_import() throws Exception {
+ @Test public void should_format_normal_import() {
SWTBotEclipseEditor editor = robot.createFile("formatNormalImport.proto");
Comments comments = commentsAbove();
editor.setText(comments.beforeFormatting);
@@ -56,7 +83,7 @@
// import public 'dummy.proto';
// import 'google/protobuf/descriptor.proto';
- @Test public void should_add_line_wrap_after_public_import() throws Exception {
+ @Test public void should_format_public_import() {
SWTBotEclipseEditor editor = robot.createFile("formatPublicImport.proto");
Comments comments = commentsAbove();
editor.setText(comments.beforeFormatting);
@@ -64,6 +91,30 @@
assertThat(editor.getText(), equalTo(comments.expected));
}
+ // import weak 'dummy.proto';import 'google/protobuf/descriptor.proto';
+
+ // import weak 'dummy.proto';
+ // import 'google/protobuf/descriptor.proto';
+ @Test public void should_format_weak_import() {
+ SWTBotEclipseEditor editor = robot.createFile("formatWeakImport.proto");
+ Comments comments = commentsAbove();
+ editor.setText(comments.beforeFormatting);
+ formatAndSave(editor);
+ assertThat(editor.getText(), equalTo(comments.expected));
+ }
+
+ // option java_package = "com.foo.bar";option optimize_for = CODE_SIZE;
+
+ // option java_package = "com.foo.bar";
+ // option optimize_for = CODE_SIZE;
+ @Test public void should_format_native_option() {
+ SWTBotEclipseEditor editor = robot.createFile("formatNativeOption.proto");
+ Comments comments = commentsAbove();
+ editor.setText(comments.beforeFormatting);
+ formatAndSave(editor);
+ assertThat(editor.getText(), equalTo(comments.expected));
+ }
+
private Comments commentsAbove() {
return new Comments(commentReader.commentsInCurrentTestMethod());
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/formatting/ProtobufFormatter.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/formatting/ProtobufFormatter.java
index 30cf32f..f7d94ba 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/formatting/ProtobufFormatter.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/formatting/ProtobufFormatter.java
@@ -27,9 +27,11 @@
@Override protected void configureFormatting(FormattingConfig c) {
ProtobufGrammarAccess g = (ProtobufGrammarAccess) getGrammarAccess();
c.setLinewrap(0, 1, 2).before(g.getSL_COMMENTRule());
- c.setLinewrap(1).after(g.getPackageRule());
+ c.setLinewrap(2).after(g.getSyntaxRule());
+ c.setLinewrap(2).after(g.getPackageRule());
c.setLinewrap(1).after(g.getNormalImportRule());
c.setLinewrap(1).after(g.getPublicImportRule());
+ c.setLinewrap(1).after(g.getWeakImportRule());
c.setLinewrap(1).after(g.getNativeOptionRule());
c.setLinewrap(1).after(g.getCustomOptionRule());
c.setLinewrap(1).after(g.getGroupRule());