1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-04-09 17:15:50 +02:00

Generating json by annotations

This commit is contained in:
Filipp Vakhitov 2023-09-30 04:05:54 +03:00
parent ced50bb2e8
commit 3046c61447
7 changed files with 93 additions and 14 deletions

View File

@ -8,6 +8,7 @@
plugins {
kotlin("jvm")
kotlin("plugin.serialization") version "1.8.21"
}
group = "com.intellij"
@ -20,4 +21,4 @@ repositories {
dependencies {
compileOnly("com.google.devtools.ksp:symbol-processing-api:1.9.20-1.0.14")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.6.0")
}
}

View File

@ -22,39 +22,34 @@ annotation class CommandOrMotion(val keys: Array<String>, vararg val modes: Mode
annotation class TextObject(val keys: String)
/**
* @author vlan
*
* COMPATIBILITY-LAYER: Do not move this class to a different package
*/
enum class Mode {
enum class Mode(val abbrev: Char) {
/**
* Indicates this key mapping applies to Normal mode
*/
NORMAL,
NORMAL('N'),
/**
* Indicates this key mapping applies to Visual mode
*/
VISUAL,
VISUAL('X'),
/**
* Indicates this key mapping applies to Select mode
*/
SELECT,
SELECT('S'),
/**
* Indicates this key mapping applies to Operator Pending mode
*/
OP_PENDING,
OP_PENDING('O'),
/**
* Indicates this key mapping applies to Insert mode
*/
INSERT,
INSERT('I'),
/**
* Indicates this key mapping applies to Command Line mode
*/
CMD_LINE,
CMD_LINE('C'),
}

View File

@ -0,0 +1,60 @@
/*
* Copyright 2003-2023 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/
package com.intellij.vim.processors
import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.getAnnotationsByType
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFile
import com.google.devtools.ksp.symbol.KSVisitorVoid
import com.intellij.vim.annotations.CommandOrMotion
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlin.io.path.Path
import kotlin.io.path.writeText
class CommandOrMotionProcessor(private val environment: SymbolProcessorEnvironment): SymbolProcessor {
private val visitor = CommandOrMotionVisitor()
private val commands = mutableListOf<CommandBean>()
private val json = Json { prettyPrint = true }
override fun process(resolver: Resolver): List<KSAnnotated> {
resolver.getAllFiles().forEach { it.accept(visitor, Unit) }
val filePath = Path(environment.options["generated_directory"]!!, environment.options["commands_file"]!!)
val fileContent = json.encodeToString(commands)
filePath.writeText(fileContent)
return emptyList()
}
private inner class CommandOrMotionVisitor : KSVisitorVoid() {
@OptIn(KspExperimental::class)
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) {
val commandAnnotation = classDeclaration.getAnnotationsByType(CommandOrMotion::class).firstOrNull() ?: return
for (key in commandAnnotation.keys) {
commands.add(
CommandBean(key, classDeclaration.qualifiedName!!.asString(), commandAnnotation.modes.map { it.abbrev }.joinToString(separator = ""))
)
}
}
override fun visitFile(file: KSFile, data: Unit) {
file.declarations.forEach { it.accept(this, Unit) }
}
}
@Serializable
private data class CommandBean(val keys: String, val `class`: String, val modes: String)
}

View File

@ -0,0 +1,20 @@
/*
* Copyright 2003-2023 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/
package com.intellij.vim.providers
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.intellij.vim.processors.CommandOrMotionProcessor
class CommandOrMotionProcessorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
return CommandOrMotionProcessor(environment)
}
}

View File

@ -1,2 +1,3 @@
com.intellij.vim.providers.CommandOrMotionProcessorProvider
com.intellij.vim.providers.ExCommandProcessorProvider
com.intellij.vim.providers.VimscriptFunctionProcessorProvider
com.intellij.vim.providers.ExCommandProcessorProvider

View File

@ -85,6 +85,7 @@ ksp {
arg("generated_directory", "$projectDir/src/main/resources")
arg("vimscript_functions_file", "intellij_vimscript_functions.json")
arg("ex_commands_file", "intellij_ex_commands.json")
arg("commands_file", "intellij_commands.json")
}
afterEvaluate {

View File

@ -27,6 +27,7 @@ ksp {
arg("generated_directory", "$projectDir/src/main/resources")
arg("vimscript_functions_file", "engine_vimscript_functions.json")
arg("ex_commands_file", "engine_ex_commands.json")
arg("commands_file", "engine_commands.json")
}
afterEvaluate {