1

我正在开发一个支持语法高亮的文本区域(基于 RichTextFX)。^当我试图创建正则表达式来突出显示 Diff 文件的语法时,我无法使用这样的方式突出显示多行

ADDITION_PATTERN = "^[\\+][^\n]*" ;

这是该类的完整代码。

public class Diff implements Language {

@Override
public Pattern generatePattern() {
    Pattern pattern;
    String ADDITION_PATTERN;
    ADDITION_PATTERN = "[\\+][^\n]*" ;
    String REM_PATTERN;
    REM_PATTERN = "[\\-][^\n]*";
    String ADDITION_ALT_PATTERN;
    ADDITION_ALT_PATTERN =  "\\>[^\n]*";
    String REM_ALT_PATTERN;
    REM_ALT_PATTERN =  "\\<[^\n]*";
    String DIFF_PATTERN;
    DIFF_PATTERN = "\\%[^\n]*";
    String CHANGE_PATTERN;
    CHANGE_PATTERN = "\\![^\n]*";
    String COMMON_PATTERN="\\@[^\n]*";
    pattern = Pattern.compile(
            "(?<ADD>" + ADDITION_PATTERN + ")"
            + "|(?<REM>" + REM_PATTERN + ")"
            + "|(?<ADDALT>" + ADDITION_ALT_PATTERN + ")"
            + "|(?<REMALT>" + REM_ALT_PATTERN + ")"
            + "|(?<DIFF>" + DIFF_PATTERN + ")"
            + "|(?<CHANGE>" + CHANGE_PATTERN + ")"
            + "|(?<COMMON>" + COMMON_PATTERN + ")"
    );
    return pattern;
}

@Override
public String getStyleClass(Matcher matcher) {
    return matcher.group("ADD") != null ? "add"
            : matcher.group("REM") != null ? "rem"
            : matcher.group("ADDALT") != null ? "add"
            : matcher.group("REMALT") != null ? "rem"
            : matcher.group("DIFF") != null ? "diff"
            : matcher.group("CHANGE") != null ? "change"
            : matcher.group("COMMON") != null ? "common"
            : null;
}

@Override
public ArrayList<String> getKeywords() {
    ArrayList<String> keywordList = new ArrayList<>();
    Collections.sort(keywordList);
    return keywordList;
}

} 

在通过任何判断之前,我不是一个仍在努力学习的专业程序员。:)

4

0 回答 0