mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 13:31:28 +07:00
File indent options provider API
This commit is contained in:
@@ -47,7 +47,7 @@ public class ImportsFormatter extends XmlRecursiveElementVisitor {
|
||||
public ImportsFormatter(@NotNull CodeStyleSettings settings, @NotNull PsiFile file) {
|
||||
myPostProcessor = new PostFormatProcessorHelper(settings);
|
||||
myDocumentModel = FormattingDocumentModelImpl.createOn(file);
|
||||
myIndentOptions = settings.getIndentOptions(file.getFileType());
|
||||
myIndentOptions = settings.getIndentOptionsByFile(file);
|
||||
}
|
||||
|
||||
@Override public void visitXmlTag(XmlTag tag) {
|
||||
|
||||
@@ -19,12 +19,12 @@ import com.intellij.lang.ASTNode;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.fileTypes.LanguageFileType;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.psi.formatter.FormatterUtil;
|
||||
import com.intellij.psi.impl.source.tree.TreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -70,14 +70,13 @@ public class TabPostFormatProcessor implements PostFormatProcessor {
|
||||
return range;
|
||||
}
|
||||
|
||||
LanguageFileType fileType = language.getAssociatedFileType();
|
||||
if (fileType == null) {
|
||||
return range;
|
||||
}
|
||||
if (!source.isValid()) return range;
|
||||
PsiFile file = source.getContainingFile();
|
||||
CommonCodeStyleSettings.IndentOptions indentOptions = settings.getIndentOptionsByFile(file);
|
||||
|
||||
boolean useTabs = settings.useTabCharacter(fileType);
|
||||
boolean smartTabs = settings.isSmartTabs(fileType);
|
||||
int tabWidth = settings.getTabSize(fileType);
|
||||
boolean useTabs = indentOptions.USE_TAB_CHARACTER;
|
||||
boolean smartTabs = indentOptions.SMART_TABS;
|
||||
int tabWidth = indentOptions.TAB_SIZE;
|
||||
return processViaPsi(node, range, new TreeHelperImpl(), useTabs, smartTabs, tabWidth);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
private void foo(boolean b) {
|
||||
int x;
|
||||
<selection>if (b) {
|
||||
System.out.println(x);
|
||||
}</selection>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
private void foo(boolean b) {
|
||||
int x;
|
||||
<selection>if (b) {
|
||||
System.out.println(x);
|
||||
}</selection>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
private void foo(boolean b) {
|
||||
int x;
|
||||
if (b) {
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
private void foo(boolean b) {
|
||||
int x;
|
||||
if (b) {
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
private void foo(boolean b) {
|
||||
int x;
|
||||
if (b) {
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
private void foo(boolean b) {
|
||||
int x;
|
||||
if (b) {
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class A {
|
||||
private void foo() {
|
||||
int x;
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class A {
|
||||
private void foo() {
|
||||
int x;<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class A {
|
||||
private void foo() {
|
||||
int x;
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class A {
|
||||
private void foo() {
|
||||
int x;
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
private void foo(boolean b) {
|
||||
int x;
|
||||
<selection>if (b) {
|
||||
System.out.println(x);
|
||||
}</selection>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
private void foo(boolean b) {
|
||||
int x;
|
||||
<selection>if (b) {
|
||||
System.out.println(x);
|
||||
}</selection>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInsight.indentOptionsProvider;
|
||||
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.editor.actions.IndentSelectionAction;
|
||||
import com.intellij.openapi.editor.actions.UnindentSelectionAction;
|
||||
import com.intellij.openapi.extensions.ExtensionPoint;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.FileIndentOptionsProvider;
|
||||
import com.intellij.testFramework.PlatformTestUtil;
|
||||
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @author Rustam Vishnyakov
|
||||
*/
|
||||
public class FileIndentProviderTest extends LightPlatformCodeInsightFixtureTestCase {
|
||||
|
||||
private final static FileIndentOptionsProvider TEST_FILE_INDENT_OPTIONS_PROVIDER = new TestIndentOptionsProvider();
|
||||
private static CommonCodeStyleSettings.IndentOptions myTestIndentOptions;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
ExtensionPoint<FileIndentOptionsProvider> extensionPoint =
|
||||
Extensions.getRootArea().getExtensionPoint(FileIndentOptionsProvider.EP_NAME);
|
||||
extensionPoint.registerExtension(TEST_FILE_INDENT_OPTIONS_PROVIDER);
|
||||
myTestIndentOptions = new CommonCodeStyleSettings.IndentOptions();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
ExtensionPoint<FileIndentOptionsProvider> extensionPoint =
|
||||
Extensions.getRootArea().getExtensionPoint(FileIndentOptionsProvider.EP_NAME);
|
||||
extensionPoint.unregisterExtension(TEST_FILE_INDENT_OPTIONS_PROVIDER);
|
||||
myTestIndentOptions = null;
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return PlatformTestUtil.getCommunityPath().replace(File.separatorChar, '/') + "/java/java-tests/testData/codeInsight/indentProvider";
|
||||
}
|
||||
|
||||
private void doTestTyping(char c) {
|
||||
myFixture.configureByFile(getTestName(true) + "_before.java");
|
||||
myFixture.type(c);
|
||||
myFixture.checkResultByFile(getTestName(true) + "_after.java");
|
||||
}
|
||||
|
||||
private void doTestAction(@NotNull AnAction action) {
|
||||
myFixture.configureByFile(getTestName(true) + "_before.java");
|
||||
assertTrue(myFixture.testAction(action).isEnabled());
|
||||
myFixture.checkResultByFile(getTestName(true) + "_after.java");
|
||||
}
|
||||
|
||||
private static class TestIndentOptionsProvider extends FileIndentOptionsProvider {
|
||||
@Nullable
|
||||
@Override
|
||||
public CommonCodeStyleSettings.IndentOptions getIndentOptions(@NotNull PsiFile file) {
|
||||
return myTestIndentOptions;
|
||||
}
|
||||
}
|
||||
|
||||
public void testTypeEnter() {
|
||||
myTestIndentOptions.INDENT_SIZE = 3;
|
||||
doTestTyping('\n');
|
||||
}
|
||||
|
||||
public void testTypeTab() {
|
||||
myTestIndentOptions.INDENT_SIZE = 3;
|
||||
doTestTyping('\t');
|
||||
}
|
||||
|
||||
public void testIndentSelection() {
|
||||
myTestIndentOptions.INDENT_SIZE = 3;
|
||||
doTestAction(new IndentSelectionAction());
|
||||
}
|
||||
|
||||
public void testUnindentSelection() {
|
||||
myTestIndentOptions.INDENT_SIZE = 3;
|
||||
doTestAction(new UnindentSelectionAction());
|
||||
}
|
||||
|
||||
public void testReformatFile() {
|
||||
myTestIndentOptions.INDENT_SIZE = 3;
|
||||
myTestIndentOptions.TAB_SIZE = 2;
|
||||
myTestIndentOptions.USE_TAB_CHARACTER = true;
|
||||
PsiFile file = myFixture.configureByFile(getTestName(true) + "_before.java");
|
||||
CodeStyleManager.getInstance(getProject()).reformat(file);
|
||||
myFixture.checkResultByFile(getTestName(true) + "_after.java");
|
||||
}
|
||||
|
||||
public void testReformatText() {
|
||||
myTestIndentOptions.INDENT_SIZE = 3;
|
||||
myTestIndentOptions.TAB_SIZE = 2;
|
||||
myTestIndentOptions.USE_TAB_CHARACTER = true;
|
||||
PsiFile file = myFixture.configureByFile(getTestName(true) + "_before.java");
|
||||
CodeStyleManager.getInstance(getProject()).reformatText(file, 0, file.getTextRange().getEndOffset());
|
||||
myFixture.checkResultByFile(getTestName(true) + "_after.java");
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import com.intellij.openapi.fileTypes.LanguageFileType;
|
||||
import com.intellij.openapi.util.*;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.SystemProperties;
|
||||
import com.intellij.util.containers.ClassMap;
|
||||
import org.jdom.Element;
|
||||
@@ -42,7 +43,7 @@ import java.util.regex.PatternSyntaxException;
|
||||
public class CodeStyleSettings extends CommonCodeStyleSettings implements Cloneable, JDOMExternalizable {
|
||||
|
||||
private static final Logger LOG = Logger.getInstance("#" + CodeStyleSettings.class.getName());
|
||||
|
||||
|
||||
private final ClassMap<CustomCodeStyleSettings> myCustomSettings = new ClassMap<CustomCodeStyleSettings>();
|
||||
|
||||
@NonNls private static final String ADDITIONAL_INDENT_OPTIONS = "ADDITIONAL_INDENT_OPTIONS";
|
||||
@@ -659,6 +660,29 @@ public class CodeStyleSettings extends CommonCodeStyleSettings implements Clonea
|
||||
|
||||
return OTHER_INDENT_OPTIONS;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public IndentOptions getIndentOptionsByFile(@Nullable PsiFile file) {
|
||||
return getIndentOptionsByFile(file, false);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public IndentOptions getIndentOptionsByFile(@Nullable PsiFile file, boolean ignoreDocOptions) {
|
||||
if (file != null && file.isValid()) {
|
||||
if (!ignoreDocOptions) {
|
||||
IndentOptions docOptions = IndentOptions.retrieveFromAssociatedDocument(file);
|
||||
if (docOptions != null) return docOptions;
|
||||
}
|
||||
FileIndentOptionsProvider[] providers = Extensions.getExtensions(FileIndentOptionsProvider.EP_NAME);
|
||||
for (FileIndentOptionsProvider provider : providers) {
|
||||
IndentOptions indentOptions = provider.getIndentOptions(file);
|
||||
if (indentOptions != null) return indentOptions;
|
||||
}
|
||||
return getIndentOptions(file.getFileType());
|
||||
}
|
||||
else
|
||||
return OTHER_INDENT_OPTIONS;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private IndentOptions getLanguageIndentOptions(@Nullable FileType fileType) {
|
||||
|
||||
@@ -18,11 +18,14 @@ package com.intellij.psi.codeStyle;
|
||||
import com.intellij.openapi.components.PersistentStateComponent;
|
||||
import com.intellij.openapi.components.ServiceManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.DefaultJDOMExternalizer;
|
||||
import com.intellij.openapi.util.DifferenceFilter;
|
||||
import com.intellij.openapi.util.InvalidDataException;
|
||||
import com.intellij.openapi.util.WriteExternalException;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import org.jdom.Element;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -122,4 +125,17 @@ public class CodeStyleSettingsManager implements PersistentStateComponent<Elemen
|
||||
public boolean isLoaded() {
|
||||
return myIsLoaded;
|
||||
}
|
||||
|
||||
public static void updateDocumentIndentOptions(@NotNull Project project, @NotNull Document document) {
|
||||
if (!project.isDisposed()) {
|
||||
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
|
||||
if (documentManager != null) {
|
||||
PsiFile file = documentManager.getPsiFile(document);
|
||||
if (file != null) {
|
||||
CommonCodeStyleSettings.IndentOptions indentOptions = getSettings(project).getIndentOptionsByFile(file, true);
|
||||
indentOptions.associateWithDocument(document);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,12 @@
|
||||
package com.intellij.psi.codeStyle;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.util.*;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.arrangement.ArrangementSettings;
|
||||
import com.intellij.psi.codeStyle.arrangement.ArrangementUtil;
|
||||
import com.intellij.util.ReflectionUtil;
|
||||
@@ -905,6 +908,8 @@ public class CommonCodeStyleSettings {
|
||||
public boolean LABEL_INDENT_ABSOLUTE = false;
|
||||
public boolean USE_RELATIVE_INDENTS = false;
|
||||
|
||||
private final static Key<CommonCodeStyleSettings.IndentOptions> INDENT_OPTIONS_KEY = Key.create("INDENT_OPTIONS");
|
||||
|
||||
@Override
|
||||
public void readExternal(Element element) throws InvalidDataException {
|
||||
DefaultJDOMExternalizer.readExternal(this, element);
|
||||
@@ -976,5 +981,19 @@ public class CommonCodeStyleSettings {
|
||||
public void copyFrom(IndentOptions other) {
|
||||
copyPublicFields(other, this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static IndentOptions retrieveFromAssociatedDocument(@NotNull PsiFile file) {
|
||||
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(file.getProject());
|
||||
if (documentManager != null) {
|
||||
Document document = documentManager.getDocument(file);
|
||||
if (document != null) return document.getUserData(INDENT_OPTIONS_KEY);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void associateWithDocument(@NotNull Document document) {
|
||||
document.putUserData(INDENT_OPTIONS_KEY, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.psi.codeStyle;
|
||||
|
||||
import com.intellij.openapi.extensions.ExtensionPointName;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author Rustam Vishnyakov
|
||||
*/
|
||||
public abstract class FileIndentOptionsProvider {
|
||||
|
||||
public final static ExtensionPointName<FileIndentOptionsProvider> EP_NAME = ExtensionPointName.create("com.intellij.fileIndentOptionsProvider");
|
||||
/**
|
||||
* Retrieves indent options for PSI file.
|
||||
* @param file The file to retrieve options for.
|
||||
* @return Indent options or <code>null</code> if the provider can't retrieve them.
|
||||
*/
|
||||
@Nullable
|
||||
public abstract CommonCodeStyleSettings.IndentOptions getIndentOptions(@NotNull PsiFile file);
|
||||
}
|
||||
@@ -129,7 +129,7 @@ public class CodeFormatterFacade {
|
||||
if (file.getTextLength() > 0) {
|
||||
try {
|
||||
FormatterEx.getInstanceEx().format(
|
||||
model, mySettings,mySettings.getIndentOptions(fileToFormat.getFileType()), new FormatTextRanges(range, true)
|
||||
model, mySettings,mySettings.getIndentOptionsByFile(fileToFormat), new FormatTextRanges(range, true)
|
||||
);
|
||||
|
||||
wrapLongLinesIfNecessary(file, document, startOffset, endOffset);
|
||||
@@ -242,7 +242,7 @@ public class CodeFormatterFacade {
|
||||
indentOptions = ((FormattingModelBuilderEx)builder).getIndentOptionsToUse(file, ranges, mySettings);
|
||||
}
|
||||
if (indentOptions == null) {
|
||||
indentOptions = mySettings.getIndentOptions(file.getFileType());
|
||||
indentOptions = mySettings.getIndentOptionsByFile(file);
|
||||
}
|
||||
|
||||
formatter.format(model, mySettings, indentOptions, ranges);
|
||||
|
||||
@@ -99,7 +99,7 @@ abstract class CodeStyleManagerRunnable<T> {
|
||||
myIndentOptions = ((FormattingModelBuilderEx)builder).getIndentOptionsToUse(file, new FormatTextRanges(mySignificantRange, true), mySettings);
|
||||
}
|
||||
if (myIndentOptions == null) {
|
||||
myIndentOptions = mySettings.getIndentOptions(file.getFileType());
|
||||
myIndentOptions = mySettings.getIndentOptionsByFile(file);
|
||||
}
|
||||
|
||||
myModel = CoreFormatterUtil.buildModel(builder, file, mySettings, myMode);
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
*/
|
||||
package com.intellij.openapi.editor.actions;
|
||||
|
||||
import com.intellij.codeStyle.CodeStyleFacade;
|
||||
import com.intellij.ide.ui.customization.CustomActionsSchema;
|
||||
import com.intellij.openapi.actionSystem.ActionGroup;
|
||||
import com.intellij.openapi.actionSystem.ActionManager;
|
||||
@@ -37,11 +36,12 @@ import com.intellij.openapi.editor.event.EditorMouseListener;
|
||||
import com.intellij.openapi.editor.ex.EditorEx;
|
||||
import com.intellij.openapi.editor.ex.util.EditorUtil;
|
||||
import com.intellij.openapi.editor.impl.EditorImpl;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.util.EditorPopupHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -205,9 +205,8 @@ public class EditorActionUtil {
|
||||
|
||||
private static boolean shouldUseSmartTabs(Project project, @NotNull Editor editor) {
|
||||
if (!(editor instanceof EditorEx)) return false;
|
||||
VirtualFile file = ((EditorEx)editor).getVirtualFile();
|
||||
FileType fileType = file == null ? null : file.getFileType();
|
||||
return fileType != null && CodeStyleFacade.getInstance(project).isSmartTabs(fileType);
|
||||
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
|
||||
return CodeStyleSettingsManager.getSettings(project).getIndentOptionsByFile(file).SMART_TABS;
|
||||
}
|
||||
|
||||
public static boolean isWordStart(@NotNull CharSequence text, int offset, boolean isCamel) {
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
*/
|
||||
package com.intellij.openapi.editor.actions;
|
||||
|
||||
import com.intellij.codeStyle.CodeStyleFacade;
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.actionSystem.Presentation;
|
||||
@@ -30,13 +29,11 @@ import com.intellij.openapi.editor.*;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorAction;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler;
|
||||
import com.intellij.openapi.editor.ex.DocumentEx;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -115,9 +112,8 @@ public class IndentSelectionAction extends EditorAction {
|
||||
endIndex = document.getLineCount() - 1;
|
||||
}
|
||||
|
||||
VirtualFile vFile = FileDocumentManager.getInstance().getFile(document);
|
||||
final FileType fileType = vFile == null ? null : vFile.getFileType();
|
||||
int blockIndent = CodeStyleFacade.getInstance(project).getIndentSize(fileType);
|
||||
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document);
|
||||
int blockIndent = CodeStyleSettingsManager.getSettings(project).getIndentOptionsByFile(file).INDENT_SIZE;
|
||||
doIndent(endIndex, startIndex, document, project, editor, blockIndent);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
*/
|
||||
package com.intellij.openapi.editor.actions;
|
||||
|
||||
import com.intellij.codeStyle.CodeStyleFacade;
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
@@ -33,11 +32,13 @@ import com.intellij.openapi.editor.actionSystem.EditorAction;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler;
|
||||
import com.intellij.openapi.editor.ex.EditorEx;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.util.ui.MacUIUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -80,19 +81,19 @@ public class TabAction extends EditorAction {
|
||||
columnNumber = editor.getCaretModel().getLogicalPosition().column;
|
||||
}
|
||||
|
||||
CodeStyleFacade settings = CodeStyleFacade.getInstance(project);
|
||||
CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
|
||||
|
||||
final Document doc = editor.getDocument();
|
||||
VirtualFile vFile = FileDocumentManager.getInstance().getFile(doc);
|
||||
final FileType fileType = vFile == null ? null : vFile.getFileType();
|
||||
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(doc);
|
||||
CommonCodeStyleSettings.IndentOptions indentOptions = settings.getIndentOptionsByFile(file);
|
||||
|
||||
int tabSize = settings.getIndentSize(fileType);
|
||||
int tabSize = indentOptions.INDENT_SIZE;
|
||||
int spacesToAddCount = tabSize - columnNumber % Math.max(1,tabSize);
|
||||
|
||||
boolean useTab = editor.getSettings().isUseTabCharacter(project);
|
||||
|
||||
CharSequence chars = doc.getCharsSequence();
|
||||
if (useTab && settings.isSmartTabs(fileType)) {
|
||||
if (useTab && indentOptions.SMART_TABS) {
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
while (offset > 0) {
|
||||
offset--;
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
*/
|
||||
package com.intellij.openapi.editor.actions;
|
||||
|
||||
import com.intellij.codeStyle.CodeStyleFacade;
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
@@ -32,10 +31,10 @@ import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorAction;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler;
|
||||
import com.intellij.openapi.editor.ex.EditorEx;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
|
||||
public class UnindentSelectionAction extends EditorAction {
|
||||
public UnindentSelectionAction() {
|
||||
@@ -82,10 +81,9 @@ public class UnindentSelectionAction extends EditorAction {
|
||||
|
||||
if (startIndex < 0 || endIndex < 0) return;
|
||||
|
||||
VirtualFile vFile = FileDocumentManager.getInstance().getFile(document);
|
||||
final FileType fileType = vFile == null ? null : vFile.getFileType();
|
||||
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document);
|
||||
|
||||
int blockIndent = CodeStyleFacade.getInstance(project).getIndentSize(fileType);
|
||||
int blockIndent = CodeStyleSettingsManager.getSettings(project).getIndentOptionsByFile(file).INDENT_SIZE;
|
||||
IndentSelectionAction.doIndent(endIndex, startIndex, document, project, editor, -blockIndent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +70,8 @@ import com.intellij.openapi.wm.ToolWindowAnchor;
|
||||
import com.intellij.openapi.wm.ex.ToolWindowManagerEx;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.ui.*;
|
||||
import com.intellij.ui.components.JBLayeredPane;
|
||||
import com.intellij.ui.components.JBScrollBar;
|
||||
@@ -703,6 +705,8 @@ public final class EditorImpl extends UserDataHolderBase implements EditorEx, Hi
|
||||
|
||||
clearTextWidthCache();
|
||||
|
||||
reinitDocumentIndentOptions();
|
||||
|
||||
boolean softWrapsUsedBefore = mySoftWrapModel.isSoftWrappingEnabled();
|
||||
|
||||
mySettings.reinitSettings();
|
||||
@@ -755,6 +759,12 @@ public final class EditorImpl extends UserDataHolderBase implements EditorEx, Hi
|
||||
}
|
||||
}
|
||||
|
||||
private void reinitDocumentIndentOptions() {
|
||||
if (myProject != null && !myProject.isDisposed()) {
|
||||
CodeStyleSettingsManager.updateDocumentIndentOptions(myProject, myDocument);
|
||||
}
|
||||
}
|
||||
|
||||
private void initTabPainter() {
|
||||
myTabPainter = new ArrowPainter(
|
||||
ColorProvider.byColorsScheme(myScheme, EditorColors.WHITESPACES_COLOR),
|
||||
|
||||
@@ -31,11 +31,10 @@ import com.intellij.openapi.editor.EditorSettings;
|
||||
import com.intellij.openapi.editor.ex.EditorEx;
|
||||
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
|
||||
import com.intellij.openapi.editor.impl.softwrap.SoftWrapAppliancePlaces;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -241,8 +240,10 @@ public class SettingsImpl implements EditorSettings {
|
||||
|
||||
@Override
|
||||
public boolean isUseTabCharacter(Project project) {
|
||||
FileType fileType = getFileType();
|
||||
return myUseTabCharacter != null ? myUseTabCharacter.booleanValue() : CodeStyleFacade.getInstance(project).useTabCharacter(fileType);
|
||||
PsiFile file = getPsiFile(project);
|
||||
return myUseTabCharacter != null
|
||||
? myUseTabCharacter.booleanValue()
|
||||
: CodeStyleSettingsManager.getSettings(project).getIndentOptionsByFile(file).USE_TAB_CHARACTER;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -265,17 +266,21 @@ public class SettingsImpl implements EditorSettings {
|
||||
public int getTabSize(Project project) {
|
||||
if (myTabSize != null) return myTabSize.intValue();
|
||||
if (myCachedTabSize != null) return myCachedTabSize.intValue();
|
||||
|
||||
FileType fileType = getFileType();
|
||||
int tabSize = project == null || project.isDisposed() ? 0 : CodeStyleFacade.getInstance(project).getTabSize(fileType);
|
||||
int tabSize = 0;
|
||||
if (project != null && !project.isDisposed()) {
|
||||
PsiFile file = getPsiFile(project);
|
||||
tabSize = CodeStyleSettingsManager.getSettings(project).getIndentOptionsByFile(file).TAB_SIZE;
|
||||
}
|
||||
myCachedTabSize = Integer.valueOf(tabSize);
|
||||
return tabSize;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private FileType getFileType() {
|
||||
VirtualFile file = myEditor == null ? null : myEditor.getVirtualFile();
|
||||
return file == null ? null : file.getFileType();
|
||||
private PsiFile getPsiFile(@NotNull Project project) {
|
||||
if (myEditor != null) {
|
||||
return PsiDocumentManager.getInstance(project).getPsiFile(myEditor.getDocument());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,10 +28,6 @@ import com.intellij.openapi.editor.ex.*;
|
||||
import com.intellij.openapi.editor.ex.util.EditorUtil;
|
||||
import com.intellij.openapi.editor.impl.softwrap.*;
|
||||
import com.intellij.openapi.editor.impl.softwrap.mapping.*;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.reference.SoftReference;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -172,7 +168,7 @@ public class SoftWrapModelImpl implements SoftWrapModelEx, PrioritizedDocumentLi
|
||||
myUseSoftWraps = settings.isUseSoftWraps();
|
||||
|
||||
int tabWidthBefore = myTabWidth;
|
||||
myTabWidth = getCurrentTabWidth();
|
||||
myTabWidth = EditorUtil.getTabSize(myEditor);
|
||||
|
||||
boolean fontsChanged = false;
|
||||
if (!myFontPreferences.equals(myEditor.getColorsScheme().getFontPreferences())
|
||||
@@ -189,20 +185,6 @@ public class SoftWrapModelImpl implements SoftWrapModelEx, PrioritizedDocumentLi
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return tab width for the file used at the current editor (if it's possible to calculate the one);
|
||||
* <code>'-1'</code> otherwise
|
||||
*/
|
||||
private int getCurrentTabWidth() {
|
||||
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(myEditor.getProject());
|
||||
final VirtualFile file = myEditor.getVirtualFile();
|
||||
if (file == null) {
|
||||
return -1;
|
||||
}
|
||||
final CommonCodeStyleSettings.IndentOptions indentOptions = settings.getIndentOptions(file.getFileType());
|
||||
return indentOptions.TAB_SIZE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRespectAdditionalColumns() {
|
||||
return myForceAdditionalColumns || !isSoftWrappingEnabled() || myApplianceManager.hasLinesWithFailedWrap();
|
||||
|
||||
@@ -550,6 +550,7 @@
|
||||
</extensionPoint>
|
||||
|
||||
<extensionPoint name="fileTypeIndentOptionsProvider" interface="com.intellij.psi.codeStyle.FileTypeIndentOptionsProvider"/>
|
||||
<extensionPoint name="fileIndentOptionsProvider" interface="com.intellij.psi.codeStyle.FileIndentOptionsProvider"/>
|
||||
<extensionPoint name="referenceImporter" interface="com.intellij.codeInsight.daemon.ReferenceImporter"/>
|
||||
<extensionPoint name="elementLookupRenderer" interface="com.intellij.codeInsight.lookup.impl.ElementLookupRenderer"/>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user