0

I am currently writing a custom lint for a library I made and I've implemented most of the stuff.

The thing which is bugging me is how to create quickDataFix to add a method in a UClass.

So for eg: I have the following code where the LintFix will be applied.

class TestClass {
}

After applying the fix, it should become

class TestClass {
    fun method1() { }
}

So far, I've been trying this rule.

val fix = LintFix.create()
    .replace()
    .name("Add method")
    .pattern("TestClass(.*)")
    .with("TestClass {\nfun method1() { }\n")
    .shortenNames()
    .range(context.getLocation(clazz.scope)) // 'context' is JavaContext & 'clazz' is UClass
    .build()

But nothing seems to work, I tried googling & finding projects that have lint checks so that I can read the source code & try to solve my problem but no luck.

Any help would be really appreciated.

kaustubhpatange
  • 442
  • 5
  • 13

1 Answers1

1

Finally after a lot of trial & error methods. I figured out how to do it.

Well, unlike UMethod, UClass provides context until it's className. So we need to modify its range in such a way that the whole class scope should be available. This can be easily done by JavaContext.getRangeLocation()

val fix = LintFix.create()
    .replace()
    .name("Add method")
    .pattern("TestClass(.*)")
    .with("TestClass {\nfun method1() { }\n")
    .shortenNames()
    .range(context.getRangeLocation(clazz.navigationElement, 0, clazz.asSourceString().length))
    .build()

Here fromDelta is 0 & toDelta is length of the whole class text.

kaustubhpatange
  • 442
  • 5
  • 13