@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
72 lines • 3.07 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2022 Ericsson 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-only WITH Classpath-exception-2.0
// *****************************************************************************
import { __decorate } from "tslib";
import { inject, injectable } from 'inversify';
import URI from '../common/uri.js';
import { SelectionService, UNTITLED_SCHEME, UriSelection } from '../common/index.js';
import { EnvVariablesServer } from '../common/env-variables/index.js';
import { Navigatable } from './navigatable-types.js';
let UserWorkingDirectoryProvider = class UserWorkingDirectoryProvider {
configure(app) {
app.shell.onDidChangeCurrentWidget(e => this.setLastOpenResource(e.newValue ?? undefined));
this.setLastOpenResource(app.shell.currentWidget);
}
setLastOpenResource(widget) {
if (Navigatable.is(widget)) {
const uri = widget.getResourceUri();
if (uri && uri.scheme !== UNTITLED_SCHEME) {
this.lastOpenResource = uri;
}
}
}
/**
* @returns A {@link URI} that represents a good guess about the directory in which the user is currently operating.
*
* Factors considered may include the current widget, current selection, user home directory, or other application state.
*/
async getUserWorkingDir() {
return await this.getFromSelection()
?? this.getFromUserHome();
}
getFromSelection() {
const uri = UriSelection.getUri(this.selectionService.selection);
if (uri?.scheme === UNTITLED_SCHEME) {
// An untitled file is not a valid working directory context.
return undefined;
}
return this.ensureIsDirectory(uri);
}
getFromLastOpenResource() {
return this.ensureIsDirectory(this.lastOpenResource);
}
getFromUserHome() {
return this.envVariables.getHomeDirUri().then(home => new URI(home));
}
ensureIsDirectory(uri) {
return uri?.parent;
}
};
__decorate([
inject(SelectionService)
], UserWorkingDirectoryProvider.prototype, "selectionService", void 0);
__decorate([
inject(EnvVariablesServer)
], UserWorkingDirectoryProvider.prototype, "envVariables", void 0);
UserWorkingDirectoryProvider = __decorate([
injectable()
], UserWorkingDirectoryProvider);
export { UserWorkingDirectoryProvider };
//# sourceMappingURL=user-working-directory-provider.js.map