1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import { named, injectable, inject } from 'inversify';
|
18 | import URI from '../common/uri';
|
19 | import { ContributionProvider, Prioritizeable, MaybePromise, Emitter, Event, Disposable } from '../common';
|
20 |
|
21 | export interface OpenerOptions {
|
22 | }
|
23 |
|
24 | export const OpenHandler = Symbol('OpenHandler');
|
25 |
|
26 |
|
27 |
|
28 | export interface OpenHandler {
|
29 | |
30 |
|
31 |
|
32 | readonly id: string;
|
33 | |
34 |
|
35 |
|
36 | readonly label?: string;
|
37 | |
38 |
|
39 |
|
40 | readonly iconClass?: string;
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | canHandle(uri: URI, options?: OpenerOptions): MaybePromise<number>;
|
49 | |
50 |
|
51 |
|
52 |
|
53 |
|
54 | open(uri: URI, options?: OpenerOptions): MaybePromise<object | undefined>;
|
55 | }
|
56 |
|
57 | export const OpenerService = Symbol('OpenerService');
|
58 |
|
59 |
|
60 |
|
61 | export interface OpenerService {
|
62 | |
63 |
|
64 |
|
65 |
|
66 | getOpeners(): Promise<OpenHandler[]>;
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 | getOpeners(uri: URI, options?: OpenerOptions): Promise<OpenHandler[]>;
|
73 | |
74 |
|
75 |
|
76 |
|
77 | getOpener(uri: URI, options?: OpenerOptions): Promise<OpenHandler>;
|
78 | |
79 |
|
80 |
|
81 | addHandler?(openHandler: OpenHandler): Disposable;
|
82 | |
83 |
|
84 |
|
85 | onDidChangeOpeners?: Event<void>;
|
86 | }
|
87 |
|
88 | export async function open(openerService: OpenerService, uri: URI, options?: OpenerOptions): Promise<object | undefined> {
|
89 | const opener = await openerService.getOpener(uri, options);
|
90 | return opener.open(uri, options);
|
91 | }
|
92 |
|
93 | @injectable()
|
94 | export class DefaultOpenerService implements OpenerService {
|
95 |
|
96 | protected readonly customEditorOpenHandlers: OpenHandler[] = [];
|
97 |
|
98 | protected readonly onDidChangeOpenersEmitter = new Emitter<void>();
|
99 | readonly onDidChangeOpeners = this.onDidChangeOpenersEmitter.event;
|
100 |
|
101 | constructor(
|
102 | @inject(ContributionProvider) @named(OpenHandler)
|
103 | protected readonly handlersProvider: ContributionProvider<OpenHandler>
|
104 | ) { }
|
105 |
|
106 | addHandler(openHandler: OpenHandler): Disposable {
|
107 | this.customEditorOpenHandlers.push(openHandler);
|
108 | this.onDidChangeOpenersEmitter.fire();
|
109 |
|
110 | return Disposable.create(() => {
|
111 | this.customEditorOpenHandlers.splice(this.customEditorOpenHandlers.indexOf(openHandler), 1);
|
112 | this.onDidChangeOpenersEmitter.fire();
|
113 | });
|
114 | }
|
115 |
|
116 | async getOpener(uri: URI, options?: OpenerOptions): Promise<OpenHandler> {
|
117 | const handlers = await this.prioritize(uri, options);
|
118 | if (handlers.length >= 1) {
|
119 | return handlers[0];
|
120 | }
|
121 | return Promise.reject(new Error(`There is no opener for ${uri}.`));
|
122 | }
|
123 |
|
124 | async getOpeners(uri?: URI, options?: OpenerOptions): Promise<OpenHandler[]> {
|
125 | return uri ? this.prioritize(uri, options) : this.getHandlers();
|
126 | }
|
127 |
|
128 | protected async prioritize(uri: URI, options?: OpenerOptions): Promise<OpenHandler[]> {
|
129 | const prioritized = await Prioritizeable.prioritizeAll(this.getHandlers(), async handler => {
|
130 | try {
|
131 | return await handler.canHandle(uri, options);
|
132 | } catch {
|
133 | return 0;
|
134 | }
|
135 | });
|
136 | return prioritized.map(p => p.value);
|
137 | }
|
138 |
|
139 | protected getHandlers(): OpenHandler[] {
|
140 | return [
|
141 | ...this.handlersProvider.getContributions(),
|
142 | ...this.customEditorOpenHandlers
|
143 | ];
|
144 | }
|
145 |
|
146 | }
|