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