forked from mirrors/gecko-dev
Bug 1740799 - Format Kotlin code using ktlint. r=geckoview-reviewers,owlish,linter-reviewers,sylvestre
Android-component and Fenix use ktlint, so mozilla-central should use same tools via spotless. This is from https://phabricator.services.mozilla.com/D131018 Differential Revision: https://phabricator.services.mozilla.com/D161557
This commit is contained in:
parent
3f99c2ac51
commit
1756e8266e
7 changed files with 49 additions and 6 deletions
|
|
@ -362,5 +362,12 @@ subprojects {
|
||||||
}
|
}
|
||||||
googleJavaFormat('1.15.0')
|
googleJavaFormat('1.15.0')
|
||||||
}
|
}
|
||||||
|
kotlin {
|
||||||
|
target project.fileTree(project.projectDir) {
|
||||||
|
include '**/*.kt'
|
||||||
|
exclude '**/thirdparty/**'
|
||||||
|
}
|
||||||
|
ktlint('0.46.1')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -132,7 +132,7 @@ In this document, we try to list these all tools.
|
||||||
- :ref:`clippy`
|
- :ref:`clippy`
|
||||||
- https://github.com/rust-lang/rust-clippy
|
- https://github.com/rust-lang/rust-clippy
|
||||||
|
|
||||||
.. list-table:: Java
|
.. list-table:: Java/Kotlin
|
||||||
:widths: 20 20 20 20 20
|
:widths: 20 20 20 20 20
|
||||||
:header-rows: 1
|
:header-rows: 1
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,10 @@ for Gradle and Android.
|
||||||
|
|
||||||
In our current configuration, Spotless includes the
|
In our current configuration, Spotless includes the
|
||||||
`Google Java Format plug-in <https://github.com/google/google-java-format>`__
|
`Google Java Format plug-in <https://github.com/google/google-java-format>`__
|
||||||
which formats all our Java code using the Google Java coding style guidelines.
|
which formats all our Java code using the Google Java coding style guidelines,
|
||||||
|
and `ktlint <https://ktlint.github.io/>`__ which formats all
|
||||||
|
our Kotlin code using the official Kotlin coding convention and Android Kotlin
|
||||||
|
Style Guide.
|
||||||
|
|
||||||
|
|
||||||
Run Locally
|
Run Locally
|
||||||
|
|
|
||||||
|
|
@ -123,6 +123,10 @@ native code. `Bug
|
||||||
1509539 <https://bugzilla.mozilla.org/show_bug.cgi?id=1509539>`_ tracks
|
1509539 <https://bugzilla.mozilla.org/show_bug.cgi?id=1509539>`_ tracks
|
||||||
making Android Studio and Gradle do this automatically.
|
making Android Studio and Gradle do this automatically.
|
||||||
|
|
||||||
|
If you want set up code formatting for Kotlin, please reference
|
||||||
|
`IntelliJ IDEA configuration
|
||||||
|
<https://pinterest.github.io/ktlint/rules/configuration-intellij-idea/>`_.
|
||||||
|
|
||||||
Custom mozconfig with Android Studio
|
Custom mozconfig with Android Studio
|
||||||
------------------------------------
|
------------------------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -284,12 +284,14 @@ def gradle_android_api_lint_tasks(build_config):
|
||||||
set_config("GRADLE_ANDROID_API_LINT_TASKS", gradle_android_api_lint_tasks)
|
set_config("GRADLE_ANDROID_API_LINT_TASKS", gradle_android_api_lint_tasks)
|
||||||
|
|
||||||
|
|
||||||
set_config("GRADLE_ANDROID_FORMAT_LINT_FIX_TASKS", ["spotlessJavaApply"])
|
set_config(
|
||||||
|
"GRADLE_ANDROID_FORMAT_LINT_FIX_TASKS", ["spotlessJavaApply", "spotlessKotlinApply"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@dependable
|
@dependable
|
||||||
def gradle_android_format_lint_check_tasks():
|
def gradle_android_format_lint_check_tasks():
|
||||||
return ["spotlessJavaCheck"]
|
return ["spotlessJavaCheck", "spotlessKotlinCheck"]
|
||||||
|
|
||||||
|
|
||||||
set_config(
|
set_config(
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ android-format:
|
||||||
description: Android formatting lint
|
description: Android formatting lint
|
||||||
include: ['mobile/android']
|
include: ['mobile/android']
|
||||||
exclude: []
|
exclude: []
|
||||||
extensions: ['java']
|
extensions: ['java', 'kt']
|
||||||
support-files:
|
support-files:
|
||||||
- 'mobile/android/**/Makefile.in'
|
- 'mobile/android/**/Makefile.in'
|
||||||
- 'mobile/android/config/**'
|
- 'mobile/android/config/**'
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,8 @@ def gradle(log, topsrcdir=None, topobjdir=None, tasks=[], extra_args=[], verbose
|
||||||
proc.kill()
|
proc.kill()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
return proc.returncode
|
||||||
|
|
||||||
|
|
||||||
def format(config, fix=None, **lintargs):
|
def format(config, fix=None, **lintargs):
|
||||||
topsrcdir = lintargs["root"]
|
topsrcdir = lintargs["root"]
|
||||||
|
|
@ -93,7 +95,7 @@ def format(config, fix=None, **lintargs):
|
||||||
else:
|
else:
|
||||||
tasks = lintargs["substs"]["GRADLE_ANDROID_FORMAT_LINT_CHECK_TASKS"]
|
tasks = lintargs["substs"]["GRADLE_ANDROID_FORMAT_LINT_CHECK_TASKS"]
|
||||||
|
|
||||||
gradle(
|
ret = gradle(
|
||||||
lintargs["log"],
|
lintargs["log"],
|
||||||
topsrcdir=topsrcdir,
|
topsrcdir=topsrcdir,
|
||||||
topobjdir=topobjdir,
|
topobjdir=topobjdir,
|
||||||
|
|
@ -116,6 +118,31 @@ def format(config, fix=None, **lintargs):
|
||||||
"level": "error",
|
"level": "error",
|
||||||
}
|
}
|
||||||
results.append(result.from_config(config, **err))
|
results.append(result.from_config(config, **err))
|
||||||
|
folder = os.path.join(
|
||||||
|
topobjdir, "gradle", "build", path, "spotless", "spotlessKotlin"
|
||||||
|
)
|
||||||
|
for filename in glob.iglob(folder + "/**/*.kt", recursive=True):
|
||||||
|
err = {
|
||||||
|
"rule": "spotless-kt",
|
||||||
|
"path": os.path.join(path, mozpath.relpath(filename, folder)),
|
||||||
|
"lineno": 0,
|
||||||
|
"column": 0,
|
||||||
|
"message": "Formatting error, please run ./mach lint -l android-format --fix",
|
||||||
|
"level": "error",
|
||||||
|
}
|
||||||
|
results.append(result.from_config(config, **err))
|
||||||
|
|
||||||
|
if len(results) == 0 and ret != 0:
|
||||||
|
# spotless seems to hit unfixed error.
|
||||||
|
err = {
|
||||||
|
"rule": "spotless",
|
||||||
|
"path": "",
|
||||||
|
"lineno": 0,
|
||||||
|
"column": 0,
|
||||||
|
"message": "Unexpected error",
|
||||||
|
"level": "error",
|
||||||
|
}
|
||||||
|
results.append(result.from_config(config, **err))
|
||||||
|
|
||||||
# If --fix was passed, we just report the number of files that were changed
|
# If --fix was passed, we just report the number of files that were changed
|
||||||
if fix:
|
if fix:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue