api.ts 12.2 KB
Newer Older
A
Asher 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
import * as vscode from "vscode";

import { localize } from "vs/nls";
import { Action } from "vs/base/common/actions";
import { SyncActionDescriptor, MenuRegistry, MenuId } from "vs/platform/actions/common/actions";
import { Registry } from "vs/platform/registry/common/platform";
import { IWorkbenchActionRegistry, Extensions as ActionExtensions} from "vs/workbench/common/actions";
import { CommandsRegistry, ICommandService } from "vs/platform/commands/common/commands";
import { IStat, IWatchOptions, FileOverwriteOptions, FileDeleteOptions, FileOpenOptions, IFileChange, FileWriteOptions, FileSystemProviderCapabilities, IFileService, FileType, FileOperation, IFileSystemProvider } from "vs/platform/files/common/files";
import { ITextFileService } from "vs/workbench/services/textfile/common/textfiles";
import { IModelService } from "vs/editor/common/services/modelService";
import { ITerminalService } from "vs/workbench/contrib/terminal/common/terminal";
import { IStorageService } from "vs/platform/storage/common/storage";
import { ServiceCollection } from "vs/platform/instantiation/common/serviceCollection";
import { INotificationService } from "vs/platform/notification/common/notification";
import { IStatusbarService, StatusbarAlignment } from "vs/platform/statusbar/common/statusbar";
import Severity from "vs/base/common/severity";
import { Emitter, Event } from "vs/base/common/event";
import * as extHostTypes from "vs/workbench/api/common/extHostTypes";
import { ServiceIdentifier, IInstantiationService } from "vs/platform/instantiation/common/instantiation";
import { URI } from "vs/base/common/uri";
import { ITreeViewDataProvider, IViewsRegistry, ITreeViewDescriptor, Extensions as ViewsExtensions, IViewContainersRegistry } from "vs/workbench/common/views";
import { CustomTreeViewPanel, CustomTreeView } from "vs/workbench/browser/parts/views/customView";
import { ViewletRegistry, Extensions as ViewletExtensions, ViewletDescriptor, ShowViewletAction } from "vs/workbench/browser/viewlet";
import { IExtensionService } from "vs/workbench/services/extensions/common/extensions";
import { ViewContainerViewlet } from "vs/workbench/browser/parts/views/viewsViewlet";
import { IConfigurationService } from "vs/platform/configuration/common/configuration";
import { IWorkbenchLayoutService } from "vs/workbench/services/layout/browser/layoutService";
import { ITelemetryService } from "vs/platform/telemetry/common/telemetry";
import { IWorkspaceContextService } from "vs/platform/workspace/common/workspace";
import { IEditorService } from "vs/workbench/services/editor/common/editorService";
import { IThemeService } from "vs/platform/theme/common/themeService";
import { IContextMenuService } from "vs/platform/contextview/browser/contextView";
import { IViewletService } from "vs/workbench/services/viewlet/browser/viewlet";
import { IEditorGroupsService } from "vs/workbench/services/editor/common/editorGroupsService";
import { createCSSRule } from "vs/base/browser/dom";
import { IDisposable } from "vs/base/common/lifecycle";

/**
 * Client-side implementation of VS Code's API.
 * TODO: Views aren't quite working.
 * TODO: Implement menu items for views (for item actions).
 * TODO: File system provider doesn't work.
 */
export const vscodeApi = (serviceCollection: ServiceCollection): typeof vscode => {
	const getService = <T>(id: ServiceIdentifier<T>): T => serviceCollection.get<T>(id) as T;
	const commandService = getService(ICommandService);
	const notificationService = getService(INotificationService);
	const fileService = getService(IFileService);
	const viewsRegistry = Registry.as<IViewsRegistry>(ViewsExtensions.ViewsRegistry);

	// It would be nice to just export what VS Code creates but it looks to me
	// that it assumes it's running in the extension host and wouldn't work here.
	// It is probably possible to create an extension host that runs in the
	// browser's main thread, but I'm not sure how much jank that would require.
	// We could have a web worker host but we want DOM access.
	return {
		EventEmitter: Emitter,
		TreeItemCollapsibleState: extHostTypes.TreeItemCollapsibleState,
		FileSystemError: extHostTypes.FileSystemError,
		FileType: FileType,
		Uri: URI,
		commands: {
			executeCommand: (commandId: string, ...args: any[]): any => {
				return commandService.executeCommand(commandId, ...args);
			},
			registerCommand: (id: string, command: () => void): any => {
				return CommandsRegistry.registerCommand(id, command);
			},
		},
		window: {
			registerTreeDataProvider: (id: string, dataProvider: ITreeViewDataProvider): void => {
				const view = viewsRegistry.getView(id);
				if (view) {
					(view as ITreeViewDescriptor).treeView.dataProvider = dataProvider;
				}
			},
			showErrorMessage: (message: string): void => {
				notificationService.error(message);
			},
		},
		workspace: {
			registerFileSystemProvider: (scheme: string, provider: vscode.FileSystemProvider): IDisposable => {
				return fileService.registerProvider(scheme, new FileSystemProvider(provider));
			},
		},
	} as any;
};

