3636import java .util .function .Consumer ;
3737import java .util .logging .Level ;
3838import java .util .logging .Logger ;
39+ import java .util .prefs .PreferenceChangeEvent ;
40+ import java .util .prefs .PreferenceChangeListener ;
41+ import java .util .prefs .Preferences ;
3942import java .util .stream .Collectors ;
4043import javax .swing .text .AttributeSet ;
4144import javax .swing .text .Document ;
4851import org .netbeans .api .debugger .jpda .JPDADebugger ;
4952import org .netbeans .api .debugger .jpda .ObjectVariable ;
5053import org .netbeans .api .debugger .jpda .Variable ;
54+ import org .netbeans .api .editor .mimelookup .MimeLookup ;
5155import org .netbeans .api .editor .mimelookup .MimeRegistration ;
5256import org .netbeans .api .editor .settings .AttributesUtilities ;
5357import org .netbeans .api .java .source .CancellableTask ;
6872import org .netbeans .modules .parsing .spi .TaskIndexingMode ;
6973import org .netbeans .spi .debugger .ContextProvider ;
7074import org .netbeans .spi .debugger .DebuggerServiceRegistration ;
75+ import org .netbeans .spi .debugger .ui .Constants ;
7176import org .netbeans .spi .editor .highlighting .HighlightsLayer ;
7277import org .netbeans .spi .editor .highlighting .HighlightsLayerFactory ;
7378import org .netbeans .spi .editor .highlighting .HighlightsSequence ;
7984import org .openide .util .Exceptions ;
8085import org .openide .util .Lookup ;
8186import org .openide .util .RequestProcessor ;
87+ import org .openide .util .WeakListeners ;
8288import org .openide .util .lookup .ServiceProvider ;
8389import org .openide .util .lookup .ServiceProviders ;
8490
8591
8692@ DebuggerServiceRegistration (path ="netbeans-JPDASession/inlineValue" , types =InlineValueComputer .class )
87- public class InlineValueComputerImpl implements InlineValueComputer , PropertyChangeListener {
93+ public class InlineValueComputerImpl implements InlineValueComputer , PreferenceChangeListener , PropertyChangeListener {
8894
8995 private static final Logger LOG = Logger .getLogger (InlineValueComputerImpl .class .getName ());
9096 private static final RequestProcessor EVALUATOR = new RequestProcessor (InlineValueComputerImpl .class .getName (), 1 , false , false );
9197 private static final String JAVA_STRATUM = "Java" ; //XXX: this is probably already defined somewhere
9298 private final JPDADebuggerImpl debugger ;
99+ private final Preferences prefs ;
93100 private TaskDescription currentTask ;
94101
95102 public InlineValueComputerImpl (ContextProvider contextProvider ) {
96103 debugger = (JPDADebuggerImpl ) contextProvider .lookupFirst (null , JPDADebugger .class );
97104 debugger .addPropertyChangeListener (this );
105+ prefs = MimeLookup .getLookup ("text/x-java" ).lookup (Preferences .class );
106+ prefs .addPreferenceChangeListener (WeakListeners .create (PreferenceChangeListener .class , this , prefs ));
98107 }
99108
100109 @ Override
@@ -105,108 +114,118 @@ public void propertyChange(PropertyChangeEvent evt) {
105114 }
106115
107116 if (JPDADebugger .PROP_CURRENT_CALL_STACK_FRAME .equals (evt .getPropertyName ())) {
108- CallStackFrame frame = debugger .getCurrentCallStackFrame ();
117+ refreshVariables ();
118+ }
119+ }
109120
110- FileObject frameFile = null ;
111- int frameLineNumber = -1 ;
112- Document frameDocument = null ;
121+ @ Override
122+ public void preferenceChange (PreferenceChangeEvent evt ) {
123+ refreshVariables ();
124+ }
113125
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 ;
127144 }
145+ } catch (InternalExceptionWrapper | InvalidStackFrameExceptionWrapper | ObjectCollectedExceptionWrapper | VMDisconnectedExceptionWrapper | MalformedURLException ex ) {
146+ Exceptions .printStackTrace (ex );
128147 }
148+ }
129149
130- TaskDescription newTask ;
150+ TaskDescription newTask ;
131151
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+ }
137157
138- if (setNewTask (newTask )) {
139- return ;
140- }
158+ if (setNewTask (newTask )) {
159+ return ;
160+ }
141161
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 );
145165
146- newTask .addCancelCallback (computationDone ::countDown );
166+ newTask .addCancelCallback (computationDone ::countDown );
147167
148- AtomicReference <Collection <InlineVariable >> values = new AtomicReference <>();
168+ AtomicReference <Collection <InlineVariable >> values = new AtomicReference <>();
149169
150- EVALUATOR .post (() -> {
151- OffsetsBag runningBag = new OffsetsBag (newTask .frameDocument );
170+ EVALUATOR .post (() -> {
171+ OffsetsBag runningBag = new OffsetsBag (newTask .frameDocument );
152172
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+ });
157177
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+ }
163183
164- if (newTask .isCancelled ()) {
165- return ;
166- }
184+ if (newTask .isCancelled ()) {
185+ return ;
186+ }
187+
188+ Collection <InlineVariable > variables = values .get ();
167189
168- Collection <InlineVariable > variables = values .get ();
190+ if (values == null ) {
191+ return ;
192+ }
169193
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 ()) {
171199 return ;
172200 }
173201
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 ;
180209 }
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 ();
206217 }
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 );
207226 }
208- });
209- }
227+ }
228+ });
210229 }
211230 }
212231
0 commit comments