Skip to content
This repository was archived by the owner on Sep 30, 2020. It is now read-only.

Commit 19e45af

Browse files
committed
Merge pull request #28 from SergioBenitez/gh-pages
Initial crop of feedback changes
2 parents aded55d + a078835 commit 19e45af

File tree

2 files changed

+86
-22
lines changed

2 files changed

+86
-22
lines changed

css/style.css

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ ul.laundry-list {
254254
padding: none;
255255
margin: none;
256256
width: 100%;
257-
height: 340px;
257+
min-height: 340px;
258258
font-size: 13px;
259259
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
260260
}
@@ -285,14 +285,24 @@ ul.laundry-list {
285285
border-radius: 4px;
286286
}
287287

288+
.ace-error-text, .ace-error-line, .ace-warning-text, .ace-warning-line {
289+
position: absolute;
290+
}
291+
288292
.ace-error-text {
289293
background-color: #e9abab;
290-
position: absolute;
291294
}
292295

293296
.ace-error-line {
294297
background-color: #F6E2E2;
295-
position: absolute;
298+
}
299+
300+
.ace-warning-text {
301+
background-color: #FFEF00;
302+
}
303+
304+
.ace-warning-line {
305+
background-color: #FFFBCB;
296306
}
297307

298308
pre { background-color: #FDFDFD; }

js/editor.js

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,27 @@ if (typeof String.prototype.startsWith != 'function') {
77

88
// Fetching DOM items
99
var activeCode = document.getElementById("active-code");
10+
var editorDiv = document.getElementById("editor");
1011
var staticCode = document.getElementById("static-code");
1112
var runButton = document.getElementById("run-code");
1213
var resultDiv = document.getElementById("result");
1314

1415
// Background colors for program result on success/error
1516
var successColor = "#E2EEF6";
1617
var errorColor = "#F6E2E2";
18+
var warningColor = "#FFFBCB";
1719

1820
// Error message to return when there's a server failure
1921
var errMsg = "The server encountered an error while running the program.";
2022

2123
// Stores ACE editor markers (highights) for errors
2224
var markers = [];
2325

26+
// Status codes, because there are no enums in Javascript
27+
var SUCCESS = 0;
28+
var ERROR = 1;
29+
var WARNING = 2;
30+
2431
// JS exists, display ACE editor
2532
staticCode.style.display = "none";
2633
activeCode.style.display = "block";
@@ -33,6 +40,21 @@ editor.getSession().setMode("ace/mode/rust");
3340
editor.getSession().setTabSize(2);
3441
editor.setShowPrintMargin(false);
3542
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();
3658

3759
// Dispatches a XMLHttpRequest to the Rust playpen, running the program, and
3860
// issues a callback to `callback` with the result (or null on error)
@@ -44,14 +66,21 @@ function runProgram(program, callback) {
4466
code: program
4567
});
4668

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);
4971
req.onload = function(e) {
5072
if (req.readyState === 4 && req.status === 200) {
5173
var result = JSON.parse(req.response).result;
74+
5275
// 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);
5584
} else {
5685
callback(false, null);
5786
}
@@ -66,16 +95,18 @@ function runProgram(program, callback) {
6695
}
6796

6897
// The callback to runProgram
69-
function handleResult(success, message) {
98+
function handleResult(statusCode, message) {
7099
var message = message.replace(/<anon>/g, '');
71100
var message = message.replace(/(?:\r\n|\r|\n)/g, '<br />');
72101

73102
// Dispatch depending on result type
74103
if (result == null) {
75104
resultDiv.style.backgroundColor = errorColor;
76105
resultDiv.innerHTML = errMsg;
77-
} else if (success) {
106+
} else if (statusCode == SUCCESS) {
78107
handleSuccess(message);
108+
} else if (statusCode == WARNING) {
109+
handleWarning(message);
79110
} else {
80111
handleError(message);
81112
}
@@ -87,18 +118,28 @@ function handleSuccess(message) {
87118
resultDiv.innerHTML = message;
88119
}
89120

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)
92128
function handleError(message) {
93129
resultDiv.style.backgroundColor = errorColor;
130+
handleProblem(message, "error");
131+
}
94132

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
96138
var lines = message.split("<br />");
97-
var ranges = parseError(lines);
98139

99-
// Cleaning up the message: keeps only relevant error output
140+
// Cleaning up the message: keeps only relevant problem output
100141
var cleanMessage = lines.map(function(line) {
101-
var errIndex = line.indexOf("error: ");
142+
var errIndex = line.indexOf(problem + ": ");
102143
if (errIndex !== -1) {
103144
return line.slice(errIndex);
104145
}
@@ -111,27 +152,30 @@ function handleError(message) {
111152
resultDiv.innerHTML = cleanMessage;
112153

113154
// Highlighting the lines
155+
var ranges = parseProblems(lines);
114156
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);
116159
});
117160

118161
// Highlighting the specific text
119162
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);
121165
}));
122166
}
123167

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) {
127171
var ranges = [];
128172
for (var i in lines) {
129173
var line = lines[i];
130174
if (line.startsWith(":") && line.indexOf(": ") !== -1) {
131175
var parts = line.split(/:\s?|\s+/, 5).slice(1, 5);
132176
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]));
135179
}
136180
}
137181

@@ -150,3 +194,13 @@ runButton.addEventListener("click", function(ev) {
150194
var program = editor.getValue();
151195
runProgram(program, handleResult);
152196
});
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

Comments
 (0)