Skip to content

Commit f8ec9b4

Browse files
committed
This revision cleans up "Reset Interactions" command in Interactions
Pane. It nows works when the Scala interpreter is busy. It also eliminates some unnecessary locking and calls on invokeLater. The following files were modified: modified: src/edu/rice/cs/drjava/model/repl/InteractionsDJDocument.java modified: src/edu/rice/cs/drjava/model/repl/InteractionsModel.java modified: src/edu/rice/cs/drjava/model/repl/InteractionsModelCallback.java modified: src/edu/rice/cs/drjava/model/repl/RMIInteractionsModel.java modified: src/edu/rice/cs/drjava/model/repl/newjvm/MainJVM.java modified: src/edu/rice/cs/drjava/ui/DrScalaErrorHandler.java modified: src/edu/rice/cs/util/text/ConsoleDocument.java modified: ../plt/src/edu/rice/cs/plt/concurrent/StateMonitor.java
1 parent 56bc1ec commit f8ec9b4

File tree

8 files changed

+195
-172
lines changed

8 files changed

+195
-172
lines changed

drjava/src/edu/rice/cs/drjava/model/repl/InteractionsDJDocument.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public Pair<Pair<Integer, Integer>, String>[] getStyles() {
234234
* returns false if the point is not in the list. Only runs in event thread.
235235
*/
236236
public boolean setColoring(int point, Graphics g) {
237-
synchronized(_stylesList) {
237+
// synchronized(_stylesList) {
238238
for(Pair<Pair<Integer,Integer>,String> p : _stylesList) {
239239
Pair<Integer,Integer> loc = p.first();
240240
if (loc.first() <= point && loc.second() >= point) {
@@ -289,14 +289,14 @@ else if (p.second().equals(InteractionsDocument.CHARACTER_RETURN_STYLE)) {
289289
}
290290
}
291291
return false;
292-
}
292+
// }
293293
}
294294

295295
/** Attempts to set the font on the graphics context based upon the styles held in the styles list. Only runs in
296296
* event thread.
297297
*/
298298
public void setBoldFonts(int point, Graphics g) {
299-
synchronized(_stylesList) {
299+
// synchronized(_stylesList) {
300300
for(Pair<Pair<Integer,Integer>,String> p : _stylesList) {
301301
Pair<Integer,Integer> loc = p.first();
302302
if (loc.first() <= point && loc.second() >= point) {
@@ -308,7 +308,7 @@ else if (p.second().equals(InteractionsDocument.DEBUGGER_STYLE))
308308
return;
309309
}
310310
}
311-
}
311+
// }
312312
}
313313

314314
/** Called when the Interactions pane is reset. Only runs in event thread. */

drjava/src/edu/rice/cs/drjava/model/repl/InteractionsModel.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ private void _prepareToInterpret(String text) {
317317
*/
318318
public abstract Pair<String,String> getVariableToString(String var);
319319

320-
/** Resets the Java interpreter with working directory wd. */
320+
/** Resets the Java interpreter with working directory wd. Only runs in event thread. */
321321
public final void resetInterpreter(File wd, boolean force) {
322322
_workingDirectory = wd;
323323
_autoImportSet.clear(); // clear list when interpreter is reset
@@ -671,24 +671,33 @@ public void replReturnedResult(String result, String style) {
671671
_interactionIsOver();
672672
}
673673
}
674-
675-
/**
676-
* Default behavior set to return what it's given.
677-
* Used to replace line number and file name in a throwable when the error
678-
* occurs in a Language Level file.
679-
* Overriden in DefaultInteractionModel when a GlobalModel is available
680-
* to make a LanguageLevelStackTraceMapper.
681-
* @param sT the stackTrace to replace line number and file name
682-
* @return the same stackTrace.
683-
*/
684-
public StackTraceElement[] replaceLLException(StackTraceElement[] sT) {
685-
return sT;
686-
687-
}
674+
675+
/* Due to delays and blocking by running code in the event handling thread, does not have any effect. */
676+
677+
// /** Displays the fact that repl is being reset requiring a restart of the Scala interpreter. The Scala interpreter
678+
// * cannot be reset because it is busy. The code that performs this restart is located in MainJVM. ONLY runs in the
679+
// * Event Handling Thread!
680+
// */
681+
// public void replIsResetting() {
682+
// _document.insertBeforeLastPrompt(" Restarting the Scala interpreter, which is a SLOW process ...\n",
683+
// InteractionsDocument.ERROR_STYLE);
684+
// }
685+
686+
// /** Default behavior set to return what it's given. Used to replace line number and file name in a throwable when
687+
// * the error
688+
// * occurs in a Language Level file.
689+
// * Overriden in DefaultInteractionModel when a GlobalModel is available
690+
// * to make a LanguageLevelStackTraceMapper.
691+
// * @param sT the stackTrace to replace line number and file name
692+
// * @return the same stackTrace.
693+
// */
694+
// public StackTraceElement[] replaceLLException(StackTraceElement[] sT) {
695+
// return sT;
696+
// }
688697

689698
/** Signifies that the most recent interpretation was ended due to an exception being thrown. */
690699
public void replThrewException(String message, StackTraceElement[] stackTrace) {
691-
stackTrace = replaceLLException(stackTrace);
700+
// stackTrace = replaceLLException(stackTrace);
692701
StringBuilder sb = new StringBuilder(message);
693702
for(StackTraceElement ste: stackTrace) {
694703
sb.append("\n\tat ");

drjava/src/edu/rice/cs/drjava/model/repl/InteractionsModelCallback.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,14 @@ public void replReturnedSyntaxError(String errorMessage, String interaction, int
123123
*/
124124
public void replCalledSystemExit(int status);
125125

126+
// /** Signifies that repl is being reset requiring a restart of the Scala interpreter. */
127+
// public void replIsResetting();
128+
126129
/** This method is called by the Main JVM if the Interpreter JVM cannot be exited (likely because of its
127130
* having a security manager)
128131
* @param th The Throwable thrown by System.exit
129132
*/
133+
130134
public void interpreterResetFailed(Throwable th);
131135

132136
/** Called when the slave JVM fails to startup */

drjava/src/edu/rice/cs/drjava/model/repl/RMIInteractionsModel.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,28 @@ public Pair<String,String> getVariableToString(String var) {
116116
public void addExtraClassPath(File f) { _jvm.addExtraClassPath(f); }
117117

118118
/** Resets the Java interpreter. If DrScala is in a normal state (implying that _jvm.interpret(...) below will
119-
* succeed, the embedded Interactions document has already been cleared. */
119+
* succeed, the embedded Interactions document has already been cleared. Only runs in the event thread. */
120120
protected void _resetInterpreter(final File wd, final boolean force) {
121-
setToDefaultInterpreter();
121+
setToDefaultInterpreter(); // TODO: tear out interpreter selection; DrScala only has one interpreter
122122
_jvm.setWorkingDirectory(wd);
123123
/* Try to reset the interpreter using the internal scala interpreter reset command. If this fails restart the
124124
* slave JVM. */
125-
Utilities.invokeLater(new Runnable() {
126-
public void run() {
127-
_document.insertBeforeLastPrompt(" Resetting Interactions ...\n", InteractionsDocument.ERROR_STYLE);
128-
boolean success = _jvm.interpret(":_$$$$$__$$$$$$_-reset");
129-
if (success) {
130-
_document.reset(generateBanner(wd));
131-
_notifyInterpreterReady(wd);
132-
}
133-
else _jvm.restartInterpreterJVM(force);
134-
}
135-
});
125+
/* Already running in the event handling thread, so this invokeLater is unnecessary. */
126+
// Utilities.invokeLater(new Runnable() {
127+
// public void run() {
128+
// Utilities.show("Executing Reset Runnable");
129+
/* The following has no effect. */
130+
// _document.insertBeforeLastPrompt(" Resetting Interactions ... which can be SLOW\n", InteractionsDocument.ERROR_STYLE);
131+
boolean success = _jvm.interpret(":_$$$$$__$$$$$$_-reset");
132+
/* NOTE: interpret only returns false when the interpreter is stopped. It throws an exception if the interpreter
133+
* is busy, triggering handler code (the uncaughtException method) in DrScalaErrorHandler. */
134+
if (success) {
135+
_document.reset(generateBanner(wd)); /* Avoids displaying two prompt symbols. Fix this hack? */
136+
}
137+
else _jvm.restartInterpreterJVM(force);
138+
_document.clearColoring();
139+
// }
140+
// });
136141
}
137142

138143
/** Adds a named interpreter to the list.
@@ -196,7 +201,12 @@ private void _updateDocument(String prompt, boolean inProgress) {
196201
* thread. */
197202
public void _notifyInterpreterReady(final File wd) { /* TODO rename this method */
198203
// System.out.println("Asynchronously notifying interpreterReady event listeners"); // DEBUG
199-
Utilities.invokeLater(new Runnable() { public void run() { _notifier.interpreterReady(wd); } });
204+
Utilities.invokeLater(new Runnable() {
205+
public void run() {
206+
_notifier.interpreterReady(wd);
207+
_document.clearColoring();
208+
}
209+
});
200210
}
201211

202212
/** Sets whether or not the interpreter should enforce access to all members. */

drjava/src/edu/rice/cs/drjava/model/repl/newjvm/MainJVM.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
import edu.rice.cs.plt.concurrent.StateMonitor;
7777
import edu.rice.cs.plt.concurrent.CompletionMonitor;
7878

79+
import edu.rice.cs.util.swing.Utilities;
80+
7981
import static edu.rice.cs.plt.debug.DebugUtil.debug;
8082
import static edu.rice.cs.drjava.config.OptionConstants.*;
8183

@@ -174,11 +176,10 @@ public void restartInterpreterJVM(boolean force) {
174176
_state.value().restart(force); /* waitUntilReady(); */
175177
_interactionsModel._notifyInterpreterReady(_workingDir);
176178
}
177-
178-
/**
179-
* Stop the interpreter JVM, do not restart it, and terminate the RMI server associated with this object.
180-
* May be useful when a number of different MainJVM objects are created (such as when running tests).
181-
*/
179+
180+
/** Stop the interpreter JVM, do not restart it, and terminate the RMI server associated with this object.
181+
* May be useful when a number of different MainJVM objects are created (such as when running tests).
182+
*/
182183
public void dispose() { _state.value().dispose(); }
183184

184185

@@ -213,8 +214,6 @@ protected void handleSlaveWontStart(Exception e) {
213214
_state.value().startFailed(e);
214215
}
215216

216-
217-
218217
/*
219218
* === MainJVMRemoteI methods ===
220219
*/
@@ -1060,8 +1059,21 @@ public Void forUnexpectedException(Throwable t) {
10601059
}
10611060

10621061
public Void forBusy() {
1063-
_interactionsModel.replReturnedVoid();
1064-
throw new UnexpectedException("MainJVM.interpret() called when InterpreterJVM was busy!");
1062+
/* Full reset of the interpreter is necessary. Force the restarting of the interpreter. */
1063+
1064+
/* Stop the interpreter which means methods that delegate to the interpreter JVM will fail until the slave
1065+
* JVM is restarted. */
1066+
// _interactionsModel.replIsResetting();
1067+
// Utilities.show("Stopping interpreter");
1068+
1069+
stopInterpreterJVM();
1070+
1071+
/* Display message in interactions pane that the interpreter is being restarted. When the interpreter is
1072+
* restarted, this display is not included int the new InteractionsDocument. */
1073+
1074+
// Utilities.show("Restarting interpreter");
1075+
restartInterpreterJVM(true);
1076+
return null;
10651077
}
10661078
}
10671079

@@ -1117,6 +1129,7 @@ public void _notifyInterpreterReady(File f) {
11171129

11181130
public void replReturnedVoid() { }
11191131
public void replReturnedResult(String result, String style) { }
1132+
// public void replIsResetting() { }
11201133
public void replThrewException(String message, StackTraceElement[] stackTrace) { }
11211134
public void replThrewException(String message) { }
11221135
public void replReturnedSyntaxError(String errorMessage, String interaction, int startRow, int startCol, int endRow,

drjava/src/edu/rice/cs/drjava/ui/DrScalaErrorHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ private DrScalaErrorHandler() { }
6262

6363
/** Handles an uncaught exception. This gets called automatically by AWT. */
6464
public void uncaughtException(Thread t, Throwable thrown) {
65+
// if (thrown instanceof
6566
record(thrown);
67+
// Utilities.show("UncaughtExceptionHandler invoked");
6668
}
6769

70+
6871
/** the list of errors */
6972
private static volatile ArrayList<Throwable> _errors = new ArrayList<Throwable>();
7073

drjava/src/edu/rice/cs/util/text/ConsoleDocument.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public void append(String str, String style) throws EditDocumentException {
273273
* @param style Name of the style to use. Must have been added using addStyle.
274274
* @throws EditDocumentException if the offset is illegal
275275
*/
276-
public void forceInsertText(int offs, String str, String style) throws EditDocumentException {
276+
public void forceInsertText(int offs, String str, String style) {
277277
_addToStyleLists(offs, str, style);
278278
// System.err.println("Inserting text '" + str + "' at position " + offs);
279279
_document.forceInsertText(offs, str, style);
@@ -285,6 +285,12 @@ private void _addToStyleLists(int offs, String str, String style) {
285285
((SwingDocument)_document).addColoring(offs, offs + str.length(), style);
286286
}
287287

288+
/** In an InteractionsDJDocument, this method clears the styles list for this document. This may
289+
* or may not be an issue in other concrete document classes extending this one. Only runs in the
290+
* event thread.
291+
*/
292+
public void clearColoring() { }
293+
288294
/** Removes a portion of the document, if the edit condition (including promptPos) allows it.
289295
* @param offs Offset to start deleting from
290296
* @param len Number of characters to remove

0 commit comments

Comments
 (0)