@@ -7,20 +7,27 @@ if (typeof String.prototype.startsWith != 'function') {
7
7
8
8
// Fetching DOM items
9
9
var activeCode = document . getElementById ( "active-code" ) ;
10
+ var editorDiv = document . getElementById ( "editor" ) ;
10
11
var staticCode = document . getElementById ( "static-code" ) ;
11
12
var runButton = document . getElementById ( "run-code" ) ;
12
13
var resultDiv = document . getElementById ( "result" ) ;
13
14
14
15
// Background colors for program result on success/error
15
16
var successColor = "#E2EEF6" ;
16
17
var errorColor = "#F6E2E2" ;
18
+ var warningColor = "#FFFBCB" ;
17
19
18
20
// Error message to return when there's a server failure
19
21
var errMsg = "The server encountered an error while running the program." ;
20
22
21
23
// Stores ACE editor markers (highights) for errors
22
24
var markers = [ ] ;
23
25
26
+ // Status codes, because there are no enums in Javascript
27
+ var SUCCESS = 0 ;
28
+ var ERROR = 1 ;
29
+ var WARNING = 2 ;
30
+
24
31
// JS exists, display ACE editor
25
32
staticCode . style . display = "none" ;
26
33
activeCode . style . display = "block" ;
@@ -32,6 +39,21 @@ editor.setTheme("ace/theme/chrome");
32
39
editor . getSession ( ) . setMode ( "ace/mode/rust" ) ;
33
40
editor . setShowPrintMargin ( false ) ;
34
41
editor . renderer . setShowGutter ( false ) ;
42
+ editor . setHighlightActiveLine ( false ) ;
43
+
44
+ // Changes the height of the editor to match its contents
45
+ function updateEditorHeight ( ) {
46
+ // http://stackoverflow.com/questions/11584061/
47
+ var newHeight = editor . getSession ( ) . getScreenLength ( )
48
+ * editor . renderer . lineHeight
49
+ + editor . renderer . scrollBar . getWidth ( ) ;
50
+
51
+ editorDiv . style . height = Math . ceil ( newHeight ) . toString ( ) + "px" ;
52
+ editor . resize ( ) ;
53
+ } ;
54
+
55
+ // Set initial size to match initial content
56
+ updateEditorHeight ( ) ;
35
57
36
58
// Dispatches a XMLHttpRequest to the Rust playpen, running the program, and
37
59
// issues a callback to `callback` with the result (or null on error)
@@ -43,14 +65,21 @@ function runProgram(program, callback) {
43
65
code : program
44
66
} ) ;
45
67
46
- <!-- console.log("Sending", data); -->
47
- req . open ( 'POST' , "http://playtest.rust-lang.org/evaluate.json" , true ) ;
68
+ // console.log("Sending", data);
69
+ req . open ( 'POST' , "http://playtest.rust-lang.org/evaluate.json" , true ) ;
48
70
req . onload = function ( e ) {
49
71
if ( req . readyState === 4 && req . status === 200 ) {
50
72
var result = JSON . parse ( req . response ) . result ;
73
+
51
74
// Need server support to get an accurate version of this.
52
- var isSuccess = ( result . indexOf ( "error:" ) === - 1 ) ;
53
- callback ( isSuccess , result ) ;
75
+ var statusCode = SUCCESS ;
76
+ if ( result . indexOf ( "error:" ) !== - 1 ) {
77
+ statusCode = ERROR ;
78
+ } else if ( result . indexOf ( "warning:" ) !== - 1 ) {
79
+ statusCode = WARNING ;
80
+ }
81
+
82
+ callback ( statusCode , result ) ;
54
83
} else {
55
84
callback ( false , null ) ;
56
85
}
@@ -65,16 +94,18 @@ function runProgram(program, callback) {
65
94
}
66
95
67
96
// The callback to runProgram
68
- function handleResult ( success , message ) {
97
+ function handleResult ( statusCode , message ) {
69
98
var message = message . replace ( / < a n o n > / g, '' ) ;
70
99
var message = message . replace ( / (?: \r \n | \r | \n ) / g, '<br />' ) ;
71
100
72
101
// Dispatch depending on result type
73
102
if ( result == null ) {
74
103
resultDiv . style . backgroundColor = errorColor ;
75
104
resultDiv . innerHTML = errMsg ;
76
- } else if ( success ) {
105
+ } else if ( statusCode == SUCCESS ) {
77
106
handleSuccess ( message ) ;
107
+ } else if ( statusCode == WARNING ) {
108
+ handleWarning ( message ) ;
78
109
} else {
79
110
handleError ( message ) ;
80
111
}
@@ -86,18 +117,27 @@ function handleSuccess(message) {
86
117
resultDiv . innerHTML = message ;
87
118
}
88
119
120
+ // Called on unsuccessful program run. Detects and prints errors in program
121
+ // output and highlights relevant lines and text in the code.
122
+ function handleWarning ( message ) {
123
+ resultDiv . style . backgroundColor = warningColor ;
124
+ handleProblem ( message , "warning" ) ;
125
+ }
126
+
89
127
// Called on unsuccessful program run. Detects and prints errors in program
90
128
// output and highlights relevant lines and text in the code.
91
129
function handleError ( message ) {
92
130
resultDiv . style . backgroundColor = errorColor ;
131
+ handleProblem ( message , "error" ) ;
132
+ }
93
133
94
- // Getting list of ranges with errors
134
+ function handleProblem ( message , problem ) {
135
+ // Getting list of ranges with problems
95
136
var lines = message . split ( "<br />" ) ;
96
- var ranges = parseError ( lines ) ;
97
137
98
- // Cleaning up the message: keeps only relevant error output
138
+ // Cleaning up the message: keeps only relevant problem output
99
139
var cleanMessage = lines . map ( function ( line ) {
100
- var errIndex = line . indexOf ( "error : ") ;
140
+ var errIndex = line . indexOf ( problem + " : ") ;
101
141
if ( errIndex !== - 1 ) {
102
142
return line . slice ( errIndex ) ;
103
143
}
@@ -110,27 +150,30 @@ function handleError(message) {
110
150
resultDiv . innerHTML = cleanMessage ;
111
151
112
152
// Highlighting the lines
153
+ var ranges = parseProblems ( lines ) ;
113
154
markers = ranges . map ( function ( range ) {
114
- return editor . getSession ( ) . addMarker ( range , "ace-error-line" , "fullLine" , false ) ;
155
+ return editor . getSession ( ) . addMarker ( range , "ace-" + problem + "-line" ,
156
+ "fullLine" , false ) ;
115
157
} ) ;
116
158
117
159
// Highlighting the specific text
118
160
markers = markers . concat ( ranges . map ( function ( range ) {
119
- return editor . getSession ( ) . addMarker ( range , "ace-error-text" , "text" , false ) ;
161
+ return editor . getSession ( ) . addMarker ( range , "ace-" + problem + "-text" ,
162
+ "text" , false ) ;
120
163
} ) ) ;
121
164
}
122
165
123
- // Parses an error message returning a list of ranges (row:col, row:col) where
124
- // erors in the code have occured.
125
- function parseError ( lines ) {
166
+ // Parses a problem message returning a list of ranges (row:col, row:col) where
167
+ // problems in the code have occured.
168
+ function parseProblems ( lines ) {
126
169
var ranges = [ ] ;
127
170
for ( var i in lines ) {
128
171
var line = lines [ i ] ;
129
172
if ( line . startsWith ( ":" ) && line . indexOf ( ": " ) !== - 1 ) {
130
173
var parts = line . split ( / : \s ? | \s + / , 5 ) . slice ( 1 , 5 ) ;
131
174
var ip = parts . map ( function ( p ) { return parseInt ( p , 10 ) - 1 ; } ) ;
132
- <!-- console.log("line:", line, parts, ip); -->
133
- ranges . push ( new Range ( ip [ 0 ] , ip [ 1 ] , ip [ 2 ] , ip [ 3 ] ) ) ;
175
+ // console.log("line:", line, parts, ip);
176
+ ranges . push ( new Range ( ip [ 0 ] , ip [ 1 ] , ip [ 2 ] , ip [ 3 ] ) ) ;
134
177
}
135
178
}
136
179
@@ -149,3 +192,13 @@ runButton.addEventListener("click", function(ev) {
149
192
var program = editor . getValue ( ) ;
150
193
runProgram ( program , handleResult ) ;
151
194
} ) ;
195
+
196
+ // Highlight active line when focused
197
+ editor . on ( 'focus' , function ( ) {
198
+ editor . setHighlightActiveLine ( true ) ;
199
+ } ) ;
200
+
201
+ // Don't when not
202
+ editor . on ( 'blur' , function ( ) {
203
+ editor . setHighlightActiveLine ( false ) ;
204
+ } ) ;
0 commit comments