https://kotlinlang.org logo
Title
l

Lulu

02/10/2022, 12:16 PM
So I'm just trying to understand here. Can KSP show the user an error while they're writing the code if the semantics don't check out? (say for example, they used a type in the wrong place) and if so, how to enable such mechanism? My use case is basically making sure functions written by the user have parameters and return types that make sense to my library.
g

Grégory Lureau

02/10/2022, 12:57 PM
I think you're looking for a static code analyzer (linter) instead. Lint rules can be executed in IDE and provides warnings/errors. KSP is more to generate code from the class declarations at the compile time, so if you don't generate code it's maybe not the best tool.
l

Lulu

02/10/2022, 1:32 PM
I do need to generate code based on function names etc. Are you suggesting I should use KSP with a static code analyzer?
g

Grégory Lureau

02/10/2022, 1:52 PM
That or you can use the environment.logger to print a warning in the build log if it's enough, depends of your needs... I'm not sure about making ksp "real time" enough for the IDE though.
l

Lulu

02/10/2022, 2:12 PM
I think KSP will do for now. If I write a custom plugin it's like I'm changing the concept of the language. Thank you for the input though really appreciate it
f

Fudge

02/10/2022, 11:34 PM
It's possible to point an error to an exact code element, meaning if the semantics don't check out compilation will fail and will point to the element you specified with your error message.
It is sort of like a normal compiler error except the IDE analyzer doesn't show it
l

Lulu

02/10/2022, 11:45 PM
That means the user won't see until they build right?
a

atlantis210

02/11/2022, 9:15 AM
exactly, that's how KSP work
g

Grégory Lureau

02/11/2022, 9:55 AM
Is it possible to run KSP in "real time" in the IDE with a custom gradle plugin or something similar?
I'm almost convinced it's too heavy to work well. A linter is only analyzing 1 file, compared to KSP that can have to analyze the full module (+ possibly other modules, external libs...). But I may be wrong.
t

Tyler Hodgkins

02/13/2022, 12:29 AM
It seems like you’d actually want to build an IDE plugin with additional functionality specific to your library (sounds more like a framework at this point).
👍 1
t

Ting-Yuan Huang

02/15/2022, 11:41 PM
Not for now if the question is to show users error in IDE in real time. Like @Grégory Lureau said, the main difficulty is the amount of work performed by processors. There's no guaranteed response time from processors. We are exploring approaches like introducing some IDE specific APIs, lightweight / restricted modes that only allow, e.g., single round or no API generation, etc. but nothing concrete yet. If the question is about semantic checking, KSP provides support to navigate through the code at symbol / signature level, as well as basic type checking. It's doable to check function signatures, types of parameters and return value etc using KSP's APIs. There are also
overrides(f, g)
that checks whether function
f
overrides function
g
and
isAssignable
that checks whether a type is assignable to the other, for example.
👍 1
l

Lulu

02/15/2022, 11:57 PM
That's a great point; I totally understand. If at least checking method signatures in real-time is added, I think many libraries could benefit from it; at least, that's where I think the line should be drawn. You provide KSP with a description of what a correct signature should look like, and it does the rest. I'm not in favor of constant processing of code, but this is probably outside my scope, and I'll leave it to the developers to figure out. For now, I'll just use the logger to display errors during build time until such feature is implemented. I appreciate everyone's input.
👍 1