codemirror-ipython.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import CodeMirror from 'codemirror';
  4. import 'codemirror/mode/meta';
  5. import 'codemirror/mode/python/python';
  6. /**
  7. * Define an IPython codemirror mode.
  8. *
  9. * It is a slightly altered Python Mode with a `?` operator.
  10. */
  11. CodeMirror.defineMode(
  12. 'ipython',
  13. (config: CodeMirror.EditorConfiguration, modeOptions?: any) => {
  14. let pythonConf: any = {};
  15. for (let prop in modeOptions) {
  16. if (modeOptions.hasOwnProperty(prop)) {
  17. pythonConf[prop] = modeOptions[prop];
  18. }
  19. }
  20. pythonConf.name = 'python';
  21. pythonConf.singleOperators = new RegExp('^[\\+\\-\\*/%&|@\\^~<>!\\?]');
  22. pythonConf.identifiers = new RegExp(
  23. '^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*'
  24. );
  25. return CodeMirror.getMode(config, pythonConf);
  26. },
  27. 'python'
  28. );
  29. CodeMirror.defineMIME('text/x-ipython', 'ipython');
  30. CodeMirror.modeInfo.push({
  31. ext: [],
  32. mime: 'text/x-ipython',
  33. mode: 'ipython',
  34. name: 'ipython'
  35. });