Skip to content

Commit

Permalink
Minor change in PrettyPrinterConfiguration : adding default char in e…
Browse files Browse the repository at this point in the history
…num IndentType
  • Loading branch information
jlerbsc committed Nov 27, 2020
1 parent 8758c34 commit d992a6f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@

package com.github.javaparser.printer;

import com.github.javaparser.ast.visitor.VoidVisitor;

import java.util.function.Function;

import static com.github.javaparser.printer.PrettyPrinterConfiguration.IndentType.SPACES;
import static com.github.javaparser.utils.Utils.SYSTEM_EOL;
import static com.github.javaparser.utils.Utils.assertNonNegative;
import static com.github.javaparser.utils.Utils.assertNotNull;
import static com.github.javaparser.utils.Utils.assertPositive;

import java.util.function.Function;

import com.github.javaparser.ast.visitor.VoidVisitor;

/**
* Configuration options for the {@link PrettyPrinter}.
*/
Expand All @@ -39,13 +39,13 @@ public enum IndentType {
/**
* Indent with spaces.
*/
SPACES,
SPACES(' '),

/**
* Indent with tabs as far as possible.
* For proper aligning, the tab width is necessary and by default 4.
*/
TABS,
TABS('\t'),

/**
* Indent with tabs but align with spaces when wrapping and aligning
Expand All @@ -68,7 +68,17 @@ public enum IndentType {
* }
* </pre>
*/
TABS_WITH_SPACE_ALIGN
TABS_WITH_SPACE_ALIGN('\t');

private Character car;

private IndentType(Character c) {
this.car = c;
}

public Character getChar() {
return this.car;
}
}

public static final int DEFAULT_MAX_ENUM_CONSTANTS_TO_ALIGN_HORIZONTALLY = 5;
Expand Down Expand Up @@ -101,20 +111,7 @@ public enum IndentType {
*/
public String getIndent() {
StringBuilder indentString = new StringBuilder();
char indentChar;
switch (indentType) {
case SPACES:
indentChar = ' ';
break;

case TABS:
case TABS_WITH_SPACE_ALIGN:
indentChar = '\t';
break;

default:
throw new AssertionError("Unhandled indent type");
}
char indentChar = indentType.getChar();
for (int i = 0; i < indentSize; i++) {
indentString.append(indentChar);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

package com.github.javaparser.printer;

import java.util.Deque;
import java.util.LinkedList;

import com.github.javaparser.Position;
import com.github.javaparser.printer.PrettyPrinterConfiguration.IndentType;
import com.github.javaparser.utils.Utils;

import java.util.Deque;
import java.util.LinkedList;

/**
* A support class for code that outputs formatted source code.
*/
Expand Down Expand Up @@ -97,29 +97,29 @@ private String calculateIndentWithAlignTo(int column) {
case SPACES:
case TABS_WITH_SPACE_ALIGN:
while (newIndent.length() < column) {
newIndent.append(' ');
newIndent.append(IndentType.SPACES.getChar());
}
break;

case TABS:
int logicalIndentLength = newIndent.length();
while ((logicalIndentLength + tabWidth) <= column) {
newIndent.insert(0, '\t');
newIndent.insert(0, indentType.getChar());
logicalIndentLength += tabWidth;
}
while (logicalIndentLength < column) {
newIndent.append(' ');
newIndent.append(IndentType.SPACES.getChar());
logicalIndentLength++;
}
StringBuilder fullTab = new StringBuilder();
for(int i=0; i<tabWidth; i++){
fullTab.append(' ');
fullTab.append(IndentType.SPACES.getChar());
}
String fullTabString = fullTab.toString();
if ((newIndent.length() >= tabWidth)
&& newIndent.substring(newIndent.length() - tabWidth).equals(fullTabString)) {
int i = newIndent.indexOf(fullTabString);
newIndent.replace(i, i + tabWidth, "\t");
newIndent.replace(i, i + tabWidth, indentType.getChar().toString());
}
break;

Expand Down

0 comments on commit d992a6f

Please sign in to comment.