mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-30 18:27:49 +08:00
Update plugin_services.md (#242)
* Update plugin_services.md discourage module level service, document Kotlin way to get services, add note about read action * Update plugin_services.md add note about light services * Update plugin_services.md add note about light services
This commit is contained in:
parent
6254ca565f
commit
0c71615b3d
@ -8,13 +8,25 @@ The *IntelliJ Platform* ensures that only one instance of a service is loaded ev
|
||||
|
||||
The *IntelliJ Platform* offers three types of services: _application level_ services, _project level_ services and _module level_ services.
|
||||
|
||||
## Light Service
|
||||
|
||||
Service that is not going to be overridden. No need to register it in a `plugin.xml`.
|
||||
|
||||
To register: annotate class using [@Service](https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/openapi/components/Service.java) annotation. If service is written in Java and not Kotlin, mark class as `final`.
|
||||
|
||||
Restrictions:
|
||||
|
||||
* constructor injection is not supported (since it is deprecated), but project level service can define constructor that accepts `Project`, and module level `Module`.
|
||||
* if service it is a [PersistentStateComponent](https://www.jetbrains.org/intellij/sdk/docs/basics/persisting_state_of_components.html), roaming must be disabled (`roamingType` is set to `RoamingType.DISABLED`).
|
||||
* service class must be `final`.
|
||||
|
||||
## How to Declare a Service?
|
||||
|
||||
To declare a service, you can use the following extension points in the IntelliJ Platform:
|
||||
|
||||
* `com.intellij.applicationService`: designed to declare an application level service.
|
||||
* `com.intellij.projectService`: designed to declare a project level service.
|
||||
* `com.intellij.moduleService`: designed to declare a module level service.
|
||||
* `com.intellij.moduleService`: designed to declare a module level service. Consider not using module services because it can lead to increased memory usage for a project with a lot of modules.
|
||||
|
||||
**To declare a service:**
|
||||
|
||||
@ -45,14 +57,21 @@ If `serviceInterface` isn't specified, it's supposed to have the same value as `
|
||||
|
||||
## Retrieving a service
|
||||
|
||||
Getting service doesn't need read action and can be performed from any thread. If service requested from several threads, it will be initialized in the first thread and another threads will be blocked until service is not fully initialized.
|
||||
|
||||
To instantiate your service, in Java code, use the following syntax:
|
||||
|
||||
```java
|
||||
MyApplicationService applicationService = ServiceManager.getService(MyApplicationService.class);
|
||||
|
||||
MyProjectService projectService = ServiceManager.getService(project, MyProjectService.class);
|
||||
MyProjectService projectService = project.getService(MyProjectService.class)
|
||||
```
|
||||
|
||||
MyModuleService moduleService = ModuleServiceManager.getService(module, MyModuleService.class);
|
||||
In Kotlin code you can use convenient methods:
|
||||
```kotlin
|
||||
MyApplicationService applicationService = service<MyApplicationService>()
|
||||
|
||||
MyProjectService projectService = project<service<MyProjectService>()
|
||||
```
|
||||
|
||||
### Sample Plugin
|
||||
|
Loading…
x
Reference in New Issue
Block a user