Skip to content

Commit

Permalink
refactor: improve PrinterHelper (less duplication, more encapsulation) (
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus authored and surli committed May 29, 2017
1 parent 7857293 commit ea0bde3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ public <T> void visitCtNewArray(CtNewArray<T> newArray) {
List<CtComment> comments = elementPrinterHelper.getComments(e, CommentOffset.AFTER);
// if the last element contains an inline comment, print a new line before closing the array
if (!comments.isEmpty() && comments.get(comments.size() - 1).getCommentType() == CtComment.CommentType.INLINE) {
printer.insertLine();
printer.writeln();
}
}
}
Expand Down
30 changes: 16 additions & 14 deletions src/main/java/spoon/reflect/visitor/printer/PrinterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public class PrinterHelper {
*/
private int line = 1;

/**
* Current column number
* Not used yet, but this shows the advantage of encapsulating all sbf.append in calls to {@link #write(char)} or {@link #write(String)}.
* This will be used for sniper mode later
*/
private int column = 1;

/**
* Mapping for line numbers.
*/
Expand All @@ -68,6 +75,7 @@ public PrinterHelper(Environment env) {
public PrinterHelper write(String s) {
if (s != null) {
sbf.append(s);
column += s.length();
}
return this;
}
Expand All @@ -77,25 +85,28 @@ public PrinterHelper write(String s) {
*/
public PrinterHelper write(char c) {
sbf.append(c);
column += 1;
return this;
}

/**
* Generates a new line.
*/
public PrinterHelper writeln() {
sbf.append(LINE_SEPARATOR);
write(LINE_SEPARATOR);
line++;
// reset the column index
column = 1;
return this;
}

public PrinterHelper writeTabs() {
for (int i = 0; i < nbTabs; i++) {
if (env.isUsingTabulations()) {
sbf.append("\t");
write('\t');
} else {
for (int j = 0; j < env.getTabulationSize(); j++) {
sbf.append(" ");
write(' ');
}
}
}
Expand Down Expand Up @@ -125,16 +136,6 @@ public PrinterHelper setTabCount(int tabCount) {
nbTabs = tabCount;
return this;
}

public void insertLine() {
int i = sbf.length() - 1;
while (i >= 0 && (sbf.charAt(i) == ' ' || sbf.charAt(i) == '\t')) {
i--;
}
sbf.insert(i + 1, LINE_SEPARATOR);
line++;
}

public boolean removeLine() {
String ls = LINE_SEPARATOR;
int i = sbf.length() - ls.length();
Expand Down Expand Up @@ -162,8 +163,9 @@ private boolean isWhite(char c) {
public void adjustPosition(CtElement e, CompilationUnit unitExpected) {
if (e.getPosition() != null && !e.isImplicit() && e.getPosition().getCompilationUnit() != null && e.getPosition().getCompilationUnit() == unitExpected) {
while (line < e.getPosition().getLine()) {
insertLine();
writeln();
}
// trying to remove some lines
while (line > e.getPosition().getLine()) {
if (!removeLine()) {
if (line > e.getPosition().getEndLine()) {
Expand Down

0 comments on commit ea0bde3

Please sign in to comment.