2023年6月1日17:49:06
parent
6152cf10e3
commit
ccf0c9c774
|
|
@ -1,6 +1,7 @@
|
|||
package com.zhangmeng.tools.controller;
|
||||
|
||||
import com.zhangmeng.tools.components.RecursiveFileList;
|
||||
import com.zhangmeng.tools.languages.ProcessGO;
|
||||
import com.zhangmeng.tools.languages.ProcessJava;
|
||||
import com.zhangmeng.tools.utils.AlertUtils;
|
||||
import javafx.event.ActionEvent;
|
||||
|
|
@ -38,7 +39,16 @@ import java.util.regex.Pattern;
|
|||
@Slf4j
|
||||
public class FileEditController {
|
||||
|
||||
public enum Type{
|
||||
|
||||
JAVA,
|
||||
HTML,
|
||||
GO,
|
||||
JS,
|
||||
CSS,
|
||||
}
|
||||
|
||||
@FXML
|
||||
public SplitPane splitPane;
|
||||
|
||||
private CodeArea codeArea;
|
||||
|
|
@ -51,7 +61,7 @@ public class FileEditController {
|
|||
URL resource = this.getClass().getClassLoader().getResource("css/code.css");
|
||||
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
|
||||
codeArea.textProperty().addListener((obs, oldText, newText) -> {
|
||||
codeArea.setStyleSpans(0, computeHighlighting(newText));
|
||||
codeArea.setStyleSpans(0, computeHighlighting(newText,Type.JAVA));
|
||||
});
|
||||
codeArea.getStylesheets().add(resource.toExternalForm());
|
||||
splitPane.getItems().add(0,recursiveFileList);
|
||||
|
|
@ -65,7 +75,7 @@ public class FileEditController {
|
|||
open_file_dir();
|
||||
}
|
||||
|
||||
public void open_file(File file){
|
||||
public void open_file(File file,Type type){
|
||||
byte[] bytes = new byte[0];
|
||||
try {
|
||||
bytes = Files.readAllBytes(file.toPath());
|
||||
|
|
@ -75,6 +85,12 @@ public class FileEditController {
|
|||
String file_content = new String(bytes, StandardCharsets.UTF_8);
|
||||
codeArea.clear();
|
||||
codeArea.replaceText(0, 0, file_content);
|
||||
|
||||
codeArea.textProperty().addListener((obs, oldText, newText) -> {
|
||||
codeArea.setStyleSpans(0, computeHighlighting(newText,type));
|
||||
});
|
||||
|
||||
codeArea.setStyleSpans(0, computeHighlighting(file_content,type));
|
||||
}
|
||||
|
||||
public void open_file_dir(){
|
||||
|
|
@ -136,7 +152,12 @@ public class FileEditController {
|
|||
// flag = true;
|
||||
// }
|
||||
if (flag){
|
||||
open_file(newValue.getValue());
|
||||
Type type = Type.JAVA;
|
||||
if (extension.equals("go")){
|
||||
type = Type.GO;
|
||||
}
|
||||
|
||||
open_file(newValue.getValue(),type);
|
||||
}else {
|
||||
AlertUtils.alert_warning("该文件不支持!");
|
||||
}
|
||||
|
|
@ -151,8 +172,13 @@ public class FileEditController {
|
|||
}
|
||||
}
|
||||
|
||||
public static StyleSpans<Collection<String>> computeHighlighting(String text) {
|
||||
public static StyleSpans<Collection<String>> computeHighlighting(String text,Type type) {
|
||||
Matcher matcher = ProcessJava.PATTERN.matcher(text);
|
||||
if (type.equals(Type.GO)){
|
||||
matcher = ProcessGO.PATTERN.matcher(text);
|
||||
}
|
||||
|
||||
|
||||
int lastKwEnd = 0;
|
||||
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
|
||||
while(matcher.find()) {
|
||||
|
|
@ -166,6 +192,8 @@ public class FileEditController {
|
|||
matcher.group("COMMENT") != null ? "comment" :
|
||||
matcher.group("ANNOTATION") != null ? "annotation" :
|
||||
matcher.group("PARAMS") != null ? "parameter" :
|
||||
matcher.group("METHOD") != null ? "method" :
|
||||
matcher.group("KEYWORD2") != null ? "method2" :
|
||||
null; /* never happens */
|
||||
assert styleClass != null;
|
||||
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
package com.zhangmeng.tools.languages;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-06-01 16:45
|
||||
*/
|
||||
public class ProcessGO {
|
||||
|
||||
public static final String[] KEYWORDS = new String[]{
|
||||
"break", "default", "func", "interface", "select",
|
||||
"case", "defer", "go", "map", "struct",
|
||||
"chan", "else", "goto", "package", "switch",
|
||||
"const", "fallthrough", "if", "range", "type",
|
||||
"continue", "for", "import", "return", "var",
|
||||
};
|
||||
|
||||
public static final String[] KEYWORDS2 = new String[]{
|
||||
"true", "false", "iota", "nil",
|
||||
"int", "int8", "int16", "int32", "int64",
|
||||
"uint", "uint8", "uint16", "uint32", "uint64", "uintptr",
|
||||
"float32", "float64", "complex128", "complex64",
|
||||
"bool", "byte", "rune", "string", "error",
|
||||
"make", "len", "cap", "new", "append", "copy", "close", "delete",
|
||||
"complexrealimag",
|
||||
"panic", ","
|
||||
};
|
||||
|
||||
public static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
|
||||
public static final String KEYWORD_PATTERN2 = "\\b(" + String.join("|", KEYWORDS2) + ")\\b";
|
||||
public static final String PAREN_PATTERN = "\\(|\\)";
|
||||
public static final String BRACE_PATTERN = "\\{|\\}";
|
||||
public static final String BRACKET_PATTERN = "\\[|\\]";
|
||||
public static final String SEMICOLON_PATTERN = "\\;";
|
||||
public static final String STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\"";
|
||||
public static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";
|
||||
public static final String ANNOTATION_PATTERN = "@[a-zA-Z]+";
|
||||
public static final String PARAMS_PATTERN = "\\\\b([a-zA-Z]+)\\\\s*=\\\\s*([a-zA-Z0-9]+)\\\\b";
|
||||
public static final String METHOD_PATTERN = "\\b(func|type|struct)\\s+(\\w+)\\b";
|
||||
|
||||
public static final Pattern PATTERN = Pattern.compile(
|
||||
"(?<KEYWORD>" + KEYWORD_PATTERN + ")"
|
||||
+ "|(?<PAREN>" + PAREN_PATTERN + ")"
|
||||
+ "|(?<BRACE>" + BRACE_PATTERN + ")"
|
||||
+ "|(?<BRACKET>" + BRACKET_PATTERN + ")"
|
||||
+ "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
|
||||
+ "|(?<STRING>" + STRING_PATTERN + ")"
|
||||
+ "|(?<COMMENT>" + COMMENT_PATTERN + ")"
|
||||
+ "|(?<ANNOTATION>" + ANNOTATION_PATTERN + ")"
|
||||
+ "|(?<PARAMS>" + PARAMS_PATTERN + ")"
|
||||
+ "|(?<METHOD>" + METHOD_PATTERN + ")"
|
||||
+ "|(?<KEYWORD2>" + KEYWORD_PATTERN2 + ")"
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
|
@ -31,6 +31,7 @@ public class ProcessJava {
|
|||
public static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";
|
||||
public static final String ANNOTATION_PATTERN = "@[a-zA-Z]+";
|
||||
public static final String PARAMS_PATTERN = "\\\\b([a-zA-Z]+)\\\\s*=\\\\s*([a-zA-Z0-9]+)\\\\b";
|
||||
public static final String METHOD_PATTERN = "\\w+\\((\\w+,\\s*)*\\w*\\)";
|
||||
|
||||
public static final Pattern PATTERN = Pattern.compile(
|
||||
"(?<KEYWORD>" + KEYWORD_PATTERN + ")"
|
||||
|
|
@ -42,6 +43,7 @@ public class ProcessJava {
|
|||
+ "|(?<COMMENT>" + COMMENT_PATTERN + ")"
|
||||
+ "|(?<ANNOTATION>" + ANNOTATION_PATTERN + ")"
|
||||
+ "|(?<PARAMS>" + PARAMS_PATTERN + ")"
|
||||
+ "|(?<METHOD>" + METHOD_PATTERN + ")"
|
||||
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@
|
|||
/*-*/
|
||||
/*字符串的样式*/
|
||||
.code-area .comment {
|
||||
-fx-fill: #cbcacb;
|
||||
-fx-fill: #699f45;
|
||||
}
|
||||
|
||||
/*-*/
|
||||
|
|
@ -155,6 +155,11 @@
|
|||
-fx-text-fill: red;
|
||||
}
|
||||
|
||||
.code-area .method2 {
|
||||
-fx-fill: #b76bd5;
|
||||
-fx-text-fill: red;
|
||||
}
|
||||
|
||||
/*-*/
|
||||
/*方法的样式*/
|
||||
.code-area .constructor {
|
||||
|
|
|
|||
Loading…
Reference in New Issue