New issue
Advanced search Search tips
Note: Color blocks (like or ) mean that a user may not be available. Tooltip shows the reason.

Issue 597762 link

Starred by 1 user

Issue metadata

Status: Archived
Owner: ----
Closed: Jan 10
Cc:
EstimatedDays: ----
NextAction: ----
OS: Android
Pri: 2
Type: ----



Sign in to add a comment

Investigate google-java-format for use as part of git cl format

Project Member Reported by n...@chromium.org, Mar 24 2016

Issue description

google-java-format (https://github.com/google/google-java-format) is an open-source tool to reformat Java code to comply with Google Java Style.

Currently, git cl format for Java files uses clang-format under the covers, but does not produce correct results in a number of edge cases. As such, the results of "git cl format" still often need manual editing. It's possible that the clang-format rules could be improved, or it's possible that using google-java-format would ultimately be more successful.
 

Comment 1 by n...@chromium.org, Mar 25 2016

According to the maintainer of google-java-format (via email), the tool is mature and ready for widespread use.

The main question is whether it produces results that are consistent enough with existing Chromium code.

Comment 2 by n...@chromium.org, Mar 25 2016

Well, the first problem is that google-java-format uses a block indentation of +2 spaces, where Chromium style uses +4.

After adjusting for the indentation issue (using sed), the tool produces results that are mostly consistent with existing code. The main downside I see is that the tool adds a significant amount of whitespace (both newlines and increased indentation):
 1. It wraps after "=" in many cases (some cases almost seem like bugs)
 2. When method parameters don't all fit on one line, it puts each param on a separate line (both when declaring a method and when calling it).

I'm OK with (2), especially since that matches C++ style in Chromium, but (1) isn't ideal. Maybe we could get used to it though?

Here are a few representative before/after comparisons from Tab.java:



-        Tab tab = new Tab(
-                INVALID_TAB_ID, parentId, incognito, activity, nativeWindow, type,
-                TabCreationState.FROZEN_FOR_LAZY_LOAD, null);
+        Tab tab =
+                new Tab(
+                        INVALID_TAB_ID,
+                        parentId,
+                        incognito,
+                        activity,
+                        nativeWindow,
+                        type,
+                        TabCreationState.FROZEN_FOR_LAZY_LOAD,
+                        null);



-            getTabModelSelector().openNewTab(loadUrlParams,
-                    TabLaunchType.FROM_LONGPRESS_FOREGROUND, Tab.this, isIncognito());
+            getTabModelSelector()
+                    .openNewTab(
+                            loadUrlParams, TabLaunchType.FROM_LONGPRESS_FOREGROUND, Tab.this, isIncognito());



-            mDefaultThemeColor = mIncognito
-                    ? ApiCompatibilityUtils.getColor(resources, R.color.incognito_primary_color)
-                    : ApiCompatibilityUtils.getColor(resources, R.color.default_primary_color);
+            mDefaultThemeColor =
+                    mIncognito
+                            ? ApiCompatibilityUtils.getColor(resources, R.color.incognito_primary_color)
+                            : ApiCompatibilityUtils.getColor(resources, R.color.default_primary_color);



-            if (otherTab != tab && otherTab.getTabUma() != null
-                    && otherTab.getTabUma().getLastShownTimestamp() > tabLastShow) {
+            if (otherTab != tab
+                    && otherTab.getTabUma() != null
+                    && otherTab.getTabUma().getLastShownTimestamp() > tabLastShow) {


-        mIsTitleDirectionRtl = LocalizationUtils.getFirstStrongCharacterDirection(title)
-                == LocalizationUtils.RIGHT_TO_LEFT;
+        mIsTitleDirectionRtl =
+                LocalizationUtils.getFirstStrongCharacterDirection(title)
+                        == LocalizationUtils.RIGHT_TO_LEFT;



-            int loadType = nativeLoadUrl(mNativeTabAndroid, params.getUrl(),
-                    params.getVerbatimHeaders(), params.getPostData(), params.getTransitionType(),
-                    params.getReferrer() != null ? params.getReferrer().getUrl() : null,
-                    // Policy will be ignored for null referrer url, 0 is just a placeholder.
-                    // TODO(ppi): Should we pass Referrer jobject and add JNI methods to read it
-                    //            from the native?
-                    params.getReferrer() != null ? params.getReferrer().getPolicy() : 0,
-                    params.getIsRendererInitiated(), params.getShouldReplaceCurrentEntry(),
-                    params.getIntentReceivedTimestamp(), params.getHasUserGesture());
+            int loadType =
+                    nativeLoadUrl(
+                            mNativeTabAndroid,
+                            params.getUrl(),
+                            params.getVerbatimHeaders(),
+                            params.getPostData(),
+                            params.getTransitionType(),
+                            params.getReferrer() != null ? params.getReferrer().getUrl() : null,
+                            // Policy will be ignored for null referrer url, 0 is just a placeholder.
+                            // TODO(ppi): Should we pass Referrer jobject and add JNI methods to read it
+                            //            from the native?
+                            params.getReferrer() != null ? params.getReferrer().getPolicy() : 0,
+                            params.getIsRendererInitiated(),
+                            params.getShouldReplaceCurrentEntry(),
+                            params.getIntentReceivedTimestamp(),
+                            params.getHasUserGesture());



-            OnClickListener suggestionAction = new OnClickListener() {
-                @Override
-                public void onClick(View view) {
-                    Activity activity = mWindowAndroid.getActivity().get();
-                    assert activity != null;
-                    HelpAndFeedback.getInstance(activity).show(activity,
-                            activity.getString(R.string.help_context_sad_tab),
-                            Profile.getLastUsedProfile(), null);
-                }
-            };
+            OnClickListener suggestionAction =
+                    new OnClickListener() {
+                        @Override
+                        public void onClick(View view) {
+                            Activity activity = mWindowAndroid.getActivity().get();
+                            assert activity != null;
+                            HelpAndFeedback.getInstance(activity)
+                                    .show(
+                                            activity,
+                                            activity.getString(R.string.help_context_sad_tab),
+                                            Profile.getLastUsedProfile(),
+                                            null);
+                        }
+                    };



-    private native void nativeSetInterceptNavigationDelegate(long nativeTabAndroid,
-            InterceptNavigationDelegate delegate);
+    private native void nativeSetInterceptNavigationDelegate(
+            long nativeTabAndroid, InterceptNavigationDelegate delegate);

Comment 3 by n...@chromium.org, Mar 25 2016

Re: 2 space indentation. The --aosp flag will cause the tool to use 4 space indentation, instead of 2. Just what we need :)

Comment 4 by cushon@google.com, Mar 25 2016

Re (1), wrapping after "=" is deliberate. The formatter was designed around the "break at the highest syntactic level" guidance in style guide [1]. It ends up following that rule a bit more closely than most people would when formatting by hand.

I've found that the additional structure often makes the code clearer, but it can take some getting used to.

[1] https://google.github.io/styleguide/javaguide.html#s4.5.1-line-wrapping-where-to-break
Status: Archived (was: Untriaged)
Archiving issues older than 2 years with no owner or component.

Sign in to add a comment