Skip to content

Commit

Permalink
Remove support to generate IntelliJ IDEA configuration files. Rename …
Browse files Browse the repository at this point in the history
…`.editorconfig` property `disabled_rules` to `ktlint_disabled_rules`. (#1570)

Closes #701
Closes #734
  • Loading branch information
paul-dingemans committed Aug 7, 2022
1 parent 6ed2589 commit 6fdb80e
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 647 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -122,6 +122,12 @@ Noteworthy changes:

The utility functions provided via `com.pinterest.ktlint.core.internal.CurrentBaseline` are moved to the new class. One new method `List<LintError>.doesNotContain(lintError: LintError)` is added.

#### .editorconfig property "disabled_rules"

The `.editorconfig` property `disabled_rules` (api property `DefaultEditorConfigProperties.disabledRulesProperty`) has been deprecated and will be removed in a future version. Use `ktlint_disabled_rules` (api property `DefaultEditorConfigProperties.ktlintDisabledRulesProperty`) instead as it more clearly identifies that ktlint is the owner of the property. This property is to be renamed in `.editorconfig` files and `ExperimentalParams.editorConfigOverride`.

Although, Ktlint 0.47.0 falls back on property `disabled_rules` whenever `ktlint_disabled_rules` is not found, this result in a warning message being printed.

### Added

* Add `format` reporter. This reporter prints a one-line-summary of the formatting status per file. ([#621](https://github.com/pinterest/ktlint/issue/621)).
Expand All @@ -147,8 +153,10 @@ The utility functions provided via `com.pinterest.ktlint.core.internal.CurrentBa

* Print an error message and return with non-zero exit code when no files are found that match with the globs ([#629](https://github.com/pinterest/ktlint/issue/629)).
* Invoke callback on `format` function for all errors including errors that are autocorrected ([#1491](https://github.com/pinterest/ktlint/issues/1491))
* Rename `.editorconfig` property `disabled_rules` to `ktlint_disabled_rules` ([#701](https://github.com/pinterest/ktlint/issues/701))

### Removed
* Remove support to generate IntelliJ IDEA configuration files as this no longer fits the scope of the ktlint project ([#701](https://github.com/pinterest/ktlint/issues/701))

## [0.46.1] - 2022-06-21

Expand Down
4 changes: 0 additions & 4 deletions docs/install/cli.md
Expand Up @@ -177,10 +177,6 @@ ktlint installGitPrePushHook

`-a` or `--android`: Turn on Android Kotlin Style Guide compatibility. This flag is most likely to be removed in a future version. Use `.editorconfig ktlint_code_style`(https://pinterest.github.io/ktlint/rules/configuration/#code-style).

`applyToIDEA` or `--apply-to-idea`: Update Intellij IDEA Kotlin codestyle settings (global)

`applyToIDEAProject` or `--apply-to-idea-project`: Update Intellij IDEA project settings

`--color` and `--color-name=<colorName>`: Make output colorful and optionally set the color name to use.

`--disabled_rules=<disabledRules>`: A comma-separated list of rules to globally disable. To disable the standard ktlint rule-set use `--disabled_rules=standard`. This flag is most likely to be removed in a future version. Use `.editorconfig disabled_rules`(https://pinterest.github.io/ktlint/rules/configuration/#disabled-rules).
Expand Down
89 changes: 0 additions & 89 deletions docs/install/integrations.md
Expand Up @@ -188,95 +188,6 @@ val ktlintFormat by tasks.creating(JavaExec::class) {
}
```

## [IntelliJ IDEA](https://www.jetbrains.com/idea/) integration

!!! Warning
`ktlint` strives to prevent code formatting conflicts with IntelliJ IDEA / Android Studio. We recommend using either IDE formatting or `ktlint` formatting. However, if you persist on using both, then please ensure that the formatting settings are aligned as described below. This reduces the chance that code which is formatted by ktlint conflicts with formatting by the IntelliJ IDEA built-in formatter.

Choose any of options below to align the formatting settings of IntelliJ IDEA.

### Update code style of single project via ktlint (recommended)

Use [ktlint cli](../cli) to change the code style settings of a single project with any of the commands below.
```sh
# Run command below from root directory of project
ktlint applyToIDEAProject
```

Or if you want to use android specific code style:

```sh
# Run command below from root directory of project
ktlint --android applyToIDEAProject
```

### Update global code style for all projects via ktlint

Use [ktlint cli](../cli) to change the code style settings of all projects with any of the commands below.
```sh
ktlint applyToIDEA
```

Or if you want to use android specific code style:
```sh
ktlint --android applyToIDEA
```

### Manually update `.editorconfig`

Create or update the code style config with `.editorconfig` by setting properties below:
```ini
[{*.kt,*.kts}]
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL

ij_kotlin_line_comment_at_first_column = false
ij_kotlin_line_comment_add_space = true

# These options can keep to use single name import
ij_kotlin_name_count_to_use_star_import = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647

ij_kotlin_keep_blank_lines_in_declarations = 1
ij_kotlin_keep_blank_lines_in_code = 1
ij_kotlin_keep_blank_lines_before_right_brace = 0

# optional but recommended
ij_kotlin_align_multiline_parameters = false

# optional but recommended
ij_continuation_indent_size = 4

# Android specific rules
ij_kotlin_import_nested_classes = false
ij_kotlin_imports_layout = *,^
```

### Manually update the IntelliJ IDEA preferences

!!! Warning
`ktlint` is *not* reading the IntelliJ IDEA preferences. If your settings are out of sync with the configuration in `.editorconfig` or the default values of `ktlint`, this results in formatting differences.

Go to <kbd>File</kbd> -> <kbd>Settings...</kbd> -> <kbd>Editor</kbd>

- <kbd>General</kbd> -> <kbd>Auto Import</kbd>
- check `Kotlin` / `Optimize imports on the fly (for current project)`.
- <kbd>Code Style</kbd> -> <kbd>Kotlin</kbd>
- <kbd>Set from...</kbd> on the right -> (<kbd>Predefined style</kbd>) -> <kbd>Kotlin style guide</kbd> (Kotlin plugin 1.2.20+).
- open <kbd>Code Generation</kbd> tab
- uncheck `Line comment at first column`;
- select `Add a space at comment start`.
- open <kbd>Imports</kbd> tab
- select `Use single name import` (all of them);
- remove `import java.util.*` from `Packages to Use Import with '*'`.
- open <kbd>Blank Lines</kbd> tab
- change `Keep Maximum Blank Lines` / `In declarations` & `In code` to 1 and `Before '}'` to 0.
- (optional but recommended) open <kbd>Wrapping and Braces</kbd> tab
- uncheck `Function declaration parameters` (OR `Methods declartion parameters` for older version) / `Align when multiline`.
- (optional but recommended) open <kbd>Tabs and Indents</kbd> tab
- change `Continuation indent` to the same value as `Indent` (4 by default).
- <kbd>Inspections</kbd>
- change `Severity` level of `Unused import directive` and `Redundant semicolon` under `Kotlin` -> `Redundant constructs` to `ERROR`.

## [GNU Emacs](https://www.gnu.org/software/emacs/) integration

See [whirm/flycheck-kotlin](https://github.com/whirm/flycheck-kotlin).
Expand Down
77 changes: 0 additions & 77 deletions docs/mkdocs.yml

This file was deleted.

36 changes: 36 additions & 0 deletions docs/rules/configuration-intellij-idea.md
@@ -0,0 +1,36 @@
!!! Warning
`ktlint` strives to prevent code formatting conflicts with IntelliJ IDEA / Android Studio. We recommend using either IDE formatting or `ktlint` formatting. However, if you persist on using both, then please ensure that the formatting settings are aligned as described below. This reduces the chance that code which is formatted by ktlint conflicts with formatting by the IntelliJ IDEA built-in formatter.

!!! Note
IntelliJ IDEA supports the [kotlin coding conventions](https://kotlinlang.org/docs/coding-conventions.html). As of version 0.47.x of ktlint, the support to overwrite some configuration files of IntelliJ IDEA has been dropped as it no longer fits the scope of the project.


Steps:

1. Go to your project directory
2. Create or replace file `.idea/codeStyles/codeStyleConfig.xml` with content below:
```xml
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>
```
3. Create or replace file `.idea/codeStyles/Project.xml` with content below:
```xml
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value />
</option>
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<codeStyleSettings language="kotlin">
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</codeStyleSettings>
</code_scheme>
</component>
```
File renamed without changes.
Expand Up @@ -85,6 +85,10 @@ public interface UsesEditorConfigProperties {
editorConfigProperty: EditorConfigProperty<T>,
codeStyleValue: CodeStyleValue
): T {
if (editorConfigProperty.deprecationWarning != null) {
logger.warn { "Property '${editorConfigProperty.type.name}' is deprecated: ${editorConfigProperty.deprecationWarning}" }
}

val property = get(editorConfigProperty.type.name)

// If the property value is remapped to a non-null value then return it immediately.
Expand Down Expand Up @@ -178,7 +182,12 @@ public interface UsesEditorConfigProperties {
* value of the property. The
*/
public val propertyMapper: ((Property?, CodeStyleValue) -> T?)? = null,
public val propertyWriter: (T) -> String = { it.toString() }
public val propertyWriter: (T) -> String = { it.toString() },

/**
* Optional message to be displayed whenever the value of the property is being retrieved.
*/
internal val deprecationWarning: String? = null
)
}

Expand Down Expand Up @@ -207,6 +216,10 @@ public object DefaultEditorConfigProperties : UsesEditorConfigProperties {
defaultAndroidValue = android
)

@Deprecated(
message = "Marked for removal in KtLint 0.48",
replaceWith = ReplaceWith("ktlintDisabledRulesProperty")
)
public val disabledRulesProperty: UsesEditorConfigProperties.EditorConfigProperty<String> =
UsesEditorConfigProperties.EditorConfigProperty(
type = PropertyType.LowerCasingPropertyType(
Expand All @@ -216,6 +229,30 @@ public object DefaultEditorConfigProperties : UsesEditorConfigProperties {
emptySet()
),
defaultValue = "",
propertyMapper = { property, _ ->
when {
property?.isUnset == true -> ""
property?.getValueAs<String>() != null -> {
// Remove spaces (most likely they occur only around the comma) as they otherwise will be seen
// as part of the rule-id which is to be disabled. But as the space is not allowed in the id's
// of rule sets and rule ids, they are just removed all.
property.getValueAs<String>().replace(" ", "")
}
else -> property?.getValueAs()
}
},
deprecationWarning = "Rename property 'disabled_rules' to 'ktlint_disabled_rules' in all '.editorconfig' files."
)

public val ktlintDisabledRulesProperty: UsesEditorConfigProperties.EditorConfigProperty<String> =
UsesEditorConfigProperties.EditorConfigProperty(
type = PropertyType.LowerCasingPropertyType(
"ktlint_disabled_rules",
"A comma separated list of rule ids which should not be run. For rules not defined in the 'standard' ruleset, the qualified rule-id should be used.",
PropertyType.PropertyValueParser.IDENTITY_VALUE_PARSER,
emptySet()
),
defaultValue = "",
propertyMapper = { property, _ ->
when {
property?.isUnset == true -> ""
Expand Down
Expand Up @@ -4,6 +4,7 @@ import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleRunner
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties.disabledRulesProperty
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties.ktlintDisabledRulesProperty
import com.pinterest.ktlint.core.api.EditorConfigProperties
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties.EditorConfigProperty
Expand All @@ -22,7 +23,10 @@ internal class VisitorProvider(
*/
recreateRuleSorter: Boolean = false
) : UsesEditorConfigProperties {
override val editorConfigProperties: List<EditorConfigProperty<*>> = listOf(disabledRulesProperty)
override val editorConfigProperties: List<EditorConfigProperty<*>> = listOf(
ktlintDisabledRulesProperty,
disabledRulesProperty
)

/**
* The list of [ruleRunners] is sorted based on the [Rule.VisitorModifier] of the rules.
Expand Down Expand Up @@ -80,12 +84,21 @@ internal class VisitorProvider(
}
}

private fun isNotDisabled(editorConfigProperties: EditorConfigProperties, qualifiedRuleId: String): Boolean =
editorConfigProperties
.getEditorConfigValue(disabledRulesProperty)
private fun isNotDisabled(editorConfigProperties: EditorConfigProperties, qualifiedRuleId: String): Boolean {
val ktlintDisabledRulesProperty =
if (editorConfigProperties.containsKey(ktlintDisabledRulesProperty.type.name) ||
!editorConfigProperties.containsKey(disabledRulesProperty.type.name)
) {
// New property takes precedence when defined, or, when both old and new property are not defined.
editorConfigProperties.getEditorConfigValue(ktlintDisabledRulesProperty)
} else {
editorConfigProperties.getEditorConfigValue(disabledRulesProperty)
}
return ktlintDisabledRulesProperty
.split(",")
.none {
// The rule set id in the disabled_rules setting may be omitted for rules in the standard rule set
it.toQualifiedRuleId() == qualifiedRuleId
}
}
}

0 comments on commit 6fdb80e

Please sign in to comment.