Skip to content

Commit 8e79b3a

Browse files
committed
Fix debugger issue
1 parent 5bc9d44 commit 8e79b3a

File tree

10 files changed

+288
-62
lines changed

10 files changed

+288
-62
lines changed

src/main/java/com/tang/intellij/lua/debugger/emmy/EmmyDebugConfigurationType.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ class EmmyDebugConfiguration(project: Project, factory: EmmyDebuggerConfiguratio
8989
var port = 9966
9090
var winArch = EmmyWinArch.X64
9191
var pipeName = "emmy"
92+
93+
/**
94+
* Source roots for path resolution during debugging.
95+
*/
96+
var sourceRoots: MutableList<String> = mutableListOf()
9297

9398
override fun getConfigurationEditor(): SettingsEditor<out RunConfiguration> {
9499
val group = SettingsEditorGroup<EmmyDebugConfiguration>()
@@ -111,6 +116,7 @@ class EmmyDebugConfiguration(project: Project, factory: EmmyDebuggerConfiguratio
111116
JDOMExternalizerUtil.writeField(element, "PORT", port.toString())
112117
JDOMExternalizerUtil.writeField(element, "PIPE", pipeName)
113118
JDOMExternalizerUtil.writeField(element, "WIN_ARCH", winArch.ordinal.toString())
119+
JDOMExternalizerUtil.writeField(element, "SOURCE_ROOTS", sourceRoots.joinToString(";"))
114120
}
115121

116122
override fun readExternal(element: Element) {
@@ -132,5 +138,8 @@ class EmmyDebugConfiguration(project: Project, factory: EmmyDebuggerConfiguratio
132138
val i = value.toInt()
133139
winArch = EmmyWinArch.values().find { it.ordinal == i } ?: EmmyWinArch.X64
134140
}
141+
JDOMExternalizerUtil.readField(element, "SOURCE_ROOTS")?.let {
142+
sourceRoots = it.split(";").filter { path -> path.isNotBlank() }.toMutableList()
143+
}
135144
}
136145
}

src/main/java/com/tang/intellij/lua/debugger/emmy/EmmyDebugProcess.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class EmmyDebugProcess(session: XDebugSession) : LuaDebugProcess(session) {
6161

6262
// State
6363
private var isConnected = false
64+
private val isServerMode: Boolean
65+
get() = configuration.type == EmmyDebugTransportType.TCP_SERVER
66+
67+
/**
68+
* Get source roots from configuration for path resolution
69+
*/
70+
fun getSourceRoots(): List<String> = configuration.sourceRoots
6471

6572
/**
6673
* Evaluation handler interface
@@ -145,8 +152,18 @@ class EmmyDebugProcess(session: XDebugSession) : LuaDebugProcess(session) {
145152
if (isConnected) {
146153
isConnected = false
147154
println("Disconnected from debugger", LogConsoleType.NORMAL, ConsoleViewContentType.SYSTEM_OUTPUT)
148-
stop()
149-
session.stop()
155+
156+
// In server mode, keep listening for new connections instead of stopping
157+
if (isServerMode) {
158+
println("Server mode: waiting for new connection...", LogConsoleType.NORMAL, ConsoleViewContentType.SYSTEM_OUTPUT)
159+
// Clear evaluation handlers for clean state
160+
evalHandlers.clear()
161+
// ServerTransport will automatically wait for new connection
162+
} else {
163+
// Client mode: stop the session
164+
stop()
165+
session.stop()
166+
}
150167
}
151168
}
152169

src/main/java/com/tang/intellij/lua/debugger/emmy/EmmyDebugSettingsPanel.form

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.tang.intellij.lua.debugger.emmy.EmmyDebugSettingsPanel">
3-
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="9" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="11" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
66
<xy x="20" y="20" width="500" height="400"/>
@@ -32,7 +32,7 @@
3232
</component>
3333
<vspacer id="7dfdb">
3434
<constraints>
35-
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
35+
<grid row="10" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
3636
</constraints>
3737
</vspacer>
3838
<component id="3bf10" class="javax.swing.JTextField" binding="tcpHostInput">
@@ -75,72 +75,90 @@
7575
</constraints>
7676
<properties/>
7777
</component>
78+
<component id="source1" class="javax.swing.JLabel" binding="sourceRootsLabel">
79+
<constraints>
80+
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
81+
</constraints>
82+
<properties>
83+
<text value="Source Roots:"/>
84+
</properties>
85+
</component>
86+
<grid id="source2" binding="sourceRootsPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
87+
<constraints>
88+
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
89+
<preferred-size width="150" height="100"/>
90+
</grid>
91+
</constraints>
92+
<properties/>
93+
<border type="none"/>
94+
<children/>
95+
</grid>
96+
<grid id="d0e40" binding="winArchPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
97+
<margin top="0" left="0" bottom="0" right="0"/>
98+
<constraints>
99+
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
100+
</constraints>
101+
<properties/>
102+
<border type="none"/>
103+
<children>
104+
<component id="3495" class="javax.swing.JRadioButton" binding="x64RadioButton" default-binding="true">
105+
<constraints>
106+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
107+
</constraints>
108+
<properties>
109+
<text value="x64"/>
110+
</properties>
111+
</component>
112+
<component id="d5640" class="javax.swing.JRadioButton" binding="x86RadioButton">
113+
<constraints>
114+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
115+
</constraints>
116+
<properties>
117+
<text value="x86"/>
118+
</properties>
119+
</component>
120+
</children>
121+
</grid>
78122
<grid id="3284" binding="codePanel" layout-manager="BorderLayout" hgap="0" vgap="0">
79123
<constraints>
80-
<grid row="8" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
124+
<grid row="10" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
81125
</constraints>
82126
<properties/>
83127
<border type="none"/>
84128
<children/>
85129
</grid>
86130
<component id="ccac7" class="javax.swing.JCheckBox" binding="waitIDECheckBox" default-binding="true">
87131
<constraints>
88-
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
132+
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
89133
</constraints>
90134
<properties>
91135
<text value="Block the program and wait for the IDE."/>
92136
</properties>
93137
</component>
94138
<component id="eca0" class="javax.swing.JLabel">
95139
<constraints>
96-
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
140+
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
97141
</constraints>
98142
<properties>
99143
<text value=""/>
100144
</properties>
101145
</component>
102146
<component id="6e8e3" class="javax.swing.JCheckBox" binding="breakWhenIDEConnectedCheckBox" default-binding="true">
103147
<constraints>
104-
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
148+
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
105149
</constraints>
106150
<properties>
107151
<text value="Force break when connected."/>
108152
</properties>
109153
</component>
110154
<component id="191a4" class="javax.swing.JLabel">
111155
<constraints>
112-
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
156+
<grid row="9" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
113157
</constraints>
114158
<properties>
115159
<text value="Copy following code and paste into the lua code entry."/>
116160
</properties>
117161
</component>
118-
<grid id="d0e40" binding="winArchPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
119-
<margin top="0" left="0" bottom="0" right="0"/>
120-
<constraints>
121-
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
122-
</constraints>
123-
<properties/>
124-
<border type="none"/>
125-
<children>
126-
<component id="3495" class="javax.swing.JRadioButton" binding="x64RadioButton" default-binding="true">
127-
<constraints>
128-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
129-
</constraints>
130-
<properties>
131-
<text value="x64"/>
132-
</properties>
133-
</component>
134-
<component id="d5640" class="javax.swing.JRadioButton" binding="x86RadioButton">
135-
<constraints>
136-
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
137-
</constraints>
138-
<properties>
139-
<text value="x86"/>
140-
</properties>
141-
</component>
142-
</children>
143-
</grid>
144162
</children>
145163
</grid>
146164
</form>

src/main/java/com/tang/intellij/lua/debugger/emmy/EmmyDebugSettingsPanel.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@
2020
import com.intellij.openapi.editor.Document;
2121
import com.intellij.openapi.editor.EditorFactory;
2222
import com.intellij.openapi.editor.ex.EditorEx;
23+
import com.intellij.openapi.fileChooser.FileChooser;
24+
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
25+
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
2326
import com.intellij.openapi.options.SettingsEditor;
2427
import com.intellij.openapi.project.Project;
2528
import com.intellij.openapi.util.SystemInfoRt;
29+
import com.intellij.openapi.vfs.VirtualFile;
30+
import com.intellij.ui.ToolbarDecorator;
31+
import com.intellij.ui.components.JBList;
2632
import com.tang.intellij.lua.lang.LuaFileType;
2733
import com.tang.intellij.lua.psi.LuaFileUtil;
2834
import org.jetbrains.annotations.NotNull;
@@ -34,6 +40,8 @@
3440
import javax.swing.text.BadLocationException;
3541
import javax.swing.text.PlainDocument;
3642
import java.awt.*;
43+
import java.util.ArrayList;
44+
import java.util.List;
3745
import java.util.Objects;
3846

3947
public class EmmyDebugSettingsPanel extends SettingsEditor<EmmyDebugConfiguration> implements DocumentListener {
@@ -54,10 +62,21 @@ public class EmmyDebugSettingsPanel extends SettingsEditor<EmmyDebugConfiguratio
5462
private JRadioButton x86RadioButton;
5563
private JPanel winArchPanel;
5664
private final ButtonGroup winArchGroup;
65+
66+
// Source roots configuration - bound from .form file
67+
private JLabel sourceRootsLabel;
68+
private JPanel sourceRootsPanel;
69+
70+
// Source roots list model and component
71+
private final DefaultListModel<String> sourceRootsModel = new DefaultListModel<>();
72+
private JBList<String> sourceRootsList;
5773

5874
private final EditorEx editorEx;
75+
private final Project project;
5976

6077
public EmmyDebugSettingsPanel(Project project) {
78+
this.project = project;
79+
6180
// type
6281
DefaultComboBoxModel<EmmyDebugTransportType> model = new DefaultComboBoxModel<>();
6382
model.addElement(EmmyDebugTransportType.TCP_CLIENT);
@@ -90,13 +109,52 @@ public EmmyDebugSettingsPanel(Project project) {
90109
winArchGroup.add(x86RadioButton);
91110
x64RadioButton.addChangeListener(e -> onChanged());
92111
x86RadioButton.addChangeListener(e -> onChanged());
112+
113+
// source roots configuration
114+
setupSourceRootsUI();
93115

94116
// editor
95117
editorEx = createEditorEx(project);
96118
codePanel.add(editorEx.getComponent(), BorderLayout.CENTER);
97119

98120
updateCode();
99121
}
122+
123+
private void setupSourceRootsUI() {
124+
// Create list component for source roots
125+
sourceRootsList = new JBList<>(sourceRootsModel);
126+
sourceRootsList.setEmptyText("No source roots configured");
127+
128+
// Create toolbar with add/remove buttons
129+
ToolbarDecorator decorator = ToolbarDecorator.createDecorator(sourceRootsList)
130+
.setAddAction(button -> {
131+
FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
132+
descriptor.setTitle("Select Source Root Directory");
133+
descriptor.setDescription("Select a directory containing your Lua source files");
134+
VirtualFile[] files = FileChooser.chooseFiles(descriptor, project, null);
135+
for (VirtualFile file : files) {
136+
String path = file.getPath();
137+
if (!sourceRootsModel.contains(path)) {
138+
sourceRootsModel.addElement(path);
139+
onChanged();
140+
}
141+
}
142+
})
143+
.setRemoveAction(button -> {
144+
int selectedIndex = sourceRootsList.getSelectedIndex();
145+
if (selectedIndex >= 0) {
146+
sourceRootsModel.remove(selectedIndex);
147+
onChanged();
148+
}
149+
})
150+
.disableUpDownActions();
151+
152+
// Add the decorated panel to sourceRootsPanel (bound from .form file)
153+
if (sourceRootsPanel != null) {
154+
sourceRootsPanel.setLayout(new BorderLayout());
155+
sourceRootsPanel.add(decorator.createPanel(), BorderLayout.CENTER);
156+
}
157+
}
100158

101159
private void onChanged() {
102160
if (isClient()) {
@@ -125,6 +183,12 @@ protected void resetEditorFrom(@NotNull EmmyDebugConfiguration configuration) {
125183
x86RadioButton.setSelected(true);
126184
}
127185
}
186+
187+
// Source roots
188+
sourceRootsModel.clear();
189+
for (String path : configuration.getSourceRoots()) {
190+
sourceRootsModel.addElement(path);
191+
}
128192
}
129193

130194
@Override
@@ -140,6 +204,14 @@ protected void applyEditorTo(@NotNull EmmyDebugConfiguration configuration) {
140204
if (SystemInfoRt.isWindows) {
141205
configuration.setWinArch(x64RadioButton.isSelected() ? EmmyWinArch.X64 : EmmyWinArch.X86);
142206
}
207+
208+
// Source roots
209+
List<String> sourceRoots = new ArrayList<>();
210+
for (int i = 0; i < sourceRootsModel.size(); i++) {
211+
sourceRoots.add(sourceRootsModel.get(i));
212+
}
213+
configuration.getSourceRoots().clear();
214+
configuration.getSourceRoots().addAll(sourceRoots);
143215
}
144216

145217
protected void setType(EmmyDebugTransportType type) {

src/main/java/com/tang/intellij/lua/debugger/emmy/EmmyDebugStackFrame.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ class EmmyDebugStackFrame(
8787
if (!sourcePositionInitialized) {
8888
sourcePosition = ReadAction.compute<XSourcePosition?, RuntimeException> {
8989
try {
90-
val file = LuaFileUtil.findFile(process.session.project, stackData.file)
90+
val sourceRoots = process.getSourceRoots()
91+
val file = LuaFileUtil.findFile(process.session.project, stackData.file, sourceRoots)
9192
if (file != null) {
9293
XSourcePositionImpl.create(file, stackData.line - 1)
9394
} else {

src/main/java/com/tang/intellij/lua/debugger/emmy/EmmyEvaluator.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@
1616

1717
package com.tang.intellij.lua.debugger.emmy
1818

19-
import com.intellij.xdebugger.evaluation.XDebuggerEvaluator
19+
import com.intellij.xdebugger.XSourcePosition
20+
import com.tang.intellij.lua.debugger.LuaDebuggerEvaluator
2021
import com.tang.intellij.lua.debugger.emmy.value.LuaXValueFactory
2122

2223
/**
2324
* Evaluator for Emmy debugger - evaluates expressions in the debug context
25+
* Extends LuaDebuggerEvaluator to get proper expression range detection for hover
2426
*/
2527
class EmmyEvaluator(
2628
private val frame: EmmyDebugStackFrame,
2729
private val process: EmmyDebugProcess
28-
) : XDebuggerEvaluator() {
30+
) : LuaDebuggerEvaluator() {
2931

30-
override fun evaluate(
32+
override fun eval(
3133
expression: String,
3234
callback: XEvaluationCallback,
33-
expressionPosition: com.intellij.xdebugger.XSourcePosition?
35+
expressionPosition: XSourcePosition?
3436
) {
3537
evaluate(expression, 0, callback)
3638
}

0 commit comments

Comments
 (0)