code-server/packages/vscode/src/client.ts

134 lines
5.2 KiB
TypeScript
Raw Normal View History

import { IdeClient } from "@coder/ide";
2019-02-27 02:12:42 +08:00
import { client as ideClientInstance } from "@coder/ide/src/fill/client";
2019-02-27 23:03:44 +08:00
import Severity from "vs/base/common/severity";
import { INotificationService } from "vs/platform/notification/common/notification";
2019-02-28 05:36:39 +08:00
import { IStatusbarService, StatusbarAlignment } from "vs/platform/statusbar/common/statusbar";
import * as paths from "./fill/paths";
import product from "./fill/product";
2019-01-19 08:04:24 +08:00
import "./vscode.scss";
2019-05-21 03:35:58 +08:00
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 } from 'vs/workbench/common/actions';
2019-02-28 05:36:39 +08:00
import { CommandsRegistry } from "vs/platform/commands/common/commands";
2019-03-13 05:43:53 +08:00
import { IFileService, FileOperation } 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";
2019-04-18 05:47:02 +08:00
import { IStorageService } from "vs/platform/storage/common/storage";
2019-05-21 03:35:58 +08:00
// NOTE: shouldn't import anything from VS Code here or anything that will
// depend on a synchronous fill like `os`.
2019-01-31 05:40:01 +08:00
class VSClient extends IdeClient {
protected initialize(): Promise<void> {
return this.task("Start workbench", 1000, async (data, sharedData) => {
paths._paths.initialize(data, sharedData);
product.initialize(data);
process.env.SHELL = data.shell;
// At this point everything should be filled, including `os`. `os` also
// relies on `initData` but it listens first so it initialize before this
// callback, meaning we are safe to include everything from VS Code now.
const { workbench } = require("./workbench") as typeof import("./workbench");
await workbench.initialize();
2019-02-27 02:12:42 +08:00
2019-02-27 23:03:44 +08:00
// tslint:disable-next-line:no-any
const getService = <T>(id: any): T => workbench.serviceCollection.get<T>(id) as T;
2019-02-27 02:12:42 +08:00
window.ide = {
client: ideClientInstance,
workbench: {
2019-05-21 03:35:58 +08:00
action: Action,
syncActionDescriptor: SyncActionDescriptor,
2019-02-28 05:36:39 +08:00
commandRegistry: CommandsRegistry,
2019-05-21 03:35:58 +08:00
actionsRegistry: Registry.as<IWorkbenchActionRegistry>(Extensions.WorkbenchActions),
menuRegistry: MenuRegistry,
statusbarService: getService<IStatusbarService>(IStatusbarService),
2019-02-27 23:03:44 +08:00
notificationService: getService<INotificationService>(INotificationService),
terminalService: getService<ITerminalService>(ITerminalService),
2019-04-18 05:47:02 +08:00
storageService: {
save: (): Promise<void> => {
// tslint:disable-next-line:no-any
const storageService = getService<IStorageService>(IStorageService) as any;
return storageService.close();
},
},
2019-03-13 05:43:53 +08:00
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());
},
2019-02-27 02:12:42 +08:00
},
2019-02-28 05:36:39 +08:00
// @ts-ignore
// tslint:disable-next-line:no-any
MenuId: MenuId as any,
// tslint:disable-next-line:no-any
Severity: Severity as any,
// @ts-ignore
// tslint:disable-next-line:no-any
StatusbarAlignment: StatusbarAlignment as any,
2019-02-27 02:12:42 +08:00
};
const event = new CustomEvent("ide-ready");
// tslint:disable-next-line:no-any
(<any>event).ide = window.ide;
window.dispatchEvent(event);
}, this.initData, this.sharedProcessData);
}
}
export const client = new VSClient();