📄 java2ansi.java
字号:
*/
private String convertLine(String line) {
if (line.length() == 0) {
return "";
}
StringBuffer lineBuffer = new StringBuffer(line.length() * 2);
StringBuffer wordBuffer = new StringBuffer();
int start = 0;
int end = line.length() - 1;
char[] contents = line.toCharArray();
//循环判断一行中的最后一个字符前的字符状况
for (int i = start; i < end; i++) {
//如果没有当前类型
if (currentType == NONTYPE) {
//如果是合法的java字符开始标识符则将类型设置为单词类型并将当
//前字符添加到单词缓冲区
if (Character.isJavaIdentifierStart(contents[i])) {
currentType = WORD;
wordBuffer.append(contents[i]);
continue;
}
//如果是可能的注释开始
else if (contents[i] == '/') {
char nextChar = contents[i + 1];
switch (nextChar) {
//如果下一个字符是'*'则表明是多行注释,将类型设置为多
//行字符并添加对应的字体颜色及字符
case '*' :
currentType = COMMENT;
lineBuffer.append(styleComment.getBegin());
lineBuffer.append(contents[i]);
continue;
//如果下一个字符是'/'则表明是单行注释,添加对应的
//字体颜色并直接取字符串的剩余内容
case '/' :
lineBuffer.append(styleComment.getBegin());
lineBuffer.append(line.substring(i));
lineBuffer.append(styleComment.getEnd());
return lineBuffer.toString();
//不是注释的开始则直接添加内容
default :
lineBuffer.append(contents[i]);
continue;
}
}
//如果是双引号
else if (contents[i] == '\"') {
//如果是最开始的位置或者其前一个字符不是'\'则表明是一个引
//用的开始,设置类型并添加对应的字体颜色及字符
if (i == 0 || contents[i - 1] != '\\') {
currentType = QUOTE;
lineBuffer.append(styleQuote.getBegin());
lineBuffer.append(contents[i]);
continue;
}
//否则不是引用的内容,添加内容
else {
lineBuffer.append(contents[i]);
continue;
}
}
//如果不是以上情况则表明是其他的杂项内容,不进行处理,将字符
//添加到缓冲区
else {
lineBuffer.append(contents[i]);
continue;
}
}
//如果当前类型是单词
else if (currentType == WORD) {
//如果是一个java标识符的合法部分表明单词没有结束,将当前字符
//添加到单词缓冲区
if (Character.isJavaIdentifierPart(contents[i])) {
wordBuffer.append(contents[i]);
continue;
}
//否则表明单词结束,将单词缓冲区的内容进行转换并添加到缓冲区
//并继续判断后面部分的语法成分
else {
lineBuffer.append(convertWord(wordBuffer.toString()));
wordBuffer = new StringBuffer();
//如果是可能的注释开始
if (contents[i] == '/') {
char nextChar = contents[i + 1];
switch (nextChar) {
//如果下一个字符是'*'则表明是多行注释,将类型设置
//为多行字符并添加对应的字体颜色及字符
case '*' :
currentType = COMMENT;
lineBuffer.append(styleComment.getBegin());
lineBuffer.append(contents[i]);
continue;
//如果下一个字符是'/'则表明是单行注释,添加对
//应的字体颜色并直接取字符串的剩余内容
case '/' :
lineBuffer.append(styleComment.getBegin());
lineBuffer.append(line.substring(i));
lineBuffer.append(styleComment.getEnd());
currentType = NONTYPE;
return lineBuffer.toString();
}
}
//如果是双引号则表明是一个引用的开始,设置类型并添加对应
//的字体颜色及字符
else if (contents[i] == '\"') {
currentType = QUOTE;
lineBuffer.append(styleQuote.getBegin());
lineBuffer.append(contents[i]);
continue;
}
//如果不是以上情况则表明是其他的杂项内容,不进行处理,将
//字符添加到缓冲区
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -