From 07e0475851e59c0778254d6522666705ba78f58a Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Wed, 19 Jun 2024 10:37:51 +0800 Subject: [PATCH] feat(compiler): refactor built-in methods to enum class #18 Refactored built-in methods from MethodCall to ExpressionBuiltInMethod enum class for better code organization and readability. This change also includes updates to the WhenConditionCompletionProvider and ShireExpression classes to use the new enum class. --- .../hobbit/ast/ExpressionBuiltInMethod.kt | 37 ++++++++++ .../compiler/hobbit/ast/ShireExpression.kt | 68 +++++-------------- .../WhenConditionCompletionProvider.kt | 7 +- 3 files changed, 58 insertions(+), 54 deletions(-) create mode 100644 shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/ast/ExpressionBuiltInMethod.kt diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/ast/ExpressionBuiltInMethod.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/ast/ExpressionBuiltInMethod.kt new file mode 100644 index 000000000..066767c93 --- /dev/null +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/ast/ExpressionBuiltInMethod.kt @@ -0,0 +1,37 @@ +package com.phodal.shirelang.compiler.hobbit.ast + +/** + * This enum class `ExpressionBuiltInMethod` provides a set of built-in methods for string manipulation in Kotlin. + * Each enum constant represents a specific built-in method, and holds information about the method's name, description, + * the string to be inserted after the method call, and the position to move the caret to after insertion. + * + */ +enum class ExpressionBuiltInMethod( + val methodName: String, + val description: String, + val postInsertString: String = "()", + val moveCaret: Int = 2, +) { + LENGTH("length", "The length of the string"), + TRIM("trim", "The trimmed string"), + CONTAINS("contains", "Check if the string contains a substring", "(\"\")", 2), + STARTS_WITH("startsWith", "Check if the string starts with a substring", "(\"\")", 2), + ENDS_WITH("endsWith", "Check if the string ends with a substring", "(\"\")", 2), + LOWERCASE("lowercase", "The lowercase version of the string"), + UPPERCASE("uppercase", "The uppercase version of the string"), + IS_EMPTY("isEmpty", "Check if the string is empty"), + IS_NOT_EMPTY("isNotEmpty", "Check if the string is not empty"), + FIRST("first", "The first character of the string"), + LAST("last", "The last character of the string"), + MATCHES("matches", "Check if the string matches a regex pattern", "(\"//\")", 3); + + companion object { + fun fromString(methodName: String): ExpressionBuiltInMethod? { + return values().find { it.methodName == methodName } + } + + fun completionProvider(): Array { + return values() + } + } +} diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/ast/ShireExpression.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/ast/ShireExpression.kt index 00aa4acbb..8dc0d1f63 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/ast/ShireExpression.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/compiler/hobbit/ast/ShireExpression.kt @@ -26,6 +26,7 @@ abstract class Statement { } "${this.objectName.display()}.${this.methodName.display()}($parameters)" } + else -> "" } } @@ -132,7 +133,7 @@ data class Comparison( val value: FrontMatterType, ) : Statement() { override fun evaluate(variables: Map): Boolean { - val variableValue = when(variable.value) { + val variableValue = when (variable.value) { is MethodCall -> variable.value.evaluate(variables) is FrontMatterType.STRING -> variable.value.value else -> { @@ -225,11 +226,11 @@ data class MethodCall( val arguments: List, ) : Statement() { override fun evaluate(variables: Map): Any { - val value = when(objectName) { + val value = when (objectName) { is FrontMatterType.STRING -> variables[objectName.value] is FrontMatterType.Variable -> variables[objectName.value] else -> null - } ?: throw IllegalArgumentException("Variable not found: ${objectName.value}") + } ?: throw IllegalArgumentException("Variable not found: ${objectName.value}") val parameters: List = this.arguments.map { when (it) { @@ -241,55 +242,22 @@ data class MethodCall( } } - return when (val methodName = methodName.value) { - "length" -> value.length - "trim" -> value.trim() - "contains" -> value.contains(parameters[0] as String) - "startsWith" -> value.startsWith(parameters[0] as String) - "endsWith" -> value.endsWith(parameters[0] as String) - "lowercase" -> value.lowercase() - "uppercase" -> value.uppercase() - "isEmpty" -> value.isEmpty() - "isNotEmpty" -> value.isNotEmpty() - "first" -> value.first().toString() - "last" -> value.last().toString() - // match regex - "matches" -> value.matches((parameters[0] as String).toRegex()) + val method = ExpressionBuiltInMethod.fromString(methodName.value.toString()) + return when (method) { + ExpressionBuiltInMethod.LENGTH -> value.length + ExpressionBuiltInMethod.TRIM -> value.trim() + ExpressionBuiltInMethod.CONTAINS -> value.contains(parameters[0] as String) + ExpressionBuiltInMethod.STARTS_WITH -> value.startsWith(parameters[0] as String) + ExpressionBuiltInMethod.ENDS_WITH -> value.endsWith(parameters[0] as String) + ExpressionBuiltInMethod.LOWERCASE -> value.lowercase() + ExpressionBuiltInMethod.UPPERCASE -> value.uppercase() + ExpressionBuiltInMethod.IS_EMPTY -> value.isEmpty() + ExpressionBuiltInMethod.IS_NOT_EMPTY -> value.isNotEmpty() + ExpressionBuiltInMethod.FIRST -> value.first().toString() + ExpressionBuiltInMethod.LAST -> value.last().toString() + ExpressionBuiltInMethod.MATCHES -> value.matches((parameters[0] as String).toRegex()) else -> throw IllegalArgumentException("Unsupported method: $methodName") } } - companion object { - - /** - * Represents a function completion item with the specified name, description, postInsertString, and moveCaret value. - * - * @param name the name of the function - * @param description the description of the function - * @param postInsertString the string to be inserted after the function name, default value is "()" - * @param moveCaret the position to move the caret after inserting the function, default value is 2 - * - For a normal case, it will be 2 (end of the string) - * - For params function,like contains, startsWith, endsWith, matches, it will be 2 (center of the string) - */ - data class FunctionCompletion( - val name: String, val description: String, val postInsertString: String = "()", val moveCaret: Int = 2 - ) - - fun completionProvider() : List { - return listOf( - FunctionCompletion("length", "The length of the string"), - FunctionCompletion("trim", "The trimmed string"), - FunctionCompletion("contains", "Check if the string contains a substring", "(\"\")", 2), - FunctionCompletion("startsWith", "Check if the string starts with a substring", "(\"\")", 2), - FunctionCompletion("endsWith", "Check if the string ends with a substring", "(\"\")", 2), - FunctionCompletion("lowercase", "The lowercase version of the string"), - FunctionCompletion("uppercase", "The uppercase version of the string"), - FunctionCompletion("isEmpty", "Check if the string is empty"), - FunctionCompletion("isNotEmpty", "Check if the string is not empty"), - FunctionCompletion("first", "The first character of the string"), - FunctionCompletion("last", "The last character of the string"), - FunctionCompletion("matches", "Check if the string matches a regex pattern", "(\"//\")", 3) - ) - } - } } diff --git a/shirelang/src/main/kotlin/com/phodal/shirelang/completion/provider/WhenConditionCompletionProvider.kt b/shirelang/src/main/kotlin/com/phodal/shirelang/completion/provider/WhenConditionCompletionProvider.kt index 4f883fe7c..daa468be4 100644 --- a/shirelang/src/main/kotlin/com/phodal/shirelang/completion/provider/WhenConditionCompletionProvider.kt +++ b/shirelang/src/main/kotlin/com/phodal/shirelang/completion/provider/WhenConditionCompletionProvider.kt @@ -7,7 +7,7 @@ import com.intellij.codeInsight.completion.PrioritizedLookupElement import com.intellij.codeInsight.lookup.LookupElementBuilder import com.intellij.util.ProcessingContext import com.phodal.shirelang.actions.validator.PsiVariables -import com.phodal.shirelang.compiler.hobbit.ast.MethodCall +import com.phodal.shirelang.compiler.hobbit.ast.ExpressionBuiltInMethod class WhenConditionCompletionProvider : CompletionProvider() { override fun addCompletions( @@ -33,7 +33,7 @@ class WhenConditionFunctionCompletionProvider : CompletionProvider @@ -43,8 +43,7 @@ class WhenConditionFunctionCompletionProvider : CompletionProvider