Skip to content

Commit 00316af

Browse files
committed
✨ feat(client): 增加Websocket支持
1 parent 34da1af commit 00316af

File tree

8 files changed

+109
-42
lines changed

8 files changed

+109
-42
lines changed

build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ repositories {
1414
mavenCentral()
1515
}
1616

17+
dependencies {
18+
implementation("org.java-websocket:Java-WebSocket:1.5.6")
19+
}
20+
1721
// Configure Gradle IntelliJ Plugin
1822
// Read more: https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html
1923
intellij {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.openapi.project.Project
1010
class QSSAction : AnAction(QSSBundle.message("action.apply.title", "Apply Style")) {
1111

1212
override fun update(e: AnActionEvent) {
13+
// 默认菜单不可用,需要判断当前文件类型
1314
e.presentation.isEnabledAndVisible = false
1415
val project: Project = e.project ?: return
1516
val editor: Editor = e.getData(CommonDataKeys.EDITOR) ?: return
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package irony.pycharm.qsseditor
2+
3+
import com.intellij.openapi.application.ApplicationManager
4+
import org.java_websocket.client.WebSocketClient
5+
import org.java_websocket.handshake.ServerHandshake
6+
import java.net.URI
7+
8+
class QSSClient(serverUri: URI?) : WebSocketClient(serverUri) {
9+
10+
override fun onOpen(handshakedata: ServerHandshake?) {
11+
println("onOpen");
12+
}
13+
14+
override fun onMessage(message: String?) {
15+
println("onMessage: $message")
16+
}
17+
18+
override fun onClose(code: Int, reason: String?, remote: Boolean) {
19+
println("onClose: code=$code, remote=$remote, reason=$reason")
20+
}
21+
22+
override fun onError(ex: Exception?) {
23+
System.err.println("onError: $ex")
24+
}
25+
26+
companion object {
27+
private var client : QSSClient? = null
28+
29+
fun connect(host: String, port: Int) {
30+
disconnect()
31+
if (client == null) {
32+
client = QSSClient(URI("ws://$host:$port"))
33+
client!!.connect()
34+
}
35+
}
36+
37+
fun reconnect() {
38+
client?.reconnect()
39+
}
40+
41+
fun disconnect() {
42+
client?.close()
43+
client = null
44+
}
45+
}
46+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ class QSSConfigurable : BoundSearchableConfigurable(
1616
override fun createPanel() : DialogPanel {
1717
return panel {
1818
row(QSSBundle.message("setting.host.title", "Host")) {
19-
textField().bindText(QSSService.instance::host)
19+
textField().bindText(QSSState.instance::host)
2020
}
2121
row(QSSBundle.message("setting.port.title", "Port")) {
22-
textField().bindIntText(QSSService.instance::port)
22+
intTextField(IntRange(1000, 65535)).bindIntText(QSSState.instance::port)
2323
}
2424
row(QSSBundle.message("setting.auto.title","Auto Apply")) {
25-
checkBox("").bindSelected(QSSService.instance::auto)
25+
checkBox("").bindSelected(QSSState.instance::auto)
2626
}
2727
}
2828
}

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

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package irony.pycharm.qsseditor
2+
3+
import com.intellij.openapi.project.Project
4+
import com.intellij.openapi.startup.StartupActivity
5+
6+
internal class QSSStartup : StartupActivity {
7+
8+
override fun runActivity(project: Project) {
9+
// 启动客户端连接
10+
QSSClient.connect(QSSState.instance.host, QSSState.instance.port)
11+
}
12+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package irony.pycharm.qsseditor
2+
3+
import com.intellij.openapi.application.ApplicationManager
4+
import com.intellij.openapi.components.*
5+
import com.intellij.util.xmlb.XmlSerializerUtil
6+
7+
class ConfigState : BaseState() {
8+
var auto: Boolean by property(true)
9+
var host: String? by string("localhost")
10+
var port: Int by property(61052)
11+
}
12+
13+
@State(name = "irony.pycharm.qsseditor.QSSState", storages = [Storage("QSSEditorSetting.xml", roamingType = RoamingType.DEFAULT)])
14+
internal class QSSState() : SimplePersistentStateComponent<ConfigState>(ConfigState()) {
15+
16+
var auto: Boolean
17+
get() = state.auto
18+
set(value) {
19+
state.auto = value
20+
}
21+
22+
var host: String
23+
get() = state.host ?: "localhost"
24+
set(value) {
25+
state.host = value
26+
}
27+
28+
var port: Int
29+
get() = state.port
30+
set(value) {
31+
state.port = value
32+
}
33+
34+
companion object {
35+
val instance: QSSState
36+
get() = ApplicationManager.getApplication().getService(
37+
QSSState::class.java
38+
)
39+
}
40+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
<!-- Extension points defined by the plugin.
2626
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
2727
<extensions defaultExtensionNs="com.intellij">
28+
<backgroundPostStartupActivity implementation="irony.pycharm.qsseditor.QSSStartup"/>
2829
<applicationService
29-
serviceImplementation="irony.pycharm.qsseditor.QSSService"/>
30+
serviceImplementation="irony.pycharm.qsseditor.QSSState"/>
3031
<applicationConfigurable
3132
parentId="language"
3233
groupWeight="-500"
3334
instance="irony.pycharm.qsseditor.QSSConfigurable"
3435
id="irony.pycharm.qsseditor.QSSConfigurable"
35-
displayName="QSS"/>
36+
displayName="QSS Editor"/>
3637
</extensions>
3738
<actions>
3839
<action id="ApplyAction" class="irony.pycharm.qsseditor.QSSAction">

0 commit comments

Comments
 (0)