How to use the @jupyterlab/codemirror.Mode.ensure function in @jupyterlab/codemirror

To help you get started, we’ve selected a few @jupyterlab/codemirror examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jupyterlab / jupyterlab / tests / test-codemirror / src / mode.spec.ts View on Github external
it('should add a spec loader', async () => {
      let called = 0;
      let loaded = 0;

      Mode.addSpecLoader(async spec => {
        called++;
        if (spec.mode !== 'bar') {
          return false;
        }
        loaded++;
        return true;
      }, 42);

      CodeMirror.modeInfo.push(fakeMode('bar'));

      let spec = await Mode.ensure('bar');
      expect(called).to.equal(1);
      expect(loaded).to.equal(1);
      expect(spec!.name).to.equal('BAR');

      spec = await Mode.ensure('python');
      expect(called).to.equal(1);
      expect(loaded).to.equal(1);

      try {
        spec = await Mode.ensure('APL');
      } catch (err) {
        // apparently one cannot use webpack `require` in jest
      }
      expect(called).to.equal(2);
      expect(loaded).to.equal(1);
    });
github jupyterlab / jupyterlab / tests / test-codemirror / src / mode.spec.ts View on Github external
called++;
        if (spec.mode !== 'bar') {
          return false;
        }
        loaded++;
        return true;
      }, 42);

      CodeMirror.modeInfo.push(fakeMode('bar'));

      let spec = await Mode.ensure('bar');
      expect(called).to.equal(1);
      expect(loaded).to.equal(1);
      expect(spec!.name).to.equal('BAR');

      spec = await Mode.ensure('python');
      expect(called).to.equal(1);
      expect(loaded).to.equal(1);

      try {
        spec = await Mode.ensure('APL');
      } catch (err) {
        // apparently one cannot use webpack `require` in jest
      }
      expect(called).to.equal(2);
      expect(loaded).to.equal(1);
    });
github yuvipanda / simplest-notebook / packages / rendermime / src / renderers.ts View on Github external
highlight: (code, lang, callback) => {
        let cb = (err: Error | null, code: string) => {
          if (callback) {
            callback(err, code);
          }
          return code;
        };
        if (!lang) {
          // no language, no highlight
          return cb(null, code);
        }
        Mode.ensure(lang)
          .then(spec => {
            let el = document.createElement('div');
            if (!spec) {
              console.log(`No CodeMirror mode: ${lang}`);
              return cb(null, code);
            }
            try {
              Mode.run(code, spec.mime, el);
              return cb(null, el.innerHTML);
            } catch (err) {
              console.log(`Failed to highlight ${lang} code`, err);
              return cb(err, code);
            }
          })
          .catch(err => {
            console.log(`No CodeMirror mode: ${lang}`);
github jupyterlab / jupyterlab / packages / rendermime / src / widgets.ts View on Github external
highlight: (code, lang, callback) => {
        if (!lang) {
            // no language, no highlight
            if (callback) {
                callback(null, code);
                return;
            } else {
                return code;
            }
        }
        Mode.ensure(lang).then(spec => {
          let el = document.createElement('div');
          if (!spec) {
              console.log(`No CodeMirror mode: ${lang}`);
              callback(null, code);
              return;
          }
          try {
            Mode.run(code, spec.mime, el);
            callback(null, el.innerHTML);
          } catch (err) {
            console.log(`Failed to highlight ${lang} code`, err);
            callback(err, code);
          }
        }).catch(err => {
          console.log(`No CodeMirror mode: ${lang}`);
          console.log(`Require CodeMirror mode error: ${err}`);
github jupyterlab / jupyterlab / packages / rendermime / src / renderers.ts View on Github external
highlight: (code, lang, callback) => {
        if (!lang) {
            // no language, no highlight
            if (callback) {
                callback(null, code);
                return;
            } else {
                return code;
            }
        }
        Mode.ensure(lang).then(spec => {
          let el = document.createElement('div');
          if (!spec) {
              console.log(`No CodeMirror mode: ${lang}`);
              callback(null, code);
              return;
          }
          try {
            Mode.run(code, spec.mime, el);
            callback(null, el.innerHTML);
          } catch (err) {
            console.log(`Failed to highlight ${lang} code`, err);
            callback(err, code);
          }
        }).catch(err => {
          console.log(`No CodeMirror mode: ${lang}`);
          console.log(`Require CodeMirror mode error: ${err}`);
github jupyterlab / jupyterlab-data-explorer / packages / rendermime / src / renderers.ts View on Github external
highlight: (code, lang, callback) => {
        let cb = (err: Error | null, code: string) => {
          if (callback) {
            callback(err, code);
          }
          return code;
        };
        if (!lang) {
          // no language, no highlight
          return cb(null, code);
        }
        Mode.ensure(lang)
          .then(spec => {
            let el = document.createElement('div');
            if (!spec) {
              console.log(`No CodeMirror mode: ${lang}`);
              return cb(null, code);
            }
            try {
              Mode.run(code, spec.mime, el);
              return cb(null, el.innerHTML);
            } catch (err) {
              console.log(`Failed to highlight ${lang} code`, err);
              return cb(err, code);
            }
          })
          .catch(err => {
            console.log(`No CodeMirror mode: ${lang}`);
github jupyterlab / jupyterlab / tests / test-codemirror / src / mode.spec.ts View on Github external
it('should load a defined spec', async () => {
      CodeMirror.modeInfo.push(fakeMode('foo'));
      CodeMirror.defineMode('foo', () => {
        return {};
      });
      let spec = (await Mode.ensure('text/foo'))!;
      expect(spec.name).to.equal('FOO');
    });
github jupyterlab / jupyterlab / tests / test-codemirror / src / mode.spec.ts View on Github external
it('should load a bundled spec', async () => {
      let spec = (await Mode.ensure('application/json'))!;
      expect(spec.name).to.equal('JSON');
    });
github minrk / thebelab / src / thebelab.js View on Github external
Widget.attach(outputArea, theDiv);

  const mode = $element.data("language") || "python";
  const required = {
    value: source,
    mode: mode,
    extraKeys: {
      "Shift-Enter": execute,
    },
  };
  let codeMirrorConfig = Object.assign(
    mergedOptions.codeMirrorconfig || {},
    required
  );
  let cm = new CodeMirror($cm_element[0], codeMirrorConfig);
  Mode.ensure(mode).then(modeSpec => {
    cm.setOption("mode", mode);
  });
  return $cell;
}