Debian Codegraph @

interactive-notebook/codemirror/lib/plugin.js

raw
1 // The jupyterlab extension
2 // Copyright (C) 2023 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8 // Based on: https://github.dev/WardBrian/jupyterlab-stan-highlight
9 import './stap'
10 import { INotebookTracker } from '@jupyterlab/notebook';
11 import { CodeCell } from '@jupyterlab/cells';
12
13 function registerStapFileType(app) {
14 app.docRegistry.addFileType({
15 name: 'systemtap',
16 displayName: 'Systemtap',
17 extensions: ['stp'],
18 mimeTypes: ['text/x-systemtap'],
19 });
20 }
21
22 function highlightStapCell(cell) {
23 if (cell && cell instanceof CodeCell) {
24 let editor = cell.editor;
25 let magic = editor.getLine(0)
26 if (magic?.trim().startsWith("%%python")) {
27 editor.setOption("mode", "text/x-python");
28 }else{
29 editor.setOption("mode", "text/x-systemtap");
30 }
31 // For options https://codemirror.net/5/doc/manual.html#Configuration
32 editor.setOption("indentUnit", 2);
33 editor.setOption('firstLineNumber', 0);
34 editor.setOption('lineNumbers', true);
35 editor.setOption('smartIndent', true);
36 }
37 }
38
39 export default [{
40 id: 'jupyterlab-stap-highlight',
41 autoStart: true,
42 requires: [INotebookTracker],
43 activate: function (app, tracker) {
44 console.log('JupyterLab extension jupyterlab-stap-highlight is activated');
45 registerStapFileType(app);
46
47 tracker.currentChanged.connect((tr, nbPanel) => {
48 nbPanel.revealed.then((r) => {
49 nbPanel.content.widgets.forEach((cell, index, array) => {
50 highlightStapCell(cell);
51 });
52 });
53 });
54
55 tracker.activeCellChanged.connect(() => {
56 highlightStapCell(tracker.activeCell)
57 });
58
59 // highlight initally
60 tracker.widgetAdded.connect((tr, nbPanel) => {
61 nbPanel.revealed.then((r) => {
62 nbPanel.content.widgets.forEach((cell, index, array) => {
63 highlightStapCell(cell);
64 });
65 });
66 });
67 }
68 }];