UNPKG

3.64 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2022 Ericsson 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.UserWorkingDirectoryProvider = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const uri_1 = require("../common/uri");
22const common_1 = require("../common");
23const env_variables_1 = require("../common/env-variables");
24const navigatable_types_1 = require("./navigatable-types");
25let UserWorkingDirectoryProvider = class UserWorkingDirectoryProvider {
26 configure(app) {
27 app.shell.onDidChangeCurrentWidget(e => { var _a; return this.setLastOpenResource((_a = e.newValue) !== null && _a !== void 0 ? _a : undefined); });
28 this.setLastOpenResource(app.shell.currentWidget);
29 }
30 setLastOpenResource(widget) {
31 if (navigatable_types_1.Navigatable.is(widget)) {
32 const uri = widget.getResourceUri();
33 if (uri && uri.scheme !== common_1.UNTITLED_SCHEME) {
34 this.lastOpenResource = uri;
35 }
36 }
37 }
38 /**
39 * @returns A {@link URI} that represents a good guess about the directory in which the user is currently operating.
40 *
41 * Factors considered may include the current widget, current selection, user home directory, or other application state.
42 */
43 async getUserWorkingDir() {
44 var _a;
45 return (_a = await this.getFromSelection()) !== null && _a !== void 0 ? _a : this.getFromUserHome();
46 }
47 getFromSelection() {
48 const uri = common_1.UriSelection.getUri(this.selectionService.selection);
49 if ((uri === null || uri === void 0 ? void 0 : uri.scheme) === common_1.UNTITLED_SCHEME) {
50 // An untitled file is not a valid working directory context.
51 return undefined;
52 }
53 return this.ensureIsDirectory(uri);
54 }
55 getFromLastOpenResource() {
56 return this.ensureIsDirectory(this.lastOpenResource);
57 }
58 getFromUserHome() {
59 return this.envVariables.getHomeDirUri().then(home => new uri_1.default(home));
60 }
61 ensureIsDirectory(uri) {
62 return uri === null || uri === void 0 ? void 0 : uri.parent;
63 }
64};
65(0, tslib_1.__decorate)([
66 (0, inversify_1.inject)(common_1.SelectionService),
67 (0, tslib_1.__metadata)("design:type", common_1.SelectionService)
68], UserWorkingDirectoryProvider.prototype, "selectionService", void 0);
69(0, tslib_1.__decorate)([
70 (0, inversify_1.inject)(env_variables_1.EnvVariablesServer),
71 (0, tslib_1.__metadata)("design:type", Object)
72], UserWorkingDirectoryProvider.prototype, "envVariables", void 0);
73UserWorkingDirectoryProvider = (0, tslib_1.__decorate)([
74 (0, inversify_1.injectable)()
75], UserWorkingDirectoryProvider);
76exports.UserWorkingDirectoryProvider = UserWorkingDirectoryProvider;
77//# sourceMappingURL=user-working-directory-provider.js.map
\No newline at end of file