Skip to content

Commit d510ac8

Browse files
committed
A simpler solution: use |-separated specific keywords in begin but surround it with optional dots in terminators. This eliminates the need to compile beginKeywords and to check against them in subMode.
1 parent d794611 commit d510ac8

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/highlight.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,17 @@ function() {
170170
return;
171171
mode.compiled = true;
172172

173-
function compileKeywords(keywords) {
174-
var result = {};
173+
mode.keywords = mode.keywords || mode.beginKeywords;
174+
if (mode.keywords) {
175+
var compiled_keywords = {};
175176

176177
function flatten(className, str) {
177178
if (language.case_insensitive) {
178179
str = str.toLowerCase();
179180
}
180181
str.split(' ').forEach(function(kw) {
181182
var pair = kw.split('|');
182-
result[pair[0]] = [className, pair[1] ? Number(pair[1]) : 1];
183+
compiled_keywords[pair[0]] = [className, pair[1] ? Number(pair[1]) : 1];
183184
});
184185
}
185186

@@ -190,19 +191,14 @@ function() {
190191
flatten(className, keywords[className]);
191192
});
192193
}
193-
return result;
194+
mode.keywords = compiled_keywords;
194195
}
195-
196196
mode.lexemesRe = langRe(mode.lexemes || /[A-Za-z0-9_\.]+/, true);
197-
mode.keywords = mode.keywords || mode.beginKeywords;
198-
if (mode.keywords) {
199-
mode.keywords = compileKeywords(mode.keywords);
200-
}
201197

202198
if (parent) {
203199
if (mode.beginKeywords) {
200+
mode.begin = mode.beginKeywords.split(' ').join('|');
204201
mode.beginKeywords = compileKeywords(mode.beginKeywords);
205-
mode.begin = mode.lexemes || /[A-Za-z0-9_\.]+/;
206202
}
207203
mode.beginRe = langRe(mode.begin ? mode.begin : '\\B|\\b');
208204
if (!mode.end && !mode.endsWithParent)
@@ -237,7 +233,7 @@ function() {
237233

238234
var terminators = [];
239235
mode.contains.forEach(function(c) {
240-
terminators.push(reStr(c.begin));
236+
terminators.push(reStr(c.beginKeywords ? '\\.?\\b(' + c.begin + ')\\b\\.?' : c.begin));
241237
});
242238
if (mode.terminator_end) {
243239
terminators.push(reStr(mode.terminator_end));
@@ -266,7 +262,7 @@ function() {
266262

267263
function subMode(lexeme, mode) {
268264
for (var i = 0; i < mode.contains.length; i++) {
269-
if (testRe(mode.contains[i].beginRe, lexeme) && (!mode.contains[i].beginKeywords || mode.contains[i].beginKeywords.hasOwnProperty(lexeme))) {
265+
if (testRe(mode.contains[i].beginRe, lexeme)) {
270266
return mode.contains[i];
271267
}
272268
}

src/languages/java.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,9 @@ function(hljs) {
3535
begin: '\\.' + hljs.UNDERSCORE_IDENT_RE, relevance: 0
3636
},
3737
{
38-
className: 'container',
3938
beginKeywords: 'protected public private', end: /[{;=]/,
4039
keywords: KEYWORDS,
4140
contains: [
42-
{
43-
begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true,
44-
contains: [
45-
hljs.UNDERSCORE_TITLE_MODE
46-
]
47-
},
4841
{
4942
className: 'class',
5043
beginKeywords: 'class interface', endsWithParent: true,
@@ -56,6 +49,12 @@ function(hljs) {
5649
},
5750
hljs.UNDERSCORE_TITLE_MODE
5851
]
52+
},
53+
{
54+
begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true,
55+
contains: [
56+
hljs.UNDERSCORE_TITLE_MODE
57+
]
5958
}
6059
]
6160
},

0 commit comments

Comments
 (0)