Skip to content

Instantly share code, notes, and snippets.

@mikesimons
Created June 27, 2012 11:23
Show Gist options
  • Save mikesimons/3003458 to your computer and use it in GitHub Desktop.
Save mikesimons/3003458 to your computer and use it in GitHub Desktop.
Primer for CodeMirror mode abuse
var code = "function func_name() { var something = 'abc'; return 1; }";
var js_mode = CodeMirror.modes["javascript"]({}, {});
var state = js_mode.startState();
var stream = new CodeMirror.StringStream(code);
var style = null;
while(!stream.eol()) {
style = js_mode.token(stream, state);
console.log(style + ": " + code.slice(stream.start, stream.pos));
stream.start = stream.pos;
}
// See https://github.com/marijnh/CodeMirror2/blob/master/lib/codemirror.js#L2524
// Note this is one line only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment