Add setMainProjectCodeStyle() to CodeStyle API

This commit is contained in:
Rustam Vishnyakov
2018-05-22 19:13:32 +03:00
parent 35943e7670
commit fcd92c3c8b
3 changed files with 20 additions and 12 deletions
@@ -138,12 +138,12 @@ public class CodeStyle {
}
/**
* Returns indent options for the given PSI file. The method attempts to use {@link com.intellij.psi.codeStyle.FileIndentOptionsProvider}
* Returns indent options for the given PSI file. The method attempts to use {@link FileIndentOptionsProvider}
* if applicable to the file. If there are no suitable indent options providers, it takes configurable language indent options or
* retrieves indent options by file type.
* @param file The file to get indent options for.
* @return The file indent options.
* @see com.intellij.psi.codeStyle.FileIndentOptionsProvider
* @see FileIndentOptionsProvider
*/
@NotNull
public static CommonCodeStyleSettings.IndentOptions getIndentOptions(@NotNull PsiFile file) {
@@ -268,10 +268,23 @@ public class CodeStyle {
if (documentManager != null) {
PsiFile file = documentManager.getPsiFile(document);
if (file != null) {
CommonCodeStyleSettings.IndentOptions indentOptions = CodeStyle.getSettings(file).getIndentOptionsByFile(file, null, true, null);
CommonCodeStyleSettings.IndentOptions indentOptions = getSettings(file).getIndentOptionsByFile(file, null, true, null);
indentOptions.associateWithDocument(document);
}
}
}
}
/**
* Assign main project-wide code style settings and force the project to use its own code style instead of a global (application) one.
*
* @param project The project to assign the settings to.
* @param settings The settings to use with the project.
*/
public static void setMainProjectSettings(@NotNull Project project, @NotNull CodeStyleSettings settings) {
@SuppressWarnings("deprecation")
CodeStyleSettingsManager codeStyleSettingsManager = CodeStyleSettingsManager.getInstance(project);
codeStyleSettingsManager.setMainProjectCodeStyle(settings);
codeStyleSettingsManager.USE_PER_PROJECT_SETTINGS = true;
}
}
@@ -1,6 +1,7 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.formatting.commandLine;
import com.intellij.application.options.CodeStyle;
import com.intellij.ide.impl.ProjectUtil;
import com.intellij.lang.LanguageFormatting;
import com.intellij.openapi.command.WriteCommandAction;
@@ -19,7 +20,6 @@ import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.util.PlatformUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -61,9 +61,7 @@ public class FileSetFormatter extends FileSetProcessor {
myProject = projectManager.createProject(myProjectUID, projectDir.getPath());
if (myProject != null) {
projectManager.openProject(myProject);
CodeStyleSettingsManager codeStyleSettingsManager = CodeStyleSettingsManager.getInstance(myProject);
codeStyleSettingsManager.setMainProjectCodeStyle(mySettings);
codeStyleSettingsManager.USE_PER_PROJECT_SETTINGS = true;
CodeStyle.setMainProjectSettings(myProject, mySettings);
}
}
@@ -15,6 +15,7 @@
*/
package org.jetbrains.idea.eclipse.importWizard;
import com.intellij.application.options.CodeStyle;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
@@ -56,8 +57,6 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.packaging.artifacts.ModifiableArtifactModel;
import com.intellij.projectImport.ProjectImportBuilder;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import java.util.HashMap;
import gnu.trove.THashMap;
import gnu.trove.THashSet;
import icons.EclipseIcons;
@@ -405,9 +404,7 @@ public class EclipseImportBuilder extends ProjectImportBuilder<String> implement
if (codeStyleData != null) {
CodeStyleSettings projectSettings = codeStyleData.importCodeStyle();
if (projectSettings != null) {
CodeStyleSettingsManager manager = CodeStyleSettingsManager.getInstance(project);
manager.USE_PER_PROJECT_SETTINGS = true;
manager.setMainProjectCodeStyle(projectSettings);
CodeStyle.setMainProjectSettings(project, projectSettings);
}
}
}