Skip to content

Commit

Permalink
#5742 support line magics not only in first line of cell
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Jurowicz committed Jul 6, 2018
1 parent cca204f commit d9fd953
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
32 changes: 16 additions & 16 deletions js/lab/src/plugin/codeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,8 @@ const lineMagicOverlay = {
},

token(stream, state) {
if (!state.firstMatched) {
state.firstMatched = true;

if (stream.match(/^%\w+/, false)) {
state.inMagicLine = true;
}
if (stream.match(/^%(%classpath|%spark|\w+)/, false)) {
state.inMagicLine = true;
}

if (state.inMagicLine) {
Expand All @@ -104,22 +100,26 @@ const lineMagicOverlay = {

export function autoHighlightLineMagics(code_mirror) {
const current_mode = code_mirror.getOption('mode');
const first_line = code_mirror.getLine(0);
const re = /^%\w+/;

if (current_mode === LINE_MAGIC_MODE) {
return;
}

if (first_line.match(re) !== null) {
// Add an overlay mode to recognize the first line as "line magic" instead
// of the mode used for the rest of the cell.
CodeMirror.defineMode(LINE_MAGIC_MODE, (config) => {
return CodeMirror.overlayMode(CodeMirror.getMode(config, current_mode), lineMagicOverlay);
});
const re = /^%(%classpath|%spark|\w+)/;

code_mirror.setOption('mode', LINE_MAGIC_MODE);
}
code_mirror.eachLine((line) => {
if (line && line.text.match(re) !== null) {
// Add an overlay mode to recognize the first line as "line magic" instead
// of the mode used for the rest of the cell.
CodeMirror.defineMode(LINE_MAGIC_MODE, (config) => {
return CodeMirror.overlayMode(CodeMirror.getMode(config, current_mode), lineMagicOverlay);
});

code_mirror.setOption('mode', LINE_MAGIC_MODE);

return false;
}
});
}

export function addLineMagicsOverlay(editor: any) {
Expand Down
28 changes: 16 additions & 12 deletions js/notebook/src/extension/codeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ const lineMagicOverlay = {
},

token(stream, state) {
if (!state.firstMatched) {
state.firstMatched = true;

if (stream.match(/^%(%classpath|%spark|\w+)/)) {
state.inMagicLine = true;
}
if (stream.match(/^%(%classpath|%spark|\w+)/)) {
state.inMagicLine = true;
}

if (state.inMagicLine) {
Expand All @@ -99,13 +95,21 @@ export function autoHighlightLineMagics(code_mirror) {
return;
}

// Add an overlay mode to recognize the first line as "line magic" instead
// of the mode used for the rest of the cell.
CodeMirror.defineMode(LINE_MAGIC_MODE, (config) => {
return CodeMirror.overlayMode(CodeMirror.getMode(config, current_mode), lineMagicOverlay);
});
const re = /^%(%classpath|%spark|\w+)/;

code_mirror.eachLine((line) => {
if (line && line.text.match(re) !== null) {
// Add an overlay mode to recognize the first line as "line magic" instead
// of the mode used for the rest of the cell.
CodeMirror.defineMode(LINE_MAGIC_MODE, (config) => {
return CodeMirror.overlayMode(CodeMirror.getMode(config, current_mode), lineMagicOverlay);
});

code_mirror.setOption('mode', LINE_MAGIC_MODE);
code_mirror.setOption('mode', LINE_MAGIC_MODE);

return false;
}
});
}

export function addLineMagicsOverlay(code_mirror) {
Expand Down

0 comments on commit d9fd953

Please sign in to comment.