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

Commit 2961a69

Browse files
author
Sergio Benitez
committed
Initial crop of feedback resulted in the following changes:
* Editor is dynamically sized to initial example to take care of browser issues. * Warnings are parsed/managed like errors. * Active line is highlighted only when editor is in focus.
1 parent 07ca30c commit 2961a69

File tree

2 files changed

+83
-20
lines changed

2 files changed

+83
-20
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: 70 additions & 17 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";
@@ -32,6 +39,21 @@ editor.setTheme("ace/theme/chrome");
3239
editor.getSession().setMode("ace/mode/rust");
3340
editor.setShowPrintMargin(false);
3441
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();
3557

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

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);
4870
req.onload = function(e) {
4971
if (req.readyState === 4 && req.status === 200) {
5072
var result = JSON.parse(req.response).result;
73+
5174
// 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);
5483
} else {
5584
callback(false, null);
5685
}
@@ -65,16 +94,18 @@ function runProgram(program, callback) {
6594
}
6695

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

72101
// Dispatch depending on result type
73102
if (result == null) {
74103
resultDiv.style.backgroundColor = errorColor;
75104
resultDiv.innerHTML = errMsg;
76-
} else if (success) {
105+
} else if (statusCode == SUCCESS) {
77106
handleSuccess(message);
107+
} else if (statusCode == WARNING) {
108+
handleWarning(message);
78109
} else {
79110
handleError(message);
80111
}
@@ -86,18 +117,27 @@ function handleSuccess(message) {
86117
resultDiv.innerHTML = message;
87118
}
88119

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+
89127
// Called on unsuccessful program run. Detects and prints errors in program
90128
// output and highlights relevant lines and text in the code.
91129
function handleError(message) {
92130
resultDiv.style.backgroundColor = errorColor;
131+
handleProblem(message, "error");
132+
}
93133

94-
// Getting list of ranges with errors
134+
function handleProblem(message, problem) {
135+
// Getting list of ranges with problems
95136
var lines = message.split("<br />");
96-
var ranges = parseError(lines);
97137

98-
// Cleaning up the message: keeps only relevant error output
138+
// Cleaning up the message: keeps only relevant problem output
99139
var cleanMessage = lines.map(function(line) {
100-
var errIndex = line.indexOf("error: ");
140+
var errIndex = line.indexOf(problem + ": ");
101141
if (errIndex !== -1) {
102142
return line.slice(errIndex);
103143
}
@@ -110,27 +150,30 @@ function handleError(message) {
110150
resultDiv.innerHTML = cleanMessage;
111151

112152
// Highlighting the lines
153+
var ranges = parseProblems(lines);
113154
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);
115157
});
116158

117159
// Highlighting the specific text
118160
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);
120163
}));
121164
}
122165

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) {
126169
var ranges = [];
127170
for (var i in lines) {
128171
var line = lines[i];
129172
if (line.startsWith(":") && line.indexOf(": ") !== -1) {
130173
var parts = line.split(/:\s?|\s+/, 5).slice(1, 5);
131174
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]));
134177
}
135178
}
136179

@@ -149,3 +192,13 @@ runButton.addEventListener("click", function(ev) {
149192
var program = editor.getValue();
150193
runProgram(program, handleResult);
151194
});
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

Comments
 (0)