mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-28 01:07:49 +08:00
sdk.md: cleanup
This commit is contained in:
parent
27a63b97fb
commit
1824f5dbb0
@ -18,25 +18,25 @@ Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk();
|
|||||||
|
|
||||||
## Getting and Setting Project SDK Attributes
|
## Getting and Setting Project SDK Attributes
|
||||||
|
|
||||||
* To get the project level SDK
|
* To get the project-level SDK:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
Sdk projectSDK = ProjectRootManager.getInstance(project).getProjectSdk();
|
Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk();
|
||||||
```
|
```
|
||||||
|
|
||||||
* To get the project level SDK name:
|
* To get the project-level SDK name:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
String projectSDKName = ProjectRootManager.getInstance(project).getProjectSdkName();
|
String projectSdkName = ProjectRootManager.getInstance(project).getProjectSdkName();
|
||||||
```
|
```
|
||||||
|
|
||||||
* To set the project level SDK:
|
* To set the project-level SDK:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
ProjectRootManager.getInstance(project).setProjectSdk(Sdk jdk);
|
ProjectRootManager.getInstance(project).setProjectSdk(Sdk jdk);
|
||||||
```
|
```
|
||||||
|
|
||||||
* To set the project level SDK name:
|
* To set the project-level SDK name:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
ProjectRootManager.getInstance(project).setProjectSdkName(String name);
|
ProjectRootManager.getInstance(project).setProjectSdkName(String name);
|
||||||
@ -57,10 +57,10 @@ To make SDK settings persistent, override `setupSdkPaths()` and save settings by
|
|||||||
```java
|
```java
|
||||||
@Override
|
@Override
|
||||||
public boolean setupSdkPaths(@NotNull Sdk sdk, @NotNull SdkModel sdkModel) {
|
public boolean setupSdkPaths(@NotNull Sdk sdk, @NotNull SdkModel sdkModel) {
|
||||||
SdkModificator modificator = sdk.getSdkModificator();
|
SdkModificator modificator = sdk.getSdkModificator();
|
||||||
modificator.setVersionString(getVersionString(sdk));
|
modificator.setVersionString(getVersionString(sdk));
|
||||||
modificator.commitChanges(); // save
|
modificator.commitChanges(); // save
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -79,32 +79,34 @@ The following is a simplified example that checks whether an instance of "DemoSd
|
|||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
class DemoProjectSdkSetupValidator : ProjectSdkSetupValidator {
|
class DemoProjectSdkSetupValidator : ProjectSdkSetupValidator {
|
||||||
override fun isApplicableFor(project: Project, file: VirtualFile): Boolean {
|
override fun isApplicableFor(project: Project, file: VirtualFile): Boolean {
|
||||||
return file.fileType == DemoFileType
|
return file.fileType == DemoFileType
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getErrorMessage(project: Project, file: VirtualFile): String? {
|
override fun getErrorMessage(project: Project, file: VirtualFile): String? {
|
||||||
if (ProjectJdkTable.getInstance().getSdksOfType(DemoSdkType.getInstance()).isEmpty())
|
if (ProjectJdkTable.getInstance().getSdksOfType(DemoSdkType.getInstance()).isEmpty()) {
|
||||||
return "No DemoSdks are configured for this project!"
|
return "No DemoSdks are configured for this project!"
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
override fun getFixHandler(project: Project, file: VirtualFile): EditorNotificationPanel.ActionHandler {
|
override fun getFixHandler(project: Project, file: VirtualFile):
|
||||||
return SdkPopupFactory.newBuilder()
|
EditorNotificationPanel.ActionHandler {
|
||||||
.withProject(project)
|
return SdkPopupFactory.newBuilder()
|
||||||
.withSdkTypeFilter { it is DemoSdkType }
|
.withProject(project)
|
||||||
.updateSdkForFile(file)
|
.withSdkTypeFilter { it is DemoSdkType }
|
||||||
.buildEditorNotificationPanelHandler()
|
.updateSdkForFile(file)
|
||||||
}
|
.buildEditorNotificationPanelHandler()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Within `DemoProjectSdkSetupValidator`:
|
Within `DemoProjectSdkSetupValidator`:
|
||||||
|
|
||||||
- `isApplicableFor()` checks what condition(s) should be met to run the validation.
|
* `isApplicableFor()` checks what condition(s) should be met to run the validation.
|
||||||
- `getErrorMessage()` runs the validation and return an appropriate error message if the validation fails.
|
* `getErrorMessage()` runs the validation and return an appropriate error message if the validation fails.
|
||||||
If the validation is successful, then it should return null.
|
* If the validation is successful, then it should return null.
|
||||||
- `getFixHandler()` returns an `EditorNotificationPanel.ActionHandler` that enables the user to execute a quick-fix to resolve the validation issue.
|
* `getFixHandler()` returns an `EditorNotificationPanel.ActionHandler` that enables the user to execute a quick-fix to resolve the validation issue.
|
||||||
|
|
||||||
|
|
||||||
> `ProjectSdkSetupValidator` will not work in IntelliJ Platform-based IDEs such as PyCharm.
|
> `ProjectSdkSetupValidator` will not work in IntelliJ Platform-based IDEs such as PyCharm.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user