mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-28 01:07:49 +08:00
[site] code style settings
This commit is contained in:
parent
0ab470318b
commit
b81177f53d
@ -56,7 +56,7 @@
|
||||
* [Go To Symbol Contributor](go_to_symbol_contributor.html)
|
||||
* [Structure View Factory](structure_view_factory.html)
|
||||
* [Formatter](formatter.html)
|
||||
* [Code Style Settings](TODO)
|
||||
* [Code Style Settings](code_style_settings.html)
|
||||
* [Commenter](TODO)
|
||||
* [Quick Fix](TODO)
|
||||
|
||||
|
142
code_style_settings.md
Normal file
142
code_style_settings.md
Normal file
@ -0,0 +1,142 @@
|
||||
---
|
||||
title: Code Style Setting
|
||||
---
|
||||
|
||||
<!--
|
||||
INITIAL_SOURCE https://confluence.jetbrains.com/display/IntelliJIDEA/Code+Style+Settings
|
||||
-->
|
||||
|
||||
### 1. Define code style settings
|
||||
|
||||
```java
|
||||
package com.simpleplugin;
|
||||
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CustomCodeStyleSettings;
|
||||
|
||||
public class SimpleCodeStyleSettings extends CustomCodeStyleSettings {
|
||||
public SimpleCodeStyleSettings(CodeStyleSettings settings) {
|
||||
super("SimpleCodeStyleSettings", settings);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Define code style settings provider
|
||||
|
||||
```java
|
||||
package com.simpleplugin;
|
||||
|
||||
import com.intellij.application.options.CodeStyleAbstractConfigurable;
|
||||
import com.intellij.application.options.CodeStyleAbstractPanel;
|
||||
import com.intellij.application.options.TabbedLanguageCodeStylePanel;
|
||||
import com.intellij.openapi.options.Configurable;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsProvider;
|
||||
import com.intellij.psi.codeStyle.CustomCodeStyleSettings;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class SimpleCodeStyleSettingsProvider extends CodeStyleSettingsProvider {
|
||||
@Override
|
||||
public CustomCodeStyleSettings createCustomSettings(CodeStyleSettings settings) {
|
||||
return new SimpleCodeStyleSettings(settings);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getConfigurableDisplayName() {
|
||||
return "Simple";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Configurable createSettingsPage(CodeStyleSettings settings, CodeStyleSettings originalSettings) {
|
||||
return new CodeStyleAbstractConfigurable(settings, originalSettings, "Simple") {
|
||||
@Override
|
||||
protected CodeStyleAbstractPanel createPanel(CodeStyleSettings settings) {
|
||||
return new SimpleCodeStyleMainPanel(getCurrentSettings(), settings);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getHelpTopic() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static class SimpleCodeStyleMainPanel extends TabbedLanguageCodeStylePanel {
|
||||
public SimpleCodeStyleMainPanel(CodeStyleSettings currentSettings, CodeStyleSettings settings) {
|
||||
super(SimpleLanguage.INSTANCE, currentSettings, settings);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### 3. Register the code style settings provider
|
||||
|
||||
```xml
|
||||
<codeStyleSettingsProvider implementation="com.simpleplugin.SimpleCodeStyleSettingsProvider"/>
|
||||
```
|
||||
|
||||
### 4. Define language code style settings provider
|
||||
|
||||
```java
|
||||
package com.simpleplugin;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable;
|
||||
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SimpleLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettingsProvider {
|
||||
@NotNull
|
||||
@Override
|
||||
public Language getLanguage() {
|
||||
return SimpleLanguage.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customizeSettings(@NotNull CodeStyleSettingsCustomizable consumer, @NotNull SettingsType settingsType) {
|
||||
if (settingsType == SettingsType.SPACING_SETTINGS) {
|
||||
consumer.showStandardOptions("SPACE_AROUND_ASSIGNMENT_OPERATORS");
|
||||
consumer.renameStandardOption("SPACE_AROUND_ASSIGNMENT_OPERATORS", "Separator");
|
||||
} else if (settingsType == SettingsType.BLANK_LINES_SETTINGS) {
|
||||
consumer.showStandardOptions("KEEP_BLANK_LINES_IN_CODE");
|
||||
} else if (settingsType == SettingsType.WRAPPING_AND_BRACES_SETTINGS) {
|
||||
consumer.showStandardOptions("KEEP_LINE_BREAKS");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCodeSample(@NotNull SettingsType settingsType) {
|
||||
return "# You are reading the \".properties\" entry.\n" +
|
||||
"! The exclamation mark can also mark text as comments.\n" +
|
||||
"website = http://en.wikipedia.org/\n" +
|
||||
"language = English\n" +
|
||||
"# The backslash below tells the application to continue reading\n" +
|
||||
"# the value onto the next line.\n" +
|
||||
"message = Welcome to \\\n" +
|
||||
" Wikipedia!\n" +
|
||||
"# Add spaces to the key\n" +
|
||||
"key\\ with\\ spaces = This is the value that could be looked up with the key \"key with spaces\".\n" +
|
||||
"# Unicode\n" +
|
||||
"tab : \\u0009";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 5. Register the language code style settings provider
|
||||
|
||||
```xml
|
||||
<langCodeStyleSettingsProvider implementation="com.simpleplugin.SimpleLanguageCodeStyleSettingsProvider"/>
|
||||
```
|
||||
|
||||
### 6. Run the project
|
||||
|
||||

|
||||
|
||||
[Previous](formatter.html)
|
||||
[Top](cls_tutorial.html)
|
||||
[Next](commenter.html)
|
BIN
img/cls_tutorial/code_style_settings.png
Normal file
BIN
img/cls_tutorial/code_style_settings.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 113 KiB |
Loading…
x
Reference in New Issue
Block a user