Skip to content

Commit

Permalink
Update docs regarding SymbolProcessorProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
edrd-f authored and ting-yuan committed Apr 9, 2021
1 parent 7dcacc6 commit 86925cb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,28 @@ KSFile
This view lists common things that are declared in the file--classes,
functions, properties, and so on.

## SymbolProcessor: The entry point
## SymbolProcessorProvider: The entry point

Every processor in KSP implements `SymbolProcessor`:
KSP expects an implementation of the `SymbolProcessorProvider` interface to instantiate `SymbolProcessor`:

```kotlin
interface SymbolProcessorProvider {
fun create(
options: Map<String, String>,
kotlinVersion: KotlinVersion,
codeGenerator: CodeGenerator,
logger: KSPLogger
): SymbolProcessor
}
```

While `SymbolProcessor` is defined as:

```kotlin
interface SymbolProcessor {
fun init(options: Map<String, String>,
kotlinVersion: KotlinVersion,
codeGenerator: CodeGenerator,
logger: KSPLogger)
fun process(resolver: Resolver) // Let's focus on this
fun finish()
fun process(resolver: Resolver): List<KSAnnotated> // Let's focus on this
fun finish() {}
fun onError() {}
}
```

Expand Down Expand Up @@ -136,6 +146,10 @@ class HelloFunctionFinderProcessor : SymbolProcessor() {
}
}
...

class Provider : SymbolProcessorProvider {
override fun create(...): SymbolProcessor = ...
}
}
```
## Resources
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package com.google.devtools.ksp.processing

/**
* [SymbolProcessorProvider] is the interface used by plugins to integrate into Kotlin Symbol Processing.
*/
interface SymbolProcessorProvider {
fun create(
options: Map<String, String>,
kotlinVersion: KotlinVersion,
codeGenerator: CodeGenerator,
logger: KSPLogger
): SymbolProcessor
/**
* Called by Kotlin Symbol Processing to create the processor.
*
* @param options passed from command line, Gradle, etc.
* @param kotlinVersion language version of compilation environment.
* @param codeGenerator creates managed files.
* @param logger for logging to build output.
*/
fun create(
options: Map<String, String>,
kotlinVersion: KotlinVersion,
codeGenerator: CodeGenerator,
logger: KSPLogger
): SymbolProcessor
}
12 changes: 6 additions & 6 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@
}
```

* The processor you're writing needs to implement [`com.google.devtools.ksp.processing.SymbolProcessor`](../api/src/main/kotlin/com/google/devtools/ksp/processing/SymbolProcessor.kt).
* The processor you're writing needs to implement [`com.google.devtools.ksp.processing.SymbolProcessorProvider`](../api/src/main/kotlin/com/google/devtools/ksp/processing/SymbolProcessorProvider.kt).
Note the following:
* Your main logic should be in the `process()` method.
* Use `CodeGenerator` in the `init()` method for code generation. You can also save
the `CodeGenerator` instance for later use in either `process()` or `finish()`.
* Your main logic should be in the `SymbolProcessor#process()` method.
* Capture any dependencies your processor needs (e.g. `CodeGenerator`) by passing
them to your implementation of `SymbolProcessor` through `SymbolProcessorProvider#create()`.
* Use `resolver.getSymbolsWithAnnotation()` to get the symbols you want to process, given
the fully-qualified name of an annotation.
* A common use case for KSP is to implement a customized visitor (interface
`com.google.devtools.ksp.symbol.KSVisitor`) for operating on symbols. A simple template
visitor is `com.google.devtools.ksp.symbol.KSDefaultVisitor`.
* For sample implementations of the `SymbolProcessor` interface, see the following files
* For sample implementations of the `SymbolProcessorProvider` and `SymbolProcessor` interfaces, see the following files
in the sample project.
* `src/main/kotlin/BuilderProcessor.kt`
* `src/main/kotlin/TestProcessor.kt`
Expand Down Expand Up @@ -144,7 +144,7 @@
</details>

## Pass Options to Processors
Processor options in `SymbolProcessor.init(options: Map<String, String>, ...)` are specified in gradle build scripts:
Processor options in `SymbolProcessorProvider.create(options: Map<String, String>, ...)` are specified in gradle build scripts:
```
ksp {
arg("option1", "value1")
Expand Down

0 comments on commit 86925cb

Please sign in to comment.