Skip to content

Commit

Permalink
refactor(runner): change return types to Any for variable resolution #14
Browse files Browse the repository at this point in the history


Update return types to Any for variable resolution methods to allow flexibility in handling different data types.

This commit modifies the return types of variable resolution methods in the runner package to Any, enabling the handling of various data types.
  • Loading branch information
phodal committed Jun 16, 2024
1 parent fb8ad5f commit 69f1d61
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ interface PsiContextVariableProvider {
* @param psiElement the PsiElement for which to calculate variable values
* @return a map containing the resolved values for each PsiVariable based on the provided PsiElement
*/
fun resolveAll(psiElement: PsiElement): Map<PsiVariable, String> {
val result = mutableMapOf<PsiVariable, String>()
fun resolveAll(psiElement: PsiElement): Map<PsiVariable, Any> {
val result = mutableMapOf<PsiVariable, Any>()
for (variable in PsiVariable.values()) {
result[variable] = resolveVariableValue(psiElement, variable)
}
Expand All @@ -31,7 +31,7 @@ interface PsiContextVariableProvider {
* @param variable the PsiVariable for which to calculate the value
* @return the calculated value for the variable as a String
*/
fun resolveVariableValue(psiElement: PsiElement?, variable: PsiVariable): String
fun resolveVariableValue(psiElement: PsiElement?, variable: PsiVariable): Any

companion object {
private val languageExtension: LanguageExtension<PsiContextVariableProvider> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.phodal.shirelang.java.toolchain.getContainingClass
import kotlinx.coroutines.runBlocking

class JavaPsiContextVariableProvider : PsiContextVariableProvider {
override fun resolveVariableValue(psiElement: PsiElement?, variable: PsiVariable): String {
override fun resolveVariableValue(psiElement: PsiElement?, variable: PsiVariable): Any {
val project = psiElement?.project ?: return ""
if (psiElement.language.id != "JAVA") {
return ""
Expand Down Expand Up @@ -45,11 +45,7 @@ class JavaPsiContextVariableProvider : PsiContextVariableProvider {

PsiVariable.IS_NEW_FILE -> {
val sourceElement = TestFinderHelper.findClassesForTest(psiElement)
return if (sourceElement.isEmpty()) {
"true"
} else {
"false"
}
return sourceElement.isEmpty()
}

PsiVariable.TARGET_TEST_FILE_NAME -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class VariableTemplateCompiler(
val file: PsiFile,
) {
private val log = logger<VariableTemplateCompiler>()
private val variableMap: MutableMap<String, String> = mutableMapOf()
private val variableMap: MutableMap<String, Any> = mutableMapOf()

fun set(key: String, value: String) {
variableMap[key] = value
}

fun set(map: Map<String, String>) {
fun set(map: Map<String, Any>) {
variableMap.putAll(map)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fun compileShireTemplate(myProject: Project, hole: HobbitHole, symbolTable: Symb
val currentElement = VariableTemplateCompiler.defaultElement(myProject, currentEditor)

if (currentElement != null && currentEditor != null) {
val additionalMap: Map<String, String> = SymbolResolver(myProject, currentEditor, hole).resolve(symbolTable)
val additionalMap: Map<String, Any> = SymbolResolver(myProject, currentEditor, hole).resolve(symbolTable)

val file = currentElement.containingFile
val templateCompiler = VariableTemplateCompiler(file.language, file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SymbolResolver(val myProject: Project, val editor: Editor, val hole: Hobbi
}
}

fun resolve(symbolTable: SymbolTable): Map<String, String> {
fun resolve(symbolTable: SymbolTable): Map<String, Any> {
val element: PsiElement? = try {
editor.caretModel.currentCaret.offset.let {
val psiFile = PsiUtilBase.getPsiFileInEditor(editor, myProject) ?: return@let null
Expand All @@ -49,8 +49,8 @@ class SymbolResolver(val myProject: Project, val editor: Editor, val hole: Hobbi
return results
}

private fun resolveBuiltInVariable(symbolTable: SymbolTable, element: PsiElement?): MutableMap<String, String> {
val result = mutableMapOf<String, String>()
private fun resolveBuiltInVariable(symbolTable: SymbolTable, element: PsiElement?): MutableMap<String, Any> {
val result = mutableMapOf<String, Any>()
symbolTable.getAllVariables().forEach {
val psiVariable = PsiVariable.fromVariableName(it.key)
if (psiVariable != null) {
Expand Down

0 comments on commit 69f1d61

Please sign in to comment.