File indent options provider API

This commit is contained in:
Rustam Vishnyakov
2014-09-18 18:30:53 +04:00
parent 74d683de3e
commit 3fb1e5d778
29 changed files with 366 additions and 71 deletions
@@ -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");
}
}