libcmo21/CodeGen/EnumsMigration/IndentHelper.java

117 lines
2.3 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;
// set indent chars
switch (mLangType) {
case Cpp:
case CSharp:
mIndentChars = CommonHelper.getIndentString(true);
break;
case Python:
mIndentChars = CommonHelper.getIndentString(false);
break;
default:
mIndentChars = "";
break;
}
2023-08-18 15:55:31 +08:00
}
private int mIndent;
2023-10-10 10:39:27 +08:00
private CommonHelper.LangType mLangType;
private String mIndentChars;
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) {
mWriter.write(mIndentChars);
2023-10-10 10:39:27 +08:00
}
}
2023-08-18 15:55:31 +08:00
public void inc() {
++mIndent;
}
public void dec() {
--mIndent;
}
public void indent() throws Exception {
mWriter.write(System.lineSeparator());
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:
case CSharp:
2023-10-10 10:39:27 +08:00
puts("/**");
mWriter.write(System.lineSeparator());
mWriter.write(CommonHelper.reindentBlockString(comment, true, mIndent));
2023-10-10 10:39:27 +08:00
puts(" */");
break;
case Python:
puts("\"\"\"!");
mWriter.write(System.lineSeparator());
mWriter.write(CommonHelper.reindentBlockString(comment, false, mIndent));
2023-10-10 10:39:27 +08:00
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
mWriter.write(mIndentChars);
2023-10-10 10:39:27 +08:00
switch (mLangType) {
case Cpp:
case CSharp:
mWriter.write(String.format("/**< %s */", CommonHelper.removeEol(comment)));
2023-10-10 10:39:27 +08:00
break;
case Python:
mWriter.write(String.format("##< %s", CommonHelper.removeEol(comment)));
2023-10-10 10:39:27 +08:00
break;
}
2023-08-18 15:55:31 +08:00
}
}