Skip to content

Commit

Permalink
feat(provider): add change count, line count, and complexity count va…
Browse files Browse the repository at this point in the history
…riables #143 and closed #139

This commit introduces new context variables to calculate and provide the number of changes, lines, and complexity within a file. These variables are added across multiple language providers to ensure consistent support.
  • Loading branch information
phodal committed Nov 17, 2024
1 parent 0cb29fb commit e23687c
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ interface PsiContextVariableProvider : VariableProvider<PsiContextVariable> {
return future.get()
}

/// try to load from git
fun calculateChangeCount(psiElement: PsiElement?): String {
return "0"
}

fun calculateLineCount(psiElement: PsiElement?): String {
return psiElement?.containingFile?.text?.lines()?.size.toString()
}

fun calculateComplexityCount(psiElement: PsiElement?): String {
return "0"
}

companion object {
private val languageExtension: LanguageExtension<PsiContextVariableProvider> =
LanguageExtension("com.phodal.shirePsiVariableProvider")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.phodal.shirecore.provider.variable.PsiContextVariableProvider
import com.phodal.shirecore.provider.variable.model.PsiContextVariable
import com.phodal.shirecore.provider.variable.model.PsiContextVariable.FRAMEWORK_CONTEXT
import com.phodal.shirecore.provider.variable.model.PsiContextVariable.*

class DefaultPsiContextVariableProvider : PsiContextVariableProvider {
override fun resolve(variable: PsiContextVariable, project: Project, editor: Editor, psiElement: PsiElement?): String {
Expand All @@ -14,6 +14,16 @@ class DefaultPsiContextVariableProvider : PsiContextVariableProvider {
return collectFrameworkContext(psiElement, project)
}

CHANGE_COUNT -> {
return calculateChangeCount(psiElement)
}
LINE_COUNT -> {
return calculateLineCount(psiElement)
}
COMPLEXITY_COUNT -> {
return calculateComplexityCount(psiElement)
}

else -> ""
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,22 @@ enum class PsiContextVariable(

SIMILAR_CODE("similarCode", "Recently 20 files similar code based on the tf-idf search"),

STRUCTURE("structure", "The structure of the current class, for programming language will be in UML format.")
STRUCTURE("structure", "The structure of the current class, for programming language will be in UML format."),

/**
* Represents the number of changes in the current file.
*/
CHANGE_COUNT("changeCount", "The number of changes in the current file"),

/**
* Represents the number of lines in the current file.
*/
LINE_COUNT("lineCount", "The number of lines in the current file"),

/**
* Represents the complexity of the current file.
*/
COMPLEXITY_COUNT("complexityCount", "The complexity of the current file")
;

companion object {
Expand All @@ -87,7 +102,7 @@ enum class PsiContextVariable(
* @return the PsiVariable with the given variable name
*/
fun from(variableName: String): PsiContextVariable? {
return values().firstOrNull { it.variableName == variableName }
return entries.firstOrNull { it.variableName == variableName }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class GoPsiContextVariableProvider : PsiContextVariableProvider {
else -> ""
}
}
PsiContextVariable.CHANGE_COUNT -> calculateChangeCount(psiElement)
PsiContextVariable.LINE_COUNT -> calculateLineCount(psiElement)
PsiContextVariable.COMPLEXITY_COUNT -> ""
} ?: ""
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class JavaPsiContextVariableProvider : PsiContextVariableProvider {
} ?: ""

FRAMEWORK_CONTEXT -> return collectFrameworkContext(psiElement, project)
PsiContextVariable.CHANGE_COUNT -> calculateChangeCount(psiElement)
PsiContextVariable.LINE_COUNT -> calculateLineCount(psiElement)
PsiContextVariable.COMPLEXITY_COUNT -> ""
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class JSPsiContextVariableProvider : PsiContextVariableProvider {
else -> null
} ?: ""
}
PsiContextVariable.CHANGE_COUNT -> calculateChangeCount(psiElement)
PsiContextVariable.LINE_COUNT -> calculateLineCount(psiElement)
PsiContextVariable.COMPLEXITY_COUNT -> ""
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class ShireProtoPsiVariableProvider : PsiContextVariableProvider {
PsiContextVariable.IS_NEED_CREATE_FILE -> ""
PsiContextVariable.TARGET_TEST_FILE_NAME -> ""
PsiContextVariable.UNDER_TEST_METHOD_CODE -> ""
PsiContextVariable.CHANGE_COUNT -> calculateChangeCount(psiElement)
PsiContextVariable.LINE_COUNT -> calculateLineCount(psiElement)
PsiContextVariable.COMPLEXITY_COUNT -> ""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ class ShirePythonPsiVariableProvider : PsiContextVariableProvider {

PsiContextVariable.SIMILAR_CODE -> TODO()
PsiContextVariable.STRUCTURE -> TODO()
PsiContextVariable.CHANGE_COUNT -> calculateChangeCount(psiElement)
PsiContextVariable.LINE_COUNT -> calculateLineCount(psiElement)
PsiContextVariable.COMPLEXITY_COUNT -> ""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class ShirePsiVariableProvider : PsiContextVariableProvider {
PsiContextVariable.CALLED_METHOD -> ""
PsiContextVariable.SIMILAR_CODE -> ""
PsiContextVariable.STRUCTURE -> ""
PsiContextVariable.CHANGE_COUNT -> calculateChangeCount(psiElement)
PsiContextVariable.LINE_COUNT -> calculateLineCount(psiElement)
PsiContextVariable.COMPLEXITY_COUNT -> ""
}
}
}

0 comments on commit e23687c

Please sign in to comment.