|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.facebook.ktfmt.cli |
| 18 | + |
| 19 | +import com.facebook.ktfmt.format.FormattingOptions |
| 20 | +import com.facebook.ktfmt.format.TrailingCommaManagementStrategy |
| 21 | +import java.io.File |
| 22 | +import java.util.concurrent.ConcurrentHashMap |
| 23 | +import org.ec4j.core.EditorConfigLoader |
| 24 | +import org.ec4j.core.PropertyTypeRegistry |
| 25 | +import org.ec4j.core.Resource |
| 26 | +import org.ec4j.core.ResourceProperties |
| 27 | +import org.ec4j.core.ResourcePropertiesService |
| 28 | +import org.ec4j.core.model.EditorConfig |
| 29 | +import org.ec4j.core.model.PropertyType |
| 30 | +import org.ec4j.core.model.Version.CURRENT |
| 31 | + |
| 32 | +object EditorConfigResolver { |
| 33 | + |
| 34 | + private val ijContinuationIndentSize: PropertyType<Int> = |
| 35 | + PropertyType.LowerCasingPropertyType( |
| 36 | + "ij_continuation_indent_size", |
| 37 | + "Denotes the continuation indent size. Useful to distinguish code blocks versus continuation lines", |
| 38 | + PropertyType.PropertyValueParser.POSITIVE_INT_VALUE_PARSER, |
| 39 | + ) |
| 40 | + |
| 41 | + private val commaManagementStrategy: PropertyType<TrailingCommaManagementStrategy> = |
| 42 | + PropertyType.LowerCasingPropertyType( |
| 43 | + "ktfmt_trailing_comma_management_strategy", |
| 44 | + "Ktfmt Trailing Comma Management Strategy", |
| 45 | + { _: String, value: String -> |
| 46 | + TrailingCommaManagementStrategy.entries |
| 47 | + .find { it.name.lowercase() == value } |
| 48 | + ?.let { PropertyType.PropertyValue.valid(value, it) } |
| 49 | + ?: PropertyType.PropertyValue.invalid( |
| 50 | + value, |
| 51 | + "Unknown ktfmt_trailing_comma_management_strategy value '$value'", |
| 52 | + ) |
| 53 | + }, |
| 54 | + TrailingCommaManagementStrategy.entries.map { it.name.lowercase() }.toSet(), |
| 55 | + ) |
| 56 | + |
| 57 | + private val propertyTypeRegistry by |
| 58 | + lazy(LazyThreadSafetyMode.NONE) { |
| 59 | + PropertyTypeRegistry.builder() |
| 60 | + .defaults() |
| 61 | + .type(PropertyType.max_line_length) // missing from defaults? |
| 62 | + .type(ijContinuationIndentSize) |
| 63 | + .type(commaManagementStrategy) |
| 64 | + .build() |
| 65 | + } |
| 66 | + |
| 67 | + private object Cache : org.ec4j.core.Cache { |
| 68 | + val cached = ConcurrentHashMap<Resource, EditorConfig>() |
| 69 | + |
| 70 | + override fun get( |
| 71 | + editorConfigFile: Resource, |
| 72 | + loader: EditorConfigLoader, |
| 73 | + ): EditorConfig = cached.computeIfAbsent(editorConfigFile, loader::load) |
| 74 | + } |
| 75 | + |
| 76 | + private val resourcePropertiesService: ResourcePropertiesService by |
| 77 | + lazy(LazyThreadSafetyMode.NONE) { |
| 78 | + ResourcePropertiesService.builder() |
| 79 | + .cache(Cache) |
| 80 | + .loader(EditorConfigLoader.of(CURRENT, propertyTypeRegistry)) |
| 81 | + .build() |
| 82 | + } |
| 83 | + |
| 84 | + fun resolveFormattingOptions(file: File, baseOptions: FormattingOptions): FormattingOptions = |
| 85 | + resourcePropertiesService |
| 86 | + .queryProperties(Resource.Resources.ofPath(file.toPath(), Charsets.UTF_8)) |
| 87 | + .resolveFormattingOptions(baseOptions) |
| 88 | + |
| 89 | + private fun ResourceProperties.resolveFormattingOptions( |
| 90 | + baseOptions: FormattingOptions, |
| 91 | + ): FormattingOptions { |
| 92 | + // `max_line_length` may return null to indicate 'off', in which case we keep the base maxWidth |
| 93 | + val maxWidth = |
| 94 | + getValue(PropertyType.max_line_length, baseOptions.maxWidth, false) ?: baseOptions.maxWidth |
| 95 | + |
| 96 | + // `indent_size` may return null to indicate 'tab', in which case we defer to `tab_width` |
| 97 | + val blockIndent = |
| 98 | + getValue(PropertyType.indent_size, baseOptions.blockIndent, false) |
| 99 | + ?: getValue(PropertyType.tab_width, baseOptions.blockIndent, false) |
| 100 | + |
| 101 | + val continuationIndent = |
| 102 | + getValue(ijContinuationIndentSize, baseOptions.continuationIndent, false) |
| 103 | + |
| 104 | + val trailingCommaStrategy = |
| 105 | + getValue(commaManagementStrategy, baseOptions.trailingCommaManagementStrategy, false) |
| 106 | + |
| 107 | + val resolved = |
| 108 | + baseOptions.copy( |
| 109 | + maxWidth = maxWidth, |
| 110 | + blockIndent = blockIndent, |
| 111 | + continuationIndent = continuationIndent, |
| 112 | + trailingCommaManagementStrategy = trailingCommaStrategy, |
| 113 | + ) |
| 114 | + return resolved |
| 115 | + } |
| 116 | +} |
0 commit comments