Skip to content

Commit

Permalink
feat(completion): add PSI context variables to VariableCompletionProv…
Browse files Browse the repository at this point in the history
…ider #29

This commit adds PSI context variables to the VariableCompletionProvider class. It also updates the documentation and the PsiContextVariable enum to include descriptions for each variable. This enhancement provides more context information for code structure generation.
  • Loading branch information
phodal committed Jul 1, 2024
1 parent 90f2364 commit 564b0fe
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,62 +1,64 @@
package com.phodal.shirecore.provider.variable

/**
* Enum representing variables used in the generation of code structures.
* Represents the context variables that can be used in the code structure generation process.
*
* @property variableName the name of the context variable
*/
enum class PsiContextVariable(val variableName: String) {
enum class PsiContextVariable(val variableName: String, val description: String?) {
/**
* Represents the PsiNameIdentifierOwner of the current class, used to retrieve the class name.
*/
CURRENT_CLASS_NAME("currentClassName"),
CURRENT_CLASS_NAME("currentClassName", "The name of the current class"),

/**
* Represents the input and output of PsiElement and PsiFile.
*/
CURRENT_CLASS_CODE("currentClassCode"),
CURRENT_CLASS_CODE("currentClassCode", "The code of the current class"),

CURRENT_METHOD_NAME("currentMethodName"),
CURRENT_METHOD_NAME("currentMethodName", "The name of the current method"),

CURRENT_METHOD_CODE("currentMethodCode"),
CURRENT_METHOD_CODE("currentMethodCode", "The code of the current method"),

/**
* Represents the input and output of PsiElement and PsiFile.
*/
RELATED_CLASSES("relatedClasses"),
RELATED_CLASSES("relatedClasses", "The related classes based on the AST analysis"),

/**
* Uses TfIDF to search for similar test cases in the code.
*/
SIMILAR_TEST_CASE("similarTestCase"),
SIMILAR_TEST_CASE("similarTestCase", "The similar test cases based on the TfIDF analysis"),

/**
* Represents the import statements required for the code structure.
*/
IMPORTS("imports"),
IMPORTS("imports", "The import statements required for the code structure"),

/**
* Flag indicating whether the code structure is being generated in a new file.
*/
IS_NEED_CREATE_FILE("isNeedCreateFile"),
IS_NEED_CREATE_FILE("isNeedCreateFile", "Flag indicating whether the code structure is being generated in a new file"),

/**
* The name of the target test file where the code structure will be generated.
*/
TARGET_TEST_FILE_NAME("targetTestFileName"),
TARGET_TEST_FILE_NAME("targetTestFileName", "The name of the target test file where the code structure will be generated"),

/**
* underTestMethod
*/
UNDER_TEST_METHOD_CODE("underTestMethodCode"),
UNDER_TEST_METHOD_CODE("underTestMethodCode", "The code of the method under test"),

/**
* Represents the framework information required for the code structure.
*/
FRAMEWORK_CONTEXT("frameworkContext"),
FRAMEWORK_CONTEXT("frameworkContext", "The framework information in dependencies of current project"),

/**
* codeSmell
*/
CODE_SMELL("codeSmell")
CODE_SMELL("codeSmell", "Include psi error and warning"),
;

companion object {
Expand Down
62 changes: 61 additions & 1 deletion docs/shire/shire-context-variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ parent: Shire Language
nav_order: 5
---

Context Variables for All
## Basic Context Variables

```kotlin
enum class ContextVariable(val variable: String, val description: String) {
Expand All @@ -23,3 +23,63 @@ enum class ContextVariable(val variable: String, val description: String) {
}
```

## PSI Context Variables

```kotlin
enum class PsiContextVariable(val variableName: String) {
/**
* Represents the PsiNameIdentifierOwner of the current class, used to retrieve the class name.
*/
CURRENT_CLASS_NAME("currentClassName"),

/**
* Represents the input and output of PsiElement and PsiFile.
*/
CURRENT_CLASS_CODE("currentClassCode"),

CURRENT_METHOD_NAME("currentMethodName"),

CURRENT_METHOD_CODE("currentMethodCode"),

/**
* Represents the input and output of PsiElement and PsiFile.
*/
RELATED_CLASSES("relatedClasses"),

/**
* Uses TfIDF to search for similar test cases in the code.
*/
SIMILAR_TEST_CASE("similarTestCase"),

/**
* Represents the import statements required for the code structure.
*/
IMPORTS("imports"),

/**
* Flag indicating whether the code structure is being generated in a new file.
*/
IS_NEED_CREATE_FILE("isNeedCreateFile"),

/**
* The name of the target test file where the code structure will be generated.
*/
TARGET_TEST_FILE_NAME("targetTestFileName"),

/**
* underTestMethod
*/
UNDER_TEST_METHOD_CODE("underTestMethodCode"),

/**
* Represents the framework information required for the code structure.
*/
FRAMEWORK_CONTEXT("frameworkContext"),

/**
* codeSmell
*/
CODE_SMELL("codeSmell")
;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.completion.PrioritizedLookupElement
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.util.ProcessingContext
import com.phodal.shirecore.provider.variable.PsiContextVariable
import com.phodal.shirelang.completion.dataprovider.ContextVariable

class VariableCompletionProvider : CompletionProvider<CompletionParameters>() {
Expand All @@ -22,5 +23,14 @@ class VariableCompletionProvider : CompletionProvider<CompletionParameters>() {
)
result.addElement(withTypeText)
}

PsiContextVariable.values().forEach {
val withTypeText =
PrioritizedLookupElement.withPriority(
LookupElementBuilder.create(it.variableName).withTypeText(it.description, true),
90.0
)
result.addElement(withTypeText)
}
}
}

0 comments on commit 564b0fe

Please sign in to comment.