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-10-12 10:42:06 +08:00
|
|
|
|
|
|
|
// set indent chars
|
|
|
|
switch (mLangType) {
|
2024-04-22 14:13:36 +08:00
|
|
|
case Cpp:
|
|
|
|
case CSharp:
|
2023-10-12 10:42:06 +08:00
|
|
|
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;
|
2023-10-12 10:42:06 +08:00
|
|
|
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) {
|
2023-10-12 10:42:06 +08:00
|
|
|
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 {
|
2023-10-12 10:42:06 +08:00
|
|
|
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) {
|
2024-04-22 14:13:36 +08:00
|
|
|
case Cpp:
|
|
|
|
case CSharp:
|
2023-10-10 10:39:27 +08:00
|
|
|
puts("/**");
|
2023-10-12 10:42:06 +08:00
|
|
|
|
|
|
|
mWriter.write(System.lineSeparator());
|
|
|
|
mWriter.write(CommonHelper.reindentBlockString(comment, true, mIndent));
|
2023-10-10 10:39:27 +08:00
|
|
|
|
|
|
|
puts(" */");
|
|
|
|
break;
|
|
|
|
case Python:
|
|
|
|
puts("\"\"\"!");
|
|
|
|
|
2023-10-12 10:42:06 +08:00
|
|
|
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
|
|
|
|
2023-10-12 10:42:06 +08:00
|
|
|
mWriter.write(mIndentChars);
|
2023-10-10 10:39:27 +08:00
|
|
|
switch (mLangType) {
|
2024-04-22 14:13:36 +08:00
|
|
|
case Cpp:
|
|
|
|
case CSharp:
|
2023-10-12 10:42:06 +08:00
|
|
|
mWriter.write(String.format("/**< %s */", CommonHelper.removeEol(comment)));
|
2023-10-10 10:39:27 +08:00
|
|
|
break;
|
|
|
|
case Python:
|
2023-10-12 10:42:06 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|