mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-05-30 04:34:08 +02:00
Add function providers
This commit is contained in:
parent
f143b6ee9f
commit
8094758a82
annotation-processors/src/main/kotlin/com/intellij/vim/processors
src/main/java/com/maddyhome/idea/vim/vimscript
vim-engine/src/main/kotlin/com/maddyhome/idea/vim/vimscript/model/functions
@ -36,7 +36,7 @@ class VimscriptFunctionProcessor(private val environment: SymbolProcessorEnviron
|
|||||||
|
|
||||||
override fun process(resolver: Resolver): List<KSAnnotated> {
|
override fun process(resolver: Resolver): List<KSAnnotated> {
|
||||||
resolver.getAllFiles().forEach { it.accept(visitor, Unit) }
|
resolver.getAllFiles().forEach { it.accept(visitor, Unit) }
|
||||||
writer.generateResourceFile("VimscriptFunctions.yaml", generateFunctionDict(), environment)
|
writer.generateResourceFile(environment.options["vimscript_functions_file"]!!, generateFunctionDict(), environment)
|
||||||
return emptyList()
|
return emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* 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.maddyhome.idea.vim.vimscript.model.functions
|
||||||
|
|
||||||
|
public object IntellijFunctionProvider : VimscriptFunctionProvider {
|
||||||
|
override val functionListFileName: String = "intellij_vimscript_functions.yaml"
|
||||||
|
}
|
@ -20,6 +20,8 @@ import com.maddyhome.idea.vim.vimscript.model.expressions.Scope
|
|||||||
import com.maddyhome.idea.vim.vimscript.model.functions.DefinedFunctionHandler
|
import com.maddyhome.idea.vim.vimscript.model.functions.DefinedFunctionHandler
|
||||||
import com.maddyhome.idea.vim.vimscript.model.functions.FunctionBeanClass
|
import com.maddyhome.idea.vim.vimscript.model.functions.FunctionBeanClass
|
||||||
import com.maddyhome.idea.vim.vimscript.model.functions.FunctionHandler
|
import com.maddyhome.idea.vim.vimscript.model.functions.FunctionHandler
|
||||||
|
import com.maddyhome.idea.vim.vimscript.model.functions.IntellijFunctionProvider
|
||||||
|
import com.maddyhome.idea.vim.vimscript.model.functions.LazyVimscriptFunction
|
||||||
import com.maddyhome.idea.vim.vimscript.model.statements.FunctionDeclaration
|
import com.maddyhome.idea.vim.vimscript.model.statements.FunctionDeclaration
|
||||||
|
|
||||||
internal class FunctionStorage : VimscriptFunctionService {
|
internal class FunctionStorage : VimscriptFunctionService {
|
||||||
@ -28,7 +30,7 @@ internal class FunctionStorage : VimscriptFunctionService {
|
|||||||
|
|
||||||
private val globalFunctions: MutableMap<String, FunctionDeclaration> = mutableMapOf()
|
private val globalFunctions: MutableMap<String, FunctionDeclaration> = mutableMapOf()
|
||||||
|
|
||||||
private val builtInFunctions: MutableMap<String, FunctionHandler> = mutableMapOf()
|
private val builtInFunctions: MutableMap<String, LazyVimscriptFunction> = mutableMapOf()
|
||||||
|
|
||||||
override fun deleteFunction(name: String, scope: Scope?, vimContext: VimLContext) {
|
override fun deleteFunction(name: String, scope: Scope?, vimContext: VimLContext) {
|
||||||
if (name[0].isLowerCase() && scope != Scope.SCRIPT_VARIABLE) {
|
if (name[0].isLowerCase() && scope != Scope.SCRIPT_VARIABLE) {
|
||||||
@ -138,7 +140,7 @@ internal class FunctionStorage : VimscriptFunctionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getBuiltInFunction(name: String): FunctionHandler? {
|
override fun getBuiltInFunction(name: String): FunctionHandler? {
|
||||||
return builtInFunctions[name]
|
return builtInFunctions[name]?.instance
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun storeScriptFunction(functionDeclaration: FunctionDeclaration) {
|
private fun storeScriptFunction(functionDeclaration: FunctionDeclaration) {
|
||||||
@ -164,16 +166,12 @@ internal class FunctionStorage : VimscriptFunctionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun registerHandlers() {
|
override fun registerHandlers() {
|
||||||
extensionPoint.getExtensionList(ApplicationManager.getApplication()).forEach(FunctionBeanClass::register)
|
val intellijFunctions = IntellijFunctionProvider.getFunctions()
|
||||||
|
intellijFunctions.forEach { addHandler(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun addHandler(handlerHolder: Any) {
|
override fun addHandler(handler: LazyVimscriptFunction) {
|
||||||
handlerHolder as FunctionBeanClass
|
builtInFunctions[handler.name] = handler
|
||||||
if (handlerHolder.name != null) {
|
|
||||||
builtInFunctions[handlerHolder.name!!] = handlerHolder.instance
|
|
||||||
} else {
|
|
||||||
logger.error("Received function handler with null name")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* 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.maddyhome.idea.vim.vimscript.model.functions
|
||||||
|
|
||||||
|
public object EngineFunctionProvider : VimscriptFunctionProvider {
|
||||||
|
override val functionListFileName: String = "engine_vimscript_functions.yaml"
|
||||||
|
}
|
@ -8,19 +8,21 @@
|
|||||||
|
|
||||||
package com.maddyhome.idea.vim.vimscript.model.functions
|
package com.maddyhome.idea.vim.vimscript.model.functions
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference
|
import org.yaml.snakeyaml.Yaml
|
||||||
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper
|
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import javax.swing.InputMap
|
|
||||||
|
|
||||||
public interface VimscriptFunctionProvider {
|
public interface VimscriptFunctionProvider {
|
||||||
public val functionListFile: InputStream
|
public val functionListFileName: String
|
||||||
|
|
||||||
public fun getFunctions(): Collection<LazyVimscriptFunction> {
|
public fun getFunctions(): Collection<LazyVimscriptFunction> {
|
||||||
val mapper = YAMLMapper()
|
val yaml = Yaml()
|
||||||
val classLoader = this.javaClass.classLoader
|
val classLoader = this.javaClass.classLoader
|
||||||
val typeReference = object : TypeReference<HashMap<String, String>>() {}
|
val functionDict: Map<String, String> = yaml.load(getFile())
|
||||||
val functionDict = mapper.readValue(functionListFile, typeReference)
|
|
||||||
return functionDict.map { LazyVimscriptFunction(it.key, it.value, classLoader) }
|
return functionDict.map { LazyVimscriptFunction(it.key, it.value, classLoader) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getFile(): InputStream {
|
||||||
|
return object {}.javaClass.classLoader.getResourceAsStream(functionListFileName)
|
||||||
|
?: throw RuntimeException("Failed to fetch functions for ${javaClass.name}")
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user