From 1aaf3e4b038980ba4bbe7e08f42aa6949bb1836d Mon Sep 17 00:00:00 2001 From: Nicolo' Mazzucato Date: Tue, 24 May 2022 21:03:55 +0000 Subject: Update ktlint prebuilt to 0.46.0 The jar has been taken from ab/8635833 artifacts. (ktlint-0.46.0-SNAPSHOT-all.jar) Test: prebuilds/ktlint/ktlint.py --format --file Fixes: 231305338 Change-Id: Ib57e759460bac8fd584188ef4499792680212a2c --- ktlint-android-all.jar | Bin 31786185 -> 62738111 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/ktlint-android-all.jar b/ktlint-android-all.jar index fce8357..8de50b3 100644 Binary files a/ktlint-android-all.jar and b/ktlint-android-all.jar differ -- cgit v1.2.3 From de088d407c7ca6bb682527e8e802242668813ed4 Mon Sep 17 00:00:00 2001 From: Jordan Demeulenaere Date: Fri, 10 Jun 2022 11:53:14 +0200 Subject: Configure ktlint the same way as google3 does Given that we are going to use ktfmt to format our files and that ktlint and ktfmt don't agree on indentation rules, let's configure ktlint to disable rules that clash with ktfmt. The configuration is the same as in google3 [1] with those additional changes: * Indentation is set to 4. * wrapping rule is disabled. This is not disabled in google3 yet because this rules is causing issue starting from release 0.45 and google3 is on 0.43 (while we are on 0.46/master). [1] http://google3/third_party/kotlin/ktlint/.editorconfig Bug: 235461679 Test: Manual Change-Id: I851e511db06ae38ea89be0f90a86746a1fa92dac --- .editorconfig | 12 ++++++++++++ ktlint.py | 2 ++ 2 files changed, 14 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e97aa07 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +[*.{kt,kts}] +indent_size = 4 +max_line_length = 100 +trim_trailing_whitespace = true +insert_final_newline = true +ij_kotlin_imports_layout=* + +# Comma-separated list of rules to disable +# ktlint disagrees on indentation with ktfmt in some cases +# TODO(b/185904220): paren spacing disabled because of a ktfmt bug +# TODO(b/189506168): curly spacing disabled because of a ktfmt bug +disabled_rules=indent,paren-spacing,curly-spacing,wrapping diff --git a/ktlint.py b/ktlint.py index 16bc0f8..80703a4 100755 --- a/ktlint.py +++ b/ktlint.py @@ -28,6 +28,7 @@ import sys MAIN_DIRECTORY = os.path.normpath(os.path.dirname(__file__)) KTLINT_JAR = os.path.join(MAIN_DIRECTORY, 'ktlint-android-all.jar') +EDITOR_CONFIG = os.path.join(MAIN_DIRECTORY, '.editorconfig') FORMAT_MESSAGE = ''' ********************************************************************** To format run: @@ -45,6 +46,7 @@ def main(args=None): args = parser.parse_args() kt_files = [f for f in args.file if f.endswith('.kt') or f.endswith('.kts')] ktlint_args = kt_files[:] + ktlint_args += ['--editorconfig', EDITOR_CONFIG] if args.format: ktlint_args += ['-F'] if not ktlint_args: -- cgit v1.2.3 From 4149cdeb0128facacfb2d09ddcfa0661259c177f Mon Sep 17 00:00:00 2001 From: Jordan Demeulenaere Date: Fri, 10 Jun 2022 16:24:30 +0200 Subject: Fix ktlint hook In ag/18798895, I broke the early exit condition when the ktlint hook would receive 0 Kotlin files. Since that CL, ktlint would always run on a big number of files if th CL didn't contain any change in a .kt or .kts file. Bug: 235461679 Test: Manual Change-Id: Iaf21df8e85943229c1d832b186825b41cf231b2d --- ktlint.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ktlint.py b/ktlint.py index 80703a4..86b15ed 100755 --- a/ktlint.py +++ b/ktlint.py @@ -45,12 +45,13 @@ def main(args=None): parser.set_defaults(format=False) args = parser.parse_args() kt_files = [f for f in args.file if f.endswith('.kt') or f.endswith('.kts')] + if not kt_files: + sys.exit(0) + ktlint_args = kt_files[:] ktlint_args += ['--editorconfig', EDITOR_CONFIG] if args.format: ktlint_args += ['-F'] - if not ktlint_args: - sys.exit(0) ktlint_args += ['--android'] -- cgit v1.2.3 From aa768f01dbb57ceca4df0d14db981e3c97957ee2 Mon Sep 17 00:00:00 2001 From: Jordan Demeulenaere Date: Thu, 6 Oct 2022 14:49:32 +0200 Subject: Allow preupload hooks to ignore more formatting ktlint rules This CL adds a --no-verify-format rule to ktlint.py so that preuploads can ignore more formatting rules. This will be used by projects who already use ktfmt for formatting new files, so that useful ktlint errors (like unused variables) are not hidden by a ton of ktlint formatting issues which we are now mostly ignoring. Bug: 235461679 Test: Manual Change-Id: I324bcaf4e9e2b087201810343471a55f2434339f --- .editorconfig | 6 ------ ktlint.py | 16 ++++++++++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.editorconfig b/.editorconfig index e97aa07..1e3f51c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,9 +4,3 @@ max_line_length = 100 trim_trailing_whitespace = true insert_final_newline = true ij_kotlin_imports_layout=* - -# Comma-separated list of rules to disable -# ktlint disagrees on indentation with ktfmt in some cases -# TODO(b/185904220): paren spacing disabled because of a ktfmt bug -# TODO(b/189506168): curly spacing disabled because of a ktfmt bug -disabled_rules=indent,paren-spacing,curly-spacing,wrapping diff --git a/ktlint.py b/ktlint.py index 86b15ed..aefff63 100755 --- a/ktlint.py +++ b/ktlint.py @@ -42,14 +42,25 @@ def main(args=None): parser.add_argument('--file', '-f', nargs='*') parser.add_argument('--format', '-F', dest='format', action='store_true') parser.add_argument('--noformat', dest='format', action='store_false') - parser.set_defaults(format=False) + parser.add_argument('--no-verify-format', dest='verify_format', action='store_false') + parser.set_defaults(format=False, verify_format=True) args = parser.parse_args() kt_files = [f for f in args.file if f.endswith('.kt') or f.endswith('.kts')] if not kt_files: sys.exit(0) + disabled_rules = ['indent', 'paren-spacing', 'curly-spacing', 'wrapping'] + + # Disable more format-related rules if we shouldn't verify the format. This is usually + # the case if files we are checking are already checked by ktfmt. + if not args.verify_format: + disabled_rules += ['final-newline', 'no-consecutive-blank-lines', 'import-ordering'] + ktlint_args = kt_files[:] ktlint_args += ['--editorconfig', EDITOR_CONFIG] + ktlint_args += ['--disabled_rules=' + ','.join(disabled_rules)] + + # Automatically format files if requested. if args.format: ktlint_args += ['-F'] @@ -64,7 +75,8 @@ def main(args=None): if stdout: print('prebuilts/ktlint found errors in files you changed:') print(stdout.decode('utf-8')) - print(FORMAT_MESSAGE.format(MAIN_DIRECTORY, ' '.join(kt_files))) + if (args.verify_format): + print(FORMAT_MESSAGE.format(MAIN_DIRECTORY, ' '.join(kt_files))) sys.exit(1) else: sys.exit(0) -- cgit v1.2.3 From f32bd8e7cb130225464f031cf71935e6de31bf2c Mon Sep 17 00:00:00 2001 From: Simon Wingrove Date: Tue, 20 Dec 2022 09:34:15 +0000 Subject: Allow custom editorconfig if needed Allows preupload config to customize the editorconfig, or remove it in favor of directoy-specific ones, if desired. Test: manually Bug: 261139484 Change-Id: Ia4dbbf7dee4f96e5dcfd496cc79e73ae66f661f5 --- ktlint.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ktlint.py b/ktlint.py index aefff63..2574319 100755 --- a/ktlint.py +++ b/ktlint.py @@ -43,6 +43,7 @@ def main(args=None): parser.add_argument('--format', '-F', dest='format', action='store_true') parser.add_argument('--noformat', dest='format', action='store_false') parser.add_argument('--no-verify-format', dest='verify_format', action='store_false') + parser.add_argument('--editorconfig', default=EDITOR_CONFIG) parser.set_defaults(format=False, verify_format=True) args = parser.parse_args() kt_files = [f for f in args.file if f.endswith('.kt') or f.endswith('.kts')] @@ -57,9 +58,12 @@ def main(args=None): disabled_rules += ['final-newline', 'no-consecutive-blank-lines', 'import-ordering'] ktlint_args = kt_files[:] - ktlint_args += ['--editorconfig', EDITOR_CONFIG] ktlint_args += ['--disabled_rules=' + ','.join(disabled_rules)] + # Setup editor config explicitly if defined - else will inherit from tree + if args.editorconfig is not None: + ktlint_args += ['--editorconfig', args.editorconfig] + # Automatically format files if requested. if args.format: ktlint_args += ['-F'] -- cgit v1.2.3 From 10cb9cdd0196b240a369e01e49b804cff7555b7e Mon Sep 17 00:00:00 2001 From: Simon Wingrove Date: Tue, 20 Dec 2022 09:28:28 +0000 Subject: Prevent java.util.* wildcards from being supported Reduces set of allowable usescases for wildcare import from 2 to 1. This matches Google style, and usual code review comments. This does not count as a formatting check, so import checks apply even in --no-verify-format cases. ktfmt does not have equivalent behavior AFAIK. Test: manually Bug: 261139484 Change-Id: I9e02d4b8426a66a743ad8a160190ea59a15be522 --- .editorconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/.editorconfig b/.editorconfig index 1e3f51c..c7cc35a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,3 +4,4 @@ max_line_length = 100 trim_trailing_whitespace = true insert_final_newline = true ij_kotlin_imports_layout=* +ij_kotlin_packages_to_use_import_on_demand=kotlinx.android.synthetic.** -- cgit v1.2.3