Skip to content

Commit

Permalink
#5742 define line magic highlighter mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Jurowicz committed Jul 5, 2018
1 parent 1c8d0c8 commit 3ae9425
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 15 deletions.
3 changes: 2 additions & 1 deletion js/notebook/src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ define([
if (inNotebook) {
// setup things to run on loading config/notebook

bxCodeMirror.extendHighlightModes(Jupyter, CodeMirror);
CodeMirror.defineInitHook(bxCodeMirror.addLineMagicsOverlay);
bxCodeMirror.extendHighlightModes(Jupyter);
GroovyMode.extendWithLineComment(Jupyter, CodeMirror);

Jupyter.notebook.config.loaded
Expand Down
82 changes: 70 additions & 12 deletions js/notebook/src/extension/codeMirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,78 @@
* limitations under the License.
*/

export function extendHighlightModes(Jupyter: any, CodeMirror: any) {
Jupyter.CodeCell.options_default.highlight_modes = _.extend(
Jupyter.CodeCell.options_default.highlight_modes,
{
magic_groovy: { reg: ['^%%groovy'] },
magic_java: { reg: ['^%%java'] },
magic_scala: { reg: ['^%%scala'] },
magic_kotlin: { reg: ['^%%kotlin'] },
magic_clojure: { reg: ['^%%clojure'] },
magic_sql: { reg: ['^%%sql'] }
/// <reference path='../types/index.d.ts'/>

export function extendHighlightModes(Jupyter: any) {
Jupyter.CodeCell.options_default.highlight_modes = {
...Jupyter.CodeCell.options_default.highlight_modes,
magic_python: {reg: ['^%%python']},
magic_groovy: {reg: ['^%%groovy']},
magic_java: {reg: ['^%%java']},
magic_scala: {reg: ['^%%scala']},
magic_kotlin: {reg: ['^%%kotlin']},
magic_clojure: {reg: ['^%%clojure']},
magic_sql: {reg: ['^%%sql']},
magic_html: {reg: ['^%%html']}
};
}

const lineMagicOverlay = {
startState() {
return { firstMatched: false, inMagicLine: false };
},

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

if (stream.match("%", false)) {
state.inMagicLine = true;
}
}

if (state.inMagicLine) {
stream.eat(() => {
return true;
});

if (stream.eol()) {
state.inMagicLine = false;
}

return "line_magic";
}
);

stream.skipToEnd();

return null;
}
};

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

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

code_mirror.setOption('mode', 'line_magic');
}
}

export function addLineMagicsOverlay(code_mirror) {
autoHighlightLineMagics(code_mirror);
code_mirror.on("focus", () => autoHighlightLineMagics(code_mirror));
code_mirror.on("change", () => autoHighlightLineMagics(code_mirror));
code_mirror.on("blur", () => autoHighlightLineMagics(code_mirror));
}

export default {
extendHighlightModes
extendHighlightModes,
addLineMagicsOverlay
};
7 changes: 5 additions & 2 deletions js/notebook/src/extension/groovyModeExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
* limitations under the License.
*/

import {addLineMagicsOverlay} from "./codeMirror";

export namespace GroovyMode {
export const LINE_COMMENT_CHAR = '//';

export function setCodeMirrorLineComment(cell: any) {
export function setCodeMirrorLineComment(cell: any, CodeMirror) {
if (cell.cell_type !== 'code') {
return;
}
Expand All @@ -32,11 +34,12 @@ export namespace GroovyMode {
}

cell.auto_highlight();
addLineMagicsOverlay(cm);
}

export function extendWithLineComment(Jupyter: any, CodeMirror: any) {
CodeMirror.extendMode('groovy', { lineComment: LINE_COMMENT_CHAR });

Jupyter.notebook.get_cells().map(setCodeMirrorLineComment);
Jupyter.notebook.get_cells().map((cell) => setCodeMirrorLineComment(cell, CodeMirror));
}
}
1 change: 1 addition & 0 deletions js/notebook/src/types/global.env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface MapConstructor {
}

declare var Map: MapConstructor;
declare var CodeMirror: any;

declare interface NumberConstructor {
isNaN: (number: number) => boolean,
Expand Down

0 comments on commit 3ae9425

Please sign in to comment.