This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathrepl.jsx
113 lines (100 loc) · 2.79 KB
/
repl.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (c) 2015-2016, Salesforce.com, Inc.
* All rights reserved.
* Licensed under the MIT license.
* For full license text, see LICENSE.md file in the repo root or
* https://opensource.org/licenses/MIT
*/
"use strict";
let Input = require('runway-compiler/lib/input.js');
let Util = require('runway-compiler/lib/util.js');
let React = require('react');
let ReactDOM = require('react-dom');
let View = function(controller) {
let module = controller.workspace.module;
let REPLView = React.createClass({
getInitialState() {
return {
input: '',
history: [],
};
},
onChangeInput: function(event) {
let input = event.target.value;
let matched = (left, right) =>
(Util.stringCount(input, left) ===
Util.stringCount(input, right));
if (input.endsWith('\n\n') ||
(input.endsWith('\n') &&
matched('(', ')') &&
matched('{', '}') &&
matched('[', ']'))) {
let output = '(any output is shown in the dev console for now)';
let error = false;
controller.workspace.tryChangeState(() => {
try {
// TODO: use forgivingLoad from main.js
let context = {};
let nmodule = compiler.load(new Input('REPL', input),
module.ast.env, context);
nmodule.ast.execute(context);
} catch (e) {
error = true;
output = e.toString();
}
return `REPL (${this.state.history.length + 1})`;
});
let repi = {
input: input,
output: output,
};
this.setState({
input: error ? input.substr(0, input.length - 1) : '',
history: this.state.history.concat(repi),
}, () => {
if (this.pre !== null) {
this.pre.scrollTop = 10000;
}
});
} else {
this.setState({
input: input,
});
}
},
render: function() {
let history = [];
this.state.history.forEach((repi, i) => {
// input
let lines = repi.input.split('\n');
history.push((i + 1) + '> ' + lines[0]);
history = history.concat(
lines.slice(1, lines.length - 1)
.map(l => '... ' + l));
// output
lines = repi.output.split('\n');
history = history.concat(
lines.map(l => ' ' + l));
});
return <div>
<pre style={{maxHeight: '30em', scroll: 'auto'}}
ref={pre => {this.pre = pre;}}>
<code>
{history.join('\n')}
</code>
</pre>
<textarea
value={this.state.input}
onChange={this.onChangeInput} />
</div>;
}
});
let reactComponent = controller.mountTab(elem => ReactDOM.render(<REPLView />, elem),
'repl', 'REPL');
return {
name: 'REPL',
update: function() {
}
};
}; // View
module.exports = View;