88import { transparent , classes , grandparents } from './theme.js'
99
1010// Source: <https://github.com/microsoft/vscode-textmate/blob/9157c7f/src/metadata.ts#L33-L35>
11+ // Also: <https://github.com/microsoft/vscode/blob/8ca37ee/src/vs/editor/common/encodedTokenAttributes.ts#L71>
1112const FONT_STYLE_MASK = 0b0000_0000_0000_0000_0111_1000_0000_0000
1213const FOREGROUND_MASK = 0b0000_0000_1111_1111_1000_0000_0000_0000
1314const BACKGROUND_MASK = 0b1111_1111_0000_0000_0000_0000_0000_0000
1415
1516// Source: <https://github.com/microsoft/vscode-textmate/blob/9157c7f/src/metadata.ts#L37-L42>
17+ // Also: <https://github.com/microsoft/vscode/blob/8ca37ee/src/vs/editor/common/encodedTokenAttributes.ts#L92-L94>
1618const FONT_STYLE_OFFSET = 11
1719const FOREGROUND_OFFSET = 15
1820const BACKGROUND_OFFSET = 24
@@ -28,42 +30,41 @@ export function parse(value, grammar, colors) {
2830 /** @type {Root } */
2931 const tree = { type : 'root' , children : [ ] }
3032 const search = / \r ? \n | \r / g
31- /** @type {StateStack| null } */
33+ /** @type {StateStack | null } */
3234 let stack = null
3335 let start = 0
3436
3537 while ( start < value . length ) {
3638 const match = search . exec ( value )
3739 const end = match ? match . index : value . length
3840
39- // Empty lines could still match `source` and be turned into a span.
40- // Ignore those.
41- if ( start !== end ) {
42- const { tokens, ruleStack} = grammar . tokenizeLine2 (
43- value . slice ( start , end ) ,
44- stack
45- )
46- let index = 0
47-
48- while ( index < tokens . length ) {
49- const tokenStart = start + tokens [ index ++ ]
50- const metadata = tokens [ index ++ ]
51- const tokenEnd = index < tokens . length ? start + tokens [ index ] : end
52- // Source: <https://github.com/microsoft/vscode-textmate/blob/9157c7f/src/metadata.ts#L71-L93>
53- const fg = ( metadata & FOREGROUND_MASK ) >>> FOREGROUND_OFFSET
54- const bg = ( metadata & BACKGROUND_MASK ) >>> BACKGROUND_OFFSET
55- const fs = ( metadata & FONT_STYLE_MASK ) >>> FONT_STYLE_OFFSET
56- /** @type {Root|Element } */
57- let scope = tree
58- scope = delveIfClassName ( scope , fontStyleToClass ( fs ) )
59- scope = delveIfClassName ( scope , colorToClass ( colors [ bg ] ) )
60- scope = delveIfClassName ( scope , colorToClass ( colors [ fg ] ) )
61- appendText ( scope , value . slice ( tokenStart , tokenEnd ) )
62- }
63-
64- stack = ruleStack
41+ // > 👉 **Important**: empty lines have to be tokenized, as some patterns
42+ // > look for them.
43+ const { tokens, ruleStack} = grammar . tokenizeLine2 (
44+ value . slice ( start , end ) ,
45+ stack
46+ )
47+ let index = 0
48+
49+ while ( index < tokens . length ) {
50+ const tokenStart = start + tokens [ index ++ ]
51+ const metadata = tokens [ index ++ ]
52+ const tokenEnd = index < tokens . length ? start + tokens [ index ] : end
53+ // Source: <https://github.com/microsoft/vscode-textmate/blob/9157c7f/src/metadata.ts#L71-L93>
54+ const fg = ( metadata & FOREGROUND_MASK ) >>> FOREGROUND_OFFSET
55+ const bg = ( metadata & BACKGROUND_MASK ) >>> BACKGROUND_OFFSET
56+ const fs = ( metadata & FONT_STYLE_MASK ) >>> FONT_STYLE_OFFSET
57+
58+ /** @type {Root|Element } */
59+ let scope = tree
60+ scope = delveIfClassName ( scope , fontStyleToClass ( fs ) )
61+ scope = delveIfClassName ( scope , colorToClass ( colors [ bg ] ) )
62+ scope = delveIfClassName ( scope , colorToClass ( colors [ fg ] ) )
63+ appendText ( scope , value . slice ( tokenStart , tokenEnd ) )
6564 }
6665
66+ stack = ruleStack
67+
6768 start = end
6869
6970 if ( match ) {
0 commit comments