generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascript): add variable provider and utility functions
Added a new variable provider class `JSPsiContextVariableProvider` for JavaScript language. This class provides context variables for JavaScript. Also, added utility functions in `JSPsiUtil` and `JSRelevantUtil` to support the variable provider. Updated the plugin XML to include the new variable provider.
- Loading branch information
Showing
4 changed files
with
274 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...s/shire-javascript/src/main/kotlin/com/phodal/shirelang/javascript/util/JSRelevantUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.phodal.shirelang.javascript.util | ||
|
||
import com.intellij.lang.javascript.psi.JSFunction | ||
import com.intellij.lang.javascript.psi.ecma6.TypeScriptInterface | ||
import com.intellij.lang.javascript.psi.ecma6.TypeScriptSingleType | ||
import com.intellij.lang.javascript.psi.ecmal4.JSClass | ||
import com.intellij.lang.javascript.psi.util.JSStubBasedPsiTreeUtil | ||
import com.intellij.openapi.application.ReadAction | ||
import com.intellij.openapi.diagnostic.logger | ||
import com.intellij.psi.PsiElement | ||
import com.phodal.shirecore.codemodel.model.ClassStructure | ||
import com.phodal.shirelang.javascript.codemodel.JavaScriptClassStructureProvider | ||
|
||
object JSRelevantUtil { | ||
fun lookupRelevantClass(element: PsiElement): List<ClassStructure> { | ||
return ReadAction.compute<List<ClassStructure>, Throwable> { | ||
val elements = mutableListOf<ClassStructure>() | ||
when (element) { | ||
is JSClass -> { | ||
element.functions.map { | ||
elements += resolveByFunction(it).values | ||
} | ||
} | ||
|
||
is JSFunction -> { | ||
elements += resolveByFunction(element).values | ||
} | ||
|
||
else -> {} | ||
} | ||
|
||
return@compute elements | ||
} | ||
} | ||
|
||
private fun resolveByFunction(jsFunction: JSFunction): Map<String, ClassStructure> { | ||
val result = mutableMapOf<String, ClassStructure>() | ||
jsFunction.parameterList?.parameters?.map { | ||
it.typeElement?.let { typeElement -> | ||
result += resolveByType(typeElement, it.typeElement!!.text) | ||
} | ||
} | ||
|
||
result += jsFunction.returnTypeElement?.let { | ||
resolveByType(it, jsFunction.returnType!!.resolvedTypeText) | ||
} ?: emptyMap() | ||
|
||
return result | ||
} | ||
|
||
private fun resolveByType( | ||
returnType: PsiElement?, | ||
typeName: String, | ||
): MutableMap<String, ClassStructure> { | ||
val result = mutableMapOf<String, ClassStructure>() | ||
when (returnType) { | ||
is TypeScriptSingleType -> { | ||
val resolveReferenceLocally = JSStubBasedPsiTreeUtil.resolveLocally( | ||
typeName, | ||
returnType | ||
) | ||
|
||
when (resolveReferenceLocally) { | ||
is TypeScriptInterface -> { | ||
JavaScriptClassStructureProvider().build(resolveReferenceLocally, false)?.let { | ||
result += mapOf(typeName to it) | ||
} | ||
} | ||
|
||
else -> { | ||
logger<JSRelevantUtil>().warn("resolveReferenceLocally is not TypeScriptInterface: $resolveReferenceLocally") | ||
} | ||
} | ||
} | ||
|
||
else -> { | ||
logger<JSRelevantUtil>().warn("returnType is not TypeScriptSingleType: $returnType") | ||
} | ||
} | ||
|
||
return result | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
.../src/main/kotlin/com/phodal/shirelang/javascript/variable/JSPsiContextVariableProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.phodal.shirelang.javascript.variable | ||
|
||
import com.intellij.lang.javascript.JavascriptLanguage | ||
import com.intellij.lang.javascript.psi.JSFile | ||
import com.intellij.lang.javascript.psi.JSFunction | ||
import com.intellij.lang.javascript.psi.ecmal4.JSImportStatement | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import com.oracle.truffle.js.runtime.builtins.JSClass | ||
import com.phodal.shirecore.provider.variable.PsiContextVariableProvider | ||
import com.phodal.shirecore.provider.variable.model.PsiContextVariable | ||
import com.phodal.shirelang.javascript.codemodel.JavaScriptClassStructureProvider | ||
import com.phodal.shirelang.javascript.codemodel.JavaScriptMethodStructureProvider | ||
import com.phodal.shirelang.javascript.util.JSPsiUtil | ||
import com.phodal.shirelang.javascript.util.JSRelevantUtil | ||
|
||
class JSPsiContextVariableProvider : PsiContextVariableProvider { | ||
override fun resolve(variable: PsiContextVariable, project: Project, editor: Editor, psiElement: PsiElement?): Any { | ||
if (psiElement?.language !is JavascriptLanguage) return "" | ||
val underTestElement = JSPsiUtil.getElementToTest(psiElement) ?: return "" | ||
val sourceFile = underTestElement.containingFile as? JSFile ?: return "" | ||
|
||
return when (variable) { | ||
PsiContextVariable.CURRENT_CLASS_NAME -> { | ||
// when (underTestElement) { | ||
// is JSClass -> underTestElement.getName() ?: "" | ||
// else -> "" | ||
// } | ||
"" | ||
} | ||
|
||
PsiContextVariable.CURRENT_CLASS_CODE -> { | ||
val underTestObj = JavaScriptClassStructureProvider() | ||
.build(underTestElement, false)?.format() | ||
|
||
if (underTestObj == null) { | ||
val funcObj = JavaScriptMethodStructureProvider() | ||
.build(underTestElement, false, false)?.format() | ||
|
||
funcObj ?: "" | ||
} else { | ||
underTestObj | ||
} | ||
|
||
} | ||
|
||
PsiContextVariable.CURRENT_METHOD_NAME -> { | ||
when (underTestElement) { | ||
is JSFunction -> underTestElement.name ?: "" | ||
else -> "" | ||
} | ||
} | ||
|
||
PsiContextVariable.CURRENT_METHOD_CODE -> { | ||
when (underTestElement) { | ||
is JSFunction -> underTestElement.text ?: "" | ||
else -> "" | ||
} | ||
} | ||
|
||
PsiContextVariable.RELATED_CLASSES -> JSRelevantUtil.lookupRelevantClass(underTestElement) | ||
PsiContextVariable.SIMILAR_TEST_CASE -> "" | ||
PsiContextVariable.IMPORTS -> PsiTreeUtil.findChildrenOfType(sourceFile, JSImportStatement::class.java) | ||
.map { it.text } | ||
|
||
PsiContextVariable.IS_NEED_CREATE_FILE -> TODO() | ||
PsiContextVariable.TARGET_TEST_FILE_NAME -> JSPsiUtil.getTestFilePath(psiElement) ?: "" | ||
PsiContextVariable.UNDER_TEST_METHOD_CODE -> TODO() | ||
PsiContextVariable.FRAMEWORK_CONTEXT -> TODO() | ||
PsiContextVariable.CODE_SMELL -> TODO() | ||
PsiContextVariable.METHOD_CALLER -> TODO() | ||
PsiContextVariable.CALLED_METHOD -> TODO() | ||
PsiContextVariable.SIMILAR_CODE -> TODO() | ||
PsiContextVariable.STRUCTURE -> TODO() | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters