Skip to content

Commit eb3be10

Browse files
committedJan 8, 2025
refactor(debugger): extract ShireDebugProcess into separate file #183
Moved ShireDebugProcess and related classes from ShireDebugRunner.kt to a new file ShireDebugProcess.kt for better code organization and maintainability. Removed unused imports and cleaned up the code structure.
1 parent 184e691 commit eb3be10

File tree

3 files changed

+168
-164
lines changed

3 files changed

+168
-164
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package com.phodal.shirelang.debugger
2+
3+
import com.intellij.execution.process.ProcessEvent
4+
import com.intellij.execution.process.ProcessListener
5+
import com.intellij.execution.runners.ExecutionEnvironment
6+
import com.intellij.execution.ui.ExecutionConsole
7+
import com.intellij.execution.ui.RunnerLayoutUi
8+
import com.intellij.execution.ui.layout.PlaceInGrid
9+
import com.intellij.icons.AllIcons
10+
import com.intellij.openapi.Disposable
11+
import com.intellij.openapi.application.ApplicationManager
12+
import com.intellij.openapi.util.Key
13+
import com.intellij.ui.content.Content
14+
import com.intellij.util.Alarm
15+
import com.intellij.xdebugger.*
16+
import com.intellij.xdebugger.breakpoints.XBreakpointHandler
17+
import com.intellij.xdebugger.breakpoints.XBreakpointProperties
18+
import com.intellij.xdebugger.breakpoints.XLineBreakpoint
19+
import com.intellij.xdebugger.evaluation.XDebuggerEditorsProvider
20+
import com.intellij.xdebugger.frame.XSuspendContext
21+
import com.intellij.xdebugger.ui.XDebugTabLayouter
22+
import com.phodal.shirelang.run.ShireConfiguration
23+
import com.phodal.shirelang.run.ShireConsoleView
24+
import com.phodal.shirelang.run.ShireRunListener
25+
26+
class ShireDebugProcess(private val session: XDebugSession, private val environment: ExecutionEnvironment) :
27+
XDebugProcess(session), Disposable {
28+
private val connection = ApplicationManager.getApplication().messageBus.connect(this)
29+
private val myRequestsScheduler: Alarm
30+
31+
init {
32+
session.addSessionListener(object : XDebugSessionListener {
33+
override fun stackFrameChanged() {
34+
35+
}
36+
37+
override fun sessionPaused() {
38+
39+
}
40+
})
41+
42+
session.positionReached(ShireSuspendContext(this@ShireDebugProcess, session.project))
43+
connection.subscribe(ShireRunListener.TOPIC, object : ShireRunListener {
44+
override fun runFinish(
45+
allOutput: String,
46+
llmOutput: String,
47+
event: ProcessEvent,
48+
scriptPath: String,
49+
consoleView: ShireConsoleView?,
50+
) {
51+
this@ShireDebugProcess.stop()
52+
}
53+
})
54+
55+
myRequestsScheduler = Alarm(Alarm.ThreadToUse.POOLED_THREAD, this)
56+
}
57+
58+
private val debuggableConfiguration: ShireConfiguration get() = session.runProfile as ShireConfiguration
59+
60+
private val breakpointHandlers = arrayOf<XBreakpointHandler<*>>(ShireBreakpointHandler(this))
61+
override fun getBreakpointHandlers(): Array<XBreakpointHandler<*>> = breakpointHandlers
62+
63+
override fun createTabLayouter(): XDebugTabLayouter = ShireDebugTabLayouter()
64+
65+
override fun resume(context: XSuspendContext?) {
66+
67+
}
68+
69+
fun start() {
70+
environment.executor.let { executor ->
71+
debuggableConfiguration
72+
.getState(executor, environment)
73+
.execute(executor, environment.runner)!!
74+
}
75+
76+
processHandler.addProcessListener(object : ProcessListener {
77+
override fun onTextAvailable(event: ProcessEvent, outputType: Key<*>) {
78+
super.onTextAvailable(event, outputType)
79+
}
80+
81+
override fun processTerminated(event: ProcessEvent) {
82+
super.processTerminated(event)
83+
stop()
84+
}
85+
})
86+
}
87+
88+
override fun startStepOut(context: XSuspendContext?) {
89+
90+
}
91+
92+
override fun startStepInto(context: XSuspendContext?) {
93+
94+
}
95+
96+
override fun startStepOver(context: XSuspendContext?) {
97+
98+
}
99+
100+
override fun startForceStepInto(context: XSuspendContext?) {
101+
102+
}
103+
104+
override fun startPausing() {
105+
106+
}
107+
108+
override fun stop() {
109+
connection.disconnect()
110+
processHandler.destroyProcess()
111+
session.stop()
112+
}
113+
114+
override fun dispose() {
115+
connection.disconnect()
116+
}
117+
118+
fun addBreakpoint(breakpoint: XLineBreakpoint<ShireBpProperties>) {
119+
120+
}
121+
122+
fun removeBreakpoint(breakpoint: XLineBreakpoint<ShireBpProperties>) {
123+
124+
}
125+
126+
override fun getEditorsProvider(): XDebuggerEditorsProvider {
127+
return ShireDebuggerEditorsProvider()
128+
}
129+
}
130+
131+
class ShireDebugTabLayouter : XDebugTabLayouter() {
132+
override fun registerConsoleContent(ui: RunnerLayoutUi, console: ExecutionConsole): Content {
133+
val content = ui
134+
.createContent(
135+
"DebuggedConsoleContent", console.component, "Shire Debugged Console",
136+
AllIcons.Debugger.Console, console.preferredFocusableComponent
137+
)
138+
139+
content.isCloseable = false
140+
ui.addContent(content, 1, PlaceInGrid.bottom, false)
141+
return content
142+
}
143+
}
144+
145+
146+
class ShireBreakpointHandler(val process: ShireDebugProcess) :
147+
XBreakpointHandler<XLineBreakpoint<ShireBpProperties>>(ShireLineBreakpointType::class.java) {
148+
override fun registerBreakpoint(breakpoint: XLineBreakpoint<ShireBpProperties>) {
149+
process.addBreakpoint(breakpoint)
150+
}
151+
152+
override fun unregisterBreakpoint(breakpoint: XLineBreakpoint<ShireBpProperties>, temporary: Boolean) {
153+
process.removeBreakpoint(breakpoint)
154+
}
155+
}
156+
157+
class ShireBpProperties : XBreakpointProperties<ShireBpProperties>() {
158+
override fun getState(): ShireBpProperties = this
159+
override fun loadState(state: ShireBpProperties) {}
160+
}
161+
162+
163+
val RUNNER_ID: String = "ShireProgramRunner"

‎shirelang/src/main/kotlin/com/phodal/shirelang/debugger/ShireDebugRunner.kt

+5-162
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,14 @@ import com.intellij.execution.configurations.RunProfile
44
import com.intellij.execution.configurations.RunProfileState
55
import com.intellij.execution.configurations.RunnerSettings
66
import com.intellij.execution.executors.DefaultDebugExecutor
7-
import com.intellij.execution.process.ProcessEvent
8-
import com.intellij.execution.process.ProcessListener
97
import com.intellij.execution.runners.ExecutionEnvironment
108
import com.intellij.execution.runners.GenericProgramRunner
11-
import com.intellij.execution.ui.ExecutionConsole
129
import com.intellij.execution.ui.RunContentDescriptor
13-
import com.intellij.execution.ui.RunnerLayoutUi
14-
import com.intellij.execution.ui.layout.PlaceInGrid
15-
import com.intellij.icons.AllIcons
16-
import com.intellij.openapi.Disposable
17-
import com.intellij.openapi.application.ApplicationManager
18-
import com.intellij.openapi.util.Key
19-
import com.intellij.ui.content.Content
20-
import com.intellij.util.Alarm
21-
import com.intellij.xdebugger.*
22-
import com.intellij.xdebugger.breakpoints.XBreakpointHandler
23-
import com.intellij.xdebugger.breakpoints.XBreakpointProperties
24-
import com.intellij.xdebugger.breakpoints.XLineBreakpoint
25-
import com.intellij.xdebugger.evaluation.XDebuggerEditorsProvider
26-
import com.intellij.xdebugger.frame.XSuspendContext
27-
import com.intellij.xdebugger.ui.XDebugTabLayouter
10+
import com.intellij.xdebugger.XDebugProcess
11+
import com.intellij.xdebugger.XDebugProcessStarter
12+
import com.intellij.xdebugger.XDebugSession
13+
import com.intellij.xdebugger.XDebuggerManager
2814
import com.phodal.shirelang.run.ShireConfiguration
29-
import com.phodal.shirelang.run.ShireConsoleView
30-
import com.phodal.shirelang.run.ShireRunListener
3115

