Skip to content

Commit

Permalink
feat(devins-java): add resolveSymbol method to DevInsSymbolProvider #101
Browse files Browse the repository at this point in the history


Add resolveSymbol method to DevInsSymbolProvider interface to resolve symbols for different programming languages.
  • Loading branch information
phodal committed Mar 18, 2024
1 parent bbf4b35 commit d20961f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
Expand Up @@ -6,6 +6,7 @@ import cc.unitmesh.devti.language.parser.CodeBlockElement
import cc.unitmesh.devti.language.psi.DevInFile
import cc.unitmesh.devti.language.psi.DevInTypes
import cc.unitmesh.devti.language.psi.DevInUsed
import cc.unitmesh.devti.provider.devins.DevInsSymbolProvider
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
Expand Down Expand Up @@ -114,7 +115,8 @@ class DevInsCompiler(private val myProject: Project, val file: DevInFile, val ed
}

BuiltinCommand.SYMBOL -> {
PrintInsCommand("/" + commandNode.commandName + ":" + prop)
result.isLocalCommand = true
SymbolInsCommand(myProject, prop)
}

BuiltinCommand.WRITE -> {
Expand Down Expand Up @@ -192,4 +194,19 @@ class DevInsCompiler(private val myProject: Project, val file: DevInFile, val ed
}
}

class SymbolInsCommand(val myProject: Project, val prop: String) :
InsCommand {
override fun execute(): String? {
val result = DevInsSymbolProvider.all().map {
it.resolveSymbol(myProject, prop).joinToString("\n")
}

if (result.isEmpty()) {
return "<DevliError> No symbol found: $prop"
}

return result.joinToString("\n")
}
}


Expand Up @@ -23,7 +23,7 @@ enum class BuiltinCommand(
* - Kotlin: [org.jetbrains.kotlin.idea.completion.KotlinCompletionContributor]
* - Python: [com.jetbrains.python.codeInsight.completion.PyClassNameCompletionContributor]
*/
SYMBOL("symbol", "[TODO] Read content by Java/Kotlin canonicalName", AllIcons.Actions.GroupBy, true),
SYMBOL("symbol", "[TODO] Read content by Java/Kotlin canonicalName", AllIcons.Actions.GroupBy, true, true),
WRITE("write", "Write content to a file, /write:path/to/file:L1-L2", AllIcons.Actions.Edit, true, true),
PATCH("patch", "Apply patch to a file, /patch:path/to/file", AllIcons.Vcs.Patch_file, false),
RUN("run", "Run the content of a file", AllIcons.Actions.Execute, false),
Expand Down
Expand Up @@ -9,10 +9,13 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiPackageStatement
import com.intellij.psi.search.FileTypeIndex
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.ProjectScope
import com.intellij.psi.search.PsiShortNamesCache
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.util.SmartList


class JavaCustomDevInsSymbolProvider : DevInsSymbolProvider {
override fun lookupSymbol(
project: Project,
Expand Down Expand Up @@ -43,4 +46,16 @@ class JavaCustomDevInsSymbolProvider : DevInsSymbolProvider {

return lookupElements
}

override fun resolveSymbol(project: Project, symbol: String): Iterable<String> {
val scope = GlobalSearchScope.allScope(project)

// for class name only
val psiClasses = PsiShortNamesCache.getInstance(project).getClassesByName(symbol, scope)
if (psiClasses.isNotEmpty()) {
return psiClasses.map { it.qualifiedName!! }
}

return emptyList()
}
}
Expand Up @@ -11,13 +11,9 @@ import com.intellij.openapi.project.Project
* - Completion will be triggered by like `/symbol:`, and the symbol provider will provide the completion for the symbol.
* - Execution will be triggered by like `/symbol:java.lang.String`, all load children level elements, like `java.lang.String#length()`
*
* For execution:
* - If parent is Root, the children will be packages
* - If parent is Package, the children will be classes
* - If parent is Class, the children will be methods and fields
* For execution, see in [DevInsSymbolProvider.resolveSymbol]
*/
interface DevInsSymbolProvider {

/**
* Lookup canonical name for different language
*/
Expand All @@ -27,6 +23,15 @@ interface DevInsSymbolProvider {
result: CompletionResultSet
): Iterable<LookupElement>

/**
* Resolves the symbol for different programming languages.
* For example, in Java:
* - If the parent is Root, the children will be packages
* - If the parent is Package, the children will be classes
* - If the parent is Class, the children will be methods and fields
*/
fun resolveSymbol(project: Project, symbol: String): Iterable<String>

companion object {
private val EP_NAME: ExtensionPointName<DevInsSymbolProvider> =
ExtensionPointName("cc.unitmesh.customDevInsCompletionProvider")
Expand Down

0 comments on commit d20961f

Please sign in to comment.