-
Notifications
You must be signed in to change notification settings - Fork 3.7k
enh(parser) multi-class in a single mode #3081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
0b022f2
5e745cc
e3b164b
b70b09a
3fa8ae7
541e037
cf5cc00
d1d2d15
ca7ed22
08e7515
ce1260d
287421d
657521a
4022f8b
edac3fa
9e02c2d
16c3585
cb1c27f
c62c36a
990874d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* eslint-disable no-throw-literal */ | ||
import * as logger from "../../lib/logger.js"; | ||
import * as regex from "../regex.js"; | ||
joshgoebel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const MultiClassError = new Error(); | ||
|
||
/** | ||
* | ||
* @param {CompiledMode} mode | ||
*/ | ||
export function MultiClass(mode) { | ||
if (!Array.isArray(mode.begin)) return; | ||
joshgoebel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (mode.skip || mode.excludeBegin || mode.returnBegin) { | ||
logger.error("skip, excludeBegin, returnBegin not compatible with multi-class"); | ||
throw MultiClassError; | ||
} | ||
|
||
if (typeof mode.className !== "object") { | ||
logger.error("className must be object"); | ||
throw MultiClassError; | ||
} | ||
|
||
const items = mode.begin.map(x => regex.concat("(", x, ")")); | ||
mode.begin = regex.concat(...items); | ||
mode.isMultiClass = true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
const hljs = require('../../build'); | ||
|
||
describe('multi-class matchers', () => { | ||
before(() => { | ||
const grammar = function() { | ||
return { | ||
contains: [ | ||
{ | ||
begin: ["a", "b", "c"], | ||
className: { | ||
1: "a", | ||
3: "c" | ||
}, | ||
contains: [ | ||
{ | ||
match: "def", | ||
className: "def" | ||
} | ||
] | ||
}, | ||
{ | ||
className: "carrot", | ||
begin: /\^\^\^/, | ||
end: /\^\^\^/, | ||
contains: [ | ||
{ | ||
begin: ["a", "b", "c"], | ||
className: { | ||
1: "a", | ||
3: "c" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
match: [ | ||
/func/, | ||
/\(\)/, | ||
/{.*}/ | ||
], | ||
className: { | ||
1: "keyword", | ||
2: "params", | ||
3: "body" | ||
} | ||
} | ||
] | ||
}; | ||
}; | ||
hljs.registerLanguage("test", grammar); | ||
}); | ||
after(() => { | ||
hljs.unregisterLanguage("test"); | ||
}); | ||
it('should support begin', () => { | ||
const code = "abcdef"; | ||
const result = hljs.highlight(code, { language: 'test' }); | ||
|
||
result.value.should.equal(`<span class="hljs-a">a</span>b<span class="hljs-c">c</span><span class="hljs-def">def</span>`); | ||
result.relevance.should.equal(2); | ||
}); | ||
it('basic functionality', () => { | ||
const code = "func(){ test }"; | ||
const result = hljs.highlight(code, { language: 'test' }); | ||
|
||
result.value.should.equal(`<span class="hljs-keyword">func</span><span class="hljs-params">()</span><span class="hljs-body">{ test }</span>`); | ||
result.relevance.should.equal(1); | ||
}); | ||
it('works inside a classified parent mode', () => { | ||
const code = "^^^what abc now^^^"; | ||
const result = hljs.highlight(code, { language: 'test' }); | ||
|
||
result.value.should.equal( | ||
`<span class="hljs-carrot">^^^what ` + | ||
`<span class="hljs-a">a</span>b<span class="hljs-c">c</span>` + | ||
` now^^^</span>`); | ||
}) | ||
}); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -206,7 +206,8 @@ type CompiledMode = Omit<Mode, 'contains'> & | |||||
endRe: RegExp | ||||||
illegalRe: RegExp | ||||||
matcher: any | ||||||
isCompiled: true | ||||||
isCompiled: true, | ||||||
isMultiClass?: boolean, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
If there's no time it'll ever There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps technically, but that bugs me though i can't explain it. This isn't the same as isCompiled IMHO. |
||||||
starts?: CompiledMode | ||||||
parent?: CompiledMode | ||||||
} | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.