-
Notifications
You must be signed in to change notification settings - Fork 3
/
text-mate.ts
80 lines (75 loc) · 2.12 KB
/
text-mate.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
import * as path from 'path';
const tm = require(path.join(require.main.filename, '../../node_modules/vscode-textmate/release/main.js'));
export function matchScope(scope: string, scopes: string[]) : boolean {
if(!scope)
return true;
const parts = scope.split(/\s+/);
let idx = 0;
for(let part of parts) {
while(idx < scopes.length && !scopes[idx].startsWith(part))
++idx;
if(idx >= scopes.length)
return false;
++idx;
}
return true;
}
// export type Registry = N.Registry;
export const Registry : Registry = tm.Registry;
export interface Registry {
new (locator?: IGrammarLocator);
/**
* Load the grammar for `scopeName` and all referenced included grammars asynchronously.
*/
loadGrammar(initialScopeName: string, callback: (err: any, grammar: IGrammar) => void): void;
/**
* Load the grammar at `path` synchronously.
*/
loadGrammarFromPathSync(path: string): IGrammar;
/**
* Get the grammar for `scopeName`. The grammar must first be created via `loadGrammar` or `loadGrammarFromPathSync`.
*/
grammarForScopeName(scopeName: string): IGrammar;
}
/**
* A registry helper that can locate grammar file paths given scope names.
*/
export interface IGrammarLocator {
getFilePath(scopeName: string): string;
getInjections?(scopeName: string): string[];
}
export interface IGrammarInfo {
readonly fileTypes: string[];
readonly name: string;
readonly scopeName: string;
readonly firstLineMatch: string;
}
/**
* A grammar
*/
export interface IGrammar {
/**
* Tokenize `lineText` using previous line state `prevState`.
*/
tokenizeLine(lineText: string, prevState: StackElement): ITokenizeLineResult;
}
export interface ITokenizeLineResult {
readonly tokens: IToken[];
/**
* The `prevState` to be passed on to the next line tokenization.
*/
readonly ruleStack: StackElement;
}
export interface IToken {
startIndex: number;
readonly endIndex: number;
readonly scopes: string[];
}
/**
* **IMPORTANT** - Immutable!
*/
export interface StackElement {
_stackElementBrand: void;
readonly _parent: StackElement;
equals(other: StackElement): boolean;
}