public class CodeStyle
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static void |
doWithTemporarySettings(Project project,
CodeStyleSettings tempSettings,
java.lang.Runnable runnable)
Execute the specified runnable with the given temporary code style settings and restore the old settings even if the runnable fails
with an exception.
|
static void |
dropTemporarySettings(Project project)
Drop temporary settings.
|
static <T extends CustomCodeStyleSettings> |
getCustomSettings(PsiFile file,
java.lang.Class<T> customSettingsClass)
Returns custom settings for the given PSI file.
|
static CodeStyleSettings |
getDefaultSettings() |
static CommonCodeStyleSettings.IndentOptions |
getIndentOptions(Project project,
Document document)
Returns indent options for the given project and document.
|
static CommonCodeStyleSettings.IndentOptions |
getIndentOptions(PsiFile file)
Returns indent options for the given PSI file.
|
static CommonCodeStyleSettings.IndentOptions |
getIndentOptionsByFileType(Project project,
VirtualFile file)
Returns indent options by virtual file's type.
|
static int |
getIndentSize(PsiFile file)
Returns indent size for the given PSI file.
|
static CommonCodeStyleSettings |
getLanguageSettings(Editor editor)
Returns language code style settings for the specified editor.
|
static CommonCodeStyleSettings |
getLanguageSettings(PsiFile file)
Returns language settings for the given PSI file.
|
static CommonCodeStyleSettings |
getLanguageSettings(PsiFile file,
Language language)
Returns language settings for the given PSI file and language.
|
static CodeStyleSettings |
getProjectOrDefaultSettings(Project project)
Returns either project settings if the project is not null or default application-wide settings otherwise.
|
static CodeStyleSettings |
getSettings(Editor editor) |
static CodeStyleSettings |
getSettings(Project project)
Returns root code style settings for the given project.
|
static CodeStyleSettings |
getSettings(PsiFile file)
Returns root
CodeStyleSettings for the given PSI file. |
static boolean |
isFormattingEnabled(PsiFile file)
Checks if the file can be formatted according to code style settings.
|
static void |
reformatWithFileContext(PsiFile fileToReformat,
PsiFile contextFile)
Reformat the given
fileToReformat using code style settings for the contextFile . |
static void |
setMainProjectSettings(Project project,
CodeStyleSettings settings)
Assign main project-wide code style settings and force the project to use its own code style instead of a global (application) one.
|
static void |
setTemporarySettings(Project project,
CodeStyleSettings settings)
Set temporary settings for the project.
|
static void |
updateDocumentIndentOptions(Project project,
Document document)
Updates document's indent options from indent options providers.
|
static boolean |
usesOwnSettings(Project project) |
public static CodeStyleSettings getDefaultSettings()
public static CodeStyleSettings getSettings(Project project)
getLanguageSettings(PsiFile)
or
getLanguageSettings(PsiFile, Language)
.project
- The project to get code style settings for.public static CodeStyleSettings getProjectOrDefaultSettings(Project project)
project
- The project to return the settings for or null
for default settings.public static CodeStyleSettings getSettings(PsiFile file)
CodeStyleSettings
for the given PSI file. In some cases the returned instance may be of
TransientCodeStyleSettings
class if the original (project) settings are modified for specific PSI file by
CodeStyleSettingsModifier
extensions. In these cases the returned instance may change upon the next call if some of
TransientCodeStyleSettings
dependencies become outdated.
A shorter way to get language-specific settings it to use one of the methods getLanguageSettings(PsiFile)
or getLanguageSettings(PsiFile, Language)
.
file
- The file to get code style settings for.public static CodeStyleSettings getSettings(Editor editor)
public static CommonCodeStyleSettings getLanguageSettings(Editor editor)
null
otherwise.editor
- The editor to get settings for.null
.public static <T extends CustomCodeStyleSettings> T getCustomSettings(PsiFile file, java.lang.Class<T> customSettingsClass)
T
- Settings class type.file
- The file.customSettingsClass
- The class of a settings object to be returned.public static CommonCodeStyleSettings getLanguageSettings(PsiFile file)
file
- The file to retrieve language settings for.public static CommonCodeStyleSettings getLanguageSettings(PsiFile file, Language language)
file
- The file to retrieve language settings for.public static CommonCodeStyleSettings.IndentOptions getIndentOptions(PsiFile file)
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.file
- The file to get indent options for.FileIndentOptionsProvider
public static CommonCodeStyleSettings.IndentOptions getIndentOptionsByFileType(Project project, VirtualFile file)
null
is given instead of the virtual file or the type of the virtual
file doesn't have it's own configuration, returns other indent options configured via "Other File Types" section in Settings.
Note: This method is faster then getIndentOptions(PsiFile)
but it doesn't take into account possible configurations
overwriting the default, for example EditorConfig.
project
- The current project.file
- The virtual file to get indent options for or null
if the file is unknown.public static CommonCodeStyleSettings.IndentOptions getIndentOptions(Project project, Document document)
project
- The current project.document
- The document to get indent options for.public static int getIndentSize(PsiFile file)
file
- The file to get indent size for.public static void setTemporarySettings(Project project, CodeStyleSettings settings)
dropTemporarySettings(Project)
is called.
Note
The method is supposed to be used in test's setUp()
method. In production code use
doWithTemporarySettings(Project, CodeStyleSettings, Runnable)
.
project
- The project or null
for default settings.settings
- The settings to use temporarily with the project.public static void dropTemporarySettings(Project project)
Note
The method is supposed to be used in test's tearDown()
method. In production code use
doWithTemporarySettings(Project, CodeStyleSettings, Runnable)
.
project
- The project to drop temporary settings for or null
for default settings.setTemporarySettings(Project, CodeStyleSettings)
public static void doWithTemporarySettings(Project project, CodeStyleSettings tempSettings, java.lang.Runnable runnable)
project
- The current project.tempSettings
- The temporary code style settings.runnable
- The runnable to execute with the temporary settings.public static boolean usesOwnSettings(Project project)
project
- The project to check.true
if the project uses its own project code style, false
if global (application-level) code style settings
are used.public static void updateDocumentIndentOptions(Project project, Document document)
Note: Calling this method directly when there is an editor associated with the document may cause the editor work
incorrectly. To keep consistency with the editor call EditorEx.reinitSettings()
instead.
project
- The project of the document.document
- The document to update indent options for.public static void setMainProjectSettings(Project project, CodeStyleSettings settings)
project
- The project to assign the settings to.settings
- The settings to use with the project.public static boolean isFormattingEnabled(PsiFile file)
file
- The PSI file to check.public static void reformatWithFileContext(PsiFile fileToReformat, PsiFile contextFile)
fileToReformat
using code style settings for the contextFile
. The method may be
useful to reformat a fragment of code (temporary file) which eventually will be inserted to the context file.fileToReformat
- The file to reformat (may be a temporary dummy file).contextFile
- The actual (target) file whose settings must be used.