forked from twxs/vs.language.cmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.ts
638 lines (569 loc) · 23.2 KB
/
extension.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import {workspace, window, languages, ExtensionContext, TextDocument, DocumentSelector, Position, commands, LanguageConfiguration, CompletionItemKind, CompletionItem, SnippetString, CompletionItemProvider, Hover, HoverProvider, Disposable, CancellationToken} from 'vscode';
import util = require('util');
import child_process = require("child_process");
/// strings Helpers
function strContains(word, pattern) {
return word.indexOf(pattern) > -1;
}
function strEquals(word, pattern) {
return word == pattern;
}
/// configuration helpers
function config<T>(key: string, defaultValue?: any): T {
const cmake_conf = workspace.getConfiguration('cmake');
return cmake_conf.get<T>(key, defaultValue);
}
// copied from https://stackoverflow.com/questions/13796594/how-to-split-string-into-arguments-and-options-in-javascript
function commandArgs2Array(text: string): string[] {
const re = /^"[^"]*"$/; // Check if argument is surrounded with double-quotes
const re2 = /^([^"]|[^"].*?[^"])$/; // Check if argument is NOT surrounded with double-quotes
let arr = [];
let argPart = null;
text && text.split(" ").forEach(function(arg) {
if ((re.test(arg) || re2.test(arg)) && !argPart) {
arr.push(arg);
} else {
argPart = argPart ? argPart + " " + arg : arg;
// If part is complete (ends with a double quote), we can add it to the array
if (/"$/.test(argPart)) {
arr.push(argPart);
argPart = null;
}
}
});
return arr;
}
/// Cmake process helpers
// Simple helper function that invoke the CMAKE executable
// and return a promise with stdout
let cmake = (args: string[]): Promise<string> => {
return new Promise(function (resolve, reject) {
let cmake_config = config<string>('cmakePath', 'cmake');
let cmake_args = commandArgs2Array(cmake_config)
let cmd = child_process.spawn(cmake_args[0], cmake_args.slice(1, cmake_args.length)
.concat(args.map(arg => { return arg.replace(/\r/gm, ''); })));
let stdout: string = '';
cmd.stdout.on('data', function (data) {
var txt: string = data.toString();
stdout += txt.replace(/\r/gm, '');
});
cmd.on("error", function (error) {
if (error && (<any>error).code === 'ENOENT') {
window.showInformationMessage('The "cmake" command is not found in PATH. Install it or use `cmake.cmakePath` in the workspace settings to define the CMake executable binary.');
}
reject();
});
cmd.on('exit', function (code) {
resolve(stdout);
});
});
}
function _extractVersion(output: string): string {
let re = /cmake\s+version\s+(\d+.\d+.\d+)/;
if (re.test(output)) {
let result = re.exec(output);
return result[1];
}
return '';
}
async function cmake_version(): Promise<string> {
let cmd_output = await cmake(['--version']);
let version = _extractVersion(cmd_output);
return version;
}
// Return the url for the online help based on the cmake executable binary used
async function cmake_help_url() {
let base_url = 'https://cmake.org/cmake/help';
let version = await cmake_version();
if (version.length > 0) {
if (version >= '3.0') {
let re = /(\d+.\d+).\d+/;
version = version.replace(re, '$1/');
} else {
let older_versions = [
'2.8.12', '2.8.11', '2.8.10', '2.8.9', '2.8.8', '2.8.7', '2.8.6', '2.8.5', '2.8.4', '2.8.3', '2.8.2', '2.8.1', '2.8.0', '2.6'
];
if (older_versions.indexOf(version) == -1) {
version = 'latest/';
} else {
version = version + '/cmake.html';
}
}
} else {
version = 'latest/';
}
return base_url + '/v' + version;
}
// return the cmake command list
function cmake_help_command_list(): Promise<string> {
return cmake(['--help-command-list']);
}
function cmake_help_command(name: string): Promise<string> {
return cmake_help_command_list()
.then(function (result: string) {
let contains = result.indexOf(name) > -1;
return new Promise(function (resolve, reject) {
if (contains) {
resolve(name);
} else {
reject('not found');
}
});
}, function (e) { })
.then(function (n: string) {
return cmake(['--help-command', n]);
}, null);
}
function cmake_help_variable_list(): Promise<string> {
return cmake(['--help-variable-list']);
}
function cmake_help_variable(name: string): Promise<string> {
return cmake_help_variable_list()
.then(function (result: string) {
let contains = result.indexOf(name) > -1;
return new Promise(function (resolve, reject) {
if (contains) {
resolve(name);
} else {
reject('not found');
}
});
}, function (e) { }).then(function (name: string) { return cmake(['--help-variable', name]); }, null);
}
function cmake_help_property_list(): Promise<string> {
return cmake(['--help-property-list']);
}
function cmake_help_property(name: string): Promise<string> {
return cmake_help_property_list()
.then(function (result: string) {
let contains = result.indexOf(name) > -1;
return new Promise(function (resolve, reject) {
if (contains) {
resolve(name);
} else {
reject('not found');
}
});
}, function (e) { }).then(function (name: string) { return cmake(['--help-property', name]); }, null);
}
function cmake_help_module_list(): Promise<string> {
return cmake(['--help-module-list']);
}
function cmake_help_module(name: string): Promise<string> {
return cmake_help_module_list()
.then(function (result: string) {
let contains = result.indexOf(name) > -1;
return new Promise(function (resolve, reject) {
if (contains) {
resolve(name);
} else {
reject('not found');
}
});
}, function (e) { }).then(function (name: string) { return cmake(['--help-module', name]); }, null);
}
function cmake_help_all() {
let promises = {
'function': (name: string) => {
return cmake_help_command(name);
},
'module': (name: string) => {
return cmake_help_module(name);
},
'variable': (name: string) => {
return cmake_help_variable(name);
}
,
'property': (name: string) => {
return cmake_help_property(name);
}
};
return promises;
}
async function cmake_online_help(search: string) {
let url = await cmake_help_url();
let v2x = url.endsWith('html'); // cmake < 3.0
return Promise.all([
cmCommandsSuggestionsExact(search),
cmVariablesSuggestionsExact(search),
cmModulesSuggestionsExact(search),
cmPropertiesSuggestionsExact(search),
]).then(function (results) {
var opener = require("opener");
var suggestions = Array.prototype.concat.apply([], results);
if (suggestions.length == 0) {
search = search.replace(/[<>]/g, '');
if (v2x || search.length == 0) {
opener(url);
} else {
opener(url + 'search.html?q=' + search + '&check_keywords=yes&area=default');
}
} else {
let suggestion = suggestions[0];
let type = cmakeTypeFromvscodeKind(suggestion.kind);
if (type == 'property') {
if (v2x) {
opener(url);
} else {
// TODO : needs to filter properties per scope to detect the right URL
opener(url + 'search.html?q=' + search + '&check_keywords=yes&area=default');
}
} else {
if (type == 'function') {
type = 'command';
}
search = search.replace(/[<>]/g, '');
if(v2x){
opener(url + '#' + type + ':' + search);
}else {
opener(url + type + '/' + search + '.html');
}
}
}
});
}
// this method is called when your extension is activated. activation is
// controlled by the activation events defined in package.json
export function activate(disposables: Disposable[]) {
commands.registerCommand('cmake.onlineHelp', () => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
var editor = window.activeTextEditor;
if (!editor) {
return; // No open text editor
}
var selection = editor.selection;
let document = editor.document;
let position = selection.start;
var currentWord = document.getText(selection);
let wordAtPosition = document.getWordRangeAtPosition(position);
var currentWord = '';
if (wordAtPosition && wordAtPosition.start.character < position.character) {
var word = document.getText(wordAtPosition);
currentWord = word;
}
window.showInputBox({ prompt: 'Search on Cmake online documentation', placeHolder: currentWord }).then(function (result) {
if (typeof result != 'undefined') { // Escape
if (result.length === 0) { //
result = currentWord;
}
if (result != "") {
cmake_online_help(result);
}
}
});
});
const CMAKE_LANGUAGE = 'cmake';
const CMAKE_SELECTOR: DocumentSelector = [
{ language: CMAKE_LANGUAGE, scheme: 'file' },
{ language: CMAKE_LANGUAGE, scheme: 'untitled' },
];
languages.registerHoverProvider(CMAKE_SELECTOR, new CMakeExtraInfoSupport());
languages.registerCompletionItemProvider(CMAKE_SELECTOR, new CMakeSuggestionSupport());
languages.setLanguageConfiguration(CMAKE_LANGUAGE, {
indentationRules: {
// ^(.*\*/)?\s*\}.*$
decreaseIndentPattern: /^(.*\*\/)?\s*\}.*$/,
// ^.*\{[^}"']*$
increaseIndentPattern: /^.*\{[^}"']*$/
},
wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g,
comments: {
lineComment: '#'
},
brackets: [
['{', '}'],
['(', ')'],
],
__electricCharacterSupport: {
brackets: [
{ tokenType: 'delimiter.curly.ts', open: '{', close: '}', isElectric: true },
{ tokenType: 'delimiter.square.ts', open: '[', close: ']', isElectric: true },
{ tokenType: 'delimiter.paren.ts', open: '(', close: ')', isElectric: true }
]
},
__characterPairSupport: {
autoClosingPairs: [
{ open: '{', close: '}' },
{ open: '(', close: ')' },
{ open: '"', close: '"', notIn: ['string'] },
]
}
});
}
// Show Tooltip on mouse over
class CMakeExtraInfoSupport implements HoverProvider {
public provideHover(document: TextDocument, position: Position, token: CancellationToken): Thenable<Hover> {
let range = document.getWordRangeAtPosition(position);
let value = document.getText(range);
let promises = cmake_help_all();
return Promise.all([
cmCommandsSuggestionsExact(value),
cmVariablesSuggestionsExact(value),
cmModulesSuggestionsExact(value),
cmPropertiesSuggestionsExact(value),
]).then(function (results) {
var suggestions = Array.prototype.concat.apply([], results);
if (suggestions.length == 0) {
return null;
}
let suggestion: CompletionItem = suggestions[0];
return promises[cmakeTypeFromvscodeKind(suggestion.kind)](suggestion.label).then(function (result: string) {
let lines = result.split('\n');
lines = lines.slice(2, lines.length);
let hover = new Hover({ language: 'md', value: lines.join('\n') });
return hover;
});
});
}
}
function vscodeKindFromCMakeCodeClass(kind: string): CompletionItemKind {
switch (kind) {
case "function":
return CompletionItemKind.Function;
case "variable":
return CompletionItemKind.Variable;
case "module":
return CompletionItemKind.Module;
}
return CompletionItemKind.Property; // TODO@EG additional mappings needed?
}
function cmakeTypeFromvscodeKind(kind: CompletionItemKind): string {
switch (kind) {
case CompletionItemKind.Function:
return "function";
case CompletionItemKind.Variable:
return "variable";
case CompletionItemKind.Module:
return "module";
}
return "property";
}
function suggestionsHelper(cmake_cmd, currentWord: string, type: string, insertText, matchPredicate): Thenable<CompletionItem[]> {
return new Promise(function (resolve, reject) {
cmake_cmd.then(function (stdout: string) {
let commands = stdout.split('\n').filter(function (v) { return matchPredicate(v, currentWord) });
if (commands.length > 0) {
let suggestions = commands.map(function (command_name) {
var item = new CompletionItem(command_name);
item.kind = vscodeKindFromCMakeCodeClass(type);
if (insertText == null || insertText == '') {
item.insertText = command_name;
} else {
let snippet = new SnippetString(insertText(command_name));
item.insertText = snippet;
}
return item;
});
resolve(suggestions);
} else {
resolve([]);
}
}).catch(function (err) {
reject(err);
});
});
}
function cmModuleInsertText(module: string) {
if (module.indexOf('Find') == 0) {
return 'find_package(' + module.replace('Find', '') + '${1: REQUIRED})';
} else {
return 'include(' + module + ')';
}
}
function cmFunctionInsertText(func: string) {
let scoped_func = ['if', 'function', 'while', 'macro', 'foreach'];
let is_scoped = scoped_func.reduceRight(function (prev, name, idx, array) { return prev || func == name; }, false);
if (is_scoped)
return func + '(${1})\n\t\nend' + func + '(${1})\n';
else
return func + '(${1})'
}
function cmVariableInsertText(variable: string) {
return variable.replace(/<(.*)>/g, '${1:<$1>}');
}
function cmPropetryInsertText(variable: string) {
return variable.replace(/<(.*)>/g, '${1:<$1>}');
}
function cmCommandsSuggestions(currentWord: string): Thenable<CompletionItem[]> {
let cmd = cmake_help_command_list();
return suggestionsHelper(cmd, currentWord, 'function', cmFunctionInsertText, strContains);
}
function cmVariablesSuggestions(currentWord: string): Thenable<CompletionItem[]> {
let cmd = cmake_help_variable_list();
return suggestionsHelper(cmd, currentWord, 'variable', cmVariableInsertText, strContains);
}
function cmPropertiesSuggestions(currentWord: string): Thenable<CompletionItem[]> {
let cmd = cmake_help_property_list();
return suggestionsHelper(cmd, currentWord, 'property', cmPropetryInsertText, strContains);
}
function cmModulesSuggestions(currentWord: string): Thenable<CompletionItem[]> {
let cmd = cmake_help_module_list();
return suggestionsHelper(cmd, currentWord, 'module', cmModuleInsertText, strContains);
}
function cmCommandsSuggestionsExact(currentWord: string): Thenable<CompletionItem[]> {
let cmd = cmake_help_command_list();
return suggestionsHelper(cmd, currentWord, 'function', cmFunctionInsertText, strEquals);
}
function cmVariablesSuggestionsExact(currentWord: string): Thenable<CompletionItem[]> {
let cmd = cmake_help_variable_list();
return suggestionsHelper(cmd, currentWord, 'variable', cmVariableInsertText, strEquals);
}
function cmPropertiesSuggestionsExact(currentWord: string): Thenable<CompletionItem[]> {
let cmd = cmake_help_property_list();
return suggestionsHelper(cmd, currentWord, 'property', cmPropetryInsertText, strEquals);
}
function cmModulesSuggestionsExact(currentWord: string): Thenable<CompletionItem[]> {
let cmd = cmake_help_module_list();
return suggestionsHelper(cmd, currentWord, 'module', cmModuleInsertText, strEquals);
}
function wordSuggestions(document: TextDocument, currentWord: string): Thenable<CompletionItem[]> {
return new Promise(function(resolve, reject) {
let suggestions = new Array<CompletionItem>();
let re = new RegExp('(\\b' + currentWord + '\\w+)', 'g');
let matches = document.getText().match(re);
let uniqueMatches = new Set<string>();
for (let match of matches) {
uniqueMatches.add(match);
}
for (let match of uniqueMatches) {
suggestions.push(new CompletionItem(match, CompletionItemKind.Variable));
}
return resolve(suggestions);
});
}
class CMakeSuggestionSupport implements CompletionItemProvider {
public triggerCharacters: string[];
public excludeTokens: string[] = ['string', 'comment', 'numeric'];
public provideCompletionItems(document: TextDocument, position: Position, token: CancellationToken): Thenable<CompletionItem[]> {
let wordAtPosition = document.getWordRangeAtPosition(position);
var currentWord = '';
if (wordAtPosition && wordAtPosition.start.character < position.character) {
var word = document.getText(wordAtPosition);
currentWord = word.substr(0, position.character - wordAtPosition.start.character);
}
return new Promise(function (resolve, reject) {
Promise.all([
cmCommandsSuggestions(currentWord),
cmVariablesSuggestions(currentWord),
cmPropertiesSuggestions(currentWord),
cmModulesSuggestions(currentWord)
]).then(function (results) {
var suggestions = Array.prototype.concat.apply([], results);
wordSuggestions(document, currentWord).then(function (results) {
for (let result of results) {
if (!suggestions.find(function (el){
return el.label == result.label;
})){
suggestions.push(result);
}
}
resolve(suggestions);
});
}).catch(err => { reject(err); });
});
}
public resolveCompletionItem(item: CompletionItem, token: CancellationToken): Thenable<CompletionItem> {
let promises = cmake_help_all();
let type = cmakeTypeFromvscodeKind(item.kind);
return promises[type](item.label).then(function (result: string) {
item.documentation = result.split('\n')[3];
return item;
});
}
}
// CMake Language Definition
// class CMakeLanguageDef /*implements LanguageConfiguration*/ {
// public comments = {
// lineComment: '#',
// }
// public name:string = 'cmake';
// public displayName:string= 'Cmake';
// public ignoreCase: boolean = true;
// public lineComment: string = '#';
// public autoClosingPairs:string[][] = [
// ['{', '}'],
// ['"', '"']];
// public keywords :string[] = [
// 'if', 'endif', 'else',
// 'foreach', 'endforeach',
// 'function', 'endfunction',
// 'macro', 'endmacro',
// 'include',
// 'set',
// 'project'
// ];
// public brackets = [
// { token: 'delimiter.parenthesis', open: '(', close: ')' },
// ];
// public textAfterBrackets:boolean = true;
// public variable= /\$\{\w+\}/;
// public enhancedBrackets = [
// {
// openTrigger: '\)',
// open: /if\((\w*)\)/i,
// closeComplete: 'endif\($1\)',
// matchCase: true,
// closeTrigger: '\)',
// close: /endif\($1\)$/,
// tokenType: 'keyword.tag-if'
// },
// {
// openTrigger: '\)',
// open: /foreach\((\w*)\)/i,
// closeComplete: 'endforeach\($1\)',
// matchCase: true,
// closeTrigger: '\)',
// close: /endforeach\($1\)$/,
// tokenType: 'keyword.tag-foreach'
// },
// {
// openTrigger: '\)',
// open: /function\((\w+)\)/i,
// closeComplete: 'endfunction\($1\)',
// matchCase: true,
// closeTrigger: '\)',
// close: /function\($1\)$/,
// tokenType: 'keyword.tag-function'
// },
// {
// openTrigger: '\)',
// open: /macro\((\w+)\)/i,
// closeComplete: 'endmacro\($1\)',
// matchCase: true,
// closeTrigger: '\)',
// close: /macro\($1\)$/,
// tokenType: 'keyword.tag-macro'
// }
// ];
// // we include these common regular expressions
// public symbols = /[=><!~?&|+\-*\/\^;\.,]+/;
// public escapes= /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/;
// // The main tokenizer for our languages
// public tokenizer= {
// root: [
// [/([a-zA-Z_]\w*)( *\()/, [{cases: { '@keywords': { token: 'keyword.$0' } , '@default': 'identifier.method'}}, '']],
// { include: '@whitespace' },
// [/\$\{\w+\}/, 'variable'],
// [/\d*\.\d+([eE][\-+]?\d+)?/, 'number.float'],
// [/0[xX][0-9a-fA-F_]*[0-9a-fA-F]/, 'number.hex'],
// [/\d+/, 'number'],
// [/"/, 'string', '@string."'],
// [/'/, 'string', '@string.\''],
// ],
// whitespace: [
// [/[ \t\r\n]+/, ''],
// [/#.*$/, 'comment'],
// ],
// string: [
// [/[^\\"'%]+/, { cases: { '@eos': { token: 'string', next: '@popall' }, '@default': 'string' } }],
// [/@escapes/, 'string.escape'],
// [/\\./, 'string.escape.invalid'],
// [/\$\{[\w ]+\}/, 'variable'],
// [/["']/, { cases: { '$#==$S2': { token: 'string', next: '@pop' }, '@default': 'string' } }],
// [/$/, 'string', '@popall']
// ],
// };
// }