mirror of
https://github.com/chylex/IntelliJ-IdeaVim.git
synced 2025-05-07 05:34:02 +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> {
|
||||
resolver.getAllFiles().forEach { it.accept(visitor, Unit) }
|
||||
writer.generateResourceFile("VimscriptFunctions.yaml", generateFunctionDict(), environment)
|
||||
writer.generateResourceFile(environment.options["vimscript_functions_file"]!!, generateFunctionDict(), environment)
|
||||
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.FunctionBeanClass
|
||||
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
|
||||
|
||||
internal class FunctionStorage : VimscriptFunctionService {
|
||||
@ -28,7 +30,7 @@ internal class FunctionStorage : VimscriptFunctionService {
|
||||
|
||||
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) {
|
||||
if (name[0].isLowerCase() && scope != Scope.SCRIPT_VARIABLE) {
|
||||
@ -138,7 +140,7 @@ internal class FunctionStorage : VimscriptFunctionService {
|
||||
}
|
||||
|
||||
override fun getBuiltInFunction(name: String): FunctionHandler? {
|
||||
return builtInFunctions[name]
|
||||
return builtInFunctions[name]?.instance
|
||||
}
|
||||
|
||||
private fun storeScriptFunction(functionDeclaration: FunctionDeclaration) {
|
||||
@ -164,16 +166,12 @@ internal class FunctionStorage : VimscriptFunctionService {
|
||||
}
|
||||
|
||||
override fun registerHandlers() {
|
||||
extensionPoint.getExtensionList(ApplicationManager.getApplication()).forEach(FunctionBeanClass::register)
|
||||
val intellijFunctions = IntellijFunctionProvider.getFunctions()
|
||||
intellijFunctions.forEach { addHandler(it) }
|
||||
}
|
||||
|
||||
override fun addHandler(handlerHolder: Any) {
|
||||
handlerHolder as FunctionBeanClass
|
||||
if (handlerHolder.name != null) {
|
||||
builtInFunctions[handlerHolder.name!!] = handlerHolder.instance
|
||||
} else {
|
||||
logger.error("Received function handler with null name")
|
||||
}
|
||||
override fun addHandler(handler: LazyVimscriptFunction) {
|
||||
builtInFunctions[handler.name] = handler
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper
|
||||
import org.yaml.snakeyaml.Yaml
|
||||
import java.io.InputStream
|
||||
import javax.swing.InputMap
|
||||
|
||||
public interface VimscriptFunctionProvider {
|
||||
public val functionListFile: InputStream
|
||||
public val functionListFileName: String
|
||||
|
||||
public fun getFunctions(): Collection<LazyVimscriptFunction> {
|
||||
val mapper = YAMLMapper()
|
||||
val yaml = Yaml()
|
||||
val classLoader = this.javaClass.classLoader
|
||||
val typeReference = object : TypeReference<HashMap<String, String>>() {}
|
||||
val functionDict = mapper.readValue(functionListFile, typeReference)
|
||||
val functionDict: Map<String, String> = yaml.load(getFile())
|
||||
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