@@ -86,6 +86,17 @@ private enum IndentationState {
8686 MULTI_LINE_COMMENT
8787 }
8888
89+ private final String text ;
90+ private final IndentationType type ;
91+ private boolean startOfLine = true ;
92+ private int numberOfBrackets = 0 ;
93+ private StringBuilder builder ;
94+
95+ private IndentationHelper (String text , IndentationType type ) {
96+ this .text = text ;
97+ this .type = type ;
98+ this .builder = new StringBuilder ((int ) (text .length () * 1.25f ));
99+ }
89100
90101 /**
91102 * Aims to indent the given String using the pattern provided. Will return the String unchanged if {@link IndentationHelper.IndentationType#NULL} is passed as the IndentationType parameter.
@@ -95,13 +106,14 @@ private enum IndentationState {
95106 * @return The indented String with the format specified.
96107 */
97108 public static String formatIndentation (String text , IndentationType type ) {
109+ return new IndentationHelper (text , type ).formatIndentation ();
110+ }
111+
112+ private String formatIndentation () {
98113 if (type == IndentationType .NULL ) {
99114 return text ;
100115 }
101- int numberOfBrackets = 0 ;
102- StringBuilder builder = new StringBuilder ((int ) (text .length () * 1.25f ));
103116 IndentationState currentState = IndentationState .CODE ;
104- boolean startOfLine = true ;
105117 for (int i = 0 ; i < text .length (); i ++) {
106118 char current = text .charAt (i );
107119 if (startOfLine && current == ' ' ) {
@@ -112,63 +124,65 @@ public static String formatIndentation(String text, IndentationType type) {
112124 builder .append (type .getPattern ().repeat (Math .max (numberOfBrackets , 0 )));
113125 startOfLine = true ;
114126 }
115- switch (currentState ) {
116- case CODE -> {
117- switch (current ) {
118- case '{' -> numberOfBrackets ++;
119- case '}' -> {
120- numberOfBrackets --;
121- if (startOfLine && builder .length () - type .getNumberOfChars () - 1 >= 0 ) {
122- builder .replace (builder .length () - type .getNumberOfChars () - 1 , builder .length (), "}" );
123- }
124- }
125- case '\'' -> {
126- currentState = IndentationState .CHARACTER ;
127- }
128- case '\"' -> {
129- currentState = IndentationState .STRING ;
130- }
131- case '/' -> {
132- if (i + 1 < text .length ()) {
133- if (text .charAt (i + 1 ) == '/' ) {
134- currentState = IndentationState .SINGLE_LINE_COMMENT ;
135- } else if (text .charAt (i + 1 ) == '*' ) {
136- currentState = IndentationState .MULTI_LINE_COMMENT ;
137- }
138- }
139- }
140- }
141- }
127+ currentState = switch (currentState ) {
128+ case CODE ->
129+ processTokenInCode (i , current );
142130 case STRING -> {
143131 if (current == '\"' && !isEscaped (builder , builder .length () - 1 )) {
144- currentState = IndentationState .CODE ;
132+ yield IndentationState .CODE ;
145133 }
134+ yield IndentationState .STRING ;
146135 }
147136 case CHARACTER -> {
148137 if (current == '\'' && !isEscaped (builder , builder .length () - 1 )) {
149- currentState = IndentationState .CODE ;
150- }
151- }
152- case SINGLE_LINE_COMMENT -> {
153- if (current == '\n' ) {
154- currentState = IndentationState .CODE ;
138+ yield IndentationState .CODE ;
155139 }
140+ yield IndentationState .CHARACTER ;
156141 }
142+ case SINGLE_LINE_COMMENT ->
143+ current == '\n' ? IndentationState .CODE : IndentationState .SINGLE_LINE_COMMENT ;
157144 case MULTI_LINE_COMMENT -> {
158- if (current == '*' && i + 1 < text .length ()) {
159- if (text .charAt (i + 1 ) == '/' ) {
160- currentState = IndentationState .CODE ;
161- }
145+ if (current == '*' && i + 1 < text .length () && text .charAt (i + 1 ) == '/' ) {
146+ yield IndentationState .CODE ;
162147 }
148+ yield IndentationState .MULTI_LINE_COMMENT ;
163149 }
164- }
150+ };
165151 if (!Character .isWhitespace (current ) && startOfLine ) {
166152 startOfLine = false ;
167153 }
168154 }
169155 return builder .toString ();
170156 }
171157
158+ private IndentationState processTokenInCode (int i , char current ) {
159+ switch (current ) {
160+ case '{' -> numberOfBrackets ++;
161+ case '}' -> {
162+ numberOfBrackets --;
163+ if (startOfLine && builder .length () - type .getNumberOfChars () - 1 >= 0 ) {
164+ builder .replace (builder .length () - type .getNumberOfChars () - 1 , builder .length (), "}" );
165+ }
166+ }
167+ case '\'' -> {
168+ return IndentationState .CHARACTER ;
169+ }
170+ case '\"' -> {
171+ return IndentationState .STRING ;
172+ }
173+ case '/' -> {
174+ if (i + 1 < text .length ()) {
175+ if (text .charAt (i + 1 ) == '/' ) {
176+ return IndentationState .SINGLE_LINE_COMMENT ;
177+ } else if (text .charAt (i + 1 ) == '*' ) {
178+ return IndentationState .MULTI_LINE_COMMENT ;
179+ }
180+ }
181+ }
182+ }
183+ return IndentationState .CODE ;
184+ }
185+
172186 /**
173187 * Determines if the character in the StringBuilder at the specified position is escaped.
174188 *
0 commit comments