generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): add ShireInputTextField component and integrate with ShireI…
…nputSection #165 - Introduced `ShireInputTextField` for handling text input with document listeners and editor customization. - Integrated `ShireInputTextField` into `ShireInputSection` with send and stop button icons. - Added support for coroutine worker threads and updated MarketplaceView to handle input submission. - Enhanced `ShireRunFileAction` with suspend execution functionality and added input validation messages.
- Loading branch information
Showing
6 changed files
with
199 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
core/src/main/kotlin/com/phodal/shirecore/ui/ShireInputTextField.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.phodal.shirecore.ui | ||
|
||
import com.intellij.openapi.Disposable | ||
import com.intellij.openapi.actionSystem.* | ||
import com.intellij.openapi.actionSystem.ex.AnActionListener | ||
import com.intellij.openapi.command.CommandProcessor | ||
import com.intellij.openapi.editor.Document | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.editor.EditorModificationUtil | ||
import com.intellij.openapi.editor.actions.EnterAction | ||
import com.intellij.openapi.editor.actions.IncrementalFindAction | ||
import com.intellij.openapi.editor.event.DocumentListener | ||
import com.intellij.openapi.editor.ex.EditorEx | ||
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider | ||
import com.intellij.openapi.fileTypes.FileTypes | ||
import com.intellij.openapi.project.DumbAwareAction | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.testFramework.LightVirtualFile | ||
import com.intellij.ui.EditorTextField | ||
import com.intellij.util.EventDispatcher | ||
import com.intellij.util.messages.MessageBusConnection | ||
import com.intellij.util.ui.JBUI | ||
import com.phodal.shirecore.sketch.highlight.findDocument | ||
import com.phodal.shirecore.utils.markdown.CodeFenceLanguage | ||
import java.awt.Color | ||
import java.awt.event.KeyEvent | ||
import java.util.* | ||
import javax.swing.KeyStroke | ||
|
||
class ShireInputTextField( | ||
project: Project, | ||
private val listeners: List<DocumentListener>, | ||
val disposable: Disposable?, | ||
val inputSection: ShireInputSection, | ||
) : EditorTextField(project, FileTypes.PLAIN_TEXT), Disposable { | ||
private var editorListeners: EventDispatcher<ShireInputListener> = inputSection.editorListeners | ||
|
||
init { | ||
isOneLineMode = false | ||
setFontInheritedFromLAF(true) | ||
addSettingsProvider { | ||
it.putUserData(IncrementalFindAction.SEARCH_DISABLED, true) | ||
it.colorsScheme.lineSpacing = 1.2f | ||
it.settings.isUseSoftWraps = true | ||
it.isEmbeddedIntoDialogWrapper = true | ||
it.contentComponent.setOpaque(false) | ||
} | ||
|
||
DumbAwareAction.create { | ||
object : AnAction() { | ||
override fun actionPerformed(actionEvent: AnActionEvent) { | ||
val editor = editor ?: return | ||
CommandProcessor.getInstance().executeCommand(project, { | ||
val eol = "\n" | ||
val caretOffset = editor.caretModel.offset | ||
editor.document.insertString(caretOffset, eol) | ||
editor.caretModel.moveToOffset(caretOffset + eol.length) | ||
EditorModificationUtil.scrollToCaret(editor) | ||
}, null, null) | ||
} | ||
} | ||
}.registerCustomShortcutSet( | ||
CustomShortcutSet( | ||
KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK), null), | ||
KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.META_DOWN_MASK), null) | ||
), this | ||
) | ||
|
||
val connect: MessageBusConnection = project.messageBus.connect(disposable ?: this) | ||
val topic = AnActionListener.TOPIC | ||
connect.subscribe(topic, object : AnActionListener { | ||
override fun afterActionPerformed(action: AnAction, event: AnActionEvent, result: AnActionResult) { | ||
if (event.dataContext.getData(CommonDataKeys.EDITOR) === editor && action is EnterAction) { | ||
editorListeners.multicaster.onSubmit(inputSection, ShireInputTrigger.Key) | ||
} | ||
} | ||
}) | ||
|
||
listeners.forEach { listener -> | ||
document.addDocumentListener(listener) | ||
} | ||
} | ||
|
||
override fun onEditorAdded(editor: Editor) { | ||
editorListeners.multicaster.editorAdded((editor as EditorEx)) | ||
} | ||
|
||
public override fun createEditor(): EditorEx { | ||
val editor = super.createEditor() | ||
editor.setVerticalScrollbarVisible(true) | ||
setBorder(JBUI.Borders.empty()) | ||
editor.setShowPlaceholderWhenFocused(true) | ||
editor.caretModel.moveToOffset(0) | ||
editor.scrollPane.setBorder(border) | ||
editor.contentComponent.setOpaque(false) | ||
return editor | ||
} | ||
|
||
override fun getBackground(): Color { | ||
val editor = editor ?: return super.getBackground() | ||
return editor.colorsScheme.defaultBackground | ||
} | ||
|
||
override fun getData(dataId: String): Any? { | ||
if (!PlatformCoreDataKeys.FILE_EDITOR.`is`(dataId)) { | ||
return super.getData(dataId) | ||
} | ||
|
||
val currentEditor = editor ?: return super.getData(dataId) | ||
return TextEditorProvider.getInstance().getTextEditor(currentEditor) | ||
} | ||
|
||
override fun dispose() { | ||
listeners.forEach { | ||
editor?.document?.removeDocumentListener(it) | ||
} | ||
} | ||
|
||
fun recreateDocument() { | ||
val id = UUID.randomUUID() | ||
val language = CodeFenceLanguage.findLanguage("Shire") | ||
val file = LightVirtualFile("ShireInput-$id", language, "") | ||
|
||
val document = file.findDocument() ?: throw IllegalStateException("Can't create in-memory document") | ||
|
||
initializeDocumentListeners(document) | ||
setDocument(document) | ||
inputSection.initEditor() | ||
} | ||
|
||
private fun initializeDocumentListeners(inputDocument: Document) { | ||
listeners.forEach { listener -> | ||
inputDocument.addDocumentListener(listener) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.