Skip to content

Commit

Permalink
Add DumpCommandHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-F authored and waleedyaseen committed Sep 4, 2024
1 parent 9174bf3 commit afde50d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.runescript.plugin.lang.psi.RsScript
import io.runescript.plugin.lang.psi.scope.RsLocalVariableResolver
import io.runescript.plugin.lang.psi.scope.RsResolveMode
import io.runescript.plugin.lang.psi.scope.RsScopesUtil
import io.runescript.plugin.lang.psi.type.RsAnyType
import io.runescript.plugin.lang.psi.type.RsPrimitiveType
import io.runescript.plugin.lang.psi.type.RsType
import io.runescript.plugin.lang.psi.type.trigger.RsTriggerType
Expand Down Expand Up @@ -65,6 +66,11 @@ class RsDynamicExpressionReference(element: RsDynamicExpression) :
if (resolvedConfig != null) {
return arrayOf(PsiElementResolveResult(resolvedConfig))
}
} else if (type == RsAnyType) {
val resolvedConfig = RsSymbolIndex.lookupAny(element, elementName)
if (resolvedConfig != null) {
return arrayOf(PsiElementResolveResult(resolvedConfig))
}
}

// Try to resolve the element as a command reference.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.runescript.plugin.lang.psi.type

object RsAnyType : RsType {
override val representation: String
get() = "any"
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,29 @@ data object EnumGetReverseIndexCommandHandler : CommandHandler {
o.type = inputType ?: RsErrorType
}
}

data object DumpCommandHandler : CommandHandler {
override fun RsTypeInferenceVisitor.inferTypes(
reference: RsScript,
o: RsCommandExpression
) {
val arguments = o.argumentList.expressionList
if (arguments.isEmpty()) {
o.error("Type mismatch: '<unit>' was given but 'any...' was expected.")
} else {
for (argument in arguments) {
argument.typeHint = RsAnyType
argument.accept(this)

val type = argument.type
if (type is RsTupleType) {
argument.error("Unable to dump multi-value types: %s".format(type.representation))
} else if (type == RsUnitType) {
argument.error("Unable to debug expression with no return value.")
}
}
}

o.type = RsPrimitiveType.STRING
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ private fun RsScript.findCommandHandler(): CommandHandler {
"db_find_refine" -> DbFindCommandHandler.DB_FIND_REFINE
"db_find_refine_with_count" -> DbFindCommandHandler.DB_FIND_REFINE_WITH_COUNT
"db_getfield" -> DbGetFieldCommandHandler
"dump" -> DumpCommandHandler
else -> DefaultCommandHandler
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class RsSymbolIndex : StringStubIndexExtension<RsSymSymbol>() {
}
}

fun lookupAny(context: PsiElement, name: String): RsSymSymbol? {
val module = ModuleUtil.findModuleForPsiElement(context) ?: return null
val scope = GlobalSearchScope.moduleScope(module)
val configs = StubIndex.getElements(KEY, name, context.project, scope, RsSymSymbol::class.java)
return configs.firstOrNull()
}

val PsiFile.nameWithoutExtension: String
get() {
val dot = name.lastIndexOf('.')
Expand Down

0 comments on commit afde50d

Please sign in to comment.