Skip to content

Commit a87b216

Browse files
committed
feat: session refactoring
1 parent 3ae58cc commit a87b216

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.xepozz.call.base
2+
3+
import com.github.xepozz.call.base.inlay.Session
4+
import com.intellij.openapi.components.Service
5+
import com.intellij.openapi.project.Project
6+
import java.util.concurrent.ConcurrentHashMap
7+
8+
@Service(Service.Level.PROJECT)
9+
class SessionStorage(val project: Project) {
10+
private val sessions = ConcurrentHashMap<String, Session>()
11+
12+
fun getSession(editorId: String): Session? = sessions[editorId]
13+
fun putSession(editorId: String, session: Session) { sessions[editorId] = session }
14+
fun remove(editorId: String): Session? = sessions.remove(editorId)
15+
16+
companion object {
17+
fun getInstance(project: Project): SessionStorage = project.getService(SessionStorage::class.java)
18+
}
19+
}

src/main/kotlin/com/github/xepozz/call/base/inlay/ExecutionInlayProvider.kt

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.xepozz.call.base.inlay
22

3+
import com.github.xepozz.call.base.SessionStorage
34
import com.github.xepozz.call.base.api.FeatureGenerator
45
import com.github.xepozz.call.base.api.FeatureMatch
56
import com.github.xepozz.call.base.api.LanguageTextExtractor
@@ -30,15 +31,11 @@ import com.intellij.psi.PsiFile
3031
import com.intellij.ui.dsl.builder.panel
3132
import java.awt.BorderLayout
3233
import java.awt.Cursor
33-
import java.util.concurrent.ConcurrentHashMap
3434
import javax.swing.Icon
3535
import javax.swing.JPanel
3636

3737
@Suppress("UnstableApiUsage")
3838
class ExecutionInlayProvider : InlayHintsProvider<NoSettings> {
39-
// key: editorId + featureId + line
40-
private val sessions = ConcurrentHashMap<String, Session>()
41-
4239
override val key: SettingsKey<NoSettings> = SettingsKey("call.implementation.inlay")
4340
override val name: String = "Call (Unified)"
4441
override val previewText: String = "// shell: echo hello\n// https://api.example.com"
@@ -54,6 +51,7 @@ class ExecutionInlayProvider : InlayHintsProvider<NoSettings> {
5451
settings: NoSettings,
5552
sink: InlayHintsSink
5653
) = object : FactoryInlayHintsCollector(editor) {
54+
val sessionStorage = SessionStorage.getInstance(file.project)
5755

5856
// Pre-compute matches for the whole file once
5957
private val matchesByElement: Map<PsiElement, List<FeatureMatch>> = computeMatches(file)
@@ -62,7 +60,7 @@ class ExecutionInlayProvider : InlayHintsProvider<NoSettings> {
6260
val matches = matchesByElement[element] ?: return true
6361
if (matches.isEmpty()) return true
6462

65-
val project = editor.project ?: return true
63+
val project = file.project
6664

6765
matches.forEach { m ->
6866
val offset = m.originalRange.startOffset
@@ -110,7 +108,7 @@ class ExecutionInlayProvider : InlayHintsProvider<NoSettings> {
110108
val line = editor.document.getLineNumber(start)
111109
val lineEndOffset = editor.document.getLineEndOffset(line)
112110
val key = makeKey(editor, match.featureId, line)
113-
val session = sessions[key]
111+
val session = sessionStorage.getSession(key)
114112

115113
val parts = mutableListOf<InlayPresentation>()
116114

@@ -194,7 +192,7 @@ class ExecutionInlayProvider : InlayHintsProvider<NoSettings> {
194192
lineEndOffset: Int,
195193
) {
196194
// Ensure wrapper exists and mounted
197-
var current = sessions[key]
195+
var current = sessionStorage.getSession(key)
198196
val wrapper = feature.createWrapper()
199197

200198
if (current?.container == null) {
@@ -204,13 +202,10 @@ class ExecutionInlayProvider : InlayHintsProvider<NoSettings> {
204202
mountWrapperIntoContainer(container, wrapper)
205203

206204
current = Session(container, wrapper)
207-
sessions[key] = current
208205
} catch (_: Throwable) {
209-
// If embedding fails, still execute without container
210206
current = Session(null, wrapper)
211-
sessions[key] = current
212207
}
213-
current.state = ExecutionState.RUNNING
208+
sessionStorage.putSession(key, current)
214209
} else {
215210
// Replace previous wrapper in the existing container
216211
val container = current.container
@@ -220,20 +215,20 @@ class ExecutionInlayProvider : InlayHintsProvider<NoSettings> {
220215
}
221216
mountWrapperIntoContainer(container, wrapper)
222217
current.wrapper = wrapper
223-
current.state = ExecutionState.RUNNING
224218
}
219+
current.state = ExecutionState.RUNNING
225220

226221
refreshInlays(editor)
227222

228223
// Execute and capture process lifecycle
229-
val sess = sessions[key] ?: return
224+
val session = current
230225
feature.execute(match, wrapper, project) { processHandler ->
231-
sess.processHandler = processHandler
226+
session.processHandler = processHandler
232227

233228
processHandler?.addProcessListener(object : ProcessListener {
234229
override fun processTerminated(event: ProcessEvent) {
235-
sess.state = ExecutionState.FINISHED
236-
sess.processHandler = null
230+
session.state = ExecutionState.FINISHED
231+
session.processHandler = null
237232
refreshInlays(editor)
238233
}
239234
})
@@ -249,14 +244,14 @@ class ExecutionInlayProvider : InlayHintsProvider<NoSettings> {
249244
}
250245

251246
private fun stop(key: String) {
252-
val session = sessions[key] ?: return
247+
val session = sessionStorage.getSession(key) ?: return
253248
session.processHandler?.destroyProcess()
254249
session.processHandler = null
255250
session.state = ExecutionState.FINISHED
256251
}
257252

258253
private fun delete(key: String) {
259-
val session = sessions.remove(key) ?: return
254+
val session = sessionStorage.remove(key) ?: return
260255
try { session.processHandler?.destroyProcess() } catch (_: Throwable) { }
261256
session.processHandler = null
262257
invokeLater {

src/main/kotlin/com/github/xepozz/call/base/inlay/Session.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.github.xepozz.call.base.handlers.ExecutionState
55
import com.intellij.execution.process.ProcessHandler
66
import javax.swing.JPanel
77

8-
internal data class Session(
8+
data class Session(
99
val container: JPanel?,
1010
var wrapper: Wrapper?,
1111
var state: ExecutionState = ExecutionState.IDLE,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
<idea-plugin>
33
<id>com.github.xepozz.call</id>
44
<name>Call</name>
5-
<vendor>xepozz</vendor>
5+
<vendor email="[email protected]" url="https://github.com/xepozz">Dmitrii Derepko (@xepozz)</vendor>
66

77
<depends>com.intellij.modules.platform</depends>
8-
<!-- Optional Kotlin dependency: used only to register Kotlin-specific extensions in kotlin.xml -->
98
<depends optional="true" config-file="language-kotlin.xml">org.jetbrains.kotlin</depends>
109
<depends optional="true" config-file="language-xml.xml">com.intellij.modules.xml</depends>
1110

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
projectService=Project service: {0}
2-
randomLabel=The random number is: {0}
3-
shuffle=Shuffle

0 commit comments

Comments
 (0)