gpt4 book ai didi

android - 如何实现 LintFix 以将缺少的注释添加到 Android 中的类定义中

转载 作者:行者123 更新时间:2023-12-05 00:16:06 28 4
gpt4 key购买 nike

我正在调查当前 Android 应用程序中的自定义 Lint 规则开发。

我的检测器找到我的项目中未使用特定注释进行注释的所有 Activity 。

我想创建一个 LintFix,添加缺少的注释,如下所示:-

Activity 出错

class MyActivity : AppCompatActivity() {
...
}

Activity 已修复

@MyMissingAnnotation
class MyActivity : AppCompatActivity() {
...
}

我开发的代码是:-

        val fix = LintFix.create()
.replace()
.text("")
.with("@MyMissingAnnotation")
.build()

但是这会导致以下损坏的代码

class @MyMissingAnnotationMyActivity : AppCompatActivity() {
...
}

因为我的报告与此类似

 context.report(
ISSUE, node,
context.getNameLocation(node),
"Activities require the @MyMissingAnnotation annotation.",
fix
)

如何在类中的正确位置添加所需的注释?

最佳答案

您可以使用 shortenNames()beginning()reformat(true) 使您的修复更加合理

val fix = LintFix.create()
.replace()
.text("")
.with("@com.foo.bar2.MyMissingAnnotation")
.beginning()
.shortenNames()
.reformat(true)
.range(context.getLocation(node as UElement))
.build()

关于android - 如何实现 LintFix 以将缺少的注释添加到 Android 中的类定义中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62171721/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com