Skip to content

Commit

Permalink
feat(commands): add commandName property to all ShireCommand implemen…
Browse files Browse the repository at this point in the history
…tations

Add a `commandName` property to the `ShireCommand` interface and implement it in all command classes. This property is used to identify the command type via the `BuiltinCommand` enum.
  • Loading branch information
phodal committed Feb 3, 2025
1 parent 4857af8 commit 08fc7f3
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package com.phodal.shirelang.compiler.execute.command
import com.phodal.shirecore.agent.agenttool.browse.BrowseTool
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.project.Project
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

class BrowseShireCommand(val myProject: Project, private val prop: String,
) : ShireCommand {
override val commandName = BuiltinCommand.BROWSE

class BrowseShireCommand(val myProject: Project, private val prop: String) : ShireCommand {
override suspend fun doExecute(): String? {
var body: String? = null
runInEdt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package com.phodal.shirelang.compiler.execute.command
import com.intellij.openapi.project.Project
import com.phodal.shirecore.provider.shire.RevisionProvider
import com.phodal.shirecore.utils.markdown.CodeFence
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

class CommitShireCommand(val myProject: Project, val commitMsg: String) : ShireCommand {
override val commandName = BuiltinCommand.COMMIT

override suspend fun doExecute(): String {
RevisionProvider.provide()?.let {
return it.commitCode(myProject, commitMsg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package com.phodal.shirelang.compiler.execute.command
import com.intellij.openapi.project.Project
import com.phodal.shirecore.provider.function.ToolchainFunctionProvider
import com.phodal.shirecore.utils.markdown.CodeFence
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

class DatabaseShireCommand(val myProject: Project, private val prop: String, private val codeContent: String?) :
ShireCommand {
override val commandName = BuiltinCommand.DATABASE

override suspend fun doExecute(): String {
val args = if (codeContent != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiManager
import com.phodal.shirecore.lookupFile
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

/**
* The `DirShireCommand` class is responsible for listing files and directories in a tree-like structure for a given directory path within a project.
Expand All @@ -29,6 +30,8 @@ import com.phodal.shirecore.lookupFile
* @param dir The path of the directory to list.
*/
class DirShireCommand(private val myProject: Project, private val dir: String) : ShireCommand {
override val commandName = BuiltinCommand.DIR

private val output = StringBuilder()

override suspend fun doExecute(): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.vfs.VirtualFile
import com.phodal.shirelang.completion.dataprovider.FileFunc
import com.phodal.shirecore.canBeAdded
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

class FileFuncShireCommand(val myProject: Project, private val prop: String) : ShireCommand {
override val commandName = BuiltinCommand.FILE_FUNC

override suspend fun doExecute(): String {
val (functionName, args) = parseRegex(prop)
?: return """$SHIRE_ERROR: file-func is not in the format @file-func:<functionName>(<arg1>, <arg2>, ...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.phodal.shirecore.ShirelangNotifications
import com.phodal.shirecore.findFile
import com.phodal.shirecore.lookupFile
import com.phodal.shirelang.compiler.ast.LineInfo
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

/**
* FileAutoCommand is responsible for reading a file and returning its contents.
Expand All @@ -17,6 +18,8 @@ import com.phodal.shirelang.compiler.ast.LineInfo
*
*/
class FileShireCommand(private val myProject: Project, private val prop: String) : ShireCommand {
override val commandName = BuiltinCommand.FILE

override suspend fun doExecute(): String? {
val range: LineInfo? = LineInfo.fromString(prop)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.phodal.shirecore.lookupFile
import com.phodal.shirecore.provider.shire.ShireSymbolProvider
import com.phodal.shirelang.compiler.ast.LineInfo
import com.phodal.shirelang.compiler.parser.SHIRE_ERROR
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand
import com.phodal.shirelang.psi.ShireUsed

/**
Expand All @@ -23,6 +24,8 @@ import com.phodal.shirelang.psi.ShireUsed
* and move the cursor to line 10, column 8.
*/
class GotoShireCommand(val myProject: Project, private val argument: String, val used: ShireUsed) : ShireCommand {
override val commandName = BuiltinCommand.GOTO

override suspend fun doExecute(): String {
if (argument.contains(".")) {
return gotoSymbol()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.vfs.VirtualFile
import com.phodal.shirecore.canBeAdded
import com.phodal.shirecore.relativePath
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand
import kotlin.collections.component1
import kotlin.collections.component2

Expand All @@ -23,6 +24,8 @@ import kotlin.collections.component2
*
*/
class LocalSearchShireCommand(val myProject: Project, private val scope: String, val text: String?) : ShireCommand {
override val commandName = BuiltinCommand.LOCAL_SEARCH

private val MAX_LINE_SIZE = 180
private val OVERLAP = 4

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.phodal.shirecore.lookupFile
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

class OpenShireCommand(val myProject: Project, private val filename: String) : ShireCommand {
override val commandName = BuiltinCommand.OPEN

override suspend fun doExecute(): String? {
FileDocumentManager.getInstance().saveAllDocuments()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import com.intellij.openapi.vcs.changes.patch.ApplyPatchDefaultExecutor
import com.intellij.openapi.vcs.changes.patch.MatchPatchPaths
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.containers.MultiMap
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

class PatchShireCommand(val myProject: Project, private val prop: String, private val codeContent: String) :
ShireCommand {
override val commandName = BuiltinCommand.PATCH

override suspend fun doExecute(): String {
FileDocumentManager.getInstance().saveAllDocuments()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.phodal.shirelang.compiler.execute.command

import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

class PrintShireCommand(private val value: String) : ShireCommand {
override val commandName = BuiltinCommand.FILE_FUNC

override suspend fun doExecute(): String {
return value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.phodal.shirelang.completion.dataprovider.BuiltinRefactorCommand
import com.phodal.shirecore.provider.shire.RefactoringTool
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand
import com.phodal.shirelang.psi.ShireFile

/**
Expand Down Expand Up @@ -48,6 +49,8 @@ import com.phodal.shirelang.psi.ShireFile
*/
class RefactorShireCommand(val myProject: Project, private val argument: String, private val textSegment: String) :
ShireCommand {
override val commandName = BuiltinCommand.REFACTOR

override suspend fun doExecute(): String? {
var currentEditFile: PsiFile? = null
val editor = FileEditorManager.getInstance(myProject).selectedTextEditor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package com.phodal.shirelang.compiler.execute.command
import com.intellij.openapi.project.Project
import com.phodal.shirecore.provider.psi.RelatedClassesProvider
import com.phodal.shirecore.provider.shire.ShireSymbolProvider
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

class RelatedSymbolInsCommand(val myProject: Project, private val symbol: String) : ShireCommand {
override val commandName = BuiltinCommand.RELATED

override suspend fun doExecute(): String? {
val elements = ShireSymbolProvider.all().map {
it.resolveSymbol(myProject, symbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.phodal.shirelang.compiler.execute.command

import com.intellij.openapi.project.Project
import com.phodal.shirecore.provider.shire.RevisionProvider
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

/**
* RevAutoCommand is used to execute a command that retrieves the committed change list for a given revision using Git.
Expand All @@ -11,6 +12,8 @@ import com.phodal.shirecore.provider.shire.RevisionProvider
*
*/
class RevShireCommand(private val myProject: Project, private val revision: String) : ShireCommand {
override val commandName = BuiltinCommand.REV

override suspend fun doExecute(): String {
return RevisionProvider.provide()?.let {
val changes = it.fetchChanges(myProject, revision)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package com.phodal.shirelang.compiler.execute.command

import com.intellij.openapi.project.Project
import com.phodal.shirelang.compiler.execute.command.search.RipgrepSearcher
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

class RipgrepSearchShireCommand(
val myProject: Project, private val scope: String, val text: String?,
) : ShireCommand {
override val commandName = BuiltinCommand.RIPGREP_SEARCH

override fun isApplicable(): Boolean {
return RipgrepSearcher.findRipgrepBinary() != null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.phodal.shirecore.lookupFile
import com.phodal.shirecore.provider.TestingService
import com.phodal.shirecore.provider.shire.ProjectRunService
import com.phodal.shirelang.compiler.parser.SHIRE_ERROR
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

/**
* The `RunAutoCommand` class is responsible for executing an auto command on a given project.
Expand All @@ -16,6 +17,8 @@ import com.phodal.shirelang.compiler.parser.SHIRE_ERROR
*
*/
class RunShireCommand(val myProject: Project, private val argument: String) : ShireCommand {
override val commandName = BuiltinCommand.RUN

override suspend fun doExecute(): String {
val task = ProjectRunService.all().mapNotNull { projectRun ->
val hasTasks = projectRun.tasks(myProject).any { task -> task.contains(argument) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.intellij.psi.PsiManager
import com.intellij.sh.psi.ShFile
import com.intellij.sh.run.ShRunner
import com.phodal.shirecore.lookupFile
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand
import com.phodal.shirelang.runner.ShellFileRunService

/**
Expand All @@ -20,6 +21,8 @@ import com.phodal.shirelang.runner.ShellFileRunService
* @param argument The path to the file within the project whose content should be executed as a shell command.
*/
class ShellShireCommand(val myProject: Project, private val argument: String) : ShireCommand {
override val commandName = BuiltinCommand.SHELL

override suspend fun doExecute(): String {
val virtualFile = myProject.lookupFile(argument.trim()) ?: return "$SHIRE_ERROR: File not found: $argument"
val psiFile = PsiManager.getInstance(myProject).findFile(virtualFile) as? ShFile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.phodal.shirelang.compiler.execute.command

import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

interface ShireCommand {
val commandName: BuiltinCommand

fun isApplicable(): Boolean {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.phodal.shirecore.provider.codemodel.FileStructureProvider
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class StructureShireCommand(val myProject: Project, val prop: String) : ShireCommand {
private val logger = logger<StructureShireCommand>()
override val commandName = BuiltinCommand.STRUCTURE

override suspend fun doExecute(): String? {
val virtualFile = FileShireCommand.file(myProject, prop)
if (virtualFile == null) {
logger.warn("File not found: $prop")
logger<StructureShireCommand>().warn("File not found: $prop")
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package com.phodal.shirelang.compiler.execute.command
import com.phodal.shirelang.compiler.parser.SHIRE_ERROR
import com.intellij.openapi.project.Project
import com.phodal.shirecore.provider.shire.ShireSymbolProvider
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

class SymbolShireCommand(val myProject: Project, private val prop: String) :
ShireCommand {
override val commandName = BuiltinCommand.SYMBOL

override suspend fun doExecute(): String {
val result = ShireSymbolProvider.all().mapNotNull {
val found = it.resolveSymbol(myProject, prop)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import com.intellij.psi.PsiManager
import com.phodal.shirecore.lookupFile
import com.phodal.shirelang.psi.ShireUsed
import com.phodal.shirecore.utils.markdown.CodeFence
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand

class WriteShireCommand(val myProject: Project, val argument: String, val content: String, private val used: ShireUsed) :
ShireCommand {
override val commandName = BuiltinCommand.WRITE

private val pathSeparator = "/"

override suspend fun doExecute(): String {
Expand Down

0 comments on commit 08fc7f3

Please sign in to comment.