Skip to content

Commit d2d9b93

Browse files
Treat KDoc @propertys as special types of @params, fixes #431 (#498)
Summary: Treat `property` the same as `param` (which matches the description in the KDoc documentation). This fixes an incompatibility between ktfmt and detekt's formatting rules. Pull Request resolved: #498 Reviewed By: strulovich Differential Revision: D60925985 Pulled By: hick209 fbshipit-source-id: 2868a20c0256d4aa85f6c0df23b3bb1642bcacd7
1 parent 09cdd27 commit d2d9b93

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

core/src/main/java/com/facebook/ktfmt/kdoc/ParagraphListBuilder.kt

+12-9
Original file line numberDiff line numberDiff line change
@@ -571,18 +571,21 @@ class ParagraphListBuilder(
571571
return when {
572572
isPriority -> -1
573573
tag.startsWith("@param") -> 0
574+
tag.startsWith("@property") -> 0
575+
// @param and @property must be sorted by parameter order
576+
// a @property is dedicated syntax for a main constructor @param that also sets a class
577+
// property
574578
tag.startsWith("@return") -> 1
575579
tag.startsWith("@constructor") -> 2
576580
tag.startsWith("@receiver") -> 3
577-
tag.startsWith("@property") -> 4
578-
tag.startsWith("@throws") -> 5
579-
tag.startsWith("@exception") -> 6
580-
tag.startsWith("@sample") -> 7
581-
tag.startsWith("@see") -> 8
582-
tag.startsWith("@author") -> 9
583-
tag.startsWith("@since") -> 10
584-
tag.startsWith("@suppress") -> 11
585-
tag.startsWith("@deprecated") -> 12
581+
tag.startsWith("@throws") -> 4
582+
tag.startsWith("@exception") -> 5
583+
tag.startsWith("@sample") -> 6
584+
tag.startsWith("@see") -> 7
585+
tag.startsWith("@author") -> 8
586+
tag.startsWith("@since") -> 9
587+
tag.startsWith("@suppress") -> 10
588+
tag.startsWith("@deprecated") -> 11
586589
else -> 100 // custom tags
587590
}
588591
}

core/src/main/java/com/facebook/ktfmt/kdoc/Utilities.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,19 @@ fun String.isKDocTag(): Boolean {
139139
}
140140

141141
/**
142-
* If this String represents a KDoc `@param` tag, returns the corresponding parameter name,
142+
* If this String represents a KDoc tag named [tag], returns the corresponding parameter name,
143143
* otherwise null.
144144
*/
145-
fun String.getParamName(): String? {
145+
fun String.getTagName(tag: String): String? {
146146
val length = this.length
147147
var start = 0
148148
while (start < length && this[start].isWhitespace()) {
149149
start++
150150
}
151-
if (!this.startsWith("@param", start)) {
151+
if (!this.startsWith(tag, start)) {
152152
return null
153153
}
154-
start += "@param".length
154+
start += tag.length
155155

156156
while (start < length) {
157157
if (this[start].isWhitespace()) {
@@ -187,6 +187,12 @@ fun String.getParamName(): String? {
187187
return null
188188
}
189189

190+
/**
191+
* If this String represents a KDoc `@param` or `@property` tag, returns the corresponding parameter
192+
* name, otherwise null.
193+
*/
194+
fun String.getParamName(): String? = getTagName("@param") ?: getTagName("@property")
195+
190196
private fun getIndent(start: Int, lookup: (Int) -> Char): String {
191197
var i = start - 1
192198
while (i >= 0 && lookup(i) != '\n') {

core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1356,8 +1356,8 @@ class FormatterTest {
13561356
| * Old {@link JavaDocLink} that gets removed.
13571357
| *
13581358
| * @param unused [Param]
1359-
| * @return [Unit] as [ReturnedValue]
13601359
| * @property JavaDocLink [Param]
1360+
| * @return [Unit] as [ReturnedValue]
13611361
| * @throws AnException
13621362
| * @throws AnException
13631363
| * @exception Sample.SampleException

core/src/test/java/com/facebook/ktfmt/kdoc/KDocFormatterTest.kt

+28
Original file line numberDiff line numberDiff line change
@@ -3145,6 +3145,34 @@ class KDocFormatterTest {
31453145
.trimIndent())
31463146
}
31473147

3148+
@Test
3149+
fun testPropertiesAreParams() {
3150+
val source =
3151+
"""
3152+
/**
3153+
* @param bar lorem ipsum
3154+
* @property baz dolor sit
3155+
* @property foo amet, consetetur
3156+
*/
3157+
"""
3158+
.trimIndent()
3159+
checkFormatter(
3160+
FormattingTask(
3161+
KDocFormattingOptions(72, 72),
3162+
source.trim(),
3163+
initialIndent = " ",
3164+
orderedParameterNames = listOf("foo", "bar", "baz"),
3165+
),
3166+
"""
3167+
/**
3168+
* @property foo amet, consetetur
3169+
* @param bar lorem ipsum
3170+
* @property baz dolor sit
3171+
*/
3172+
"""
3173+
.trimIndent())
3174+
}
3175+
31483176
@Test
31493177
fun testKnit() {
31503178
// Some tests for the knit plugin -- https://github.com/Kotlin/kotlinx-knit

core/src/test/java/com/facebook/ktfmt/kdoc/UtilitiesTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class UtilitiesTest {
101101
assertThat("@param[foo]".getParamName()).isEqualTo("foo")
102102
assertThat("@param [foo]".getParamName()).isEqualTo("foo")
103103
assertThat("@param ".getParamName()).isNull()
104+
assertThat("@property foo".getParamName()).isEqualTo("foo")
104105
}
105106

106107
@Test

0 commit comments

Comments
 (0)