forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcellMatcher.ts
More file actions
88 lines (76 loc) · 3.21 KB
/
cellMatcher.ts
File metadata and controls
88 lines (76 loc) · 3.21 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import '../common/extensions';
import { IDataScienceSettings } from '../common/types';
import { noop } from '../common/utils/misc';
import { RegExpValues } from './constants';
export class CellMatcher {
private codeMatchRegEx: RegExp;
private markdownMatchRegEx: RegExp;
private codeExecRegEx: RegExp;
private markdownExecRegEx: RegExp;
private defaultCellMarker: string;
private defaultCellMarkerExec: RegExp;
constructor(settings?: IDataScienceSettings) {
this.codeMatchRegEx = this.createRegExp(
settings ? settings.codeRegularExpression : undefined,
RegExpValues.PythonCellMarker
);
this.markdownMatchRegEx = this.createRegExp(
settings ? settings.markdownRegularExpression : undefined,
RegExpValues.PythonMarkdownCellMarker
);
this.codeExecRegEx = new RegExp(`${this.codeMatchRegEx.source}(.*)`);
this.markdownExecRegEx = new RegExp(`${this.markdownMatchRegEx.source}(.*)`);
this.defaultCellMarker = settings?.defaultCellMarker ? settings.defaultCellMarker : '# %%';
this.defaultCellMarkerExec = this.createRegExp(`${this.defaultCellMarker}(.*)`, /# %%(.*)/);
}
public isCell(code: string): boolean {
return this.isCode(code) || this.isMarkdown(code);
}
public isMarkdown(code: string): boolean {
return this.markdownMatchRegEx.test(code);
}
public isCode(code: string): boolean {
return this.codeMatchRegEx.test(code) || code.trim() === this.defaultCellMarker;
}
public getCellType(code: string): string {
return this.isMarkdown(code) ? 'markdown' : 'code';
}
public stripFirstMarker(code: string): string {
const lines = code.splitLines({ trim: false, removeEmptyEntries: false });
// Only strip this off the first line. Otherwise we want the markers in the code.
if (lines.length > 0 && (this.isCode(lines[0]) || this.isMarkdown(lines[0]))) {
return lines.slice(1).join('\n');
}
return code;
}
public exec(code: string): string | undefined {
let result: RegExpExecArray | null = null;
if (this.defaultCellMarkerExec.test(code)) {
this.defaultCellMarkerExec.lastIndex = -1;
result = this.defaultCellMarkerExec.exec(code);
} else if (this.codeMatchRegEx.test(code)) {
this.codeExecRegEx.lastIndex = -1;
result = this.codeExecRegEx.exec(code);
} else if (this.markdownMatchRegEx.test(code)) {
this.markdownExecRegEx.lastIndex = -1;
result = this.markdownExecRegEx.exec(code);
}
if (result) {
return result.length > 1 ? result[result.length - 1].trim() : '';
}
return undefined;
}
private createRegExp(potential: string | undefined, backup: RegExp): RegExp {
try {
if (potential) {
return new RegExp(potential);
}
} catch {
noop();
}
return backup;
}
}