@@ -233,13 +233,26 @@ def found_kconfig_search_directive(self) -> bool:
233
233
return self ._found
234
234
235
235
236
+ class KconfigRegexRole (XRefRole ):
237
+ """Role for creating links to Kconfig regex searches."""
238
+
239
+ def process_link (self , env : BuildEnvironment , refnode : nodes .Element , has_explicit_title : bool ,
240
+ title : str , target : str ) -> tuple [str , str ]:
241
+ # render as "normal" text when explicit title is provided, literal otherwise
242
+ if has_explicit_title :
243
+ self .innernodeclass = nodes .inline
244
+ else :
245
+ self .innernodeclass = nodes .literal
246
+ return title , target
247
+
248
+
236
249
class KconfigDomain (Domain ):
237
250
"""Kconfig domain"""
238
251
239
252
name = "kconfig"
240
253
label = "Kconfig"
241
254
object_types = {"option" : ObjType ("option" , "option" )}
242
- roles = {"option" : XRefRole ()}
255
+ roles = {"option" : XRefRole (), "option-regex" : KconfigRegexRole () }
243
256
directives = {"search" : KconfigSearch }
244
257
initial_data : dict [str , Any ] = {"options" : set ()}
245
258
@@ -259,20 +272,56 @@ def resolve_xref(
259
272
node : pending_xref ,
260
273
contnode : nodes .Element ,
261
274
) -> nodes .Element | None :
262
- match = [
263
- ( docname , anchor )
264
- for name , _ , _ , docname , anchor , _ in self .get_objects ( )
265
- if name == target
266
- ]
267
-
268
- if match :
269
- todocname , anchor = match [ 0 ]
270
-
271
- return make_refnode (
272
- builder , fromdocname , todocname , anchor , contnode , anchor
273
- )
275
+ if typ == "option-regex" :
276
+ # Handle regex search links
277
+ search_docname = self ._find_search_docname ( env )
278
+ if search_docname :
279
+ # Create a reference to the search page with the regex as a fragment
280
+ ref_uri = builder . get_relative_uri ( fromdocname , search_docname ) + f"#! { target } "
281
+ ref_node = nodes . reference ( '' , '' , refuri = ref_uri , internal = True )
282
+ ref_node . append ( contnode )
283
+ return ref_node
284
+ else :
285
+ # Fallback to plain text if no search page is found
286
+ return contnode
274
287
else :
275
- return None
288
+ # Handle regular option links
289
+ match = [
290
+ (docname , anchor )
291
+ for name , _ , _ , docname , anchor , _ in self .get_objects ()
292
+ if name == target
293
+ ]
294
+
295
+ if match :
296
+ todocname , anchor = match [0 ]
297
+
298
+ return make_refnode (
299
+ builder , fromdocname , todocname , anchor , contnode , anchor
300
+ )
301
+ else :
302
+ return None
303
+
304
+ def _find_search_docname (self , env : BuildEnvironment ) -> str | None :
305
+ """Find the document containing the kconfig search directive."""
306
+ # Cache the result to avoid repeated searches
307
+ if hasattr (env , '_kconfig_search_docname' ):
308
+ return env ._kconfig_search_docname
309
+
310
+ for docname in env .all_docs :
311
+ try :
312
+ doctree = env .get_doctree (docname )
313
+ visitor = _FindKconfigSearchDirectiveVisitor (doctree )
314
+ doctree .walk (visitor )
315
+ if visitor .found_kconfig_search_directive :
316
+ env ._kconfig_search_docname = docname
317
+ return docname
318
+ except Exception :
319
+ # Skip documents that can't be loaded
320
+ continue
321
+
322
+ # No search directive found
323
+ env ._kconfig_search_docname = None
324
+ return None
276
325
277
326
def add_option (self , option ):
278
327
"""Register a new Kconfig option to the domain."""
0 commit comments