forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode.ts
203 lines (176 loc) · 4.96 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { ArrayExt } from '@phosphor/algorithm';
import { JSONValue } from '@phosphor/coreutils';
import { IEditorMimeTypeService } from '@jupyterlab/codeeditor';
import 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 modeInfo spec.
*/
export interface ISpec {
ext?: string[];
name?: string;
mode: string;
mime: string;
}
/**
* The interface of a codemirror mode spec.
*/
export interface IMode {
name: string;
[key: string]: JSONValue;
}
/**
* The interface for a codemirror spec resolver.
*/
export interface ISpecLoader {
/**
* A function which returns whether it was successfully loaded
*/
(spec: ISpec): Promise<boolean>;
}
let specLoaders: Private.IRankItem[] = [
{
// Simplest, cheapest check by mode name.
loader: async spec => CodeMirror.modes.hasOwnProperty(spec.mode),
rank: 0
},
{
// Fetch the mode asynchronously.
loader: function(spec) {
return new Promise<boolean>((resolve, reject) => {
// An arrow function below seems to miscompile in our current webpack to
// invalid js.
require([`codemirror/mode/${spec.mode}/${spec.mode}.js`], function() {
resolve(true);
});
});
},
rank: 99
}
];
/**
* 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 async function ensure(mode: string | ISpec): Promise<ISpec> {
let spec = findBest(mode);
for (let specLoader of specLoaders) {
if (await specLoader.loader(spec)) {
return spec;
}
}
return null;
}
export function addSpecLoader(loader: ISpecLoader, rank: number) {
let item = { loader, rank };
let index = ArrayExt.upperBound(specLoaders, item, Private.itemCmp);
ArrayExt.insert(specLoaders, index, item);
}
/**
* 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;
}
}
}
}
namespace Private {
export interface IRankItem {
/**
* The loader for the item
*/
loader: Mode.ISpecLoader;
/**
* The sort rank of the widget.
*/
rank: number;
}
/**
* A less-than comparison function for the loader rank
*/
export function itemCmp(first: IRankItem, second: IRankItem): number {
return first.rank - second.rank;
}
}