Skip to content

Commit

Permalink
refactor: PrinterHelper.writeTabs() automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Sep 30, 2017
1 parent 64e6f20 commit 21b2e31
Showing 1 changed file with 59 additions and 8 deletions.
67 changes: 59 additions & 8 deletions src/main/java/spoon/reflect/visitor/PrinterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public class PrinterHelper {
*/
private Map<Integer, Integer> lineNumberMapping = new HashMap<>();

/*
* each writeln() sets this to true.
* if true then first call of write first writes tabs and then resets this to false
*/
private boolean shouldWriteTabs = true;
/*
* true if last written character was \r
* It helps to detect windows EOL, which is \r\n
*/
private boolean lastCharWasCR = false;

public PrinterHelper(Environment env) {
this.env = env;
}
Expand All @@ -82,6 +93,7 @@ public void reset() {
nbTabs = 0;
line = 1;
column = 1;
shouldWriteTabs = true;
//create new map, because clients keeps reference to it
lineNumberMapping = new HashMap<>();
}
Expand All @@ -91,8 +103,10 @@ public void reset() {
*/
public PrinterHelper write(String s) {
if (s != null) {
sbf.append(s);
column += s.length();
int len = s.length();
for (int i = 0; i < len; i++) {
write(s.charAt(i));
}
}
return this;
}
Expand All @@ -101,8 +115,35 @@ public PrinterHelper write(String s) {
* Outputs a char.
*/
public PrinterHelper write(char c) {
if (c == '\r') {
sbf.append(c);
line++;
// reset the column index
column = 1;
shouldWriteTabs = true;
lastCharWasCR = true;
return this;
}
if (c == '\n') {
sbf.append(c);
if (lastCharWasCR) {
//increment line only once in sequence of \r\n.
//last was \r, so nothing to do
} else {
//increment line only once in sequence of \r\n.
//last was NOT \r, so do it now
line++;
// reset the column index
column = 1;
shouldWriteTabs = true;
}
lastCharWasCR = false;
return this;
}
autoWriteTabs();
sbf.append(c);
column += 1;
lastCharWasCR = false;
return this;
}

Expand All @@ -111,23 +152,33 @@ public PrinterHelper write(char c) {
*/
public PrinterHelper writeln() {
write(lineSeparator);
line++;
// reset the column index
column = 1;
return this;
}

@Deprecated
public PrinterHelper writeTabs() {
return this;
}

private void writeTabsInternal() {
for (int i = 0; i < nbTabs; i++) {
if (env.isUsingTabulations()) {
write('\t');
sbf.append('\t');
column += 1;
} else {
for (int j = 0; j < env.getTabulationSize(); j++) {
write(' ');
sbf.append(' ');
column += 1;
}
}
}
return this;
}

private void autoWriteTabs() {
if (shouldWriteTabs) {
writeTabsInternal();
shouldWriteTabs = false;
}
}

/**
Expand Down

0 comments on commit 21b2e31

Please sign in to comment.