-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'tasks.registerTaskProvider' for Plugin API
Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
- Loading branch information
1 parent
29dd2bd
commit 558d4b5
Showing
11 changed files
with
1,140 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2018 Red Hat, Inc. 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 { | ||
TasksMain, | ||
MAIN_RPC_CONTEXT, | ||
TasksExt | ||
} from '../../api/plugin-api'; | ||
import { RPCProtocol } from '../../api/rpc-protocol'; | ||
import { DisposableCollection } from '@theia/core'; | ||
import { TaskProviderRegistry, TaskResolverRegistry, TaskProvider, TaskResolver } from '@theia/task/lib/browser/task-contribution'; | ||
import { interfaces } from 'inversify'; | ||
|
||
export class TasksMainImpl implements TasksMain { | ||
|
||
private readonly proxy: TasksExt; | ||
private readonly disposables = new Map<number, monaco.IDisposable>(); | ||
private readonly taskProviderRegistry: TaskProviderRegistry; | ||
private readonly taskResolverRegistry: TaskResolverRegistry; | ||
|
||
constructor(rpc: RPCProtocol, container: interfaces.Container, ) { | ||
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.TASKS_EXT); | ||
this.taskProviderRegistry = container.get(TaskProviderRegistry); | ||
this.taskResolverRegistry = container.get(TaskResolverRegistry); | ||
} | ||
|
||
$registerTaskProvider(handle: number, type: string): void { | ||
const taskProvider = this.createTaskProvider(handle); | ||
const taskResolver = this.createTaskResolver(handle); | ||
|
||
const disposable = new DisposableCollection(); | ||
disposable.push(this.taskProviderRegistry.register(type, taskProvider)); | ||
disposable.push(this.taskResolverRegistry.register(type, taskResolver)); | ||
this.disposables.set(handle, disposable); | ||
} | ||
|
||
$unregister(handle: number): void { | ||
const disposable = this.disposables.get(handle); | ||
if (disposable) { | ||
disposable.dispose(); | ||
this.disposables.delete(handle); | ||
} | ||
} | ||
|
||
protected createTaskProvider(handle: number): TaskProvider { | ||
return { | ||
provideTasks: () => | ||
this.proxy.$provideTasks(handle).then(v => v!), | ||
}; | ||
} | ||
|
||
protected createTaskResolver(handle: number): TaskResolver { | ||
return { | ||
resolveTask: taskConfig => | ||
this.proxy.$resolveTask(handle, taskConfig).then(v => v!), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2018 Red Hat, Inc. 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 * as theia from '@theia/plugin'; | ||
import * as Converter from '../type-converters'; | ||
import { ObjectIdentifier } from '../../common/object-identifier'; | ||
import { createToken } from '../token-provider'; | ||
import { TaskDto } from '../../common'; | ||
|
||
export class TaskProviderAdapter { | ||
private cacheId = 0; | ||
private cache = new Map<number, theia.Task>(); | ||
|
||
constructor(private readonly provider: theia.TaskProvider) { } | ||
|
||
provideTasks(): Promise<TaskDto[] | undefined> { | ||
return Promise.resolve(this.provider.provideTasks(createToken())).then(tasks => { | ||
if (!Array.isArray(tasks)) { | ||
return undefined; | ||
} | ||
const result: TaskDto[] = []; | ||
for (const task of tasks) { | ||
const data = Converter.fromTask(task); | ||
if (!data) { | ||
continue; | ||
} | ||
|
||
const id = this.cacheId++; | ||
ObjectIdentifier.mixin(data, id); | ||
this.cache.set(id, task); | ||
result.push(data); | ||
} | ||
return result; | ||
}); | ||
} | ||
|
||
resolveTask(task: TaskDto): Promise<TaskDto | undefined> { | ||
if (typeof this.provider.resolveTask !== 'function') { | ||
return Promise.resolve(undefined); | ||
} | ||
const id = ObjectIdentifier.of(task); | ||
const item = this.cache.get(id); | ||
if (!item) { | ||
return Promise.resolve(undefined); | ||
} | ||
|
||
return Promise.resolve(this.provider.resolveTask(item, createToken())).then(value => { | ||
if (value) { | ||
return Converter.fromTask(value); | ||
} | ||
return undefined; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2018 Red Hat, Inc. 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 { | ||
PLUGIN_RPC_CONTEXT, | ||
TasksExt, | ||
TasksMain, | ||
TaskDto | ||
} from '../../api/plugin-api'; | ||
import * as theia from '@theia/plugin'; | ||
import { Disposable } from '../types-impl'; | ||
import { RPCProtocol } from '../../api/rpc-protocol'; | ||
import { TaskProviderAdapter } from './task-provider'; | ||
|
||
export class TasksExtImpl implements TasksExt { | ||
private proxy: TasksMain; | ||
|
||
private callId = 0; | ||
private adaptersMap = new Map<number, TaskProviderAdapter>(); | ||
|
||
constructor(rpc: RPCProtocol) { | ||
this.proxy = rpc.getProxy(PLUGIN_RPC_CONTEXT.TASKS_MAIN); | ||
} | ||
|
||
registerTaskProvider(type: string, provider: theia.TaskProvider): theia.Disposable { | ||
const callId = this.addNewAdapter(new TaskProviderAdapter(provider)); | ||
this.proxy.$registerTaskProvider(callId, type); | ||
return this.createDisposable(callId); | ||
} | ||
|
||
$provideTasks(handle: number): Promise<TaskDto[] | undefined> { | ||
const adapter = this.adaptersMap.get(handle); | ||
if (adapter) { | ||
return adapter.provideTasks(); | ||
} else { | ||
return Promise.reject(new Error('No adapter found to provide tasks')); | ||
} | ||
} | ||
|
||
$resolveTask(handle: number, task: TaskDto): Promise<TaskDto | undefined> { | ||
const adapter = this.adaptersMap.get(handle); | ||
if (adapter) { | ||
return adapter.resolveTask(task); | ||
} else { | ||
return Promise.reject(new Error('No adapter found to resolve task')); | ||
} | ||
} | ||
|
||
private addNewAdapter(adapter: TaskProviderAdapter): number { | ||
const callId = this.nextCallId(); | ||
this.adaptersMap.set(callId, adapter); | ||
return callId; | ||
} | ||
|
||
private nextCallId(): number { | ||
return this.callId++; | ||
} | ||
|
||
private createDisposable(callId: number): theia.Disposable { | ||
return new Disposable(() => { | ||
this.adaptersMap.delete(callId); | ||
this.proxy.$unregister(callId); | ||
}); | ||
} | ||
} |
Oops, something went wrong.