/**
 * Coder API.
 */
export const coderApi = (serviceCollection: ServiceCollection): typeof coder => {
	const getService = <T>(id: ServiceIdentifier<T>): T => serviceCollection.get<T>(id) as T;
	return {
		workbench: {
			action: Action,
			syncActionDescriptor: SyncActionDescriptor,
			commandRegistry: CommandsRegistry,
			actionsRegistry: Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions),
			registerView: (viewId, viewName, containerId, containerName, icon): void =>  {
				const cssClass = `extensionViewlet-${containerId}`;
				const id = `workbench.view.extension.${containerId}`;
				class CustomViewlet extends ViewContainerViewlet {
					public constructor(
						@IConfigurationService configurationService: IConfigurationService,
						@IWorkbenchLayoutService layoutService: IWorkbenchLayoutService,
						@ITelemetryService telemetryService: ITelemetryService,
						@IWorkspaceContextService contextService: IWorkspaceContextService,
						@IStorageService storageService: IStorageService,
						@IEditorService _editorService: IEditorService,
						@IInstantiationService instantiationService: IInstantiationService,
						@IThemeService themeService: IThemeService,
						@IContextMenuService contextMenuService: IContextMenuService,
						@IExtensionService extensionService: IExtensionService,
					) {
						super(id, `${id}.state`, true, configurationService, layoutService, telemetryService, storageService, instantiationService, themeService, contextMenuService, extensionService, contextService);
					}
				}

A
Asher 已提交
121 122
				Registry.as<ViewletRegistry>(ViewletExtensions.Viewlets).registerViewlet(
					new ViewletDescriptor(CustomViewlet as any, id, containerName, cssClass, undefined, URI.parse(icon)),
A
Asher 已提交
123 124
				);

A
Asher 已提交
125
				Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions).registerWorkbenchAction(
A
Asher 已提交
126 127 128 129 130
					new SyncActionDescriptor(OpenCustomViewletAction as any, id, localize("showViewlet", "Show {0}", containerName)),
					"View: Show {0}",
					localize("view", "View"),
				);

A
Asher 已提交
131
				// Generate CSS to show the icon in the activity bar.
A
Asher 已提交
132 133 134
				const iconClass = `.monaco-workbench .activitybar .monaco-action-bar .action-label.${cssClass}`;
				createCSSRule(iconClass, `-webkit-mask: url('${icon}') no-repeat 50% 50%`);

A
Asher 已提交
135 136
				const container = Registry.as<IViewContainersRegistry>(ViewsExtensions.ViewContainersRegistry).registerViewContainer(containerId);
				Registry.as<IViewsRegistry>(ViewsExtensions.ViewsRegistry).registerViews([{
A
Asher 已提交
137 138 139 140
					id: viewId,
					name: viewName,
					ctorDescriptor: { ctor: CustomTreeViewPanel },
					treeView: getService(IInstantiationService).createInstance(CustomTreeView as any, viewId, container),
A
Asher 已提交
141
				}] as ITreeViewDescriptor[], container);
A
Asher 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
			},
			menuRegistry: MenuRegistry as any,
			statusbarService: getService(IStatusbarService) as any,
			notificationService: getService(INotificationService),
			terminalService: getService(ITerminalService),
			onFileCreate: (cb): void => {
				getService<IFileService>(IFileService).onAfterOperation((e) => {
					if (e.operation === FileOperation.CREATE) {
						cb(e.resource.path);
					}
				});
			},
			onFileMove: (cb): void => {
				getService<IFileService>(IFileService).onAfterOperation((e) => {
					if (e.operation === FileOperation.MOVE) {
						cb(e.resource.path, e.target ? e.target.resource.path : undefined!);
					}
				});
			},
			onFileDelete: (cb): void => {
				getService<IFileService>(IFileService).onAfterOperation((e) => {
					if (e.operation === FileOperation.DELETE) {
						cb(e.resource.path);
					}
				});
			},
			onFileSaved: (cb): void => {
				getService<ITextFileService>(ITextFileService).models.onModelSaved((e) => {
					cb(e.resource.path);
				});
			},
			onFileCopy: (cb): void => {
				getService<IFileService>(IFileService).onAfterOperation((e) => {
					if (e.operation === FileOperation.COPY) {
						cb(e.resource.path, e.target ? e.target.resource.path : undefined!);
					}
				});
			},
			onModelAdded: (cb): void => {
				getService<IModelService>(IModelService).onModelAdded((e) => {
					cb(e.uri.path, e.getLanguageIdentifier().language);
				});
			},
			onModelRemoved: (cb): void => {
				getService<IModelService>(IModelService).onModelRemoved((e) => {
					cb(e.uri.path, e.getLanguageIdentifier().language);
				});
			},
			onModelLanguageChange: (cb): void => {
				getService<IModelService>(IModelService).onModelModeChanged((e) => {
					cb(e.model.uri.path, e.model.getLanguageIdentifier().language, e.oldModeId);
				});
			},
			onTerminalAdded: (cb): void => {
				getService<ITerminalService>(ITerminalService).onInstanceCreated(() => cb());
			},
			onTerminalRemoved: (cb): void => {
				getService<ITerminalService>(ITerminalService).onInstanceDisposed(() => cb());
			},
		},
		// @ts-ignore
		MenuId: MenuId,
		Severity: Severity,
		// @ts-ignore
		StatusbarAlignment: StatusbarAlignment,
	};
};

