From 25d86b6d27b552b682cd257b4ca388f02786cd89 Mon Sep 17 00:00:00 2001 From: Azat Alimov <32402726+mkslanc@users.noreply.github.com> Date: Mon, 16 Oct 2023 11:27:48 +0400 Subject: [PATCH] Update java highlight mode to support 21 version features (#5343) * update java highlight mode to 21 version * re-generate tokens * allow user-defined template processors * highlight record name as identifier --- demo/kitchen-sink/docs/java.java | 21 ++++ src/mode/_test/tokens_drools.json | 14 +-- src/mode/_test/tokens_java.json | 169 ++++++++++++++++++++++---- src/mode/_test/tokens_jsp.json | 12 +- src/mode/java_highlight_rules.js | 189 ++++++++++++++++++++++++------ 5 files changed, 335 insertions(+), 70 deletions(-) diff --git a/demo/kitchen-sink/docs/java.java b/demo/kitchen-sink/docs/java.java index 4e036b2f005..aa16571416c 100644 --- a/demo/kitchen-sink/docs/java.java +++ b/demo/kitchen-sink/docs/java.java @@ -9,6 +9,7 @@ public class InfiniteLoop { * Taken from: * http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ */ + @Override public static void main(String[] args) { double d = Double.parseDouble("2.2250738585072012e-308"); @@ -16,3 +17,23 @@ public static void main(String[] args) { System.out.println("Value: " + d); } } + +String name = "Joan"; String info = STR."My name is \{name}"; + +STR."Today's weather is \{ getFeelsLike() }, with a temperature of \{ getTemperature()++ } degrees \{ getUnit() }" + +String nestedMultilineTemplates() { + return STR.""" + { + "outerKey1": "outerValue1", + "nestedTemplate": "\{ + STR.""" + { + "innerKey": "\{innerValue.get()}" + } + """ + }", + "outerKey2": "outerValue2" + } + """; +} diff --git a/src/mode/_test/tokens_drools.json b/src/mode/_test/tokens_drools.json index 8df67565899..e312d03d442 100644 --- a/src/mode/_test/tokens_drools.json +++ b/src/mode/_test/tokens_drools.json @@ -449,7 +449,7 @@ ["text"," "], ["identifier","a"], ["text","."], - ["identifier","toString"], + ["entity.name.function","toString"], ["lparen","("], ["rparen",")"], ["text",";"] @@ -462,7 +462,7 @@ ["lparen","("], ["identifier","$s"], ["text","."], - ["identifier","trim"], + ["entity.name.function","trim"], ["lparen","("], ["rparen","))"], ["text",";"] @@ -566,7 +566,7 @@ ["text","."], ["identifier","out"], ["text","."], - ["identifier","println"], + ["entity.name.function","println"], ["lparen","("], ["text"," "], ["identifier","$s"], @@ -681,7 +681,7 @@ ["text","."], ["identifier","out"], ["text","."], - ["identifier","println"], + ["entity.name.function","println"], ["lparen","("], ["text"," "], ["string","\"office is in the house\""], @@ -734,7 +734,7 @@ ["text","."], ["identifier","out"], ["text","."], - ["identifier","println"], + ["entity.name.function","println"], ["lparen","("], ["text"," "], ["string","\"Draw in the House\""], @@ -884,7 +884,7 @@ ["text","."], ["identifier","out"], ["text","."], - ["identifier","println"], + ["entity.name.function","println"], ["lparen","("], ["text"," "], ["string","\"monster right\""], @@ -909,7 +909,7 @@ ["text"," "], ["keyword","new"], ["text"," "], - ["identifier","Direction"], + ["entity.name.function","Direction"], ["lparen","("], ["identifier","$df"], ["text","."], diff --git a/src/mode/_test/tokens_java.json b/src/mode/_test/tokens_java.json index c55649a34b2..b8950f72c83 100644 --- a/src/mode/_test/tokens_java.json +++ b/src/mode/_test/tokens_java.json @@ -1,5 +1,27 @@ [[ "start", + ["keyword","import"], + ["text"," "], + ["identifier","java"], + ["text","."], + ["identifier","util"], + ["text","."], + ["identifier","ArrayList"], + ["text",";"] +],[ + "start", + ["keyword","import"], + ["text"," "], + ["identifier","java"], + ["text","."], + ["identifier","util"], + ["text","."], + ["identifier","Vector"], + ["text",";"] +],[ + "start" +],[ + "start", ["keyword","public"], ["text"," "], ["keyword","class"], @@ -28,6 +50,10 @@ ],[ "start", ["comment"," */"] +],[ + "start", + ["text"," "], + ["storage.type.annotation","@Override"] ],[ "start", ["text"," "], @@ -37,7 +63,7 @@ ["text"," "], ["keyword","void"], ["text"," "], - ["identifier","main"], + ["entity.name.function","main"], ["lparen","("], ["support.function","String"], ["lparen","["], @@ -58,7 +84,7 @@ ["text"," "], ["support.function","Double"], ["text","."], - ["identifier","parseDouble"], + ["entity.name.function","parseDouble"], ["lparen","("], ["string","\"2.2250738585072012e-308\""], ["rparen",")"], @@ -76,7 +102,7 @@ ["text","."], ["identifier","out"], ["text","."], - ["identifier","println"], + ["entity.name.function","println"], ["lparen","("], ["string","\"Value: \""], ["text"," "], @@ -87,42 +113,143 @@ ["text",";"] ],[ "start", - ["text"," "], - ["keyword","int"], + ["text"," "], + ["rparen","}"] +],[ + "start", + ["rparen","}"] +],[ + "start" +],[ + "start", + ["support.function","String"], ["text"," "], - ["identifier","exports"], + ["identifier","name"], ["text"," "], ["keyword.operator","="], ["text"," "], - ["constant.numeric","10"], - ["text",";"] -],[ - "start", - ["text"," "], - ["keyword","int"], + ["string","\"Joan\""], + ["text","; "], + ["support.function","String"], ["text"," "], - ["identifier","requires"], + ["identifier","info"], ["text"," "], ["keyword.operator","="], ["text"," "], - ["constant.numeric","20"], + ["identifier","STR"], + ["punctuation","."], + ["string","\""], + ["string","My name is "], + ["lparen","\\{"], + ["identifier","name"], + ["rparen","}"], + ["string","\""], ["text",";"] +],[ + "start" ],[ "start", - ["text"," "], - ["keyword","int"], + ["identifier","STR"], + ["punctuation","."], + ["string","\""], + ["string","Today's weather is "], + ["lparen","\\{"], + ["text"," "], + ["entity.name.function","getFeelsLike"], + ["lparen","("], + ["rparen",")"], ["text"," "], - ["identifier","open"], + ["rparen","}"], + ["string",", with a temperature of "], + ["lparen","\\{"], ["text"," "], - ["keyword.operator","="], + ["entity.name.function","getTemperature"], + ["lparen","("], + ["rparen",")"], + ["keyword.operator","++"], ["text"," "], - ["constant.numeric","30"], - ["text",";"] + ["rparen","}"], + ["string"," degrees "], + ["lparen","\\{"], + ["text"," "], + ["entity.name.function","getUnit"], + ["lparen","("], + ["rparen",")"], + ["text"," "], + ["rparen","}"], + ["string","\""] +],[ + "start" ],[ "start", + ["support.function","String"], + ["text"," "], + ["entity.name.function","nestedMultilineTemplates"], + ["lparen","("], + ["rparen",")"], + ["text"," "], + ["lparen","{"] +],[ + "punctuation", ["text"," "], - ["rparen","}"] + ["keyword","return"], + ["text"," "], + ["identifier","STR"], + ["punctuation","."], + ["string","\"\"\""] +],[ + "punctuation", + ["string"," {"] +],[ + "punctuation", + ["string"," \"outerKey1\": \"outerValue1\","] +],[ + ["lparen","punctuation"], + ["string"," \"nestedTemplate\": \""], + ["lparen","\\{"] +],[ + ["punctuation","lparen","lparen","punctuation"], + ["text"," "], + ["identifier","STR"], + ["punctuation","."], + ["string","\"\"\""] +],[ + ["punctuation","lparen","lparen","punctuation"], + ["string"," {"] +],[ + ["punctuation","lparen","lparen","punctuation"], + ["string"," \"innerKey\": \""], + ["lparen","\\{"], + ["identifier","innerValue"], + ["text","."], + ["entity.name.function","get"], + ["lparen","("], + ["rparen",")}"], + ["string","\""] +],[ + ["punctuation","lparen","lparen","punctuation"], + ["string"," }"] +],[ + ["lparen","punctuation"], + ["string"," \"\"\""] +],[ + "punctuation", + ["text"," "], + ["rparen","}"], + ["string","\","] +],[ + "punctuation", + ["string"," \"outerKey2\": \"outerValue2\""] +],[ + "punctuation", + ["string"," }"] +],[ + "start", + ["string"," \"\"\""], + ["text",";"] ],[ "start", ["rparen","}"] +],[ + "start" ]] \ No newline at end of file diff --git a/src/mode/_test/tokens_jsp.json b/src/mode/_test/tokens_jsp.json index 75a5043c617..93808afb620 100644 --- a/src/mode/_test/tokens_jsp.json +++ b/src/mode/_test/tokens_jsp.json @@ -94,11 +94,11 @@ ["text","."], ["identifier","util"], ["text","."], - ["identifier","Date"], + ["entity.name.function","Date"], ["lparen","("], ["rparen","))"], ["text","."], - ["identifier","toLocaleString"], + ["entity.name.function","toLocaleString"], ["lparen","("], ["rparen",")"], ["meta.tag","%>"] @@ -333,7 +333,7 @@ ["text"," "], ["variable.language","request"], ["text","."], - ["identifier","getParameterValues"], + ["entity.name.function","getParameterValues"], ["lparen","("], ["string","\"id\""], ["rparen",")"], @@ -367,7 +367,7 @@ ["text"," "], ["variable.language","out"], ["text","."], - ["identifier","println"], + ["entity.name.function","println"], ["lparen","("], ["string","\"You have selected: \""], ["rparen",")"], @@ -404,7 +404,7 @@ ["text"," "], ["variable.language","out"], ["text","."], - ["identifier","println"], + ["entity.name.function","println"], ["lparen","("], ["identifier","select"], ["lparen","["], @@ -433,4 +433,4 @@ ["meta.tag.punctuation.end-tag-open.xml",""] -]] +]] \ No newline at end of file diff --git a/src/mode/java_highlight_rules.js b/src/mode/java_highlight_rules.js index b44f22e1d97..e01998ba769 100644 --- a/src/mode/java_highlight_rules.js +++ b/src/mode/java_highlight_rules.js @@ -5,6 +5,7 @@ var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocComme var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules; var JavaHighlightRules = function() { + var identifierRe = "[a-zA-Z_$][a-zA-Z0-9_$]*"; // taken from http://download.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html var keywords = ( @@ -18,7 +19,10 @@ var JavaHighlightRules = function() { "char|final|interface|static|void|" + "class|finally|long|strictfp|volatile|" + "const|float|native|super|while|" + - "var" + "var|exports|opens|requires|uses|yield|" + + "module|permits|(?:non\\-)?sealed|var|" + + "provides|to|when|" + + "open|record|transitive|with" ); var buildinConstants = ("null|Infinity|NaN|undefined"); @@ -53,7 +57,6 @@ var JavaHighlightRules = function() { var keywordMapper = this.createKeywordMapper({ "variable.language": "this", - "keyword": keywords, "constant.language": buildinConstants, "support.function": langClasses }, "identifier"); @@ -72,22 +75,11 @@ var JavaHighlightRules = function() { token : "comment", // multi line comment regex : "\\/\\*", next : "comment" - }, { - token : "string", // single line - regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]' - }, { - token : "string", // single line - regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']" - }, { - token : "constant.numeric", // hex - regex : /0(?:[xX][0-9a-fA-F][0-9a-fA-F_]*|[bB][01][01_]*)[LlSsDdFfYy]?\b/ - }, { - token : "constant.numeric", // float - regex : /[+-]?\d[\d_]*(?:(?:\.[\d_]*)?(?:[eE][+-]?[\d_]+)?)?[LlSsDdFfYy]?\b/ - }, { - token : "constant.language.boolean", - regex : "(?:true|false)\\b" - }, { + }, + {include: "multiline-strings"}, + {include: "strings"}, + {include: "constants"}, + { regex: "(open(?:\\s+))?module(?=\\s*\\w)", token: "keyword", next: [{ @@ -118,24 +110,8 @@ var JavaHighlightRules = function() { regex: "", // exit if there is anything else next: "start" }] - }, { - token : keywordMapper, - // TODO: Unicode escape sequences - // TODO: Unicode identifiers - regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b" - }, { - token : "keyword.operator", - regex : "!|\\$|%|&|\\||\\^|\\*|\\/|\\-\\-|\\-|\\+\\+|\\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?|\\:|\\*=|\\/=|%=|\\+=|\\-=|&=|\\|=|\\^=|\\b(?:in|instanceof|new|delete|typeof|void)" - }, { - token : "lparen", - regex : "[[({]" - }, { - token : "rparen", - regex : "[\\])}]" - }, { - token : "text", - regex : "\\s+" - } + }, + {include: "statements"} ], "comment" : [ { @@ -145,6 +121,147 @@ var JavaHighlightRules = function() { }, { defaultToken : "comment" } + ], + "strings": [ + { + token: ["punctuation", "string"], + regex: /(\.)(")/, + push: [ + { + token: "lparen", + regex: /\\\{/, + push: [ + { + token: "text", + regex: /$/, + next: "start" + }, { + token: "rparen", + regex: /}/, + next: "pop" + }, { + include: "strings" + }, { + include: "constants" + }, { + include: "statements" + } + ] + }, { + token: "string", + regex: /"/, + next: "pop" + }, { + defaultToken: "string" + } + ] + }, { + token : "string", // single line + regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]' + }, { + token : "string", // single line + regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']" + } + ], + "multiline-strings": [ + { + token: ["punctuation", "string"], + regex: /(\.)(""")/, + push: [ + { + token: "string", + regex: '"""', + next: "pop" + }, { + token: "lparen", + regex: /\\\{/, + push: [ + { + token: "text", + regex: /$/, + next: "start" + }, { + token: "rparen", + regex: /}/, + next: "pop" + }, { + include: "multiline-strings" + }, { + include: "strings" + }, { + include: "constants" + }, { + include: "statements" + } + ] + }, { + token: "constant.language.escape", + regex: /\\./ + }, { + defaultToken: "string" + } + ] + }, + { + token: "string", + regex: '"""', + push: [ + { + token: "string", + regex: '"""', + next: "pop" + }, { + token : "constant.language.escape", + regex : /\\./ + }, { + defaultToken: "string" + } + ] + } + ], + "constants": [ + { + token: "constant.numeric", // hex + regex: /0(?:[xX][0-9a-fA-F][0-9a-fA-F_]*|[bB][01][01_]*)[LlSsDdFfYy]?\b/ + }, { + token: "constant.numeric", // float + regex: /[+-]?\d[\d_]*(?:(?:\.[\d_]*)?(?:[eE][+-]?[\d_]+)?)?[LlSsDdFfYy]?\b/ + }, { + token: "constant.language.boolean", + regex: "(?:true|false)\\b" + } + ], + "statements": [ + { + token: ["keyword", "text", "identifier"], + regex: "(record)(\\s+)("+identifierRe+")\\b" + }, + { + token: "keyword", + regex: "(?:" + keywords + ")\\b" + }, {//annotations + token: "storage.type.annotation", + regex: "@" + identifierRe + "\\b" + }, { + token: "entity.name.function", + regex: identifierRe + "(?=\\()" + }, { + token: keywordMapper, // TODO: Unicode escape sequences + // TODO: Unicode identifiers + regex: identifierRe + "\\b" + }, { + token: "keyword.operator", + regex: "!|\\$|%|&|\\||\\^|\\*|\\/|\\-\\-|\\-|\\+\\+|\\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?|\\:|\\*=|\\/=|%=|\\+=|\\-=|&=|\\|=|\\^=|\\b(?:in|instanceof|new|delete|typeof|void)" + }, { + token: "lparen", + regex: "[[({]" + }, { + token: "rparen", + regex: "[\\])}]" + }, { + token: "text", + regex: "\\s+" + } ] };