libcmo21/CodeGen/EnumsMigration/IndentHelper.java

108 lines
1.9 KiB
Java
Raw Normal View History

2023-08-18 15:55:31 +08:00
import java.io.OutputStreamWriter;
2023-08-20 12:13:40 +08:00
public class IndentHelper {
2023-10-10 10:39:27 +08:00
public IndentHelper(OutputStreamWriter writer, CommonHelper.LangType lang_type) {
2023-08-18 15:55:31 +08:00
mIndent = 0;
2023-10-10 10:39:27 +08:00
mLangType = lang_type;
2023-08-19 00:03:00 +08:00
mWriter = writer;
2023-08-18 15:55:31 +08:00
}
private int mIndent;
2023-10-10 10:39:27 +08:00
private CommonHelper.LangType mLangType;
2023-08-18 15:55:31 +08:00
private OutputStreamWriter mWriter;
2023-10-10 10:39:27 +08:00
private void rawIndent() throws Exception {
for (int i = 0; i < mIndent; ++i) {
switch (mLangType) {
case CPP:
mWriter.write("\t");
break;
case Python:
mWriter.write(" ");
break;
}
}
}
2023-08-18 15:55:31 +08:00
public void inc() {
++mIndent;
}
public void dec() {
--mIndent;
}
public void indent() throws Exception {
mWriter.write("\n");
2023-10-10 10:39:27 +08:00
rawIndent();
2023-08-18 15:55:31 +08:00
}
2023-08-20 12:13:40 +08:00
2023-08-18 15:55:31 +08:00
public void puts(String data) throws Exception {
indent();
mWriter.write(data);
}
2023-08-20 12:13:40 +08:00
2023-08-18 15:55:31 +08:00
public void printf(String fmt, Object... args) throws Exception {
indent();
mWriter.write(String.format(fmt, args));
}
2023-08-20 12:13:40 +08:00
2023-08-20 16:10:22 +08:00
/**
* Write standard Doxygen document comment.
* <p>
* Usually be called before writing content.
2023-10-10 10:39:27 +08:00
*
2023-08-20 16:10:22 +08:00
* @param comment
* @throws Exception
*/
2023-08-20 12:13:40 +08:00
public void briefComment(String comment) throws Exception {
if (comment == null)
return;
2023-10-10 10:39:27 +08:00
switch (mLangType) {
case CPP:
rawIndent();
puts("/**");
puts(comment);
rawIndent();
puts(" */");
break;
case Python:
rawIndent();
puts("\"\"\"!");
puts(comment);
rawIndent();
puts("\"\"\"");
break;
}
2023-08-18 15:55:31 +08:00
}
2023-08-20 12:13:40 +08:00
2023-08-20 16:10:22 +08:00
/**
* Write suffix style Doxygen document comment.
* <p>
* Usually be called after writing content.
2023-10-10 10:39:27 +08:00
*
2023-08-20 16:10:22 +08:00
* @param comment
* @throws Exception
*/
2023-08-20 12:13:40 +08:00
public void afterMemberComment(String comment) throws Exception {
if (comment == null)
return;
2023-10-10 10:39:27 +08:00
switch (mLangType) {
case CPP:
mWriter.write(String.format("\t/**< %s */", CommonHelper.removeEol(comment)));
break;
case Python:
mWriter.write(String.format(" ##< %s", CommonHelper.removeEol(comment)));
break;
}
2023-08-18 15:55:31 +08:00
}
}