Skip to content

Commit 2cbbaae

Browse files
committed
🌈 style(code): 优化了部分代码逻辑
1 parent 3c088b9 commit 2cbbaae

File tree

8 files changed

+135
-47
lines changed

8 files changed

+135
-47
lines changed
Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,45 @@
1+
/*
2+
* Copyright (c) 2024. Irony All Rights Reserved.
3+
* Project: QSSEditor
4+
* File: QSSAction.kt
5+
* Date: 2024/5/18 上午1:02
6+
* Author: Irony
7+
* Email: 892768447@qq.com
8+
* Site: https://pyqt.site , https://pyqt5.com
9+
*/
10+
111
package irony.pycharm.qsseditor
212

13+
import com.intellij.openapi.actionSystem.ActionUpdateThread
314
import com.intellij.openapi.actionSystem.AnAction
415
import com.intellij.openapi.actionSystem.AnActionEvent
516
import com.intellij.openapi.actionSystem.CommonDataKeys
617
import com.intellij.openapi.diagnostic.logger
718
import com.intellij.openapi.editor.Editor
8-
import com.intellij.openapi.project.Project
19+
import com.intellij.openapi.vfs.VirtualFile
920

10-
private val LOG = logger<QSSAction>()
21+
private val Log = logger<QSSAction>()
1122

12-
class QSSAction : AnAction(QSSBundle.message("action.apply.title", "Apply Style")) {
23+
class QSSAction : AnAction() {
24+
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT
1325

1426
override fun update(e: AnActionEvent) {
1527
// 默认菜单不可用,需要判断当前文件类型
1628
e.presentation.isEnabledAndVisible = false
17-
val project: Project = e.project ?: return
18-
val editor: Editor = e.getData(CommonDataKeys.EDITOR) ?: return
29+
val file: VirtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE) ?: return
30+
val ext: String = file.extension?.lowercase() ?: file.fileType.name.lowercase()
31+
if (!(ext == "qss" || ext == "css" || ext == "style")) {
32+
return
33+
}
34+
e.presentation.isEnabledAndVisible = true
1935
}
2036

2137
override fun actionPerformed(e: AnActionEvent) {
22-
38+
val editor: Editor = e.getData(CommonDataKeys.EDITOR) ?: return
39+
// 获取编辑器中的文本
40+
val text: String = editor.selectionModel.selectedText ?: editor.document.text
41+
if (text.isEmpty()) {
42+
return
43+
}
2344
}
2445
}

src/main/kotlin/irony/pycharm/qsseditor/QSSBundle.kt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/*
2+
* Copyright (c) 2024. Irony All Rights Reserved.
3+
* Project: QSSEditor
4+
* File: QSSBundle.kt
5+
* Date: 2024/5/18 上午1:02
6+
* Author: Irony
7+
* Email: 892768447@qq.com
8+
* Site: https://pyqt.site , https://pyqt5.com
9+
*/
10+
111
package irony.pycharm.qsseditor
212

