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