class OpenCustomViewletAction extends ShowViewletAction {
	public constructor(
		id: string, label: string,
		@IViewletService viewletService: IViewletService,
		@IEditorGroupsService editorGroupService: IEditorGroupsService,
		@IWorkbenchLayoutService layoutService: IWorkbenchLayoutService,
	) {
		super(id, label, id, viewletService, editorGroupService, layoutService);
	}
}

class FileSystemProvider implements IFileSystemProvider {
	private readonly _onDidChange = new Emitter<IFileChange[]>();

	public readonly onDidChangeFile: Event<IFileChange[]> = this._onDidChange.event;

	public readonly capabilities: FileSystemProviderCapabilities;
	public readonly onDidChangeCapabilities: Event<void> = Event.None;

A
Asher 已提交
229
	public constructor(private readonly provider: vscode.FileSystemProvider) {
A
Asher 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
		this.capabilities = FileSystemProviderCapabilities.Readonly;
	}

	public watch(resource: URI, opts: IWatchOptions): IDisposable {
		return this.provider.watch(resource, opts);
	}

	public async stat(resource: URI): Promise<IStat> {
		return this.provider.stat(resource);
	}

	public async readFile(resource: URI): Promise<Uint8Array> {
		return this.provider.readFile(resource);
	}

	public async writeFile(resource: URI, content: Uint8Array, opts: FileWriteOptions): Promise<void> {
		return this.provider.writeFile(resource, content, opts);
	}

	public async delete(resource: URI, opts: FileDeleteOptions): Promise<void> {
		return this.provider.delete(resource, opts);
	}

	public mkdir(_resource: URI): Promise<void> {
		throw new Error("not implemented");
	}

	public async readdir(resource: URI): Promise<[string, FileType][]> {
		return this.provider.readDirectory(resource);
	}

	public async rename(resource: URI, target: URI, opts: FileOverwriteOptions): Promise<void> {
		return this.provider.rename(resource, target, opts);
	}

	public async copy(resource: URI, target: URI, opts: FileOverwriteOptions): Promise<void> {
		return this.provider.copy!(resource, target, opts);
	}

	public open(_resource: URI, _opts: FileOpenOptions): Promise<number> {
		throw new Error("not implemented");
	}

	public close(_fd: number): Promise<void> {
		throw new Error("not implemented");
	}

	public read(_fd: number, _pos: number, _data: Uint8Array, _offset: number, _length: number): Promise<number> {
		throw new Error("not implemented");
	}

	public write(_fd: number, _pos: number, _data: Uint8Array, _offset: number, _length: number): Promise<number> {
		throw new Error("not implemented");
	}
}