313
import com.intellij.DynamicBundle
@@ -12,23 +22,27 @@ object QSSBundle {
1222
private val INSTANCE = DynamicBundle(QSSBundle::class.java, BUNDLE)
1323

1424
fun message(
15-
key: @PropertyKey(resourceBundle = BUNDLE) String,
16-
vararg params: Any
25+
key:
26+
@PropertyKey(resourceBundle = BUNDLE)
27+
String,
28+
vararg params: Any,
1729
): @Nls String {
1830
return INSTANCE.messageOrDefault(key, key, *params)
1931
}
2032

2133
fun message(
22-
key: @PropertyKey(resourceBundle = BUNDLE) String,
34+
key:
35+
@PropertyKey(resourceBundle = BUNDLE)
36+
String,
2337
@Nullable @Nls defaultValue: String,
24-
vararg params: Any
38+
vararg params: Any,
2539
): @Nls String {
2640
return INSTANCE.messageOrDefault(key, defaultValue, *params)
2741
}
2842

2943
fun lazyMessage(
3044
@PropertyKey(resourceBundle = BUNDLE) key: String,
31-
vararg params: Any
45+
vararg params: Any,
3246
): Supplier<@Nls String> {
3347
return INSTANCE.getLazyMessage(key, *params)
3448
}
Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,71 @@
1+
/*
2+
* Copyright (c) 2024. Irony All Rights Reserved.
3+
* Project: QSSEditor
4+
* File: QSSClient.kt
5+
* Date: 2024/5/18 上午1:02
6+
* Author: Irony
7+
* Email: 892768447@qq.com
8+
* Site: https://pyqt.site , https://pyqt5.com
9+
*/
10+
111
package irony.pycharm.qsseditor
212

313
import com.intellij.openapi.diagnostic.logger
414
import org.java_websocket.client.WebSocketClient
515
import org.java_websocket.handshake.ServerHandshake
616
import java.net.URI
717

8-
private val LOG = logger<QSSClient>()
18+
private val Log = logger<QSSClient>()
919

1020
class QSSClient(serverUri: URI?) : WebSocketClient(serverUri) {
11-
1221
override fun onOpen(handshakedata: ServerHandshake?) {
13-
LOG.debug("onOpen");
22+
Log.debug("onOpen")
1423
}
1524

1625
override fun onMessage(message: String?) {
17-
LOG.debug("onMessage: $message")
26+
Log.debug("onMessage: $message")
1827
}
1928

20-
override fun onClose(code: Int, reason: String?, remote: Boolean) {
21-
LOG.debug("onClose: code=$code, remote=$remote, reason=$reason")
29+
override fun onClose(
30+
code: Int,
31+
reason: String?,
32+
remote: Boolean,
33+
) {
34+
Log.debug("onClose: code=$code, remote=$remote, reason=$reason")
2235
}
2336

2437
override fun onError(e: Exception?) {
25-
LOG.error("onError", e)
38+
Log.error("onError", e)
2639
}
2740

2841
companion object {
29-
private var client : QSSClient? = null
42+
private var client: QSSClient? = null
3043

31-
fun connect(host: String, port: Int) {
44+
fun connect(
45+
host: String,
46+
port: Int,
47+
) {
3248
disconnect()
3349
if (client == null) {
34-
LOG.debug("connect to node: ws://$host:$port")
50+
Log.debug("connect to node: ws://$host:$port")
3551
client = QSSClient(URI("ws://$host:$port"))
3652
client!!.connect()
3753
}
3854
}
3955

4056
fun reconnect() {
41-
LOG.debug("do reconnect")
57+
Log.debug("do reconnect")
4258
client?.reconnect()
4359
}
4460

4561
fun disconnect() {
46-
LOG.debug("do disconnect")
62+
Log.debug("do disconnect")
4763
client?.close()
4864
client = null
4965
}
66+
67+
fun send(message: String) {
68+
client?.send(message)
69+
}
5070
}
51-
}
71+
}
Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/*
2+
* Copyright (c) 2024. Irony All Rights Reserved.
3+
* Project: QSSEditor
4+
* File: QSSConfig.kt
5+
* Date: 2024/5/18 上午1:02
6+
* Author: Irony
7+
* Email: 892768447@qq.com
8+
* Site: https://pyqt.site , https://pyqt5.com
9+
*/
10+
111
package irony.pycharm.qsseditor
212

313
import com.intellij.openapi.diagnostic.logger
@@ -9,24 +19,25 @@ import com.intellij.ui.dsl.builder.bindSelected
919
import com.intellij.ui.dsl.builder.bindText
1020
import com.intellij.ui.dsl.builder.panel
1121

12-
private val LOG = logger<QSSConfig>()
13-
14-
class QSSConfig : BoundSearchableConfigurable(
15-
"QSS Editor",
16-
QSSBundle.message("setting.topic.title", "QSS Editor Configurable")
17-
), NoScroll {
22+
private val Log = logger<QSSConfig>()
1823

19-
override fun createPanel() : DialogPanel {
24+
class QSSConfig :
25+
BoundSearchableConfigurable(
26+
"QSS Editor",
27+
QSSBundle.message("setting.topic.title", "QSS Editor Configurable"),
28+
),
29+
NoScroll {
30+
override fun createPanel(): DialogPanel {
2031
return panel {
2132
row(QSSBundle.message("setting.host.title", "Host")) {
2233
textField().bindText(QSSState.instance::host)
2334
}
2435
row(QSSBundle.message("setting.port.title", "Port")) {
2536
intTextField(IntRange(1000, 65535)).bindIntText(QSSState.instance::port)
2637
}
27-
row(QSSBundle.message("setting.auto.title","Auto Apply")) {
38+
row(QSSBundle.message("setting.auto.title", "Auto Apply")) {
2839
checkBox("").bindSelected(QSSState.instance::auto)
2940
}
3041
}
3142
}
32-
}
43+
}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1+
/*
2+
* Copyright (c) 2024. Irony All Rights Reserved.
3+
* Project: QSSEditor
4+
* File: QSSStartup.kt
5+
* Date: 2024/5/18 上午1:02
6+
* Author: Irony
7+
* Email: 892768447@qq.com
8+
* Site: https://pyqt.site , https://pyqt5.com
9+
*/
10+
111
package irony.pycharm.qsseditor
212

313
import com.intellij.openapi.diagnostic.logger
414
import com.intellij.openapi.project.Project
515
import com.intellij.openapi.startup.StartupActivity
616

7-
private val LOG = logger<QSSStartup>()
17+
private val Log = logger<QSSStartup>()
818

919
internal class QSSStartup : StartupActivity {
10-
1120
override fun runActivity(project: Project) {
1221
// 启动客户端连接
13-
LOG.debug("project[${project.name}] opened")
22+
Log.debug("project[${project.name}] opened")
1423
QSSClient.connect(QSSState.instance.host, QSSState.instance.port)
1524
}
16-
}
25+
}

src/main/kotlin/irony/pycharm/qsseditor/QSSState.kt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
/*
2+
* Copyright (c) 2024. Irony All Rights Reserved.
3+
* Project: QSSEditor
4+
* File: QSSState.kt
5+
* Date: 2024/5/18 上午1:02
6+
* Author: Irony
7+
* Email: 892768447@qq.com
8+
* Site: https://pyqt.site , https://pyqt5.com
9+
*/
10+
111
package irony.pycharm.qsseditor
212

313
import com.intellij.openapi.application.ApplicationManager
4-
import com.intellij.openapi.components.*
5-
import com.intellij.util.xmlb.XmlSerializerUtil
14+
import com.intellij.openapi.components.BaseState
15+
import com.intellij.openapi.components.RoamingType
16+
import com.intellij.openapi.components.SimplePersistentStateComponent
17+
import com.intellij.openapi.components.State
18+
import com.intellij.openapi.components.Storage
619

720
class ConfigState : BaseState() {
821
var auto: Boolean by property(true)
@@ -11,8 +24,7 @@ class ConfigState : BaseState() {
1124
}
1225

1326
@State(name = "irony.pycharm.qsseditor.QSSState", storages = [Storage("QSSEditorSetting.xml", roamingType = RoamingType.DEFAULT)])
14-
internal class QSSState() : SimplePersistentStateComponent<ConfigState>(ConfigState()) {
15-
27+
internal class QSSState : SimplePersistentStateComponent<ConfigState>(ConfigState()) {
1628
var auto: Boolean
1729
get() = state.auto
1830
set(value) {
@@ -33,8 +45,9 @@ internal class QSSState() : SimplePersistentStateComponent<ConfigState>(ConfigSt
3345

3446
companion object {
3547
val instance: QSSState
36-
get() = ApplicationManager.getApplication().getService(
37-
QSSState::class.java
38-
)
48+
get() =
49+
ApplicationManager.getApplication().getService(
50+
QSSState::class.java,
51+
)
3952
}
4053
}

src/main/resources/META-INF/plugin.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
id="irony.pycharm.qsseditor.QSSConfig"
3636
displayName="QSS Editor"/>
3737
</extensions>
38-
<actions>
39-
<action id="ApplyAction" class="irony.pycharm.qsseditor.QSSAction">
38+
<actions resource-bundle="messages.QSSBundle">
39+
<action id="apply" class="irony.pycharm.qsseditor.QSSAction" text="Apply Style">
4040
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
4141
</action>
4242
</actions>

src/main/resources/messages/QSSBundle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
action.apply.title=Apply Style
1+
action.apply.text=Apply Style
22
setting.topic.title=QSS Editor Configurable
33
setting.auto.title=Auto Apply
44
setting.host.title=Host

0 commit comments

Comments
 (0)