Skip to content

Commit

Permalink
Fix indentation vatlab/sos-notebook#52
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo Peng committed Mar 30, 2018
1 parent 992a7f8 commit 5a633cc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
26 changes: 21 additions & 5 deletions src/codemirror-sos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,20 @@ CodeMirror.defineMode("sos", function(conf: CodeMirror.EditorConfiguration, pars
for (var i = 0; i < sosActions.length; i++) {
if (stream.match(sosActions[i])) {
// switch to submode?
state.sos_state = 'start ' + stream.current().slice(0, -1);
state.overlay_state.sigil = null;
if (stream.eol()) {
// really
let mode = findMode(stream.current().slice(0, -1).toLowerCase());
if (mode) {
state.sos_state = null;
state.inner_mode = CodeMirror.getMode(conf, mode);
state.inner_state = CodeMirror.startState(state.inner_mode);
} else {
state.sos_state = 'nomanland';
}
} else {
state.sos_state = 'start ' + stream.current().slice(0, -1);
state.overlay_state.sigil = null;
}
return "builtin strong";
}
}
Expand Down Expand Up @@ -420,11 +432,14 @@ CodeMirror.defineMode("sos", function(conf: CodeMirror.EditorConfiguration, pars
let found = stream.match(/expand\s*=\s*"(\S+) (\S+)"/, false);
if (!found)
found = stream.match(/expand\s*=\s*'(\S+) (\S+)'/, false);
if (found)
if (found) {
state.overlay_state.sigil = {
'left': found[1],
'right': found[2]
}
} else {
state.overlay_state.sigil = null;
}
}
// if it is end of line, ending the starting switch mode
if (stream.eol() && sl !== ',') {
Expand Down Expand Up @@ -479,7 +494,7 @@ CodeMirror.defineMode("sos", function(conf: CodeMirror.EditorConfiguration, pars
indent: function(state, textAfter) {
// inner indent
if (state.inner_mode) {
return state.inner_mode.indent(state.inner_mode, textAfter) + 4;
return state.inner_mode.indent(state.inner_mode, textAfter) + 2;
} else {
return 0;
}
Expand All @@ -493,7 +508,8 @@ CodeMirror.defineMode("sos", function(conf: CodeMirror.EditorConfiguration, pars
},

lineComment: "#",
fold: "indent"
fold: "indent",
electricInput: /^\s*[\}\]\)]$/,
};
};
}, "python");
Expand Down
31 changes: 24 additions & 7 deletions src/sos-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@
// inner indent
if (!state.sos_mode) {
if (!base_mode.indent) return CodeMirror.Pass;
// inner mode will autoamtically indent + 4
return base_mode.indent(state.base_state, textAfter);
} else {
// sos mode has no indent
Expand Down Expand Up @@ -365,8 +364,20 @@
for (var i = 0; i < sosActions.length; i++) {
if (stream.match(sosActions[i])) {
// switch to submode?
state.sos_state = 'start ' + stream.current().slice(0, -1);
state.overlay_state.sigil = null;
if (stream.eol()) {
// really
let mode = findMode(stream.current().slice(0, -1).toLowerCase());
if (mode) {
state.sos_state = null;
state.inner_mode = CodeMirror.getMode(conf, mode);
state.inner_state = CodeMirror.startState(state.inner_mode);
} else {
state.sos_state = 'nomanland';
}
} else {
state.sos_state = 'start ' + stream.current().slice(0, -1);
state.overlay_state.sigil = null;
}
return "builtin strong";
}
}
Expand Down Expand Up @@ -423,11 +434,15 @@
let found = stream.match(/expand\s*=\s*"(\S+) (\S+)"/, false);
if (!found)
found = stream.match(/expand\s*=\s*'(\S+) (\S+)'/, false);
if (found)
if (found) {
state.overlay_state.sigil = {
'left': found[1],
'right': found[2]
}
} else {
state.overlay_state.sigil = null;
}

}
// if it is end of line, ending the starting switch mode
if (stream.eol() && sl !== ',') {
Expand Down Expand Up @@ -482,9 +497,10 @@
indent: function(state, textAfter) {
// inner indent
if (state.inner_mode) {
return state.inner_mode.indent(state.inner_mode, textAfter) + 4;
if (!state.inner_mode.indent) return CodeMirror.Pass;
return state.inner_mode.indent(state.inner_mode, textAfter) + 2;
} else {
return 0;
return base_mode.indent(state.base_state, textAfter);
}
},

Expand All @@ -496,7 +512,8 @@
},

lineComment: "#",
fold: "indent"
fold: "indent",
electricInput: /^\s*[\}\]\)]$/,
};
};
}, "python");
Expand Down

0 comments on commit 5a633cc

Please sign in to comment.