UNPKG

2.09 kBPlain TextView Raw
1// *****************************************************************************
2// Copyright (C) 2022 Ericsson and others.
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License v. 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0.
7//
8// This Source Code may also be made available under the following Secondary
9// Licenses when the conditions for such availability set forth in the Eclipse
10// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11// with the GNU Classpath Exception which is available at
12// https://www.gnu.org/software/classpath/license.html.
13//
14// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15// *****************************************************************************
16
17import { inject, injectable } from 'inversify';
18import URI from '../common/uri';
19import { MaybePromise, SelectionService, UriSelection } from '../common';
20import { EnvVariablesServer } from '../common/env-variables';
21
22@injectable()
23export class UserWorkingDirectoryProvider {
24 @inject(SelectionService) protected readonly selectionService: SelectionService;
25 @inject(EnvVariablesServer) protected readonly envVariables: EnvVariablesServer;
26
27 /**
28 * @returns A {@link URI} that represents a good guess about the directory in which the user is currently operating.
29 *
30 * Factors considered may include the current widget, current selection, user home directory, or other application state.
31 */
32 async getUserWorkingDir(): Promise<URI> {
33 return await this.getFromSelection()
34 ?? this.getFromUserHome();
35 }
36
37 protected getFromSelection(): MaybePromise<URI | undefined> {
38 return this.ensureIsDirectory(UriSelection.getUri(this.selectionService.selection));
39 }
40
41 protected getFromUserHome(): MaybePromise<URI> {
42 return this.envVariables.getHomeDirUri().then(home => new URI(home));
43 }
44
45 protected ensureIsDirectory(uri?: URI): MaybePromise<URI | undefined> {
46 return uri?.parent;
47 }
48}