1616 *******************************************************************************/
1717package org .jetbrains .kotlin .ui .tests .editors .formatter ;
1818
19- import java .lang .reflect .Field ;
20- import java .lang .reflect .InvocationTargetException ;
21- import java .lang .reflect .Method ;
22- import java .util .Arrays ;
23- import java .util .List ;
24-
2519import org .eclipse .jface .text .BadLocationException ;
2620import org .eclipse .jface .text .IDocument ;
2721import org .eclipse .jface .text .TextUtilities ;
2822import org .eclipse .ui .editors .text .EditorsUI ;
2923import org .eclipse .ui .texteditor .AbstractDecoratedTextEditorPreferenceConstants ;
30- import org .jetbrains .kotlin .idea .KotlinLanguage ;
31- import org .jetbrains .kotlin .idea .core .formatter .KotlinCodeStyleSettings ;
3224import org .jetbrains .kotlin .testframework .editor .KotlinEditorWithAfterFileTestCase ;
25+ import org .jetbrains .kotlin .testframework .utils .CodeStyleConfigurator ;
3326import org .jetbrains .kotlin .testframework .utils .EditorTestUtils ;
34- import org .jetbrains .kotlin .testframework .utils .InTextDirectivesUtils ;
3527import org .junit .After ;
3628import org .junit .Assert ;
3729import org .junit .Before ;
3830
39- import com .intellij .psi .codeStyle .CodeStyleSettings ;
40- import com .intellij .psi .codeStyle .CommonCodeStyleSettings ;
41-
42- import kotlin .Pair ;
43- import kotlin .collections .CollectionsKt ;
44- import kotlin .jvm .functions .Function1 ;
45- import org .jetbrains .kotlin .ui .formatter .KotlinFormatterKt ;
46-
4731public abstract class KotlinFormatActionTestCase extends KotlinEditorWithAfterFileTestCase {
4832 @ Before
4933 public void before () {
@@ -52,7 +36,7 @@ public void before() {
5236
5337 @ After
5438 public void setDefaultSettings () {
55- KotlinFormatterKt . setSettings ( new CodeStyleSettings ());
39+ CodeStyleConfigurator . INSTANCE . deconfigure ( getTestProject (). getProject ());
5640 }
5741
5842 @ Override
@@ -61,8 +45,8 @@ protected void performTest(String fileText, String content) {
6145
6246 EditorsUI .getPreferenceStore ().setValue (AbstractDecoratedTextEditorPreferenceConstants .EDITOR_SPACES_FOR_TABS , true );
6347 EditorsUI .getPreferenceStore ().setValue (AbstractDecoratedTextEditorPreferenceConstants .EDITOR_TAB_WIDTH , 4 );
64-
65- configureSettings ( fileText );
48+
49+ CodeStyleConfigurator . INSTANCE . configure ( getTestProject (). getProject (), fileText );
6650
6751 getTestEditor ().runFormatAction ();
6852
@@ -79,94 +63,4 @@ private void assertLineDelimiters(String expectedLineDelimiter, IDocument docume
7963 throw new RuntimeException (e );
8064 }
8165 }
82-
83- public static void configureSettings (String fileText ) {
84- List <String > settingsToTrue = InTextDirectivesUtils .findListWithPrefixes (fileText , "SET_TRUE:" );
85- List <String > settingsToFalse = InTextDirectivesUtils .findListWithPrefixes (fileText , "SET_FALSE:" );
86- List <Pair > settingsToIntValue = CollectionsKt .map (InTextDirectivesUtils .findListWithPrefixes (fileText , "SET_INT:" ), new Function1 <String , Pair >() {
87- @ Override
88- public Pair <String , Integer > invoke (String s ) {
89- String [] tokens = s .split ("=" );
90- return new Pair <String , Integer >(tokens [0 ].trim (), Integer .valueOf (tokens [1 ].trim ()));
91- }
92- });
93-
94- KotlinCodeStyleSettings kotlinSettings = KotlinFormatterKt .getSettings ().getCustomSettings (KotlinCodeStyleSettings .class );
95- CommonCodeStyleSettings commonSettings = KotlinFormatterKt .getSettings ().getCommonSettings (KotlinLanguage .INSTANCE );
96-
97- List <Object > objects = Arrays .asList (kotlinSettings , commonSettings );
98-
99- for (String trueSetting : settingsToTrue ) {
100- setBooleanSetting (trueSetting , true , objects );
101- }
102-
103- for (String falseSetting : settingsToFalse ) {
104- setBooleanSetting (falseSetting , false , objects );
105- }
106-
107- for (Pair <String , Integer > setting : settingsToIntValue ) {
108- setIntSetting (setting .getFirst (), setting .getSecond (), objects );
109- }
110-
111- String rightMarginString = InTextDirectivesUtils .findStringWithPrefixes (fileText , "// RIGHT_MARGIN: " );
112- if (rightMarginString != null ) {
113- Integer rightMargin = Integer .parseInt (rightMarginString );
114- commonSettings .RIGHT_MARGIN = rightMargin ;
115- }
116-
117- }
118-
119- public static void setBooleanSetting (String setting , Boolean value , List <Object > objects ) {
120- setSettingValue (setting , value , boolean .class , objects );
121- }
122-
123- public static void setIntSetting (String setting , Integer value , List <Object > objects ) {
124- setSettingValue (setting , value , int .class , objects );
125- }
126-
127- public static void setSettingValue (String settingName , Object value , Class <?> valueType , List <Object > objects ) {
128- for (Object object : objects ) {
129- if (setSettingWithField (settingName , object , value ) || setSettingWithMethod (settingName , object , value , valueType )) {
130- return ;
131- }
132- }
133-
134- throw new IllegalArgumentException (String .format (
135- "There's no property or method with name '%s' in given objects: %s" , settingName , objects ));
136- }
137-
138- private static boolean setSettingWithField (String settingName , Object object , Object value ) {
139- try {
140- Field field = object .getClass ().getDeclaredField (settingName );
141- field .set (object , value );
142- return true ;
143- }
144- catch (IllegalAccessException e ) {
145- throw new IllegalArgumentException (String .format ("Can't set property with the name %s in object %s" , settingName , object ));
146- }
147- catch (NoSuchFieldException e ) {
148- // Do nothing - will try other variants
149- }
150-
151- return false ;
152- }
153-
154- private static boolean setSettingWithMethod (String setterName , Object object , Object value , Class <?> valueType ) {
155- try {
156- Method method = object .getClass ().getMethod (setterName , valueType );
157- method .invoke (object , value );
158- return true ;
159- }
160- catch (InvocationTargetException e ) {
161- throw new IllegalArgumentException (String .format ("Can't call method with name %s for object %s" , setterName , object ));
162- }
163- catch (IllegalAccessException e ) {
164- throw new IllegalArgumentException (String .format ("Can't access to method with name %s for object %s" , setterName , object ));
165- }
166- catch (NoSuchMethodException e ) {
167- // Do nothing - will try other variants
168- }
169-
170- return false ;
171- }
17266}
0 commit comments