Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/codemirror/multiplex.js
Views: 687
// CodeMirror, copyright (c) by Marijn Haverbeke and others1// Distributed under an MIT license: https://codemirror.net/LICENSE23/*4Multiplexing mode -- exactly like the original CodeMirror multiplexingMode,5https://codemirror.net/demo/multiplex.html,6except use the option start:true to make it so the mode switch pattern7must be at the beginning of the line.89Original copyright on https://codemirror.net/addon/mode/multiplex.js:10CodeMirror, copyright (c) by Marijn Haverbeke and others11Distributed under an MIT license: http://codemirror.net/LICENSE12*/1314import CodeMirror from "./codemirror";1516CodeMirror.cocalcMultiplexingMode = function (outer /*, others */) {17// Others should be {open, close, mode [, delimStyle] [, innerStyle]} objects18var others = Array.prototype.slice.call(arguments, 1);1920function indexOf(string, pattern, from, returnEnd) {21if (typeof pattern == "string") {22var found = string.indexOf(pattern, from);23return returnEnd && found > -1 ? found + pattern.length : found;24}25var m = pattern.exec(from ? string.slice(from) : string);26return m ? m.index + from + (returnEnd ? m[0].length : 0) : -1;27}2829return {30startState: function () {31return {32outer: CodeMirror.startState(outer),33innerActive: null,34inner: null,35};36},3738copyState: function (state) {39return {40outer: CodeMirror.copyState(outer, state.outer),41innerActive: state.innerActive,42inner:43state.innerActive &&44CodeMirror.copyState(state.innerActive.mode, state.inner),45};46},4748token: function (stream, state) {49if (!state.innerActive) {50var cutOff = Infinity,51oldContent = stream.string;52for (var i = 0; i < others.length; ++i) {53var other = others[i];54if (55// This is our modification of usptream.56other.start &&57oldContent.slice(0, other.open.length) != other.open58) {59continue;60}61var found = indexOf(oldContent, other.open, stream.pos);62if (found == stream.pos) {63if (!other.parseDelimiters) stream.match(other.open);64state.innerActive = other;6566// Get the outer indent, making sure to handle CodeMirror.Pass67var outerIndent = 0;68if (outer.indent) {69var possibleOuterIndent = outer.indent(state.outer, "", "");70if (possibleOuterIndent !== CodeMirror.Pass)71outerIndent = possibleOuterIndent;72}7374state.inner = CodeMirror.startState(other.mode, outerIndent);75return (76other.delimStyle &&77other.delimStyle + " " + other.delimStyle + "-open"78);79} else if (found != -1 && found < cutOff) {80cutOff = found;81}82}83if (cutOff != Infinity) stream.string = oldContent.slice(0, cutOff);84var outerToken = outer.token(stream, state.outer);85if (cutOff != Infinity) stream.string = oldContent;86return outerToken;87} else {88var curInner = state.innerActive,89oldContent = stream.string;90if (!curInner.close && stream.sol()) {91state.innerActive = state.inner = null;92return this.token(stream, state);93}94var found = curInner.close95? indexOf(96oldContent,97curInner.close,98stream.pos,99curInner.parseDelimiters100)101: -1;102if (found == stream.pos && !curInner.parseDelimiters) {103stream.match(curInner.close);104state.innerActive = state.inner = null;105return (106curInner.delimStyle &&107curInner.delimStyle + " " + curInner.delimStyle + "-close"108);109}110if (found > -1) stream.string = oldContent.slice(0, found);111var innerToken = curInner.mode.token(stream, state.inner);112if (found > -1) stream.string = oldContent;113114if (found == stream.pos && curInner.parseDelimiters)115state.innerActive = state.inner = null;116117if (curInner.innerStyle) {118if (innerToken) innerToken = innerToken + " " + curInner.innerStyle;119else innerToken = curInner.innerStyle;120}121122return innerToken;123}124},125126indent: function (state, textAfter, line) {127var mode = state.innerActive ? state.innerActive.mode : outer;128if (!mode.indent) return CodeMirror.Pass;129return mode.indent(130state.innerActive ? state.inner : state.outer,131textAfter,132line133);134},135136blankLine: function (state) {137var mode = state.innerActive ? state.innerActive.mode : outer;138if (mode.blankLine) {139mode.blankLine(state.innerActive ? state.inner : state.outer);140}141if (!state.innerActive) {142for (var i = 0; i < others.length; ++i) {143var other = others[i];144if (other.open === "\n") {145state.innerActive = other;146state.inner = CodeMirror.startState(147other.mode,148mode.indent ? mode.indent(state.outer, "", "") : 0149);150}151}152} else if (state.innerActive.close === "\n") {153state.innerActive = state.inner = null;154}155},156157electricChars: outer.electricChars,158159innerMode: function (state) {160return state.inner161? { state: state.inner, mode: state.innerActive.mode }162: { state: state.outer, mode: outer };163},164};165};166167168