fix(gui): don't highlight whitespaces and special symbols (#1429)

This commit is contained in:
Skylot 2022-03-28 19:21:05 +01:00
parent e5fa818b5c
commit e4f4c1b84a
No known key found for this signature in database
GPG Key ID: 1E23F5B52567AA39

View File

@ -25,6 +25,7 @@ import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.TokenTypes;
import org.fife.ui.rtextarea.SearchContext;
import org.fife.ui.rtextarea.SearchEngine;
import org.jetbrains.annotations.Nullable;
@ -196,13 +197,37 @@ public abstract class AbstractCodeArea extends RSyntaxTextArea {
public String getWordByPosition(int pos) {
try {
Token token = modelToToken(pos);
if (token != null) {
return token.getLexeme();
}
return getWordFromToken(token);
} catch (Exception e) {
LOG.error("Failed to get word at pos: {}", pos, e);
return null;
}
}
@Nullable
private static String getWordFromToken(@Nullable Token token) {
if (token == null) {
return null;
}
switch (token.getType()) {
case TokenTypes.NULL:
case TokenTypes.WHITESPACE:
case TokenTypes.SEPARATOR:
case TokenTypes.OPERATOR:
return null;
case TokenTypes.IDENTIFIER:
if (token.length() == 1) {
char ch = token.charAt(0);
if (ch == ';' || ch == '.') {
return null;
}
}
return token.getLexeme();
default:
return token.getLexeme();
}
return null;
}
/**