3216
/// refs to: https://github.com/KronicDeth/intellij-elixir/pull/643/files#diff-b1ba5c87ca6f66a455e4c1539cb2d99a62722d067a3d9e8043b290426cea5470
3317
class ShireDebugRunner : GenericProgramRunner<RunnerSettings>() {
@@ -47,145 +31,4 @@ class ShireDebugRunner : GenericProgramRunner<RunnerSettings>() {
4731
}
4832
}).runContentDescriptor
4933
}
50-
}
51-
52-
class ShireDebugProcess(private val session: XDebugSession, private val environment: ExecutionEnvironment) :
53-
XDebugProcess(session), Disposable {
54-
private val connection = ApplicationManager.getApplication().messageBus.connect(this)
55-
private val myRequestsScheduler: Alarm
56-
57-
init {
58-
session.addSessionListener(object : XDebugSessionListener {
59-
override fun stackFrameChanged() {
60-
61-
}
62-
63-
override fun sessionPaused() {
64-
65-
}
66-
})
67-
68-
session.positionReached(ShireSuspendContext(this@ShireDebugProcess, session.project))
69-
connection.subscribe(ShireRunListener.TOPIC, object : ShireRunListener {
70-
override fun runFinish(
71-
allOutput: String,
72-
llmOutput: String,
73-
event: ProcessEvent,
74-
scriptPath: String,
75-
consoleView: ShireConsoleView?,
76-
) {
77-
this@ShireDebugProcess.stop()
78-
}
79-
})
80-
81-
myRequestsScheduler = Alarm(Alarm.ThreadToUse.POOLED_THREAD, this)
82-
}
83-
84-
// override fun createConsole(): ExecutionConsole = debuggedExecutionResult.executionConsole
85-
86-
private val debuggableConfiguration: ShireConfiguration get() = session.runProfile as ShireConfiguration
87-
88-
private val breakpointHandlers = arrayOf<XBreakpointHandler<*>>(ShireBreakpointHandler(this))
89-
override fun getBreakpointHandlers(): Array<XBreakpointHandler<*>> = breakpointHandlers
90-
91-
override fun createTabLayouter(): XDebugTabLayouter = ShireDebugTabLayouter()
92-
93-
override fun resume(context: XSuspendContext?) {
94-
95-
}
96-
97-
fun start() {
98-
environment.executor.let { executor ->
99-
debuggableConfiguration
100-
.getState(executor, environment)
101-
.execute(executor, environment.runner)!!
102-
}
103-
104-
processHandler.addProcessListener(object : ProcessListener {
105-
override fun onTextAvailable(event: ProcessEvent, outputType: Key<*>) {
106-
super.onTextAvailable(event, outputType)
107-
}
108-
109-
override fun processTerminated(event: ProcessEvent) {
110-
super.processTerminated(event)
111-
stop()
112-
}
113-
})
114-
}
115-
116-
override fun startStepOut(context: XSuspendContext?) {
117-
118-
}
119-
120-
override fun startStepInto(context: XSuspendContext?) {
121-
122-
}
123-
124-
override fun startStepOver(context: XSuspendContext?) {
125-
126-
}
127-
128-
override fun startForceStepInto(context: XSuspendContext?) {
129-
130-
}
131-
132-
override fun startPausing() {
133-
134-
}
135-
136-
override fun stop() {
137-
connection.disconnect()
138-
processHandler.destroyProcess()
139-
session.stop()
140-
}
141-
142-
override fun dispose() {
143-
connection.disconnect()
144-
}
145-
146-
fun addBreakpoint(breakpoint: XLineBreakpoint<ShireBpProperties>) {
147-
148-
}
149-
150-
fun removeBreakpoint(breakpoint: XLineBreakpoint<ShireBpProperties>) {
151-
152-
}
153-
154-
override fun getEditorsProvider(): XDebuggerEditorsProvider {
155-
return ShireDebuggerEditorsProvider()
156-
}
157-
}
158-
159-
class ShireDebugTabLayouter : XDebugTabLayouter() {
160-
override fun registerConsoleContent(ui: RunnerLayoutUi, console: ExecutionConsole): Content {
161-
val content = ui
162-
.createContent(
163-
"DebuggedConsoleContent", console.component, "Shire Debugged Console",
164-
AllIcons.Debugger.Console, console.preferredFocusableComponent
165-
)
166-
167-
content.isCloseable = false
168-
ui.addContent(content, 1, PlaceInGrid.bottom, false)
169-
return content
170-
}
171-
}
172-
173-
174-
class ShireBreakpointHandler(val process: ShireDebugProcess) :
175-
XBreakpointHandler<XLineBreakpoint<ShireBpProperties>>(ShireLineBreakpointType::class.java) {
176-
override fun registerBreakpoint(breakpoint: XLineBreakpoint<ShireBpProperties>) {
177-
process.addBreakpoint(breakpoint)
178-
}
179-
180-
override fun unregisterBreakpoint(breakpoint: XLineBreakpoint<ShireBpProperties>, temporary: Boolean) {
181-
process.removeBreakpoint(breakpoint)
182-
}
183-
}
184-
185-
class ShireBpProperties : XBreakpointProperties<ShireBpProperties>() {
186-
override fun getState(): ShireBpProperties = this
187-
override fun loadState(state: ShireBpProperties) {}
188-
}
189-
190-
191-
val RUNNER_ID: String = "ShireProgramRunner"
34+
}

‎shirelang/src/main/kotlin/com/phodal/shirelang/run/ShireProgramRunner.kt

-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ class ShireProgramRunner : GenericProgramRunner<RunnerSettings>(), Disposable {
5151
) {
5252
environment.project.getService(ShireProcessProcessor::class.java)
5353
.process(allOutput, event, scriptPath, consoleView)
54-
55-
// if need, maybe we need a toggle to show the run content
5654
}
5755
})
5856

0 commit comments

Comments
 (0)
Please sign in to comment.