UNPKG

2.76 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2024 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.OpenWithService = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const disposable_1 = require("../common/disposable");
22const nls_1 = require("../common/nls");
23const quick_input_1 = require("./quick-input");
24let OpenWithService = class OpenWithService {
25 constructor() {
26 this.handlers = [];
27 }
28 registerHandler(handler) {
29 this.handlers.push(handler);
30 return disposable_1.Disposable.create(() => {
31 const index = this.handlers.indexOf(handler);
32 if (index !== -1) {
33 this.handlers.splice(index, 1);
34 }
35 });
36 }
37 async openWith(uri) {
38 const handlers = this.getHandlers(uri);
39 const result = await this.quickInputService.pick(handlers.map(handler => {
40 var _a;
41 return ({
42 handler: handler,
43 label: (_a = handler.label) !== null && _a !== void 0 ? _a : handler.id,
44 detail: handler.providerName
45 });
46 }), {
47 placeHolder: nls_1.nls.localizeByDefault("Select editor for '{0}'", uri.path.base)
48 });
49 if (result) {
50 return result.handler.open(uri);
51 }
52 }
53 getHandlers(uri) {
54 const map = new Map(this.handlers.map(handler => [handler, handler.canHandle(uri)]));
55 return this.handlers.filter(handler => map.get(handler) > 0).sort((a, b) => map.get(b) - map.get(a));
56 }
57};
58(0, tslib_1.__decorate)([
59 (0, inversify_1.inject)(quick_input_1.QuickInputService),
60 (0, tslib_1.__metadata)("design:type", Object)
61], OpenWithService.prototype, "quickInputService", void 0);
62OpenWithService = (0, tslib_1.__decorate)([
63 (0, inversify_1.injectable)()
64], OpenWithService);
65exports.OpenWithService = OpenWithService;
66//# sourceMappingURL=open-with-service.js.map
\No newline at end of file