Skip to content

Commit

Permalink
Treat KDoc @propertys as special types of @params, fixes #431 (#498)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
no-preserve-root authored and facebook-github-bot committed Aug 8, 2024
1 parent 09cdd27 commit d2d9b93
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
21 changes: 12 additions & 9 deletions core/src/main/java/com/facebook/ktfmt/kdoc/ParagraphListBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -571,18 +571,21 @@ class ParagraphListBuilder(
return when {
isPriority -> -1
tag.startsWith("@param") -> 0
tag.startsWith("@property") -> 0
// @param and @property must be sorted by parameter order
// a @property is dedicated syntax for a main constructor @param that also sets a class
// property
tag.startsWith("@return") -> 1
tag.startsWith("@constructor") -> 2
tag.startsWith("@receiver") -> 3
tag.startsWith("@property") -> 4
tag.startsWith("@throws") -> 5
tag.startsWith("@exception") -> 6
tag.startsWith("@sample") -> 7
tag.startsWith("@see") -> 8
tag.startsWith("@author") -> 9
tag.startsWith("@since") -> 10
tag.startsWith("@suppress") -> 11
tag.startsWith("@deprecated") -> 12
tag.startsWith("@throws") -> 4
tag.startsWith("@exception") -> 5
tag.startsWith("@sample") -> 6
tag.startsWith("@see") -> 7
tag.startsWith("@author") -> 8
tag.startsWith("@since") -> 9
tag.startsWith("@suppress") -> 10
tag.startsWith("@deprecated") -> 11
else -> 100 // custom tags
}
}
Expand Down
14 changes: 10 additions & 4 deletions core/src/main/java/com/facebook/ktfmt/kdoc/Utilities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@ fun String.isKDocTag(): Boolean {
}

/**
* If this String represents a KDoc `@param` tag, returns the corresponding parameter name,
* If this String represents a KDoc tag named [tag], returns the corresponding parameter name,
* otherwise null.
*/
fun String.getParamName(): String? {
fun String.getTagName(tag: String): String? {
val length = this.length
var start = 0
while (start < length && this[start].isWhitespace()) {
start++
}
if (!this.startsWith("@param", start)) {
if (!this.startsWith(tag, start)) {
return null
}
start += "@param".length
start += tag.length

while (start < length) {
if (this[start].isWhitespace()) {
Expand Down Expand Up @@ -187,6 +187,12 @@ fun String.getParamName(): String? {
return null
}

/**
* If this String represents a KDoc `@param` or `@property` tag, returns the corresponding parameter
* name, otherwise null.
*/
fun String.getParamName(): String? = getTagName("@param") ?: getTagName("@property")

private fun getIndent(start: Int, lookup: (Int) -> Char): String {
var i = start - 1
while (i >= 0 && lookup(i) != '\n') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1356,8 +1356,8 @@ class FormatterTest {
| * Old {@link JavaDocLink} that gets removed.
| *
| * @param unused [Param]
| * @return [Unit] as [ReturnedValue]
| * @property JavaDocLink [Param]
| * @return [Unit] as [ReturnedValue]
| * @throws AnException
| * @throws AnException
| * @exception Sample.SampleException
Expand Down
28 changes: 28 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/kdoc/KDocFormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3145,6 +3145,34 @@ class KDocFormatterTest {
.trimIndent())
}

@Test
fun testPropertiesAreParams() {
val source =
"""
/**
* @param bar lorem ipsum
* @property baz dolor sit
* @property foo amet, consetetur
*/
"""
.trimIndent()
checkFormatter(
FormattingTask(
KDocFormattingOptions(72, 72),
source.trim(),
initialIndent = " ",
orderedParameterNames = listOf("foo", "bar", "baz"),
),
"""
/**
* @property foo amet, consetetur
* @param bar lorem ipsum
* @property baz dolor sit
*/
"""
.trimIndent())
}

@Test
fun testKnit() {
// Some tests for the knit plugin -- https://github.com/Kotlin/kotlinx-knit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class UtilitiesTest {
assertThat("@param[foo]".getParamName()).isEqualTo("foo")
assertThat("@param [foo]".getParamName()).isEqualTo("foo")
assertThat("@param ".getParamName()).isNull()
assertThat("@property foo".getParamName()).isEqualTo("foo")
}

@Test
Expand Down

0 comments on commit d2d9b93

Please sign in to comment.