Skip to content

Commit f015405

Browse files
committed
[multiplex addon] Fix handling of sub-modes with parseDelimiters and identical delimiters
Closes #5369 Closes #5368
1 parent 562f077 commit f015405

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

addon/mode/multiplex.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"use strict";
1313

1414
CodeMirror.multiplexingMode = function(outer /*, others */) {
15-
// Others should be {open, close, mode [, delimStyle] [, innerStyle]} objects
15+
// Others should be {open, close, mode [, delimStyle] [, innerStyle] [, parseDelimiters]} objects
1616
var others = Array.prototype.slice.call(arguments, 1);
1717

1818
function indexOf(string, pattern, from, returnEnd) {
@@ -29,15 +29,17 @@ CodeMirror.multiplexingMode = function(outer /*, others */) {
2929
return {
3030
outer: CodeMirror.startState(outer),
3131
innerActive: null,
32-
inner: null
32+
inner: null,
33+
startingInner: false
3334
};
3435
},
3536

3637
copyState: function(state) {
3738
return {
3839
outer: CodeMirror.copyState(outer, state.outer),
3940
innerActive: state.innerActive,
40-
inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner)
41+
inner: state.innerActive && CodeMirror.copyState(state.innerActive.mode, state.inner),
42+
startingInner: state.startingInner
4143
};
4244
},
4345

@@ -49,6 +51,7 @@ CodeMirror.multiplexingMode = function(outer /*, others */) {
4951
var found = indexOf(oldContent, other.open, stream.pos);
5052
if (found == stream.pos) {
5153
if (!other.parseDelimiters) stream.match(other.open);
54+
state.startingInner = !!other.parseDelimiters
5255
state.innerActive = other;
5356

5457
// Get the outer indent, making sure to handle CodeMirror.Pass
@@ -74,7 +77,8 @@ CodeMirror.multiplexingMode = function(outer /*, others */) {
7477
state.innerActive = state.inner = null;
7578
return this.token(stream, state);
7679
}
77-
var found = curInner.close ? indexOf(oldContent, curInner.close, stream.pos, curInner.parseDelimiters) : -1;
80+
var found = curInner.close && !state.startingInner ?
81+
indexOf(oldContent, curInner.close, stream.pos, curInner.parseDelimiters) : -1;
7882
if (found == stream.pos && !curInner.parseDelimiters) {
7983
stream.match(curInner.close);
8084
state.innerActive = state.inner = null;
@@ -83,6 +87,7 @@ CodeMirror.multiplexingMode = function(outer /*, others */) {
8387
if (found > -1) stream.string = oldContent.slice(0, found);
8488
var innerToken = curInner.mode.token(stream, state.inner);
8589
if (found > -1) stream.string = oldContent;
90+
else if (stream.pos > stream.start) state.startingInner = false
8691

8792
if (found == stream.pos && curInner.parseDelimiters)
8893
state.innerActive = state.inner = null;

addon/mode/multiplex_test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,20 @@
3030
MT(
3131
"stexInsideMarkdown",
3232
"[strong **Equation:**] [delim&delim-open $][inner&tag \\pi][delim&delim-close $]");
33+
34+
CodeMirror.defineMode("identical_delim_multiplex", function() {
35+
return CodeMirror.multiplexingMode(CodeMirror.getMode({indentUnit: 2}, "javascript"), {
36+
open: "#",
37+
close: "#",
38+
mode: CodeMirror.getMode({}, "markdown"),
39+
parseDelimiters: true,
40+
innerStyle: "q"
41+
});
42+
});
43+
44+
var mode2 = CodeMirror.getMode({}, "identical_delim_multiplex");
45+
46+
test.mode("identical_delimiters_with_parseDelimiters", mode2, [
47+
"[keyword let] [def x] [operator =] [q #foo][q&em *bar*][q #];"
48+
], "multiplexing")
3349
})();

0 commit comments

Comments
 (0)