Skip to content

Commit

Permalink
Implement feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed May 10, 2018
1 parent d4ab49f commit a45c695
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
Expand Up @@ -1447,7 +1447,7 @@ public void visit(NodeList n, Void arg) {
//noinspection unchecked
NodeList<ImportDeclaration> modifiableList = new NodeList<>(n);
modifiableList.sort(
comparingInt((ImportDeclaration left) -> left.isStatic() ? 0 : 1)
comparingInt((ImportDeclaration i) -> i.isStatic() ? 0 : 1)
.thenComparing(NodeWithName::getNameAsString));
for (Object node : modifiableList) {
((Node) node).accept(this, arg);
Expand Down
Expand Up @@ -21,6 +21,8 @@

package com.github.javaparser.printer;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.function.Function;

import static com.github.javaparser.utils.Utils.EOL;
Expand All @@ -31,30 +33,49 @@
*/
public class PrettyPrinterConfiguration {
public static final int DEFAULT_MAX_ENUM_CONSTANTS_TO_ALIGN_HORIZONTALLY = 5;

private boolean orderImports = false;
private boolean printComments = true;
private boolean printJavadoc = true;
private boolean columnAlignParameters = false;
private boolean columnAlignFirstMethodChain = false;
private int indent = 4;
private int indentSize = 4;
private String endOfLineCharacter = EOL;
private Function<PrettyPrinterConfiguration, PrettyPrintVisitor> visitorFactory = PrettyPrintVisitor::new;
private int maxEnumConstantsToAlignHorizontally = DEFAULT_MAX_ENUM_CONSTANTS_TO_ALIGN_HORIZONTALLY;

/**
* Set the string to use for indenting. For example: "\t", " ", "".
* @deprecated use setIndentSize
*/
@Deprecated
public PrettyPrinterConfiguration setIndent(String indent) {
throw new NotImplementedException();
}

/**
* @return the string that will be used to indent.
*/
public String getIndent() {
StringBuilder indentString = new StringBuilder();
for(int i=0; i<indent; i++){
indentString.append(" ");
for (int i = 0; i < indentSize; i++) {
indentString.append(' ');
}
return indentString.toString();
}

public int getIndentSize() {
return indentSize;
}

/**
* Set the size of the indent in spaces.
*/
public PrettyPrinterConfiguration setIndent(int indent) {
this.indent = assertNotNull(indent);
public PrettyPrinterConfiguration setIndentSize(int size) {
if (size < 0) {
throw new IllegalArgumentException("Indent must be >= 0");
}
this.indentSize = size;
return this;
}

Expand Down
Expand Up @@ -27,53 +27,76 @@
import java.util.Deque;
import java.util.LinkedList;

import static com.github.javaparser.Position.*;

public class SourcePrinter {
private final String endOfLineCharacter;
private final String indentation;

private final Deque<String> indents = new LinkedList<>();
private final StringBuilder buf = new StringBuilder();
private Position cursor = new Position(1, 0);
private boolean indented=false;
private boolean indented = false;

SourcePrinter(final String indentation, final String endOfLineCharacter) {
this.indentation = indentation;
this.endOfLineCharacter = endOfLineCharacter;
indents.push("");
}

/**
* Add the default indentation to the current indentation and push it on the indentation stack.
* Does not actually output anything.
*/
public SourcePrinter indent() {
String currentIndent = indents.peek();
indents.push(currentIndent + indentation);
return this;
}

/**
* Add spaces to the current indentation until it is reaches "column" and push it on the indentation stack.
* Does not actually output anything.
*/
public SourcePrinter indentTo(int column) {
StringBuilder newIndent = new StringBuilder(indents.peek());
if (indents.isEmpty()) {
throw new IllegalStateException("Indent/unindent calls are not well-balanced.");
}
final String lastIndent = indents.peek();
if(column< lastIndent.length()){
throw new IllegalStateException("Attempt to indent less than the previous indent.");
}

StringBuilder newIndent = new StringBuilder(lastIndent);
while (newIndent.length() < column) {
newIndent.append(' ');
}
indents.push(newIndent.toString());
return this;
}

/**
* Pop the last indentation of the indentation stack.
* Does not actually output anything.
*/
public SourcePrinter unindent() {
if (indents.isEmpty()) {
// Since we start out with an empty indent on the stack, this will only occur
// the second time we over-unindent.
throw new IllegalStateException("Indent/unindent calls are not well-balanced.");
}
indents.pop();
return this;
}

private void append(String arg) {
buf.append(arg);
cursor = Position.pos(cursor.line, cursor.column + arg.length());
}

private void makeIndent() {
append(indents.peek());
cursor = cursor.withColumn(cursor.column + arg.length());
}

/**
* Append the source string passed as argument to the buffer.
* If this is being appended at the beginning of a line, performs indentSize first.
* If this is being appended at the beginning of a line, performs indentation first.
* <p>
* The source line to be printed should not contain newline/carriage-return characters;
* use {@link #println(String)} to automatically append a newline at the end of the source string.
Expand All @@ -86,17 +109,16 @@ private void makeIndent() {
*/
public SourcePrinter print(final String arg) {
if (!indented) {
makeIndent();
append(indents.peek());
indented = true;
}
buf.append(arg);
cursor = Position.pos(cursor.line, cursor.column + arg.length());
append(arg);
return this;
}

/**
* Append the source string passed as argument to the buffer, then append a newline.
* If this is being appended at the beginning of a line, performs indentSize first.
* If this is being appended at the beginning of a line, performs indentation first.
* <p>
* The source line to be printed should not contain newline/carriage-return characters.
* If the source line passed as argument contains newline/carriage-return characters would
Expand All @@ -118,7 +140,7 @@ public SourcePrinter println(final String arg) {
*/
public SourcePrinter println() {
buf.append(endOfLineCharacter);
cursor = Position.pos(cursor.line + 1, 0);
cursor = pos(cursor.line + 1, 0);
indented = false;
return this;
}
Expand All @@ -137,23 +159,42 @@ public Position getCursor() {
return cursor;
}

/**
* @return the currently printed source code.
*/
public String getSource() {
return buf.toString();
}

/**
* @return the currently printed source code.
*/
@Override
public String toString() {
return getSource();
}

/**
* Changes all EOL characters in "content" to the EOL character this SourcePrinter is using.
*/
public String normalizeEolInTextBlock(String content) {
return Utils.normalizeEolInTextBlock(content, endOfLineCharacter);
}

/**
* Set the indent of the next line to the column the cursor is currently in.
* Does not actually output anything.
*/
public void indentToCursor() {
indentTo(cursor.column);
}

/**
* Adds an indent to the top of the stack that is a copy of the current top indent.
* With this you announce "I'm going to indent the next line(s)" but not how far yet.
* Once you do know, you can pop this indent ("unindent") and indent to the right column.
* (Does not actually output anything.)
*/
public void duplicateIndent() {
indents.push(indents.peek());
}
Expand Down

0 comments on commit a45c695

Please sign in to comment.