Commit 9f646e69 authored by Rustam Vishnyakov's avatar Rustam Vishnyakov
Browse files

Status bar information, notification and actions (initial)

parent 28457e96
Showing with 62 additions and 12 deletions
+62 -12
package org.editorconfig.configmanagement;
import com.intellij.ide.actions.ShowSettingsUtilImpl;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions;
import com.intellij.psi.codeStyle.FileIndentOptionsProvider;
import com.intellij.util.containers.ContainerUtil;
import org.editorconfig.Utils;
import org.editorconfig.core.EditorConfig;
import org.editorconfig.plugincomponents.SettingsProviderComponent;
......@@ -24,9 +29,11 @@ public class EditorConfigIndentOptionsProvider extends FileIndentOptionsProvider
public static final String tabWidthKey = "tab_width";
public static final String indentStyleKey = "indent_style";
private static final Key<Boolean> PROJECT_ADVERTISEMENT_FLAG = Key.create("editor.config.ad.shown");
@Nullable
@Override
public CommonCodeStyleSettings.IndentOptions getIndentOptions(@NotNull CodeStyleSettings settings, @NotNull PsiFile psiFile) {
public IndentOptions getIndentOptions(@NotNull CodeStyleSettings settings, @NotNull PsiFile psiFile) {
final VirtualFile file = psiFile.getVirtualFile();
if (file == null) return null;
......@@ -39,16 +46,16 @@ public class EditorConfigIndentOptionsProvider extends FileIndentOptionsProvider
return applyCodeStyleSettings(project, outPairs, file, settings);
}
private static CommonCodeStyleSettings.IndentOptions applyCodeStyleSettings(Project project,
final List<EditorConfig.OutPair> outPairs,
final VirtualFile file,
final CodeStyleSettings settings) {
private static IndentOptions applyCodeStyleSettings(Project project,
final List<EditorConfig.OutPair> outPairs,
final VirtualFile file,
final CodeStyleSettings settings) {
// Apply indent options
final String indentSize = Utils.configValueForKey(outPairs, indentSizeKey);
final String continuationIndentSize = Utils.configValueForKey(outPairs, continuationSizeKey);
final String tabWidth = Utils.configValueForKey(outPairs, tabWidthKey);
final String indentStyle = Utils.configValueForKey(outPairs, indentStyleKey);
final CommonCodeStyleSettings.IndentOptions indentOptions = (CommonCodeStyleSettings.IndentOptions)settings.getIndentOptions(file.getFileType()).clone();
final IndentOptions indentOptions = (IndentOptions)settings.getIndentOptions(file.getFileType()).clone();
if (applyIndentOptions(project, indentOptions, indentSize, continuationIndentSize, tabWidth, indentStyle, file.getCanonicalPath())) {
indentOptions.setOverrideLanguageOptions(true);
return indentOptions;
......@@ -56,7 +63,7 @@ public class EditorConfigIndentOptionsProvider extends FileIndentOptionsProvider
return null;
}
private static boolean applyIndentOptions(Project project, CommonCodeStyleSettings.IndentOptions indentOptions,
private static boolean applyIndentOptions(Project project, IndentOptions indentOptions,
String indentSize, String continuationIndentSize, String tabWidth,
String indentStyle, String filePath) {
boolean changed = false;
......@@ -117,7 +124,7 @@ public class EditorConfigIndentOptionsProvider extends FileIndentOptionsProvider
}
}
private static boolean applyIndentSize(final CommonCodeStyleSettings.IndentOptions indentOptions, final String indentSize) {
private static boolean applyIndentSize(final IndentOptions indentOptions, final String indentSize) {
try {
indentOptions.INDENT_SIZE = Integer.parseInt(indentSize);
return true;
......@@ -127,7 +134,7 @@ public class EditorConfigIndentOptionsProvider extends FileIndentOptionsProvider
}
}
private static boolean applyContinuationIndentSize(final CommonCodeStyleSettings.IndentOptions indentOptions, final String continuationIndentSize) {
private static boolean applyContinuationIndentSize(final IndentOptions indentOptions, final String continuationIndentSize) {
try {
indentOptions.CONTINUATION_INDENT_SIZE = Integer.parseInt(continuationIndentSize);
return true;
......@@ -136,7 +143,7 @@ public class EditorConfigIndentOptionsProvider extends FileIndentOptionsProvider
return false;
}
}
private static boolean applyTabWidth(final CommonCodeStyleSettings.IndentOptions indentOptions, final String tabWidth) {
private static boolean applyTabWidth(final IndentOptions indentOptions, final String tabWidth) {
try {
indentOptions.TAB_SIZE = Integer.parseInt(tabWidth);
return true;
......@@ -146,11 +153,54 @@ public class EditorConfigIndentOptionsProvider extends FileIndentOptionsProvider
}
}
private static boolean applyIndentStyle(CommonCodeStyleSettings.IndentOptions indentOptions, String indentStyle) {
private static boolean applyIndentStyle(IndentOptions indentOptions, String indentStyle) {
if (indentStyle.equals("tab") || indentStyle.equals("space")) {
indentOptions.USE_TAB_CHARACTER = indentStyle.equals("tab");
return true;
}
return false;
}
@Override
public boolean areActionsAvailable(@NotNull VirtualFile file, @NotNull IndentOptions indentOptions) {
return isEditorConfigOptions(indentOptions);
}
@Nullable
@Override
public AnAction[] getActions(@NotNull PsiFile file, @NotNull IndentOptions indentOptions) {
if (isEditorConfigOptions(indentOptions)) {
List<AnAction> actions = ContainerUtil.newArrayList();
actions.add(
DumbAwareAction.create(
"Show settings",
e -> ShowSettingsUtilImpl.showSettingsDialog(file.getProject(), "preferences.sourceCode", "EditorConfig")
)
);
return actions.toArray(AnAction.EMPTY_ARRAY);
}
return null;
}
@Nullable
@Override
protected String getHint(@NotNull IndentOptions indentOptions) {
return isEditorConfigOptions(indentOptions) ? ".editorconfig" : null;
}
private static boolean isEditorConfigOptions(@NotNull IndentOptions indentOptions) {
return indentOptions.getFileIndentOptionsProvider() instanceof EditorConfigIndentOptionsProvider;
}
@Nullable
@Override
public String getAdvertisementText(@NotNull PsiFile psiFile, @NotNull IndentOptions indentOptions) {
Project project = psiFile.getProject();
Boolean adFlag = project.getUserData(PROJECT_ADVERTISEMENT_FLAG);
if (adFlag != null && adFlag) return null;
project.putUserData(PROJECT_ADVERTISEMENT_FLAG, true);
return "Current indent options are overridden by .editorconfig";
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment