-
Notifications
You must be signed in to change notification settings - Fork 205
/
typescriptGrammar.ts
252 lines (218 loc) · 9.79 KB
/
typescriptGrammar.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
///ts:ref=globals
/// <reference path="../../globals.ts"/> ///ts:ref:generated
// Help:
// https://github.com/atom/first-mate/
// https://github.com/fdecampredon/brackets-typescript/blob/master/src/main/mode.ts
// https://github.com/p-e-w/language-javascript-semantic/blob/master/lib/javascript-semantic-grammar.coffee
import ts = require('typescript');
import TokenClass = ts.TokenClass;
declare class AtomTSBaseGrammar {
constructor(registry, config)
}
interface AtomTSTokens { tokens: any /* Atom's Token */[]; ruleStack: any[] }
interface TSTokens { tokens: { style: string; str: string }[]; ruleStack: any[] }
// This should be
// {Grammar} = require "first-mate"
// but doing so throws "Error: Cannot find module 'first-mate'"
global.AtomTSBaseGrammar = require((<any> atom).config.resourcePath + "/node_modules/first-mate/lib/grammar.js");
export class TypeScriptSemanticGrammar extends AtomTSBaseGrammar {
constructor(public registry) {
super(registry,
{
name: "TypeScript",
scopeName: "source.ts",
patterns: {
// include: 'source.js' // Just makes us slower :P
},
fileTypes: ['ts']
});
}
/** only set to true if we have a trailingWhiteSpace for the currenlty analyzed line */
trailingWhiteSpaceLength = 0;
tokenizeLine(line: string, ruleStack: any[], firstLine = false): AtomTSTokens {
// BOM handling:
// NOTE THERE ARE OTHER BOMS. I just wanted a proof of concept.
// Feel free to add here if you know of ones that are giving you pain.
if (firstLine
&& line.length > 1
&& (line.charCodeAt(0) == 0xFFFE || line.charCodeAt(0) == 0xFEFF)) {
this.trailingWhiteSpaceLength = 1;
}
else {
this.trailingWhiteSpaceLength = 0;
}
// Note: the Atom Tokenizer supports multiple nesting of ruleStacks
// The TypeScript tokenizer has a single final state it cares about
// So we only need to pass it the final lex state
var finalLexState = firstLine ? ts.EndOfLineState.Start
: ruleStack.length ? ruleStack[0]
: ts.EndOfLineState.Start;
// If we are in some TS tokenizing process use TS tokenizer
// Otherwise use the specific ones we match
// Otherwise fall back to TS tokenizer
if (finalLexState !== ts.EndOfLineState.Start) {
return this.getAtomTokensForLine(line, finalLexState);
}
if (line.match(this.fullTripleSlashReferencePathRegEx)) {
return this.getfullTripleSlashReferencePathTokensForLine(line);
}
else if (line.match(this.importRequireRegex)) {
return this.getImportRequireTokensForLine(line);
}
else if (line.match(this.es6importRegex)) {
return this.getEs6importTokensForLine(line);
}
else {
return this.getAtomTokensForLine(line, finalLexState);
}
}
///////////////////
////////////////////////////////// THE REMAINING CODE IS SPECIFIC TO US ////////////////////////////////////////
///////////////////
classifier: ts.Classifier = ts.createClassifier();
// Useful to tokenize these differently for autocomplete ease
fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*<reference\s+path\s*=\s*)('|")(.+?)\2.*?\/>/;
// Note this will not match multiple imports on same line. So shame on you
importRequireRegex = /^import\s*(\w*)\s*=\s*require\((?:'|")(\S*)(?:'|")\.*\)/;
// es6
es6importRegex = /^import.*from.*/;
getfullTripleSlashReferencePathTokensForLine(line: string): AtomTSTokens {
var tsTokensWithRuleStack = this.getTsTokensForLine(line);
var matches = line.match(this.fullTripleSlashReferencePathRegEx);
if (matches[3]) {
var path = matches[3];
if (line.search('"' + path + '"') != -1) {
path = '"' + path + '"';
}
else {
path = "'" + path + "'";
}
var startPosition = line.search(path);
var endPosition = startPosition + path.length;
var atomTokens = [];
atomTokens.push(this.registry.createToken(line.substr(0, startPosition), ['source.ts', 'keyword']));
atomTokens.push(this.registry.createToken(line.substr(startPosition, path.length), ['source.ts', 'reference.path.string']));
atomTokens.push(this.registry.createToken(line.substr(endPosition, line.length - endPosition), ['source.ts', 'keyword']));
return { tokens: atomTokens, ruleStack: [] };
}
else {
return this.convertTsTokensToAtomTokens(tsTokensWithRuleStack);
}
}
getImportRequireTokensForLine(line: string): { tokens: any /* Atom's Token */[]; ruleStack: any[] } {
var tsTokensWithRuleStack = this.getTsTokensForLine(line);
// Based on ts tokenizer we should have a single "identifier" and a single "string"
// Update these tokens to be more specific
tsTokensWithRuleStack.tokens.forEach(t=> {
if (t.style == "identifier") {
t.style = "require.identifier";
}
if (t.style == "string") {
t.style = "require.path.string";
}
});
return this.convertTsTokensToAtomTokens(tsTokensWithRuleStack);
}
getEs6importTokensForLine(line: string): { tokens: any /* Atom's Token */[]; ruleStack: any[] } {
var tsTokensWithRuleStack = this.getTsTokensForLine(line);
// Based on ts tokenizer we should have a few "identifiers" and a single "string"
// Update these tokens to be more specific
tsTokensWithRuleStack.tokens.forEach(t=> {
if (t.style == "identifier") {
t.style = "es6import.identifier";
}
if (t.style == "string") {
t.style = "es6import.path.string";
}
});
return this.convertTsTokensToAtomTokens(tsTokensWithRuleStack);
}
getTsTokensForLine(line: string, finalLexState: ts.EndOfLineState = ts.EndOfLineState.Start)
: TSTokens {
var output = this.classifier.getClassificationsForLine(line, finalLexState, true);
var ruleStack = [output.finalLexState];
var classificationResults = output.entries;
// TypeScript classifier returns empty for "". But Atom wants to have some Token and it needs to be "whitespace" for autoindent to work
if (!classificationResults.length) return { tokens: [{ style: 'whitespace', str: '' }], ruleStack: ruleStack };
// Start with trailing whitespace taken into account.
// This is needed because classification for that is already done by ATOM internally (somehow)
var totalLength = this.trailingWhiteSpaceLength;
var tokens = classificationResults.map((info) => {
var tokenStartPosition = totalLength;
var str = line.substr(tokenStartPosition, info.length);
totalLength = totalLength + info.length;
var style = getAtomStyleForToken(info, str);
return { style: style, str: str };
});
return { tokens, ruleStack };
}
getAtomTokensForLine(line: string, finalLexState: ts.EndOfLineState): AtomTSTokens {
var tsTokensWithRuleStack = this.getTsTokensForLine(line, finalLexState);
return this.convertTsTokensToAtomTokens(tsTokensWithRuleStack);
}
convertTsTokensToAtomTokens(tsTokensWithRuleStack: TSTokens): AtomTSTokens {
var tokens = tsTokensWithRuleStack.tokens.map((info) => {
var atomToken = this.registry.createToken(info.str, ["source.ts", info.style]);
return atomToken;
});
return { tokens, ruleStack: tsTokensWithRuleStack.ruleStack };
}
}
/// NOTE: best way I have found for these is to just look at theme "less" files
// Alternatively just inspect the token for a .js file
function getAtomStyleForToken(token: ts.ClassificationInfo, str: string): string {
switch (token.classification) {
case TokenClass.Punctuation:
switch(str){
case '{':
return "punctuation.section.scope.begin.ts";
case '}':
return "punctuation.section.scope.end.ts";
case ')':
return "meta.brace.round.ts";
case '(':
return "meta.brace.round.ts";
default:
return 'punctuation';
}
case TokenClass.Keyword:
switch (str) {
case 'static':
case 'public':
case 'private':
case 'export':
case 'get':
case 'set':
return 'support.function';
case 'class':
case 'module':
case 'var':
return 'storage.modifier';
case 'function':
return 'storage.type.function';
case 'string':
case 'number':
case 'void':
case 'boolean':
return 'keyword';
default:
return 'keyword';
}
case TokenClass.Operator:
return 'keyword.operator.js';
case TokenClass.Comment:
return 'comment';
case TokenClass.Whitespace:
return 'whitespace';
case TokenClass.Identifier:
return 'identifier';
case TokenClass.NumberLiteral:
return 'constant.numeric';
case TokenClass.StringLiteral:
return 'string';
case TokenClass.RegExpLiteral:
return 'constant.character';
default:
return null; // This should not happen
}
}