Skip to content

Commit

Permalink
[runmode addon] Add support for lookahead
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed May 14, 2020
1 parent a810aee commit 0884405
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 6 additions & 4 deletions addon/runmode/runmode-standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ root.CodeMirror = {};

function splitLines(string){ return string.split(/\r?\n|\r/); };

function StringStream(string) {
function StringStream(strings, i) {
this.pos = this.start = 0;
this.string = string;
this.string = strings[i];
this.strings = strings
this.i = i
this.lineStart = 0;
}
StringStream.prototype = {
Expand Down Expand Up @@ -67,7 +69,7 @@ StringStream.prototype = {
try { return inner(); }
finally { this.lineStart -= n; }
},
lookAhead: function() { return null }
lookAhead: function(n) { return this.strings[this.i + n] }
};
CodeMirror.StringStream = StringStream;

Expand Down Expand Up @@ -151,7 +153,7 @@ CodeMirror.runMode = function (string, modespec, callback, options) {
var lines = splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
for (var i = 0, e = lines.length; i < e; ++i) {
if (i) callback("\n");
var stream = new CodeMirror.StringStream(lines[i]);
var stream = new CodeMirror.StringStream(lines, i);
if (!stream.string && mode.blankLine) mode.blankLine(state);
while (!stream.eol()) {
var style = mode.token(stream, state);
Expand Down
5 changes: 4 additions & 1 deletion addon/runmode/runmode.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ CodeMirror.runMode = function(string, modespec, callback, options) {
var lines = CodeMirror.splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
for (var i = 0, e = lines.length; i < e; ++i) {
if (i) callback("\n");
var stream = new CodeMirror.StringStream(lines[i]);
var stream = new CodeMirror.StringStream(lines[i], null, {
lookAhead: function(n) { return lines[i + n] },
baseToken: function() {}
});
if (!stream.string && mode.blankLine) mode.blankLine(state);
while (!stream.eol()) {
var style = mode.token(stream, state);
Expand Down

0 comments on commit 0884405

Please sign in to comment.