英文:
Wired coloring issue Android EditText
问题
我需要在EditText中的用户输入中添加类似于代码的主题。我执行此操作的方式是将文本词法分析为标记,每个标记都有值和类型,相同类型具有相同的颜色。之后,我将这些标记加载到我的EditText中并对其进行着色,但是出现了一个奇怪的错误。
在此之后发生:
这很奇怪,因为我的词法分析器工作正常,这是控制台输出:
ColoredToken{key='KEYWORD', value='int'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='NAME', value='a'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='SIGNS', value='='}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='BID', value='10'}
ColoredToken{key='KEYWORD', value='if'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='SIGNS', value='('}
ColoredToken{key='SIGNS', value=')'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='SIGNS', value='{'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='KEYWORD', value='int'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='NAME', value='b'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='SIGNS', value='='}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='BID', value='10'}
ColoredToken{key='SIGNS', value='}'}
现在为了让你理解,蓝色用于名称,如第一张图片,关键字和符号为白色,问题是在键入一段时间后,一切开始变为蓝色,尽管标记是正确的。
这是着色和加载标记的代码:
public static SpannableStringBuilder runCodeColor(String codeString) {
ArrayList<String> code = new ArrayList<>(Arrays.asList(codeString.split("\n")));
SpannableStringBuilder coloredCode = new SpannableStringBuilder("");
int newLines = countNewLine(codeString);
for (int i = 0; i < code.size(); i++) {
ArrayList<ColoredToken> tmpTokens = new ArrayList<>(ColoredLexer.lexer(code.get(i)));
for (int j = 0; j < tmpTokens.size(); j++) {
coloredCode.append(tmpTokens.get(j).getValue());
coloredCode.setSpan(tmpTokens.get(j).getDarkColorSpan(), coloredCode.length() - tmpTokens.get(j).getValue().length(), coloredCode.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
System.out.println(tmpTokens.get(j).toString());
}
if (newLines > 0) {
coloredCode.append("\n");
newLines--;
}
}
return coloredCode;
}
如果需要,我可以提供词法分析器代码,但如上所述,标记是正确的,问题在于着色。
编辑:
如果有人知道更好的代码着色/主题方式,如果您能提供一些示例代码并解释其工作原理,那将是很棒的。
英文:
I need to add code-like theme to the user input in EditText. The way I do it is by lexing text into tokens and each token has value and type, the same type has the same color. After that, I load those tokens into my EditText and color them, however, a wired bug happens.
This is an example of the bug:
Everything is coloring ok at the begining
This happens after:
This is weird because my lexer works fine, this is the console output:
ColoredToken{key='KEYWORD', value='int'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='NAME', value='a'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='SIGNS', value='='}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='BID', value='10'}
ColoredToken{key='KEYWORD', value='if'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='SIGNS', value='('}
ColoredToken{key='SIGNS', value=')'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='SIGNS', value='{'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='KEYWORD', value='int'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='NAME', value='b'}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='SIGNS', value='='}
ColoredToken{key='SPACE', value=' '}
ColoredToken{key='BID', value='10'}
ColoredToken{key='SIGNS', value='}'}
Now for you to understand, the blue color is for names as in the first picture, white for keywords and signs, problem is that after typing for a while everything starts turning in blue, while tokens being correct.
This is the code for coloring and loading tokens:
public static SpannableStringBuilder runCodeColor(String codeString) {
ArrayList<String> code = new ArrayList<>(Arrays.asList(codeString.split("\n")));
SpannableStringBuilder coloredCode = new SpannableStringBuilder("");
int newLines = countNewLine(codeString);
for (int i = 0; i < code.size(); i++) {
ArrayList<ColoredToken> tmpTokens = new ArrayList<>(ColoredLexer.lexer(code.get(i)));
for (int j = 0; j < tmpTokens.size(); j++) {
coloredCode.append(tmpTokens.get(j).getValue());
coloredCode.setSpan(tmpTokens.get(j).getDarkColorSpan(), coloredCode.length() - tmpTokens.get(j).getValue().length(), coloredCode.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
System.out.println(tmpTokens.get(j).toString());
}
if (newLines > 0) {
coloredCode.append("\n");
newLines--;
}
}
return coloredCode;
}
If needed I can provide lexer code, but as stated above the tokens are alright, coloring is what's problematic.
EDIT:
If someone knows better ways of coloring/theming the code it would be great if you could provide some sample code and an explanation of how it works.
答案1
得分: 1
尝试使用文本处理器,这样您就不必自己实现,甚至可以对其进行训练,以适应您自己的语言。
英文:
Try to use textprocessor so that you do not have to implement this yourself, you can even train it for your own language
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论