Skip to content

Commit

Permalink
Merge pull request #15 from anboralabs/settings
Browse files Browse the repository at this point in the history
Settings
  • Loading branch information
dalgarins authored Nov 23, 2024
2 parents a27e910 + bb24ede commit 5fb2d16
Show file tree
Hide file tree
Showing 15 changed files with 290 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,7 @@ gradle-app.setting
# Java heap dump
*.hprof

### Intellij plugins sdk ###
.intellijPlatform/

# End of https://www.toptal.com/developers/gitignore/api/intellij,intellij+iml,intellij+all,windows,java,kotlin,gradle,macos,linux,node
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
id("java")
id("com.github.node-gradle.node") version "5.0.0" // NodeJS support
id("org.jetbrains.kotlin.jvm") version "2.0.0"
id("org.jetbrains.intellij.platform") version "2.0.1"
id("org.jetbrains.intellij.platform") version "2.1.0"
}

group = properties("pluginGroup").get()
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ kotlin.incremental.useClasspathSnapshot=false
pluginGroup = co.anbora.labs
pluginName = JSonCrack
# SemVer format -> https://semver.org
pluginVersion = 1.3.1
pluginVersion = 1.3.2

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 243
Expand Down
7 changes: 6 additions & 1 deletion src/main/html/change-notes.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
Versions:
<ul>
<li>All Intellij products support: 1.3.1</li>
<li>All Intellij products support: 1.3.2</li>
</ul>
<br>
Plugin updates:
<ul>
<li><b>1.3.2</b> <em>(2023-11-22)</em> - setting panel
<ul>
<li>Add setting panel to manage extensions</li>
</ul>
</li>
<li><b>1.3.1</b> <em>(2024-09-15)</em> - update sdk </li>
<li><b>1.3.0</b> <em>(2024-06-15)</em> - update sdk </li>
<li><b>1.2.3</b> <em>(2024-04-20)</em> - fix issue with chinese characters </li>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="co.anbora.labs.jsoncrack.ide.fileType.FileTypePanel">
<grid id="27dc6" binding="myMainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="47"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="61a3d" class="javax.swing.JTextField" binding="myPatternField">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="7a639" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="61a3d"/>
<text resource-bundle="messages/jsoncrack" key="filetype.edit.add.pattern.prompt"/>
</properties>
</component>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package co.anbora.labs.jsoncrack.ide.fileType;

