@@ -85,16 +85,48 @@ export function getPlainClassMatchedPositionsForPug(codeSplit: string, matchedPl
8585 return result
8686}
8787
88+ export interface GetMatchedPositionsOptions {
89+ isPug ?: boolean
90+ /**
91+ * Regex to only limit the matched positions for certain code
92+ */
93+ includeRegex ?: RegExp [ ]
94+ /**
95+ * Regex to exclude the matched positions for certain code, excludeRegex has higher priority than includeRegex
96+ */
97+ excludeRegex ?: RegExp [ ]
98+ }
99+
88100export function getMatchedPositions (
89101 code : string ,
90102 matched : string [ ] ,
91103 extraAnnotations : HighlightAnnotation [ ] = [ ] ,
92- isPug = false ,
104+ options : GetMatchedPositionsOptions = { } ,
93105) {
94106 const result : ( readonly [ start : number , end : number , text : string ] ) [ ] = [ ]
95107 const attributify : RegExpMatchArray [ ] = [ ]
96108 const plain = new Set < string > ( )
97109
110+ const includeRanges : [ number , number ] [ ] = [ ]
111+ const excludeRanges : [ number , number ] [ ] = [ ]
112+
113+ if ( options . includeRegex ) {
114+ for ( const regex of options . includeRegex ) {
115+ for ( const match of code . matchAll ( regex ) )
116+ includeRanges . push ( [ match . index ! , match . index ! + match [ 0 ] . length ] )
117+ }
118+ }
119+ else {
120+ includeRanges . push ( [ 0 , code . length ] )
121+ }
122+
123+ if ( options . excludeRegex ) {
124+ for ( const regex of options . excludeRegex ) {
125+ for ( const match of code . matchAll ( regex ) )
126+ excludeRanges . push ( [ match . index ! , match . index ! + match [ 0 ] . length ] )
127+ }
128+ }
129+
98130 Array . from ( matched )
99131 . forEach ( ( v ) => {
100132 const match = isAttributifySelector ( v )
@@ -106,7 +138,9 @@ export function getMatchedPositions(
106138 highlightLessGreaterThanSign ( match [ 1 ] )
107139 plain . add ( match [ 1 ] )
108140 }
109- else { attributify . push ( match ) }
141+ else {
142+ attributify . push ( match )
143+ }
110144 } )
111145
112146 // highlight classes that includes `><`
@@ -124,7 +158,7 @@ export function getMatchedPositions(
124158 let start = 0
125159 code . split ( splitWithVariantGroupRE ) . forEach ( ( i ) => {
126160 const end = start + i . length
127- if ( isPug ) {
161+ if ( options . isPug ) {
128162 result . push ( ...getPlainClassMatchedPositionsForPug ( i , plain , start ) )
129163 }
130164 else {
@@ -172,9 +206,18 @@ export function getMatchedPositions(
172206 } )
173207 } )
174208
175- result . push ( ...extraAnnotations . map ( i => [ i . offset , i . offset + i . length , i . className ] as const ) )
209+ result
210+ . push ( ...extraAnnotations . map ( i => [ i . offset , i . offset + i . length , i . className ] as const ) )
176211
177- return result . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] )
212+ return result
213+ . filter ( ( [ start , end ] ) => {
214+ if ( excludeRanges . some ( ( [ s , e ] ) => start >= s && end <= e ) )
215+ return false
216+ if ( includeRanges . some ( ( [ s , e ] ) => start >= s && end <= e ) )
217+ return true
218+ return false
219+ } )
220+ . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] )
178221}
179222
180223// remove @unocss /transformer-directives transformer to get matched result from source code
@@ -183,7 +226,12 @@ const ignoreTransformers = [
183226 '@unocss/transformer-compile-class' ,
184227]
185228
186- export async function getMatchedPositionsFromCode ( uno : UnoGenerator , code : string , id = '' ) {
229+ export async function getMatchedPositionsFromCode (
230+ uno : UnoGenerator ,
231+ code : string ,
232+ id = '' ,
233+ options : GetMatchedPositionsOptions = { } ,
234+ ) {
187235 const s = new MagicString ( code )
188236 const tokens = new Set ( )
189237 const ctx = { uno, tokens } as any
@@ -201,5 +249,8 @@ export async function getMatchedPositionsFromCode(uno: UnoGenerator, code: strin
201249
202250 const { pug, code : pugCode } = await isPug ( uno , s . toString ( ) , id )
203251 const result = await uno . generate ( pug ? pugCode : s . toString ( ) , { preflights : false } )
204- return getMatchedPositions ( code , [ ...result . matched ] , annotations , pug )
252+ return getMatchedPositions ( code , [ ...result . matched ] , annotations , {
253+ isPug : pug ,
254+ ...options ,
255+ } )
205256}
0 commit comments