-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
task-service.ts
703 lines (624 loc) · 30.5 KB
/
task-service.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/********************************************************************************
* Copyright (C) 2017 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { ApplicationShell, FrontendApplication, WidgetManager } from '@theia/core/lib/browser';
import { open, OpenerService } from '@theia/core/lib/browser/opener-service';
import { ILogger, CommandService } from '@theia/core/lib/common';
import { MessageService } from '@theia/core/lib/common/message-service';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { QuickPickItem, QuickPickService } from '@theia/core/lib/common/quick-pick-service';
import URI from '@theia/core/lib/common/uri';
import { EditorManager } from '@theia/editor/lib/browser';
import { ProblemManager } from '@theia/markers/lib/browser/problem/problem-manager';
import { TerminalService } from '@theia/terminal/lib/browser/base/terminal-service';
import { TerminalWidget } from '@theia/terminal/lib/browser/base/terminal-widget';
import { TerminalWidgetFactoryOptions, TERMINAL_WIDGET_FACTORY_ID } from '@theia/terminal/lib/browser/terminal-widget-impl';
import { VariableResolverService } from '@theia/variable-resolver/lib/browser';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { inject, injectable, named, postConstruct } from 'inversify';
import { Range } from 'vscode-languageserver-types';
import {
NamedProblemMatcher,
ProblemMatchData,
ProblemMatcher,
RunTaskOption,
TaskConfiguration,
TaskCustomization,
TaskExitedEvent,
TaskInfo,
TaskOutputProcessedEvent,
TaskServer
} from '../common';
import { TaskWatcher } from '../common/task-watcher';
import { ProvidedTaskConfigurations } from './provided-task-configurations';
import { TaskConfigurationClient, TaskConfigurations } from './task-configurations';
import { TaskProviderRegistry, TaskResolverRegistry } from './task-contribution';
import { TaskDefinitionRegistry } from './task-definition-registry';
import { TaskNameResolver } from './task-name-resolver';
import { ProblemMatcherRegistry } from './task-problem-matcher-registry';
import { TaskSchemaUpdater } from './task-schema-updater';
import { TaskConfigurationManager } from './task-configuration-manager';
import { PROBLEMS_WIDGET_ID, ProblemWidget } from '@theia/markers/lib/browser/problem/problem-widget';
export interface QuickPickProblemMatcherItem {
problemMatchers: NamedProblemMatcher[] | undefined;
learnMore?: boolean;
}
@injectable()
export class TaskService implements TaskConfigurationClient {
/**
* The last executed task.
*/
protected lastTask: { source: string, taskLabel: string } | undefined = undefined;
protected cachedRecentTasks: TaskConfiguration[] = [];
protected runningTasks = new Map<number, {
exitCode: Deferred<number | undefined>,
terminateSignal: Deferred<string | undefined>
}>();
@inject(FrontendApplication)
protected readonly app: FrontendApplication;
@inject(ApplicationShell)
protected readonly shell: ApplicationShell;
@inject(TaskServer)
protected readonly taskServer: TaskServer;
@inject(ILogger) @named('task')
protected readonly logger: ILogger;
@inject(WidgetManager)
protected readonly widgetManager: WidgetManager;
@inject(TaskWatcher)
protected readonly taskWatcher: TaskWatcher;
@inject(MessageService)
protected readonly messageService: MessageService;
@inject(WorkspaceService)
protected readonly workspaceService: WorkspaceService;
@inject(TaskConfigurations)
protected readonly taskConfigurations: TaskConfigurations;
@inject(ProvidedTaskConfigurations)
protected readonly providedTaskConfigurations: ProvidedTaskConfigurations;
@inject(VariableResolverService)
protected readonly variableResolverService: VariableResolverService;
@inject(TaskResolverRegistry)
protected readonly taskResolverRegistry: TaskResolverRegistry;
@inject(TerminalService)
protected readonly terminalService: TerminalService;
@inject(EditorManager)
protected readonly editorManager: EditorManager;
@inject(ProblemManager)
protected readonly problemManager: ProblemManager;
@inject(TaskDefinitionRegistry)
protected readonly taskDefinitionRegistry: TaskDefinitionRegistry;
@inject(ProblemMatcherRegistry)
protected readonly problemMatcherRegistry: ProblemMatcherRegistry;
@inject(QuickPickService)
protected readonly quickPick: QuickPickService;
@inject(OpenerService)
protected readonly openerService: OpenerService;
@inject(TaskNameResolver)
protected readonly taskNameResolver: TaskNameResolver;
@inject(TaskSchemaUpdater)
protected readonly taskSchemaUpdater: TaskSchemaUpdater;
@inject(TaskConfigurationManager)
protected readonly taskConfigurationManager: TaskConfigurationManager;
@inject(CommandService)
protected readonly commands: CommandService;
/**
* @deprecated To be removed in 0.5.0
*/
@inject(TaskProviderRegistry)
protected readonly taskProviderRegistry: TaskProviderRegistry;
@postConstruct()
protected init(): void {
this.getRunningTasks().then(tasks =>
tasks.forEach(task => {
if (!this.runningTasks.has(task.taskId)) {
this.runningTasks.set(task.taskId, { exitCode: new Deferred<number | undefined>(), terminateSignal: new Deferred<string | undefined>() });
}
}));
// notify user that task has started
this.taskWatcher.onTaskCreated((event: TaskInfo) => {
if (!this.isEventForThisClient(event.ctx)) {
return;
}
this.runningTasks.set(event.taskId, { exitCode: new Deferred<number | undefined>(), terminateSignal: new Deferred<string | undefined>() });
const task = event.config;
let taskIdentifier = event.taskId.toString();
if (task) {
taskIdentifier = this.taskNameResolver.resolve(task);
}
this.messageService.info(`Task ${taskIdentifier} has been started`);
});
this.taskWatcher.onOutputProcessed((event: TaskOutputProcessedEvent) => {
if (!this.isEventForThisClient(event.ctx)) {
return;
}
if (event.problems) {
event.problems.forEach(problem => {
const existingMarkers = this.problemManager.findMarkers({ owner: problem.description.owner });
const uris = new Set<string>();
existingMarkers.forEach(marker => uris.add(marker.uri));
if (ProblemMatchData.is(problem) && problem.resource) {
const uri = new URI(problem.resource.path).withScheme(problem.resource.scheme);
if (uris.has(uri.toString())) {
const newData = [
...existingMarkers
.filter(marker => marker.uri === uri.toString())
.map(markerData => markerData.data),
problem.marker
];
this.problemManager.setMarkers(uri, problem.description.owner, newData);
} else {
this.problemManager.setMarkers(uri, problem.description.owner, [problem.marker]);
}
} else { // should have received an event for finding the "background task begins" pattern
uris.forEach(uriString => this.problemManager.setMarkers(new URI(uriString), problem.description.owner, []));
}
});
}
});
// notify user that task has finished
this.taskWatcher.onTaskExit((event: TaskExitedEvent) => {
if (!this.isEventForThisClient(event.ctx)) {
return;
}
if (!this.runningTasks.has(event.taskId)) {
this.runningTasks.set(event.taskId, { exitCode: new Deferred<number | undefined>(), terminateSignal: new Deferred<string | undefined>() });
}
this.runningTasks.get(event.taskId)!.exitCode.resolve(event.code);
this.runningTasks.get(event.taskId)!.terminateSignal.resolve(event.signal);
setTimeout(() => this.runningTasks.delete(event.taskId), 60 * 1000);
const taskConfiguration = event.config;
let taskIdentifier = event.taskId.toString();
if (taskConfiguration) {
taskIdentifier = this.taskNameResolver.resolve(taskConfiguration);
}
if (event.code !== undefined) {
const message = `Task ${taskIdentifier} has exited with code ${event.code}.`;
if (event.code === 0) {
this.messageService.info(message);
} else {
this.messageService.error(message);
}
} else if (event.signal !== undefined) {
this.messageService.info(`Task ${taskIdentifier} was terminated by signal ${event.signal}.`);
} else {
console.error('Invalid TaskExitedEvent received, neither code nor signal is set.');
}
});
}
/** Returns an array of the task configurations configured in tasks.json and provided by the extensions. */
async getTasks(): Promise<TaskConfiguration[]> {
const configuredTasks = await this.getConfiguredTasks();
const providedTasks = await this.getProvidedTasks();
const notCustomizedProvidedTasks = providedTasks.filter(provided =>
!configuredTasks.some(configured => this.taskDefinitionRegistry.compareTasks(configured, provided))
);
return [...configuredTasks, ...notCustomizedProvidedTasks];
}
/** Returns an array of the valid task configurations which are configured in tasks.json files */
async getConfiguredTasks(): Promise<TaskConfiguration[]> {
const invalidTaskConfig = this.taskConfigurations.getInvalidTaskConfigurations()[0];
if (invalidTaskConfig) {
const widget = <ProblemWidget>await this.widgetManager.getOrCreateWidget(PROBLEMS_WIDGET_ID);
const isProblemsWidgetVisible = widget && widget.isVisible;
const currentEditorUri = this.editorManager.currentEditor && this.editorManager.currentEditor.editor.getResourceUri();
let isInvalidTaskConfigFileOpen = false;
if (currentEditorUri) {
const folderUri = this.workspaceService.getWorkspaceRootUri(currentEditorUri);
if (folderUri && folderUri.toString() === invalidTaskConfig._scope) {
isInvalidTaskConfigFileOpen = true;
}
}
const warningMessage = 'Invalid task configurations are found. Open tasks.json and find details in the Problems view.';
if (!isProblemsWidgetVisible || !isInvalidTaskConfigFileOpen) {
this.messageService.warn(warningMessage, 'Open').then(actionOpen => {
if (actionOpen) {
if (invalidTaskConfig && invalidTaskConfig._scope) {
this.taskConfigurationManager.openConfiguration(invalidTaskConfig._scope);
}
if (!isProblemsWidgetVisible) {
this.commands.executeCommand('problemsView:toggle');
}
}
});
} else {
this.messageService.warn(warningMessage);
}
}
const validTaskConfigs = await this.taskConfigurations.getTasks();
return validTaskConfigs;
}
/** Returns an array of the task configurations which are provided by the extensions. */
getProvidedTasks(): Promise<TaskConfiguration[]> {
return this.providedTaskConfigurations.getTasks();
}
addRecentTasks(tasks: TaskConfiguration | TaskConfiguration[]): void {
if (Array.isArray(tasks)) {
tasks.forEach(task => this.addRecentTasks(task));
} else {
const ind = this.cachedRecentTasks.findIndex(recent => this.taskDefinitionRegistry.compareTasks(recent, tasks));
if (ind >= 0) {
this.cachedRecentTasks.splice(ind, 1);
}
this.cachedRecentTasks.unshift(tasks);
}
}
get recentTasks(): TaskConfiguration[] {
return this.cachedRecentTasks;
}
set recentTasks(recent: TaskConfiguration[]) {
this.cachedRecentTasks = recent;
}
/**
* Clears the list of recently used tasks.
*/
clearRecentTasks(): void {
this.cachedRecentTasks = [];
}
/**
* Returns a task configuration provided by an extension by task source and label.
* If there are no task configuration, returns undefined.
*/
async getProvidedTask(source: string, label: string): Promise<TaskConfiguration | undefined> {
return this.providedTaskConfigurations.getTask(source, label);
}
/** Returns an array of running tasks 'TaskInfo' objects */
getRunningTasks(): Promise<TaskInfo[]> {
return this.taskServer.getTasks(this.getContext());
}
/** Returns an array of task types that are registered, including the default types */
getRegisteredTaskTypes(): Promise<string[]> {
return this.taskSchemaUpdater.getRegisteredTaskTypes();
}
/**
* Get the last executed task.
*
* @returns the last executed task or `undefined`.
*/
getLastTask(): { source: string, taskLabel: string } | undefined {
return this.lastTask;
}
/**
* Runs a task, by task configuration label.
* Note, it looks for a task configured in tasks.json only.
*/
async runConfiguredTask(source: string, taskLabel: string): Promise<void> {
const task = this.taskConfigurations.getTask(source, taskLabel);
if (!task) {
this.logger.error(`Can't get task launch configuration for label: ${taskLabel}`);
return;
}
this.run(source, taskLabel);
}
/**
* Run the last executed task.
*/
async runLastTask(): Promise<TaskInfo | undefined> {
if (!this.lastTask) {
return;
}
const { source, taskLabel } = this.lastTask;
return this.run(source, taskLabel);
}
/**
* Runs a task, by the source and label of the task configuration.
* It looks for configured and detected tasks.
*/
async run(source: string, taskLabel: string): Promise<TaskInfo | undefined> {
let task = await this.getProvidedTask(source, taskLabel);
if (!task) { // if a detected task cannot be found, search from tasks.json
task = this.taskConfigurations.getTask(source, taskLabel);
if (!task) {
this.logger.error(`Can't get task launch configuration for label: ${taskLabel}`);
return;
}
}
const customizationObject = await this.getTaskCustomization(task);
if (!customizationObject.problemMatcher) {
// ask the user what s/he wants to use to parse the task output
const items = this.getCustomizeProblemMatcherItems();
const selected = await this.quickPick.show(items, {
placeholder: 'Select for which kind of errors and warnings to scan the task output'
});
if (selected) {
if (selected.problemMatchers) {
let matcherNames: string[] = [];
if (selected.problemMatchers && selected.problemMatchers.length === 0) { // never parse output for this task
matcherNames = [];
} else if (selected.problemMatchers && selected.problemMatchers.length > 0) { // continue with user-selected parser
matcherNames = selected.problemMatchers.map(matcher => matcher.name);
}
customizationObject.problemMatcher = matcherNames;
// write the selected matcher (or the decision of "never parse") into the `tasks.json`
this.updateTaskConfiguration(task, { problemMatcher: matcherNames });
} else if (selected.learnMore) { // user wants to learn more about parsing task output
open(this.openerService, new URI('https://code.visualstudio.com/docs/editor/tasks#_processing-task-output-with-problem-matchers'));
}
// else, continue the task with no parser
} else { // do not start the task in case that the user did not select any item from the list
return;
}
}
const resolvedMatchers = await this.resolveProblemMatchers(task, customizationObject);
return this.runTask(task, {
customization: { ...customizationObject, ...{ problemMatcher: resolvedMatchers } }
});
}
async runTask(task: TaskConfiguration, option?: RunTaskOption): Promise<TaskInfo | undefined> {
if (option && option.customization) {
const taskDefinition = this.taskDefinitionRegistry.getDefinition(task);
if (taskDefinition) { // use the customization object to override the task config
Object.keys(option.customization).forEach(customizedProperty => {
// properties used to define the task cannot be customized
if (customizedProperty !== 'type' && !taskDefinition.properties.all.some(pDefinition => pDefinition === customizedProperty)) {
task[customizedProperty] = option.customization![customizedProperty];
}
});
}
}
const resolvedTask = await this.getResolvedTask(task);
if (resolvedTask) {
// remove problem markers from the same source before running the task
await this.removeProblemMarks(option);
return this.runResolvedTask(resolvedTask, option);
}
}
async runTaskByLabel(taskLabel: string): Promise<TaskInfo | undefined> {
const tasks: TaskConfiguration[] = await this.getTasks();
for (const task of tasks) {
if (task.label === taskLabel) {
return this.runTask(task);
}
}
return;
}
async runWorkspaceTask(workspaceFolderUri: string | undefined, taskName: string): Promise<TaskInfo | undefined> {
const tasks = await this.getWorkspaceTasks(workspaceFolderUri);
const task = tasks.filter(t => taskName === this.taskNameResolver.resolve(t))[0];
if (!task) {
return undefined;
}
const taskCustomization = await this.getTaskCustomization(task);
const resolvedMatchers = await this.resolveProblemMatchers(task, taskCustomization);
return this.runTask(task, {
customization: { ...taskCustomization, ...{ problemMatcher: resolvedMatchers } }
});
}
/**
* Updates the task configuration in the `tasks.json`.
* The task config, together with updates, will be written into the `tasks.json` if it is not found in the file.
*
* @param task task that the updates will be applied to
* @param update the updates to be appplied
*/
// tslint:disable-next-line:no-any
async updateTaskConfiguration(task: TaskConfiguration, update: { [name: string]: any }): Promise<void> {
if (update.problemMatcher) {
if (Array.isArray(update.problemMatcher)) {
update.problemMatcher.forEach((name, index) => {
if (!name.startsWith('$')) {
update.problemMatcher[index] = `$${update.problemMatcher[index]}`;
}
});
} else if (!update.problemMatcher.startsWith('$')) {
update.problemMatcher = `$${update.problemMatcher}`;
}
}
this.taskConfigurations.updateTaskConfig(task, update);
}
protected async getWorkspaceTasks(workspaceFolderUri: string | undefined): Promise<TaskConfiguration[]> {
const tasks = await this.getTasks();
return tasks.filter(t => t._scope === workspaceFolderUri || t._scope === undefined);
}
protected async resolveProblemMatchers(task: TaskConfiguration, customizationObject: TaskCustomization): Promise<ProblemMatcher[] | undefined> {
const notResolvedMatchers = customizationObject.problemMatcher ?
(Array.isArray(customizationObject.problemMatcher) ? customizationObject.problemMatcher : [customizationObject.problemMatcher]) : undefined;
let resolvedMatchers: ProblemMatcher[] | undefined = [];
if (notResolvedMatchers) {
// resolve matchers before passing them to the server
for (const matcher of notResolvedMatchers) {
let resolvedMatcher: ProblemMatcher | undefined;
await this.problemMatcherRegistry.onReady();
if (typeof matcher === 'string') {
resolvedMatcher = this.problemMatcherRegistry.get(matcher);
} else {
resolvedMatcher = await this.problemMatcherRegistry.getProblemMatcherFromContribution(matcher);
}
if (resolvedMatcher) {
const scope = task._scope || task._source;
if (resolvedMatcher.filePrefix && scope) {
const options = {
context: new URI(scope).withScheme('file'),
configurationSection: 'tasks'
};
const resolvedPrefix = await this.variableResolverService.resolve(resolvedMatcher.filePrefix, options);
Object.assign(resolvedMatcher, { filePrefix: resolvedPrefix });
}
resolvedMatchers.push(resolvedMatcher);
}
}
} else {
resolvedMatchers = undefined;
}
return resolvedMatchers;
}
protected async getTaskCustomization(task: TaskConfiguration): Promise<TaskCustomization> {
const customizationObject: TaskCustomization = { type: '' };
const customizationFound = this.taskConfigurations.getCustomizationForTask(task);
if (customizationFound) {
Object.assign(customizationObject, customizationFound);
} else {
Object.assign(customizationObject, {
type: task.type,
problemMatcher: task.problemMatcher
});
}
return customizationObject;
}
private async removeProblemMarks(option?: RunTaskOption): Promise<void> {
if (option && option.customization) {
const matchersFromOption = option.customization.problemMatcher || [];
for (const matcher of matchersFromOption) {
if (matcher && matcher.owner) {
const existingMarkers = this.problemManager.findMarkers({ owner: matcher.owner });
const uris = new Set<string>();
existingMarkers.forEach(marker => uris.add(marker.uri));
uris.forEach(uriString => this.problemManager.setMarkers(new URI(uriString), matcher.owner, []));
}
}
}
}
private async getResolvedTask(task: TaskConfiguration): Promise<TaskConfiguration | undefined> {
const resolver = await this.taskResolverRegistry.getResolver(task.type);
try {
const resolvedTask = resolver ? await resolver.resolveTask(task) : task;
this.addRecentTasks(task);
return resolvedTask;
} catch (error) {
const errMessage = `Error resolving task '${task.label}': ${error}`;
this.logger.error(errMessage);
this.messageService.error(errMessage);
}
}
/**
* Runs the resolved task and opens terminal widget if the task is based on a terminal process
* @param resolvedTask the resolved task
* @param option options to run the resolved task
*/
private async runResolvedTask(resolvedTask: TaskConfiguration, option?: RunTaskOption): Promise<TaskInfo | undefined> {
const source = resolvedTask._source;
const taskLabel = resolvedTask.label;
try {
const taskInfo = await this.taskServer.run(resolvedTask, this.getContext(), option);
this.lastTask = { source, taskLabel };
this.logger.debug(`Task created. Task id: ${taskInfo.taskId}`);
/**
* open terminal widget if the task is based on a terminal process (type: 'shell' or 'process')
*
* @todo Use a different mechanism to determine if the task should be attached?
* Reason: Maybe a new task type wants to also be displayed in a terminal.
*/
if (typeof taskInfo.terminalId === 'number') {
this.attach(taskInfo.terminalId, taskInfo.taskId);
}
return taskInfo;
} catch (error) {
const errorStr = `Error launching task '${taskLabel}': ${error.message}`;
this.logger.error(errorStr);
this.messageService.error(errorStr);
}
}
private getCustomizeProblemMatcherItems(): QuickPickItem<QuickPickProblemMatcherItem>[] {
const items: QuickPickItem<QuickPickProblemMatcherItem>[] = [];
items.push({
label: 'Continue without scanning the task output',
value: { problemMatchers: undefined }
});
items.push({
label: 'Never scan the task output',
value: { problemMatchers: [] }
});
items.push({
label: 'Learn more about scanning the task output',
value: { problemMatchers: undefined, learnMore: true }
});
items.push({ type: 'separator', label: 'registered parsers' });
const registeredProblemMatchers = this.problemMatcherRegistry.getAll();
items.push(...registeredProblemMatchers.map(matcher =>
({
label: matcher.label,
value: { problemMatchers: [matcher] },
description: matcher.name.startsWith('$') ? matcher.name : `$${matcher.name}`
})
));
return items;
}
/**
* Run selected text in the last active terminal.
*/
async runSelectedText(): Promise<void> {
if (!this.editorManager.currentEditor) { return; }
const startLine = this.editorManager.currentEditor.editor.selection.start.line;
const startCharacter = this.editorManager.currentEditor.editor.selection.start.character;
const endLine = this.editorManager.currentEditor.editor.selection.end.line;
const endCharacter = this.editorManager.currentEditor.editor.selection.end.character;
let selectedRange: Range = Range.create(startLine, startCharacter, endLine, endCharacter);
// if no text is selected, default to selecting entire line
if (startLine === endLine && startCharacter === endCharacter) {
selectedRange = Range.create(startLine, 0, endLine + 1, 0);
}
const selectedText: string = this.editorManager.currentEditor.editor.document.getText(selectedRange).trimRight() + '\n';
let terminal = this.terminalService.currentTerminal;
if (!terminal) {
terminal = <TerminalWidget>await this.terminalService.newTerminal(<TerminalWidgetFactoryOptions>{ created: new Date().toString() });
await terminal.start();
this.terminalService.activateTerminal(terminal);
}
terminal.sendText(selectedText);
}
async attach(processId: number, taskId: number): Promise<void> {
// Get the list of all available running tasks.
const runningTasks: TaskInfo[] = await this.getRunningTasks();
// Get the corresponding task information based on task id if available.
const taskInfo: TaskInfo | undefined = runningTasks.find((t: TaskInfo) => t.taskId === taskId);
// Create terminal widget to display an execution output of a task that was launched as a command inside a shell.
const widget = <TerminalWidget>await this.widgetManager.getOrCreateWidget(
TERMINAL_WIDGET_FACTORY_ID,
<TerminalWidgetFactoryOptions>{
created: new Date().toString(),
id: 'terminal-' + processId,
title: taskInfo
? `Task: ${taskInfo.config.label}`
: `Task: #${taskId}`,
destroyTermOnClose: true
}
);
this.shell.addWidget(widget, { area: 'bottom' });
this.shell.activateWidget(widget.id);
widget.start(processId);
}
async configure(task: TaskConfiguration): Promise<void> {
await this.taskConfigurations.configure(task);
}
protected isEventForThisClient(context: string | undefined): boolean {
if (context === this.getContext()) {
return true;
}
return false;
}
taskConfigurationChanged(event: string[]): void {
// do nothing for now
}
protected getContext(): string | undefined {
return this.workspaceService.workspace && this.workspaceService.workspace.uri;
}
/** Kill task for a given id if task is found */
async kill(id: number): Promise<void> {
try {
await this.taskServer.kill(id);
} catch (error) {
this.logger.error(`Error killing task '${id}': ${error}`);
this.messageService.error(`Error killing task '${id}': ${error}`);
return;
}
this.logger.debug(`Task killed. Task id: ${id}`);
}
async getExitCode(id: number): Promise<number | undefined> {
const completedTask = this.runningTasks.get(id);
return completedTask && completedTask.exitCode.promise;
}
async getTerminateSignal(id: number): Promise<string | undefined> {
const completedTask = this.runningTasks.get(id);
return completedTask && completedTask.terminateSignal.promise;
}
}