36
36
import java .util .function .Consumer ;
37
37
import java .util .logging .Level ;
38
38
import java .util .logging .Logger ;
39
+ import java .util .prefs .PreferenceChangeEvent ;
40
+ import java .util .prefs .PreferenceChangeListener ;
41
+ import java .util .prefs .Preferences ;
39
42
import java .util .stream .Collectors ;
40
43
import javax .swing .text .AttributeSet ;
41
44
import javax .swing .text .Document ;
48
51
import org .netbeans .api .debugger .jpda .JPDADebugger ;
49
52
import org .netbeans .api .debugger .jpda .ObjectVariable ;
50
53
import org .netbeans .api .debugger .jpda .Variable ;
54
+ import org .netbeans .api .editor .mimelookup .MimeLookup ;
51
55
import org .netbeans .api .editor .mimelookup .MimeRegistration ;
52
56
import org .netbeans .api .editor .settings .AttributesUtilities ;
53
57
import org .netbeans .api .java .source .CancellableTask ;
68
72
import org .netbeans .modules .parsing .spi .TaskIndexingMode ;
69
73
import org .netbeans .spi .debugger .ContextProvider ;
70
74
import org .netbeans .spi .debugger .DebuggerServiceRegistration ;
75
+ import org .netbeans .spi .debugger .ui .Constants ;
71
76
import org .netbeans .spi .editor .highlighting .HighlightsLayer ;
72
77
import org .netbeans .spi .editor .highlighting .HighlightsLayerFactory ;
73
78
import org .netbeans .spi .editor .highlighting .HighlightsSequence ;
79
84
import org .openide .util .Exceptions ;
80
85
import org .openide .util .Lookup ;
81
86
import org .openide .util .RequestProcessor ;
87
+ import org .openide .util .WeakListeners ;
82
88
import org .openide .util .lookup .ServiceProvider ;
83
89
import org .openide .util .lookup .ServiceProviders ;
84
90
85
91
86
92
@ DebuggerServiceRegistration (path ="netbeans-JPDASession/inlineValue" , types =InlineValueComputer .class )
87
- public class InlineValueComputerImpl implements InlineValueComputer , PropertyChangeListener {
93
+ public class InlineValueComputerImpl implements InlineValueComputer , PreferenceChangeListener , PropertyChangeListener {
88
94
89
95
private static final Logger LOG = Logger .getLogger (InlineValueComputerImpl .class .getName ());
90
96
private static final RequestProcessor EVALUATOR = new RequestProcessor (InlineValueComputerImpl .class .getName (), 1 , false , false );
91
97
private static final String JAVA_STRATUM = "Java" ; //XXX: this is probably already defined somewhere
92
98
private final JPDADebuggerImpl debugger ;
99
+ private final Preferences prefs ;
93
100
private TaskDescription currentTask ;
94
101
95
102
public InlineValueComputerImpl (ContextProvider contextProvider ) {
96
103
debugger = (JPDADebuggerImpl ) contextProvider .lookupFirst (null , JPDADebugger .class );
97
104
debugger .addPropertyChangeListener (this );
105
+ prefs = MimeLookup .getLookup ("text/x-java" ).lookup (Preferences .class );
106
+ prefs .addPreferenceChangeListener (WeakListeners .create (PreferenceChangeListener .class , this , prefs ));
98
107
}
99
108
100
109
@ Override
@@ -105,108 +114,118 @@ public void propertyChange(PropertyChangeEvent evt) {
105
114
}
106
115
107
116
if (JPDADebugger .PROP_CURRENT_CALL_STACK_FRAME .equals (evt .getPropertyName ())) {
108
- CallStackFrame frame = debugger .getCurrentCallStackFrame ();
117
+ refreshVariables ();
118
+ }
119
+ }
109
120
110
- FileObject frameFile = null ;
111
- int frameLineNumber = -1 ;
112
- Document frameDocument = null ;
121
+ @ Override
122
+ public void preferenceChange (PreferenceChangeEvent evt ) {
123
+ refreshVariables ();
124
+ }
113
125
114
- if (frame != null && !frame .isObsolete () &&
115
- frame .getThread ().isSuspended () &&
116
- JAVA_STRATUM .equals (frame .getDefaultStratum ())) {
117
- try {
118
- String url = debugger .getEngineContext ().getURL (frame , JAVA_STRATUM );
119
- frameFile = url != null ? URLMapper .findFileObject (URI .create (url ).toURL ()) : null ;
120
- if (frameFile != null ) {
121
- frameLineNumber = frame .getLineNumber (JAVA_STRATUM );
122
- EditorCookie ec = frameFile .getLookup ().lookup (EditorCookie .class );
123
- frameDocument = ec != null ? ec .getDocument () : null ;
124
- }
125
- } catch (InternalExceptionWrapper | InvalidStackFrameExceptionWrapper | ObjectCollectedExceptionWrapper | VMDisconnectedExceptionWrapper | MalformedURLException ex ) {
126
- Exceptions .printStackTrace (ex );
126
+ private void refreshVariables () {
127
+ CallStackFrame frame = debugger .getCurrentCallStackFrame ();
128
+
129
+ FileObject frameFile = null ;
130
+ int frameLineNumber = -1 ;
131
+ Document frameDocument = null ;
132
+
133
+ if (prefs .getBoolean (Constants .KEY_INLINE_VALUES , Constants .DEF_INLINE_VALUES ) &&
134
+ frame != null && !frame .isObsolete () &&
135
+ frame .getThread ().isSuspended () &&
136
+ JAVA_STRATUM .equals (frame .getDefaultStratum ())) {
137
+ try {
138
+ String url = debugger .getEngineContext ().getURL (frame , JAVA_STRATUM );
139
+ frameFile = url != null ? URLMapper .findFileObject (URI .create (url ).toURL ()) : null ;
140
+ if (frameFile != null ) {
141
+ frameLineNumber = frame .getLineNumber (JAVA_STRATUM );
142
+ EditorCookie ec = frameFile .getLookup ().lookup (EditorCookie .class );
143
+ frameDocument = ec != null ? ec .getDocument () : null ;
127
144
}
145
+ } catch (InternalExceptionWrapper | InvalidStackFrameExceptionWrapper | ObjectCollectedExceptionWrapper | VMDisconnectedExceptionWrapper | MalformedURLException ex ) {
146
+ Exceptions .printStackTrace (ex );
128
147
}
148
+ }
129
149
130
- TaskDescription newTask ;
150
+ TaskDescription newTask ;
131
151
132
- if (frameFile != null && frameDocument != null ) {
133
- newTask = new TaskDescription (frameFile , frameLineNumber , frameDocument );
134
- } else {
135
- newTask = null ;
136
- }
152
+ if (frameFile != null && frameDocument != null ) {
153
+ newTask = new TaskDescription (frameFile , frameLineNumber , frameDocument );
154
+ } else {
155
+ newTask = null ;
156
+ }
137
157
138
- if (setNewTask (newTask )) {
139
- return ;
140
- }
158
+ if (setNewTask (newTask )) {
159
+ return ;
160
+ }
141
161
142
- if (newTask != null ) {
143
- //TODO: cancel any already running computation if the configuration is different:
144
- CountDownLatch computationDone = new CountDownLatch (1 );
162
+ if (newTask != null ) {
163
+ //TODO: cancel any already running computation if the configuration is different:
164
+ CountDownLatch computationDone = new CountDownLatch (1 );
145
165
146
- newTask .addCancelCallback (computationDone ::countDown );
166
+ newTask .addCancelCallback (computationDone ::countDown );
147
167
148
- AtomicReference <Collection <InlineVariable >> values = new AtomicReference <>();
168
+ AtomicReference <Collection <InlineVariable >> values = new AtomicReference <>();
149
169
150
- EVALUATOR .post (() -> {
151
- OffsetsBag runningBag = new OffsetsBag (newTask .frameDocument );
170
+ EVALUATOR .post (() -> {
171
+ OffsetsBag runningBag = new OffsetsBag (newTask .frameDocument );
152
172
153
- Lookup .getDefault ().lookup (ComputeInlineVariablesFactory .class ).set (newTask .frameFile , newTask .frameLineNumber , variables -> {
154
- values .set (variables );
155
- computationDone .countDown ();
156
- });
173
+ Lookup .getDefault ().lookup (ComputeInlineVariablesFactory .class ).set (newTask .frameFile , newTask .frameLineNumber , variables -> {
174
+ values .set (variables );
175
+ computationDone .countDown ();
176
+ });
157
177
158
- try {
159
- computationDone .await ();
160
- } catch (InterruptedException ex ) {
161
- Exceptions .printStackTrace (ex );
162
- }
178
+ try {
179
+ computationDone .await ();
180
+ } catch (InterruptedException ex ) {
181
+ Exceptions .printStackTrace (ex );
182
+ }
163
183
164
- if (newTask .isCancelled ()) {
165
- return ;
166
- }
184
+ if (newTask .isCancelled ()) {
185
+ return ;
186
+ }
187
+
188
+ Collection <InlineVariable > variables = values .get ();
167
189
168
- Collection <InlineVariable > variables = values .get ();
190
+ if (values == null ) {
191
+ return ;
192
+ }
169
193
170
- if (values == null ) {
194
+ Map <String , Variable > expression2Value = new HashMap <>();
195
+ Map <Integer , Map <String , String >> line2Values = new HashMap <>();
196
+
197
+ for (InlineVariable v : variables ) {
198
+ if (newTask .isCancelled ()) {
171
199
return ;
172
200
}
173
201
174
- Map <String , Variable > expression2Value = new HashMap <>();
175
- Map <Integer , Map <String , String >> line2Values = new HashMap <>();
176
-
177
- for (InlineVariable v : variables ) {
178
- if (newTask .isCancelled ()) {
179
- return ;
202
+ Variable value = expression2Value .computeIfAbsent (v .expression , expr -> {
203
+ try {
204
+ return debugger .evaluate (expr );
205
+ } catch (InvalidExpressionException ex ) {
206
+ //the variable may not exist
207
+ LOG .log (Level .FINE , null , ex );
208
+ return null ;
180
209
}
181
-
182
- Variable value = expression2Value .computeIfAbsent (v .expression , expr -> {
183
- try {
184
- return debugger .evaluate (expr );
185
- } catch (InvalidExpressionException ex ) {
186
- //the variable may not exist
187
- LOG .log (Level .FINE , null , ex );
188
- return null ;
189
- }
190
- });
191
- if (value != null ) {
192
- String valueText ;
193
- if (value instanceof ObjectVariable ov ) {
194
- valueText = toValue (ov ).replace ("\n " , "\\ n" );
195
- } else {
196
- valueText = value .getValue ();
197
- }
198
- line2Values .computeIfAbsent (v .lineEnd , __ -> new LinkedHashMap <>())
199
- .putIfAbsent (v .expression , v .expression + " = " + valueText );
200
- String mergedValues = line2Values .get (v .lineEnd ).values ().stream ().collect (Collectors .joining (", " , " " , "" ));
201
- AttributeSet attrs = AttributesUtilities .createImmutable ("virtual-text-prepend" , mergedValues );
202
-
203
- runningBag .addHighlight (v .lineEnd , v .lineEnd + 1 , attrs );
204
-
205
- setHighlights (newTask , runningBag );
210
+ });
211
+ if (value != null ) {
212
+ String valueText ;
213
+ if (value instanceof ObjectVariable ov ) {
214
+ valueText = toValue (ov ).replace ("\n " , "\\ n" );
215
+ } else {
216
+ valueText = value .getValue ();
206
217
}
218
+ line2Values .computeIfAbsent (v .lineEnd , __ -> new LinkedHashMap <>())
219
+ .putIfAbsent (v .expression , v .expression + " = " + valueText );
220
+ String mergedValues = line2Values .get (v .lineEnd ).values ().stream ().collect (Collectors .joining (", " , " " , "" ));
221
+ AttributeSet attrs = AttributesUtilities .createImmutable ("virtual-text-prepend" , mergedValues );
222
+
223
+ runningBag .addHighlight (v .lineEnd , v .lineEnd + 1 , attrs );
224
+
225
+ setHighlights (newTask , runningBag );
207
226
}
208
- });
209
- }
227
+ }
228
+ });
210
229
}
211
230
}
212
231
0 commit comments