mode.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. IEditorMimeTypeService
  5. } from '@jupyterlab/codeeditor';
  6. import * as CodeMirror
  7. from 'codemirror';
  8. import 'codemirror/mode/meta';
  9. import 'codemirror/addon/runmode/runmode';
  10. import './codemirror-ipython';
  11. import './codemirror-ipythongfm';
  12. // Bundle other common modes
  13. import 'codemirror/mode/javascript/javascript';
  14. import 'codemirror/mode/css/css';
  15. import 'codemirror/mode/julia/julia';
  16. import 'codemirror/mode/r/r';
  17. import 'codemirror/mode/markdown/markdown';
  18. import 'codemirror/mode/clike/clike';
  19. import 'codemirror/mode/shell/shell';
  20. import 'codemirror/mode/sql/sql';
  21. import {
  22. PathExt
  23. } from '@jupyterlab/coreutils';
  24. // Stub for the require function.
  25. declare var require: any;
  26. /**
  27. * The namespace for CodeMirror Mode functionality.
  28. */
  29. export
  30. namespace Mode {
  31. /**
  32. * The interface of a codemirror mode spec.
  33. */
  34. export
  35. interface ISpec {
  36. ext?: string[];
  37. name?: string;
  38. mode: string;
  39. mime: string;
  40. }
  41. /**
  42. * Get the raw list of available modes specs.
  43. */
  44. export
  45. function getModeInfo(): ISpec[] {
  46. return CodeMirror.modeInfo;
  47. }
  48. /**
  49. * Running a CodeMirror mode outside of an editor.
  50. */
  51. export
  52. function run(code: string, mode: string | ISpec, el: HTMLElement): void {
  53. CodeMirror.runMode(code, mode, el);
  54. }
  55. /**
  56. * Ensure a codemirror mode is available by name or Codemirror spec.
  57. *
  58. * @param mode - The mode to ensure. If it is a string, uses [findBest]
  59. * to get the appropriate spec.
  60. *
  61. * @returns A promise that resolves when the mode is available.
  62. */
  63. export
  64. function ensure(mode: string | ISpec): Promise<ISpec> {
  65. let spec = findBest(mode);
  66. // Simplest, cheapest check by mode name.
  67. if (CodeMirror.modes.hasOwnProperty(spec.mode)) {
  68. return Promise.resolve(spec);
  69. }
  70. // Fetch the mode asynchronously.
  71. return new Promise<ISpec>((resolve, reject) => {
  72. require([`codemirror/mode/${spec.mode}/${spec.mode}.js`], () => {
  73. resolve(spec);
  74. });
  75. });
  76. }
  77. /**
  78. * Find a codemirror mode by name or CodeMirror spec.
  79. */
  80. export
  81. function findBest(mode: string | ISpec): ISpec {
  82. let modename = (typeof mode === 'string') ? mode :
  83. mode.mode || mode.name;
  84. let mimetype = (typeof mode !== 'string') ? mode.mime : modename;
  85. let ext = (typeof mode !== 'string') ? mode.ext : [];
  86. return (
  87. CodeMirror.findModeByName(modename || '') ||
  88. CodeMirror.findModeByMIME(mimetype || '') ||
  89. findByExtension(ext) ||
  90. CodeMirror.findModeByMIME(IEditorMimeTypeService.defaultMimeType) ||
  91. CodeMirror.findModeByMIME('text/plain')
  92. );
  93. }
  94. /**
  95. * Find a codemirror mode by MIME.
  96. */
  97. export
  98. function findByMIME(mime: string): ISpec {
  99. return CodeMirror.findModeByMIME(mime);
  100. }
  101. /**
  102. * Find a codemirror mode by name.
  103. */
  104. export
  105. function findByName(name: string): ISpec {
  106. return CodeMirror.findModeByName(name);
  107. }
  108. /**
  109. * Find a codemirror mode by filename.
  110. */
  111. export
  112. function findByFileName(name: string): ISpec {
  113. let basename = PathExt.basename(name);
  114. return CodeMirror.findModeByFileName(basename);
  115. }
  116. /**
  117. * Find a codemirror mode by extension.
  118. */
  119. export
  120. function findByExtension(ext: string | string[]): ISpec {
  121. if (typeof ext === 'string') {
  122. return CodeMirror.findModeByExtension(name);
  123. }
  124. for (let i = 0; i < ext.length; i++) {
  125. let mode = CodeMirror.findModeByExtension(ext[i]);
  126. if (mode) {
  127. return mode;
  128. }
  129. }
  130. }
  131. }