Skip to content

Commit

Permalink
feat(shire): add FileCreateService and integrate with ShireInput
Browse files Browse the repository at this point in the history
- Introduce FileCreateService to handle file creation logic.
- Update ShireInput to use FileCreateService for creating virtual files.
- Move ChatBoxShireFileService to shirelang and refactor as ChatBoxShireFileCreateService.
  • Loading branch information
phodal committed Dec 23, 2024
1 parent 8403ee5 commit 50e698e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.phodal.shirecore.provider.shire

import com.intellij.lang.Language
import com.intellij.lang.LanguageExtension
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile


interface FileCreateService {
fun createFile(prompt: String, project: Project): VirtualFile?

companion object {
private val languageExtension: LanguageExtension<FileCreateService> =
LanguageExtension("com.phodal.shireFileCreateService")

fun provide(language: Language): FileCreateService? {
return languageExtension.forLanguage(language)
}
}
}
17 changes: 8 additions & 9 deletions core/src/main/kotlin/com/phodal/shirecore/ui/input/ShireInput.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.phodal.shirecore.ui.input

import com.intellij.ide.scratch.ScratchFileService
import com.intellij.ide.scratch.ScratchRootType
import com.intellij.lang.Language
import com.intellij.openapi.Disposable
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.phodal.shirecore.SHIRE_CHAT_BOX_FILE
import com.phodal.shirecore.ShireCoreBundle
import com.phodal.shirecore.ShireCoroutineScope
import com.phodal.shirecore.provider.shire.FileCreateService
import com.phodal.shirecore.provider.shire.FileRunService
import java.awt.BorderLayout
import javax.swing.BorderFactory
Expand All @@ -33,8 +30,7 @@ class ShireInput(val project: Project) : JPanel(BorderLayout()), Disposable {
return
}

val virtualFile =
project.getService(ChatBoxShireFileService::class.java).createShireFile(prompt, project)
val virtualFile = getVirtualFile(prompt)
this@ShireInput.scratchFile = virtualFile

FileRunService.provider(project, virtualFile!!)
Expand All @@ -45,10 +41,13 @@ class ShireInput(val project: Project) : JPanel(BorderLayout()), Disposable {
this.add(inputSection, BorderLayout.CENTER)
}

override fun dispose() {
scratchFile?.delete(this)
private fun getVirtualFile(prompt: String): VirtualFile? {
val findLanguageByID = Language.findLanguageByID("Shire")
val provide = FileCreateService.provide(findLanguageByID!!)
return provide!!.createFile(prompt, project)
}

companion object {
override fun dispose() {
scratchFile?.delete(this)
}
}
6 changes: 6 additions & 0 deletions core/src/main/resources/com.phodal.shirecore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
<with attribute="implementationClass" implements="com.phodal.shirecore.provider.TestingService"/>
</extensionPoint>

<extensionPoint qualifiedName="com.phodal.shireFileCreateService"
beanClass="com.intellij.lang.LanguageExtensionPoint"
dynamic="true">
<with attribute="implementationClass" implements="com.phodal.shirecore.provider.shire.FileCreateService"/>
</extensionPoint>

<!-- Toolchain Provider -->
<extensionPoint qualifiedName="com.phodal.shireLanguageToolchainProvider"
beanClass="com.intellij.lang.LanguageExtensionPoint" dynamic="true">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package com.phodal.shirecore.ui.input
package com.phodal.shirelang.provider

import com.intellij.ide.scratch.ScratchFileService
import com.intellij.ide.scratch.ScratchRootType
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.phodal.shirecore.SHIRE_CHAT_BOX_FILE
import com.phodal.shirecore.config.ShireActionLocation
import com.phodal.shirecore.provider.shire.FileCreateService
import com.phodal.shirelang.actions.base.DynamicShireActionService
import org.intellij.lang.annotations.Language

@Service(Service.Level.PROJECT)
class ChatBoxShireFileService(val project: Project) {
private var baseContent: String = ""
class ChatBoxShireFileCreateService : FileCreateService {
override fun createFile(prompt: String, project: Project): VirtualFile? {
val actions = DynamicShireActionService.getInstance(project).getActions(ShireActionLocation.CHAT_BOX)
var baseContent = ""
if (actions.isNotEmpty()) {
baseContent = actions.first().shireFile.text ?: ""
}

fun createShireFile(prompt: String, project: Project): VirtualFile? {
if (baseContent.isNotEmpty()) {
return createInputWithBase(prompt, project)
return createInputWithBase(prompt, project, baseContent)
}

return createInputOnly(prompt, project)
Expand Down Expand Up @@ -50,6 +55,7 @@ class ChatBoxShireFileService(val project: Project) {
private fun createInputWithBase(
prompt: String,
project: Project,
baseContent: String,
): VirtualFile? {
val content = baseContent.replace("\$chatPrompt", prompt)

Expand Down
4 changes: 4 additions & 0 deletions shirelang/src/main/resources/com.phodal.shirelang.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@

<shireFileRunService implementation="com.phodal.shirelang.runner.ShireFileRunService"/>

<shireFileCreateService
language="Shire"
implementationClass="com.phodal.shirelang.provider.ChatBoxShireFileCreateService"/>

<shirePsiVariableProvider language="Shire"
implementationClass="com.phodal.shirelang.provider.ShirePsiVariableProvider"/>
<shireLanguageToolchainProvider language="Shire"
Expand Down

0 comments on commit 50e698e

Please sign in to comment.