diff --git a/dev/bots/analyze.dart b/dev/bots/analyze.dart index 7102ecb51f..5663ac062e 100644 --- a/dev/bots/analyze.dart +++ b/dev/bots/analyze.dart @@ -2126,7 +2126,8 @@ Future lintKotlinFiles(String workingDirectory) async { 'To reproduce this lint locally:\n' '1. Identify the CIPD version tag used to resolve this particular version of ktlint (check the dependencies section of this shard in the ci.yaml). \n' '2. Download that version from https://chrome-infra-packages.appspot.com/p/flutter/ktlint/linux-amd64/+/\n' - '3. From the repository root, run `/ktlint --editorconfig=$editorConfigRelativePath --baseline=$baselineRelativePath`'; + '3. From the repository root, run `/ktlint --editorconfig=$editorConfigRelativePath --baseline=$baselineRelativePath`\n' + 'Alternatively, if you use Android Studio, follow the docs at docs/platforms/android/Kotlin-android-studio-formatting.md to enable auto formatting.'; foundError([errorMessage]); } } diff --git a/docs/platforms/android/Kotlin-android-studio-formatting.md b/docs/platforms/android/Kotlin-android-studio-formatting.md new file mode 100644 index 0000000000..c5a1117be0 --- /dev/null +++ b/docs/platforms/android/Kotlin-android-studio-formatting.md @@ -0,0 +1,27 @@ +# Setting up Kotlin formatting in Android Studio + +Kotlin code in this repository is formatted and linted with [`ktlint`](https://github.com/pinterest/ktlint). +If you +1. have submitted Kotlin code only to learn this when the analyzer check fails +2. use Android Studio + + +then there is good news! Android Studio can be configured to use `ktlint` to automatically apply formatting and highlight issues. To do this: + + +1. Install the `ktlint` extension for Android Studio + + a. On Mac, this is `Android Studio > Settings > Plugins > ` Search for `ktlint`. + +2. Set the ruleset to be the same as the version used in [`.ci.yaml`](../../../.ci.yaml) (as of writing this is 1.1.1), and the baseline to be `dev/bots/test/analyze-test-input/ktlint-baseline.xml`. + + a. Both of these options should be available under `Android Studio > Settings > Tools > ktlint`. + +3. Additionally, Kotlin code in the Flutter repository currently uses some additional rules for compatibility with older versions of Kotlin. +These rules can only be configured by an `.editorconfig` file in the directory from which Android Studio was opened. To configure these rules, create a new `.editorconfig` file in the root of your flutter repository with the following content +``` +[*.{kt,kts}] +# Disable trailing commas to allow compatibility with Kotlin versions less than 1.4. +ij_kotlin_allow_trailing_comma = false +ij_kotlin_allow_trailing_comma_on_call_site = false +```