Investigate google-java-format for use as part of git cl format |
||
Issue descriptiongoogle-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.
,
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);
,
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 :)
,
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
,
Jan 10
Archiving issues older than 2 years with no owner or component. |
||
►
Sign in to add a comment |
||
Comment 1 by n...@chromium.org
, Mar 25 2016