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 29, 2017
1 parent 64e6f20 commit d4d6b86
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/main/java/spoon/reflect/visitor/PrinterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ 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
*/
boolean shouldWriteTabs = true;

public PrinterHelper(Environment env) {
this.env = env;
}
Expand All @@ -82,6 +88,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,6 +98,7 @@ public void reset() {
*/
public PrinterHelper write(String s) {
if (s != null) {
autoWriteTabs();
sbf.append(s);
column += s.length();
}
Expand All @@ -101,6 +109,7 @@ public PrinterHelper write(String s) {
* Outputs a char.
*/
public PrinterHelper write(char c) {
autoWriteTabs();
sbf.append(c);
column += 1;
return this;
Expand All @@ -117,7 +126,12 @@ public PrinterHelper writeln() {
return this;
}

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

private void writeTabsInternal() {
for (int i = 0; i < nbTabs; i++) {
if (env.isUsingTabulations()) {
write('\t');
Expand All @@ -127,7 +141,13 @@ public PrinterHelper writeTabs() {
}
}
}
return this;
}

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

/**
Expand Down

0 comments on commit d4d6b86

Please sign in to comment.