import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class FileTypePanel {
private JTextField myPatternField;
private JPanel myMainPanel;

@NotNull
public JTextField getPatternField() {
return myPatternField;
}

@NotNull
public JPanel getMainPanel() {
return myMainPanel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package co.anbora.labs.jsoncrack.ide

import com.intellij.openapi.vfs.VirtualFile

private val SUPPORTED_EXTENSIONS = arrayOf(".json")
val SUPPORTED_EXTENSIONS = setOf(".json")

fun VirtualFile?.isJsonFile(): Boolean {
fun VirtualFile?.isJsonFile(extensions: Set<String>): Boolean {
if (this == null) {
return false
}
Expand All @@ -13,7 +13,5 @@ fun VirtualFile?.isJsonFile(): Boolean {
return false
}

val extensions = SUPPORTED_EXTENSIONS

return extensions.any { ext -> this.name.lowercase().endsWith(ext) }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.anbora.labs.jsoncrack.ide.editor

import co.anbora.labs.jsoncrack.ide.fileType.FileTypeService.Companion.fileTypeSettings
import co.anbora.labs.jsoncrack.ide.isJsonFile
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.fileEditor.FileEditorPolicy
Expand All @@ -13,7 +14,7 @@ private const val EDITOR_TYPE_ID = "co.anbora.labs.jsoncrack.editor"

class EditorProvider : FileEditorProvider, DumbAware {
override fun accept(project: Project, file: VirtualFile): Boolean {
return file.isJsonFile()
return file.isJsonFile(fileTypeSettings.extensions())
}

override fun createEditor(project: Project, file: VirtualFile): FileEditor = Editor(project, file)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package co.anbora.labs.jsoncrack.ide.fileType

import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.ui.ValidationInfo
import javax.swing.JComponent


class FileTypeDialog(title: String, project: Project): DialogWrapper(project, false) {

private val fileTypePanel = FileTypePanel()

init {
this.title = title
init()
}

override fun createCenterPanel(): JComponent = fileTypePanel.mainPanel

fun getExtension(): String = fileTypePanel.patternField.text

override fun doValidate(): ValidationInfo? {
if (fileTypePanel.patternField.text.isEmpty()) {
return ValidationInfo("Not valid extension", fileTypePanel.patternField)
}

if (!fileTypePanel.patternField.text.startsWith(".")) {
return ValidationInfo("Extension must start with `.`", fileTypePanel.patternField)
}

return null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package co.anbora.labs.jsoncrack.ide.fileType

import co.anbora.labs.jsoncrack.ide.SUPPORTED_EXTENSIONS
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service
import com.intellij.util.xmlb.XmlSerializerUtil
import com.intellij.util.xmlb.annotations.XCollection

@State(
name = "JSONCrackN",
storages = [Storage("JSONCrackN.xml")]
)
class FileTypeService: PersistentStateComponent<FileTypeService.ToolchainState?> {

private var state = ToolchainState()

fun extensions(): Set<String> {
if (state.extensions.isEmpty()) {
this.addExtensions(SUPPORTED_EXTENSIONS)
}
return state.extensions
}

class ToolchainState {
@XCollection
var extensions = mutableSetOf<String>()
}

override fun getState(): ToolchainState = this.state

override fun loadState(state: ToolchainState) {
XmlSerializerUtil.copyBean(state, this.state)
}

fun addExtensions(supportedExtensions: Set<String>) {
this.state.extensions.clear()
this.state.extensions.addAll(supportedExtensions)
}

companion object {
val fileTypeSettings
get() = service<FileTypeService>()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.anbora.labs.jsoncrack.ide.i18n

import com.intellij.DynamicBundle
import org.jetbrains.annotations.NonNls

object JSonCrackBundle {

@NonNls
private val INSTANCE: DynamicBundle = DynamicBundle(JSonCrackBundle::class.java, "messages.jsoncrack")

fun message(key: String): String {
return INSTANCE.getMessage(key)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package co.anbora.labs.jsoncrack.ide.settings

import co.anbora.labs.jsoncrack.ide.fileType.FileTypeService.Companion.fileTypeSettings
import com.intellij.openapi.Disposable
import com.intellij.openapi.options.Configurable
import com.intellij.openapi.project.Project
import javax.swing.JComponent

class FileTypeSettingsConfigurable(
project: Project
): Configurable, Disposable {

private val dialog = PatternsPanel(project, HashSet(fileTypeSettings.extensions()))

override fun createComponent(): JComponent = dialog

override fun isModified(): Boolean = fileTypeSettings.extensions() != dialog.getExtensions()

override fun apply() {
fileTypeSettings.addExtensions(dialog.getExtensions())
}

override fun getDisplayName(): String = "JSonCrack"

override fun dispose() = Unit
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package co.anbora.labs.jsoncrack.ide.settings

import co.anbora.labs.jsoncrack.ide.fileType.FileTypeDialog
import co.anbora.labs.jsoncrack.ide.i18n.JSonCrackBundle
import com.intellij.openapi.project.Project
import com.intellij.ui.IdeBorderFactory
import com.intellij.ui.JBColor
import com.intellij.ui.ListUtil
import com.intellij.ui.ToolbarDecorator
import com.intellij.ui.components.JBList
import com.intellij.ui.components.JBScrollPane
import com.intellij.util.ui.JBUI
import java.awt.BorderLayout
import javax.swing.DefaultListModel
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.ListSelectionModel


class PatternsPanel(
private val project: Project,
private val extensions: Collection<String>
): JPanel() {

private val myList = JBList<String>(DefaultListModel())
private val TITLE_INSETS = JBUI.insetsTop(8)

init {
layout = BorderLayout()
myList.selectionMode = ListSelectionModel.SINGLE_SELECTION
//myList.setCellRenderer(ExtensionRenderer())
myList.emptyText.setText(JSonCrackBundle.message("filetype.settings.no.patterns"))
myList.border = JBUI.Borders.empty()

val decorator: ToolbarDecorator = ToolbarDecorator.createDecorator(myList)
.setScrollPaneBorder(JBUI.Borders.empty())
.setPanelBorder(JBUI.Borders.customLine(JBColor.border(), 1, 1, 0, 1))
.setAddAction { showAddExtensionDialog() }
.setRemoveAction { removeExtension() }
.disableUpDownActions()
add(decorator.createPanel(), BorderLayout.NORTH)
val scrollPane: JScrollPane = JBScrollPane(myList)
scrollPane.border = JBUI.Borders.customLine(JBColor.border(), 0, 1, 1, 1)
add(scrollPane, BorderLayout.CENTER)


border = IdeBorderFactory.createTitledBorder(
JSonCrackBundle.message("filetype.registered.patterns.group"),
false,
TITLE_INSETS
).setShowLine(false)


this.refill()
}

private fun refill() {
val model: DefaultListModel<String> =
myList.model as DefaultListModel<String>

model.addAll(extensions)
}

private fun addExtensions(extension: String) {
val model: DefaultListModel<String> =
myList.model as DefaultListModel<String>

model.addElement(extension)
}

private fun removeExtension() {
val selectedItem = myList.selectedValue
if (selectedItem != null) {
ListUtil.removeSelectedItems(myList)
}
}

fun getExtensions(): Set<String> {
val model: DefaultListModel<String> =
myList.model as DefaultListModel<String>

return model.elements().toList().toSet()
}

private fun showAddExtensionDialog() {
val dialog = FileTypeDialog("Add Extension", project)

if (dialog.showAndGet()) {
this.addExtensions(dialog.getExtension())
}
}

}
9 changes: 9 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html -->
<depends>com.intellij.modules.platform</depends>

<resource-bundle>messages.jsoncrack</resource-bundle>

<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
<extensions defaultExtensionNs="com.intellij">
<projectConfigurable
groupId="tools"
displayName="JSonCrack"
id="preference.JSonCrackFileTypeConfigurable"
instance="co.anbora.labs.jsoncrack.ide.settings.FileTypeSettingsConfigurable" />
<fileEditorProvider implementation="co.anbora.labs.jsoncrack.ide.editor.EditorProvider" />

<applicationService serviceImplementation="co.anbora.labs.jsoncrack.ide.fileType.FileTypeService"/>
</extensions>
</idea-plugin>
3 changes: 3 additions & 0 deletions src/main/resources/messages/jsoncrack.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
filetype.settings.no.patterns=No registered file patterns
filetype.registered.patterns.group=File name extensions:
filetype.edit.add.pattern.prompt=Enter new extension:

0 comments on commit 5fb2d16

Please sign in to comment.