generated from JetBrains/intellij-platform-plugin-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(json-text-provider): refactor to use extension point and remove …
…duplicate code. #183
- Loading branch information
Showing
5 changed files
with
133 additions
and
59 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
local-bundle/src/main/kotlin/cc/unitmesh/local/provider/LocalJsonTextProvider.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,62 @@ | ||
package cc.unitmesh.local.provider | ||
|
||
import cc.unitmesh.devti.custom.schema.CUSTOM_AGENT_FILE_NAME | ||
import cc.unitmesh.devti.provider.local.JsonTextProvider | ||
import com.intellij.json.JsonLanguage | ||
import com.intellij.lang.Language | ||
import com.intellij.openapi.editor.Document | ||
import com.intellij.openapi.editor.colors.EditorColorsUtil | ||
import com.intellij.openapi.editor.ex.EditorEx | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.ui.LanguageTextField | ||
import java.awt.Dimension | ||
import java.awt.FontMetrics | ||
|
||
class LocalJsonTextProvider : JsonTextProvider { | ||
override fun createComponent( | ||
myProject: Project, | ||
value: String, | ||
placeholder: String, | ||
fileName: String | ||
): LanguageTextField { | ||
return JsonLanguageField(myProject, value, placeholder, fileName) | ||
} | ||
} | ||
|
||
class JsonLanguageField( | ||
private val myProject: Project, | ||
val value: String, | ||
private val placeholder: String, | ||
private val fileName: String | ||
) : | ||
LanguageTextField( | ||
JsonLanguage.INSTANCE, myProject, value, | ||
object : SimpleDocumentCreator() { | ||
override fun createDocument(value: String?, language: Language?, project: Project?): Document { | ||
return createDocument(value, language, project, this) | ||
} | ||
|
||
override fun customizePsiFile(file: PsiFile?) { | ||
file?.name = CUSTOM_AGENT_FILE_NAME | ||
} | ||
} | ||
) { | ||
|
||
override fun createEditor(): EditorEx { | ||
return super.createEditor().apply { | ||
setShowPlaceholderWhenFocused(true) | ||
setHorizontalScrollbarVisible(false) | ||
setVerticalScrollbarVisible(true) | ||
setPlaceholder(placeholder) | ||
|
||
val scheme = EditorColorsUtil.getColorSchemeForBackground(this.colorsScheme.defaultBackground) | ||
this.colorsScheme = this.createBoundColorSchemeDelegate(scheme) | ||
|
||
val metrics: FontMetrics = getFontMetrics(font) | ||
val columnWidth = metrics.charWidth('m') | ||
isOneLineMode = false | ||
preferredSize = Dimension(25 * columnWidth, 25 * metrics.height) | ||
} | ||
} | ||
} |
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
41 changes: 3 additions & 38 deletions
41
src/main/kotlin/cc/unitmesh/devti/gui/component/JsonLanguageField.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 |
---|---|---|
@@ -1,45 +1,10 @@ | ||
package cc.unitmesh.devti.gui.component | ||
|
||
import cc.unitmesh.devti.custom.schema.CUSTOM_AGENT_FILE_NAME | ||
import com.intellij.json.JsonLanguage | ||
import com.intellij.lang.Language | ||
import com.intellij.openapi.editor.Document | ||
import com.intellij.openapi.editor.colors.EditorColorsUtil | ||
import com.intellij.openapi.editor.ex.EditorEx | ||
import cc.unitmesh.devti.provider.local.JsonTextProvider | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.ui.LanguageTextField | ||
import java.awt.Dimension | ||
import java.awt.FontMetrics | ||
|
||
class JsonLanguageField(private val myProject: Project, val value: String, private val placeholder: String, private val fileName: String) : | ||
LanguageTextField(JsonLanguage.INSTANCE, myProject, value, | ||
object : SimpleDocumentCreator() { | ||
override fun createDocument(value: String?, language: Language?, project: Project?): Document { | ||
return createDocument(value, language, project, this) | ||
} | ||
|
||
override fun customizePsiFile(file: PsiFile?) { | ||
file?.name = CUSTOM_AGENT_FILE_NAME | ||
} | ||
} | ||
) { | ||
|
||
override fun createEditor(): EditorEx { | ||
return super.createEditor().apply { | ||
setShowPlaceholderWhenFocused(true) | ||
setHorizontalScrollbarVisible(false) | ||
setVerticalScrollbarVisible(true) | ||
setPlaceholder(placeholder) | ||
|
||
val scheme = EditorColorsUtil.getColorSchemeForBackground(this.colorsScheme.defaultBackground) | ||
this.colorsScheme = this.createBoundColorSchemeDelegate(scheme) | ||
|
||
val metrics: FontMetrics = getFontMetrics(font) | ||
val columnWidth = metrics.charWidth('m') | ||
isOneLineMode = false | ||
preferredSize = Dimension(25 * columnWidth, 25 * metrics.height) | ||
} | ||
} | ||
fun JsonLanguageField(myProject: Project, value: String, placeholder: String, fileName: String) : LanguageTextField { | ||
return JsonTextProvider.create(myProject, value, placeholder, fileName) | ||
} | ||
|
65 changes: 65 additions & 0 deletions
65
src/main/kotlin/cc/unitmesh/devti/provider/local/JsonEditorProvider.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,65 @@ | ||
package cc.unitmesh.devti.provider.local | ||
|
||
import cc.unitmesh.devti.custom.schema.CUSTOM_AGENT_FILE_NAME | ||
import com.intellij.lang.Language | ||
import com.intellij.openapi.editor.Document | ||
import com.intellij.openapi.editor.colors.EditorColorsUtil | ||
import com.intellij.openapi.editor.ex.EditorEx | ||
import com.intellij.openapi.extensions.ExtensionPointName | ||
import com.intellij.openapi.fileTypes.PlainTextLanguage | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiFile | ||
import com.intellij.ui.LanguageTextField | ||
import java.awt.Dimension | ||
import java.awt.FontMetrics | ||
|
||
interface JsonTextProvider { | ||
fun createComponent(myProject: Project, value: String, placeholder: String, fileName: String): LanguageTextField | ||
|
||
companion object { | ||
private val EP_NAME: ExtensionPointName<JsonTextProvider> = | ||
ExtensionPointName("cc.unitmesh.jsonTextProvider") | ||
|
||
fun create(myProject: Project, value: String, placeholder: String, fileName: String): LanguageTextField { | ||
return EP_NAME.extensionList.map { | ||
it.createComponent(myProject, value, placeholder, fileName) | ||
}.firstOrNull() ?: DefaultLanguageField(myProject, value, placeholder, fileName) | ||
} | ||
} | ||
} | ||
|
||
class DefaultLanguageField( | ||
private val myProject: Project, | ||
val value: String, | ||
private val placeholder: String, | ||
private val fileName: String | ||
) : | ||
LanguageTextField(PlainTextLanguage.INSTANCE, myProject, value, | ||
object : SimpleDocumentCreator() { | ||
override fun createDocument(value: String?, language: Language?, project: Project?): Document { | ||
return createDocument(value, language, project, this) | ||
} | ||
|
||
override fun customizePsiFile(file: PsiFile?) { | ||
file?.name = CUSTOM_AGENT_FILE_NAME | ||
} | ||
} | ||
) { | ||
|
||
override fun createEditor(): EditorEx { | ||
return super.createEditor().apply { | ||
setShowPlaceholderWhenFocused(true) | ||
setHorizontalScrollbarVisible(false) | ||
setVerticalScrollbarVisible(true) | ||
setPlaceholder(placeholder) | ||
|
||
val scheme = EditorColorsUtil.getColorSchemeForBackground(this.colorsScheme.defaultBackground) | ||
this.colorsScheme = this.createBoundColorSchemeDelegate(scheme) | ||
|
||
val metrics: FontMetrics = getFontMetrics(font) | ||
val columnWidth = metrics.charWidth('m') | ||
isOneLineMode = false | ||
preferredSize = Dimension(25 * columnWidth, 25 * metrics.height) | ||
} | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
src/main/kotlin/cc/unitmesh/devti/provider/local/JsonEditorProvider.ts.kt
This file was deleted.
Oops, something went wrong.