f4e47df9e516c55d8630bdb3c8c15b2eb11aef53
[idea/community.git] / platform / lang-impl / src / com / intellij / formatting / FormatterEx.java
1 /*
2  * Copyright 2000-2017 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.intellij.formatting;
18
19 import com.intellij.openapi.application.Application;
20 import com.intellij.openapi.application.ApplicationManager;
21 import com.intellij.openapi.util.TextRange;
22 import com.intellij.psi.PsiFile;
23 import com.intellij.psi.codeStyle.CodeStyleSettings;
24 import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
25 import com.intellij.util.IncorrectOperationException;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28
29 public abstract class FormatterEx{
30
31   private static FormatterEx myTestInstance;
32
33   public static FormatterEx getInstance() {
34     final Application application = ApplicationManager.getApplication();
35     if (application != null) {
36       return application.getComponent(FormatterEx.class);
37     }
38     else {
39       return getTestInstance();
40     }
41   }
42
43
44   private static FormatterEx getTestInstance() {
45     if (myTestInstance == null) {
46       try {
47         myTestInstance = (FormatterEx)Class.forName("com.intellij.formatting.FormatterImpl").newInstance();
48       }
49       catch (Exception e) {
50         throw new RuntimeException(e);
51       }
52     }
53     return myTestInstance;
54   }
55
56   public abstract void format(FormattingModel model,
57                               CodeStyleSettings settings,
58                               CommonCodeStyleSettings.IndentOptions indentOptions,
59                               FormatTextRanges affectedRanges) throws IncorrectOperationException;
60
61   public abstract void format(FormattingModel model,
62                               CodeStyleSettings settings,
63                               CommonCodeStyleSettings.IndentOptions indentOptions,
64                               CommonCodeStyleSettings.IndentOptions javaIndentOptions,
65                               FormatTextRanges affectedRanges) throws IncorrectOperationException;
66
67   public abstract void format(final FormattingModel model,
68                               final CodeStyleSettings settings,
69                               final CommonCodeStyleSettings.IndentOptions indentOptions,
70                               final FormatTextRanges affectedRanges,
71                               final boolean formatContextAroundRanges) throws IncorrectOperationException;
72
73
74   public abstract IndentInfo getWhiteSpaceBefore(final FormattingDocumentModel psiBasedFormattingModel,
75                                                  final Block block,
76                                                  final CodeStyleSettings settings,
77                                                  final CommonCodeStyleSettings.IndentOptions indentOptions,
78                                                  final TextRange textRange, final boolean mayChangeLineFeeds);
79
80   public abstract int adjustLineIndent(final FormattingModel psiBasedFormattingModel,
81                                        final CodeStyleSettings settings,
82                                        final CommonCodeStyleSettings.IndentOptions indentOptions,
83                                        final int offset,
84                                        final TextRange affectedRange) throws IncorrectOperationException;
85
86   @Nullable
87   public abstract String getLineIndent(final FormattingModel psiBasedFormattingModel,
88                                        final CodeStyleSettings settings,
89                                        final CommonCodeStyleSettings.IndentOptions indentOptions,
90                                        final int offset,
91                                        final TextRange affectedRange);
92
93   public abstract void adjustTextRange(final FormattingModel documentModel,
94                                        final CodeStyleSettings settings,
95                                        final CommonCodeStyleSettings.IndentOptions indentOptions,
96                                        final TextRange textRange,
97                                        final boolean keepBlankLines,
98                                        final boolean keepLineBreaks,
99                                        final boolean changeWSBeforeFirstElement,
100                                        final boolean changeLineFeedsBeforeFirstElement, final IndentInfoStorage indentInfoStorage);
101
102   public abstract void saveIndents(final FormattingModel model, final TextRange affectedRange,
103                                    IndentInfoStorage storage,
104                                    final CodeStyleSettings settings,
105                                    final CommonCodeStyleSettings.IndentOptions indentOptions);
106
107   public abstract boolean isDisabled();
108
109
110
111   public abstract void adjustLineIndentsForRange(final FormattingModel model,
112                                                  final CodeStyleSettings settings,
113                                                  final CommonCodeStyleSettings.IndentOptions indentOptions,
114                                                  final TextRange rangeToAdjust);
115
116   public abstract void formatAroundRange(final FormattingModel model,
117                                          final CodeStyleSettings settings,
118                                          final PsiFile file,
119                                          final TextRange textRange);
120
121   public abstract void adjustTextRange(FormattingModel model,
122                                        CodeStyleSettings settings,
123                                        CommonCodeStyleSettings.IndentOptions indentOptions,
124                                        TextRange affectedRange);
125
126   public abstract void setProgressTask(@NotNull FormattingProgressTask progressIndicator);
127
128   /**
129    * Calculates minimum spacing, allowed by formatting model (in columns) for a block starting at given offset,
130    * relative to its previous sibling block.
131    * Returns <code>-1</code>, if required block cannot be found at provided offset,
132    * or spacing cannot be calculated due to some other reason.
133    */
134   public abstract int getSpacingForBlockAtOffset(FormattingModel model, int offset);
135   
136   /**
137    * Calculates minimum number of line feeds that should precede block starting at given offset, as dictated by formatting model.
138    * Returns <code>-1</code>, if required block cannot be found at provided offset,
139    * or spacing cannot be calculated due to some other reason.
140    */
141   public abstract int getMinLineFeedsBeforeBlockAtOffset(FormattingModel model, int offset);
142
143   public interface IndentInfoStorage {
144     void saveIndentInfo(@Nullable IndentInfo info, int startOffset);
145
146     IndentInfo getIndentInfo(int startOffset);
147   }
148
149   public static FormatterEx getInstanceEx() {
150     return getInstance();
151   }
152
153 }