Move vscode.newWindow & vscode.removeFromRecentlyOpened out of apiCommands. For #110583

This commit is contained in:
Martin Aeschlimann 2021-05-31 20:09:07 +02:00
parent 2ae32273fd
commit 91b7e6027a
No known key found for this signature in database
GPG key ID: 2609A01E695523E3
3 changed files with 90 additions and 132 deletions

View file

@ -7,8 +7,6 @@ import { URI } from 'vs/base/common/uri';
import * as typeConverters from 'vs/workbench/api/common/extHostTypeConverters';
import { CommandsRegistry, ICommandService, ICommandHandler } from 'vs/platform/commands/common/commands';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IOpenEmptyWindowOptions } from 'vs/platform/windows/common/windows';
import { IWorkspacesService, IRecent } from 'vs/platform/workspaces/common/workspaces';
import { ILogService } from 'vs/platform/log/common/log';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IViewDescriptorService, IViewsService, ViewVisibilityState } from 'vs/workbench/common/views';
@ -30,83 +28,6 @@ function adjustHandler(handler: (executor: ICommandsExecutor, ...args: any[]) =>
};
}
interface INewWindowAPICommandOptions {
reuseWindow?: boolean;
/**
* If set, defines the remoteAuthority of the new window. `null` will open a local window.
* If not set, defaults to remoteAuthority of the current window.
*/
remoteAuthority?: string | null;
}
export class NewWindowAPICommand {
public static readonly ID = 'vscode.newWindow';
public static execute(executor: ICommandsExecutor, options?: INewWindowAPICommandOptions): Promise<any> {
const commandOptions: IOpenEmptyWindowOptions = {
forceReuseWindow: options && options.reuseWindow,
remoteAuthority: options && options.remoteAuthority
};
return executor.executeCommand('_files.newWindow', commandOptions);
}
}
CommandsRegistry.registerCommand({
id: NewWindowAPICommand.ID,
handler: adjustHandler(NewWindowAPICommand.execute),
description: {
description: 'Opens an new window',
args: [
]
}
});
CommandsRegistry.registerCommand('_workbench.removeFromRecentlyOpened', function (accessor: ServicesAccessor, uri: URI) {
const workspacesService = accessor.get(IWorkspacesService);
return workspacesService.removeRecentlyOpened([uri]);
});
export class RemoveFromRecentlyOpenedAPICommand {
public static readonly ID = 'vscode.removeFromRecentlyOpened';
public static execute(executor: ICommandsExecutor, path: string | URI): Promise<any> {
if (typeof path === 'string') {
path = path.match(/^[^:/?#]+:\/\//) ? URI.parse(path) : URI.file(path);
} else {
path = URI.revive(path); // called from extension host
}
return executor.executeCommand('_workbench.removeFromRecentlyOpened', path);
}
}
CommandsRegistry.registerCommand(RemoveFromRecentlyOpenedAPICommand.ID, adjustHandler(RemoveFromRecentlyOpenedAPICommand.execute));
interface RecentEntry {
uri: URI;
type: 'workspace' | 'folder' | 'file';
label?: string;
remoteAuthority?: string;
}
CommandsRegistry.registerCommand('_workbench.addToRecentlyOpened', async function (accessor: ServicesAccessor, recentEntry: RecentEntry) {
const workspacesService = accessor.get(IWorkspacesService);
let recent: IRecent | undefined = undefined;
const uri = recentEntry.uri;
const label = recentEntry.label;
const remoteAuthority = recentEntry.remoteAuthority;
if (recentEntry.type === 'workspace') {
const workspace = await workspacesService.getWorkspaceIdentifier(uri);
recent = { workspace, label, remoteAuthority };
} else if (recentEntry.type === 'folder') {
recent = { folderUri: uri, label, remoteAuthority };
} else {
recent = { fileUri: uri, label, remoteAuthority };
}
return workspacesService.addRecentlyOpened([recent]);
});
CommandsRegistry.registerCommand('_workbench.getRecentlyOpened', async function (accessor: ServicesAccessor) {
const workspacesService = accessor.get(IWorkspacesService);
return workspacesService.getRecentlyOpened();
});
CommandsRegistry.registerCommand('_extensionTests.setLogLevel', function (accessor: ServicesAccessor, level: number) {
const logService = accessor.get(ILogService);
const environmentService = accessor.get(IEnvironmentService);

View file

@ -4,17 +4,14 @@
*--------------------------------------------------------------------------------------------*/
import { URI } from 'vs/base/common/uri';
import { DisposableStore } from 'vs/base/common/lifecycle';
import type * as vscode from 'vscode';
import * as typeConverters from 'vs/workbench/api/common/extHostTypeConverters';
import * as types from 'vs/workbench/api/common/extHostTypes';
import { IRawColorInfo, IWorkspaceEditDto, ICallHierarchyItemDto, IIncomingCallDto, IOutgoingCallDto } from 'vs/workbench/api/common/extHost.protocol';
import * as modes from 'vs/editor/common/modes';
import * as search from 'vs/workbench/contrib/search/common/search';
import { ICommandHandlerDescription } from 'vs/platform/commands/common/commands';
import { ApiCommand, ApiCommandArgument, ApiCommandResult, ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
import { CustomCodeAction } from 'vs/workbench/api/common/extHostLanguageFeatures';
import { ICommandsExecutor, RemoveFromRecentlyOpenedAPICommand } from './apiCommands';
import { isFalsyOrEmpty } from 'vs/base/common/arrays';
import { IRange } from 'vs/editor/common/core/range';
import { IPosition } from 'vs/editor/common/core/position';
@ -420,56 +417,8 @@ export class ExtHostApiCommands {
static register(commands: ExtHostCommands) {
newCommands.forEach(commands.registerApiCommand, commands);
return new ExtHostApiCommands(commands).registerCommands();
}
private _commands: ExtHostCommands;
private readonly _disposables = new DisposableStore();
private constructor(commands: ExtHostCommands) {
this._commands = commands;
}
registerCommands() {
// -----------------------------------------------------------------
// The following commands are registered on both sides separately.
//
// We are trying to maintain backwards compatibility for cases where
// API commands are encoded as markdown links, for example.
// -----------------------------------------------------------------
type ICommandHandler = (...args: any[]) => any;
const adjustHandler = (handler: (executor: ICommandsExecutor, ...args: any[]) => any): ICommandHandler => {
return (...args: any[]) => {
return handler(this._commands, ...args);
};
};
this._register(RemoveFromRecentlyOpenedAPICommand.ID, adjustHandler(RemoveFromRecentlyOpenedAPICommand.execute), {
description: 'Removes an entry with the given path from the recently opened list.',
args: [
{ name: 'path', description: 'Path to remove from recently opened.', constraint: (value: any) => typeof value === 'string' }
]
});
}
// --- command impl
/**
* @deprecated use the ApiCommand instead
*/
private _register(id: string, handler: (...args: any[]) => any, description?: ICommandHandlerDescription): void {
const disposable = this._commands.registerCommand(false, id, handler, this, description);
this._disposables.add(disposable);
}
}
function tryMapWith<T, R>(f: (x: T) => R) {

View file

@ -20,8 +20,8 @@ import { IModeService } from 'vs/editor/common/services/modeService';
import { IFileDialogService, IPickAndOpenOptions } from 'vs/platform/dialogs/common/dialogs';
import { URI } from 'vs/base/common/uri';
import { Schemas } from 'vs/base/common/network';
import { IOpenWindowOptions, IWindowOpenable } from 'vs/platform/windows/common/windows';
import { hasWorkspaceFileExtension } from 'vs/platform/workspaces/common/workspaces';
import { IOpenEmptyWindowOptions, IOpenWindowOptions, IWindowOpenable } from 'vs/platform/windows/common/windows';
import { hasWorkspaceFileExtension, IRecent, IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
import { IPathService } from 'vs/workbench/services/path/common/pathService';
import { ILocalizedString } from 'vs/platform/actions/common/actions';
@ -185,3 +185,91 @@ CommandsRegistry.registerCommand({
]
}
});
interface INewWindowAPICommandOptions {
reuseWindow?: boolean;
/**
* If set, defines the remoteAuthority of the new window. `null` will open a local window.
* If not set, defaults to remoteAuthority of the current window.
*/
remoteAuthority?: string | null;
}
CommandsRegistry.registerCommand({
id: 'vscode.newWindow',
handler: (accessor: ServicesAccessor, options?: INewWindowAPICommandOptions) => {
const commandOptions: IOpenEmptyWindowOptions = {
forceReuseWindow: options && options.reuseWindow,
remoteAuthority: options && options.remoteAuthority
};
const commandService = accessor.get(ICommandService);
return commandService.executeCommand('_files.newWindow', commandOptions);
},
description: {
description: 'Opens an new window depending on the newWindow argument.',
args: [
{
name: 'options',
description: '(optional) Options. Object with the following properties: ' +
'`reuseWindow`: Whether to open a new window or the same. Defaults to opening in a new window. ',
constraint: (value: any) => value === undefined || typeof value === 'object'
}
]
}
});
// recent history commands
CommandsRegistry.registerCommand('_workbench.removeFromRecentlyOpened', function (accessor: ServicesAccessor, uri: URI) {
const workspacesService = accessor.get(IWorkspacesService);
return workspacesService.removeRecentlyOpened([uri]);
});
CommandsRegistry.registerCommand({
id: 'vscode.removeFromRecentlyOpened',
handler: (accessor: ServicesAccessor, path: string | URI): Promise<any> => {
if (typeof path === 'string') {
path = path.match(/^[^:/?#]+:\/\//) ? URI.parse(path) : URI.file(path);
} else {
path = URI.revive(path); // called from extension host
}
const workspacesService = accessor.get(IWorkspacesService);
return workspacesService.removeRecentlyOpened([path]);
},
description: {
description: 'Removes an entry with the given path from the recently opened list.',
args: [
{ name: 'path', description: 'URI or URI string to remove from recently opened.', constraint: (value: any) => typeof value === 'string' || value instanceof URI }
]
}
});
interface RecentEntry {
uri: URI;
type: 'workspace' | 'folder' | 'file';
label?: string;
remoteAuthority?: string;
}
CommandsRegistry.registerCommand('_workbench.addToRecentlyOpened', async function (accessor: ServicesAccessor, recentEntry: RecentEntry) {
const workspacesService = accessor.get(IWorkspacesService);
let recent: IRecent | undefined = undefined;
const uri = recentEntry.uri;
const label = recentEntry.label;
const remoteAuthority = recentEntry.remoteAuthority;
if (recentEntry.type === 'workspace') {
const workspace = await workspacesService.getWorkspaceIdentifier(uri);
recent = { workspace, label, remoteAuthority };
} else if (recentEntry.type === 'folder') {
recent = { folderUri: uri, label, remoteAuthority };
} else {
recent = { fileUri: uri, label, remoteAuthority };
}
return workspacesService.addRecentlyOpened([recent]);
});
CommandsRegistry.registerCommand('_workbench.getRecentlyOpened', async function (accessor: ServicesAccessor) {
const workspacesService = accessor.get(IWorkspacesService);
return workspacesService.getRecentlyOpened();
});