3.1 KiB
Code Organization
Code consists a layered and modular core that can be extended using extensions. Extensions are run in a separate process refered to as the
extension host. Extensions are implemented by utilizing the extension API.
Layers
The core is partitioned into the following layers:
base: Provides general utilities and user interface building blocksplatform: Defines service injection support and the base services for Codeeditor: The "Monaco" editor is available as a separate downloadable componentlanguages: For historical reasons, not all languages are implemented as extensions (yet) - as Code evolves we will migrate more languages to towards extensionsworkbench: Hosts the "Monaco" editor and provides the framework for "viewlets" like the Explorer, Status Bar, or Menu Bar, leveraging Electron to implement the Code desktop application.
Target Environments
The core of Code is fully implemented in TypeScript. Inside each layer the code is organized by the target runtime environment. This ensures that only the runtime specific APIs are used. In the code we distinguish between the following target environments:
common: Source code that only requires basic JavaScript APIs and run in all the other target environmentsbrowser: Source code that requires thebrowserAPIs like access to the DOM- may use code from:
common
- may use code from:
node: Source code that requiresnodejsAPIs- may use code from:
common
- may use code from:
electron-browser: Source code that requires the Electron renderer-process APIs- may use code from:
common,browser,node
- may use code from:
electron-main: Source code that requires the Electron main-process APIs- may use code from:
common,node
- may use code from:
Dependency Injection
The code is organised around services of which most are defined in the platform layer. Services get to its clients via constructor injection.
A service definition is two parts: (1) the interface of a service, and (2) a service identifier - the latter is required because TypeScript doesn't use nominal but structural typing. A service identifier is a decoration (as proposed for ES7) and should have the same name as the service interface.
Declaring a service dependency happens by adding a corresponding decoration to a constructor argument. In the snippet below @IModelService is the service identifier decoration and IModelService is the (optional) type annotation for this argument.
class Client {
constructor(@IModelService modelService: IModelService) {
// use modelService
}
}
Use the instantiation service to create instances for service consumers, like so instantiationService.createInstance(Client). Usually, this is done for you when being registered as a contribution, like a Viewlet or Language.