File tree 4 files changed +63
-6
lines changed
4 files changed +63
-6
lines changed Original file line number Diff line number Diff line change @@ -246,11 +246,30 @@ const HLJS = function(hljs) {
246
246
modeBuffer = '' ;
247
247
}
248
248
249
+ /**
250
+ * @param {Mode } mode
251
+ * @param {EnhancedMatch } match
252
+ */
253
+ function emitMultiClass ( mode , match ) {
254
+ let i = 1 ;
255
+ while ( match [ i ] ) {
256
+ const klass = mode . className [ i ] ;
257
+ const text = match [ i ] ;
258
+ if ( klass ) { emitter . addKeyword ( text , klass ) ; } else { emitter . addText ( text ) ; }
259
+ i ++ ;
260
+ }
261
+ }
262
+
249
263
/**
250
264
* @param {Mode } mode - new mode to start
265
+ * @param {EnhancedMatch } match
251
266
*/
252
- function startNewMode ( mode ) {
253
- if ( mode . className ) {
267
+ function startNewMode ( mode , match ) {
268
+ if ( mode . isMultiClass ) {
269
+ // at this point modeBuffer should just be the match
270
+ modeBuffer = "" ;
271
+ emitMultiClass ( mode , match ) ;
272
+ } else if ( mode . className ) {
254
273
emitter . openNode ( language . classNameAliases [ mode . className ] || mode . className ) ;
255
274
}
256
275
top = Object . create ( mode , { parent : { value : top } } ) ;
@@ -340,7 +359,7 @@ const HLJS = function(hljs) {
340
359
modeBuffer = lexeme ;
341
360
}
342
361
}
343
- startNewMode ( newMode ) ;
362
+ startNewMode ( newMode , match ) ;
344
363
// if (mode["after:begin"]) {
345
364
// let resp = new Response(mode);
346
365
// mode["after:begin"](match, resp);
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ Website: https://www.vim.org
6
6
Category: scripting
7
7
*/
8
8
9
+ import * as regex from '../lib/regex.js' ;
10
+
9
11
export default function ( hljs ) {
10
12
return {
11
13
name : 'Vim Script' ,
@@ -99,8 +101,15 @@ export default function(hljs) {
99
101
begin : / [ b w t g l s a v ] : [ \w \d _ ] + /
100
102
} ,
101
103
{
102
- className : 'function' ,
103
- beginKeywords : 'function function!' ,
104
+ begin : [
105
+ / f u n c t i o n | f u n c t i o n ! / ,
106
+ / \s + / ,
107
+ hljs . IDENT_RE
108
+ ] ,
109
+ className : {
110
+ 1 : "keyword" ,
111
+ 3 : "title"
112
+ } ,
104
113
end : '$' ,
105
114
relevance : 0 ,
106
115
contains : [
Original file line number Diff line number Diff line change
1
+ /* eslint-disable no-throw-literal */
2
+ import * as regex from "../regex.js" ;
3
+
4
+ const MultiClassError = new Error ( ) ;
5
+
6
+ /**
7
+ *
8
+ * @param {CompiledMode } mode
9
+ */
10
+ export function MultiClass ( mode ) {
11
+ if ( ! Array . isArray ( mode . begin ) ) return ;
12
+
13
+ if ( mode . skip || mode . excludeBegin || mode . returnBegin ) {
14
+ console . error ( "skip, excludeBegin, returnBegin not compatible with multi-class" )
15
+ throw MultiClassError ;
16
+ }
17
+
18
+ if ( typeof mode . className !== "object" ) {
19
+ console . error ( "className must be object or array" ) ;
20
+ throw MultiClassError ;
21
+ }
22
+
23
+ const items = mode . begin . map ( x => regex . concat ( "(" , x , ")" ) ) ;
24
+ mode . begin = regex . concat ( ...items ) ;
25
+ mode . isMultiClass = true ;
26
+
27
+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import * as regex from './regex.js';
2
2
import { inherit } from './utils.js' ;
3
3
import * as EXT from "./compiler_extensions.js" ;
4
4
import { compileKeywords } from "./compile_keywords.js" ;
5
+ import { MultiClass } from "./ext/multi_class.js" ;
5
6
6
7
// compilation
7
8
@@ -284,7 +285,8 @@ export function compileLanguage(language, { plugins }) {
284
285
[
285
286
// do this early so compiler extensions generally don't have to worry about
286
287
// the distinction between match/begin
287
- EXT . compileMatch
288
+ EXT . compileMatch ,
289
+ MultiClass
288
290
] . forEach ( ext => ext ( mode , parent ) ) ;
289
291
290
292
language . compilerExtensions . forEach ( ext => ext ( mode , parent ) ) ;
You can’t perform that action at this time.
0 commit comments