Skip to content

Commit 527206b

Browse files
committed
Make attribute (names) searchable
1 parent f10b632 commit 527206b

File tree

4 files changed

+135
-55
lines changed

4 files changed

+135
-55
lines changed

crates/wesl_docs/src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ impl<T> Default for Item<T> {
127127
}
128128

129129
pub trait ItemInstance {
130+
const ITEM_KIND: ItemKind;
130131
fn conditional(&self) -> Option<&Conditional>;
132+
fn all_attributes(&self) -> impl Iterator<Item = &Attribute>;
131133
}
132134

133135
#[derive(Debug, Clone)]
@@ -141,9 +143,13 @@ pub struct Constant {
141143
}
142144

143145
impl ItemInstance for Constant {
146+
const ITEM_KIND: ItemKind = ItemKind::Constant;
144147
fn conditional(&self) -> Option<&Conditional> {
145148
self.conditional.as_ref()
146149
}
150+
fn all_attributes(&self) -> impl Iterator<Item = &Attribute> {
151+
self.attributes.iter()
152+
}
147153
}
148154

149155
#[derive(Debug, Clone)]
@@ -158,9 +164,13 @@ pub struct GlobalVariable {
158164
}
159165

160166
impl ItemInstance for GlobalVariable {
167+
const ITEM_KIND: ItemKind = ItemKind::GlobalVariable;
161168
fn conditional(&self) -> Option<&Conditional> {
162169
self.conditional.as_ref()
163170
}
171+
fn all_attributes(&self) -> impl Iterator<Item = &Attribute> {
172+
self.attributes.iter()
173+
}
164174
}
165175

166176
#[derive(Debug, Clone, Copy)]
@@ -237,9 +247,15 @@ pub struct Struct {
237247
}
238248

239249
impl ItemInstance for Struct {
250+
const ITEM_KIND: ItemKind = ItemKind::Struct;
240251
fn conditional(&self) -> Option<&Conditional> {
241252
self.conditional.as_ref()
242253
}
254+
fn all_attributes(&self) -> impl Iterator<Item = &Attribute> {
255+
self.attributes
256+
.iter()
257+
.chain(self.members.iter().flat_map(|m| m.attributes.iter()))
258+
}
243259
}
244260

245261
#[derive(Debug, Clone)]
@@ -282,9 +298,16 @@ pub struct Function {
282298
}
283299

284300
impl ItemInstance for Function {
301+
const ITEM_KIND: ItemKind = ItemKind::Function;
285302
fn conditional(&self) -> Option<&Conditional> {
286303
self.conditional.as_ref()
287304
}
305+
fn all_attributes(&self) -> impl Iterator<Item = &Attribute> {
306+
self.attributes
307+
.iter()
308+
.chain(self.parameters.iter().flat_map(|p| p.attributes.iter()))
309+
.chain(self.return_attributes.iter())
310+
}
288311
}
289312

290313
#[derive(Debug, Clone)]
@@ -305,9 +328,13 @@ pub struct TypeAlias {
305328
}
306329

307330
impl ItemInstance for TypeAlias {
331+
const ITEM_KIND: ItemKind = ItemKind::TypeAlias;
308332
fn conditional(&self) -> Option<&Conditional> {
309333
self.conditional.as_ref()
310334
}
335+
fn all_attributes(&self) -> impl Iterator<Item = &Attribute> {
336+
self.attributes.iter()
337+
}
311338
}
312339

313340
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -354,6 +381,31 @@ pub enum Attribute {
354381
},
355382
}
356383

384+
impl Attribute {
385+
pub fn name(&self) -> &str {
386+
match self {
387+
Attribute::Align(_) => "align",
388+
Attribute::Binding(_) => "binding",
389+
Attribute::BlendSrc(_) => "blend_src",
390+
Attribute::Builtin(_) => "builtin",
391+
Attribute::Const => "const",
392+
Attribute::Diagnostic { .. } => "diagnostic",
393+
Attribute::Group(_) => "group",
394+
Attribute::Id(_) => "id",
395+
Attribute::Interpolate { .. } => "interpolate",
396+
Attribute::Invariant => "invariant",
397+
Attribute::Location(_) => "location",
398+
Attribute::MustUse => "must_use",
399+
Attribute::Size(_) => "size",
400+
Attribute::WorkgroupSize { .. } => "workgroup_size",
401+
Attribute::Vertex => "vertex",
402+
Attribute::Fragment => "fragment",
403+
Attribute::Compute => "compute",
404+
Attribute::Custom { name, .. } => name,
405+
}
406+
}
407+
}
408+
357409
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
358410
pub enum BuiltinValue {
359411
VertexIndex,
Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,116 @@
11
use serde::Serialize;
2-
use wesl_docs::{Module, WeslDocs};
2+
use wesl_docs::{Attribute, Ident, IndexMap, Item, ItemInstance, ItemKind, Module, WeslDocs};
33

4-
pub fn all_items(doc: &WeslDocs) -> Vec<Item> {
4+
pub fn all_items(doc: &WeslDocs) -> impl Serialize {
55
let mut items = Vec::new();
66
all_items_module(&doc.root, &[], &mut items);
77
items.sort();
88
items
99
}
1010

11-
fn all_items_module(module: &Module, parent: &[String], items: &mut Vec<Item>) {
11+
fn all_items_module(
12+
module: &Module,
13+
parent: &[String],
14+
serialized_items: &mut Vec<SerializedItem>,
15+
) {
1216
let path = parent
1317
.iter()
1418
.cloned()
1519
.chain([module.name.clone()])
1620
.collect::<Vec<_>>();
1721
for inner in &module.modules {
18-
all_items_module(inner, &path, items);
22+
all_items_module(inner, &path, serialized_items);
1923

20-
items.push(Item::new(
24+
serialized_items.push(SerializedItem::new(
2125
path.clone(),
2226
inner.name.clone(),
23-
ItemKind::Module,
27+
[],
28+
SerializedItemKind::Module,
2429
));
2530
}
2631

27-
for name in module.constants.keys() {
28-
items.push(Item::new(path.clone(), name.0.clone(), ItemKind::Constant));
29-
}
32+
add_items(&module.constants, path.clone(), serialized_items);
33+
add_items(&module.global_variables, path.clone(), serialized_items);
34+
add_items(&module.structs, path.clone(), serialized_items);
35+
add_items(&module.functions, path.clone(), serialized_items);
36+
add_items(&module.type_aliases, path.clone(), serialized_items);
37+
}
3038

31-
for name in module.global_variables.keys() {
32-
items.push(Item::new(
39+
fn add_items<T>(
40+
items: &IndexMap<Ident, Item<T>>,
41+
path: Vec<String>,
42+
serialized_items: &mut Vec<SerializedItem>,
43+
) where
44+
T: ItemInstance,
45+
{
46+
for (name, item) in items {
47+
serialized_items.push(SerializedItem::new(
3348
path.clone(),
3449
name.0.clone(),
35-
ItemKind::GlobalVariable,
50+
item.instances.iter().flat_map(|i| i.all_attributes()),
51+
T::ITEM_KIND.into(),
3652
));
3753
}
38-
39-
for name in module.structs.keys() {
40-
items.push(Item::new(path.clone(), name.0.clone(), ItemKind::Struct));
41-
}
42-
43-
for name in module.functions.keys() {
44-
items.push(Item::new(path.clone(), name.0.clone(), ItemKind::Function));
45-
}
46-
47-
for name in module.type_aliases.keys() {
48-
items.push(Item::new(path.clone(), name.0.clone(), ItemKind::TypeAlias));
49-
}
5054
}
5155

5256
#[derive(Debug, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
53-
pub struct Item {
57+
struct SerializedItem {
5458
path: Vec<String>,
5559
name: String,
56-
kind: ItemKind,
60+
attributes: Vec<String>,
61+
kind: SerializedItemKind,
5762
url: String,
5863
}
5964

60-
impl Item {
61-
fn new(path: Vec<String>, name: String, kind: ItemKind) -> Self {
65+
impl SerializedItem {
66+
fn new<'a>(
67+
path: Vec<String>,
68+
name: String,
69+
attributes: impl IntoIterator<Item = &'a Attribute>,
70+
kind: SerializedItemKind,
71+
) -> Self {
6272
let mut url = path.join("/");
6373
match kind {
64-
ItemKind::Module => url.push_str(&format!("/{}/index.html", name)),
65-
ItemKind::Constant => url.push_str(&format!("/const.{}.html", name)),
66-
ItemKind::GlobalVariable => url.push_str(&format!("/var.{}.html", name)),
67-
ItemKind::Struct => url.push_str(&format!("/struct.{}.html", name)),
68-
ItemKind::Function => url.push_str(&format!("/fn.{}.html", name)),
69-
ItemKind::TypeAlias => url.push_str(&format!("/type.{}.html", name)),
74+
SerializedItemKind::Module => url.push_str(&format!("/{}/index.html", name)),
75+
SerializedItemKind::Constant => url.push_str(&format!("/const.{}.html", name)),
76+
SerializedItemKind::GlobalVariable => url.push_str(&format!("/var.{}.html", name)),
77+
SerializedItemKind::Struct => url.push_str(&format!("/struct.{}.html", name)),
78+
SerializedItemKind::Function => url.push_str(&format!("/fn.{}.html", name)),
79+
SerializedItemKind::TypeAlias => url.push_str(&format!("/type.{}.html", name)),
7080
}
7181

7282
Self {
7383
path,
7484
name,
85+
attributes: attributes
86+
.into_iter()
87+
.map(|attr| format!("@{}", attr.name()))
88+
.collect(),
7589
kind,
7690
url,
7791
}
7892
}
7993
}
8094

8195
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
82-
enum ItemKind {
96+
enum SerializedItemKind {
8397
Module,
8498
Constant,
8599
GlobalVariable,
86100
Struct,
87101
Function,
88102
TypeAlias,
89103
}
104+
105+
impl From<ItemKind> for SerializedItemKind {
106+
fn from(kind: ItemKind) -> Self {
107+
match kind {
108+
ItemKind::Module => SerializedItemKind::Module,
109+
ItemKind::Constant => SerializedItemKind::Constant,
110+
ItemKind::GlobalVariable => SerializedItemKind::GlobalVariable,
111+
ItemKind::Struct => SerializedItemKind::Struct,
112+
ItemKind::Function => SerializedItemKind::Function,
113+
ItemKind::TypeAlias => SerializedItemKind::TypeAlias,
114+
}
115+
}
116+
}

crates/wesl_docs_generator/static/js/search.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ function search() {
5454
}
5555

5656
var itemsFiltered = items.filter(function (item) {
57-
return item.name.toLowerCase().includes(query.toLowerCase());
57+
return item.name.toLowerCase().includes(query.toLowerCase())
58+
|| item.attributes.some((attr) => attr.toLowerCase().startsWith(query.toLowerCase()));
5859
});
5960

6061
innerContentElement.innerHTML = "";

crates/wesl_docs_generator/templates/render_attribute.html

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
{% match attr %}
22
{%- when Attribute::Align with (expr) -%}
3-
@align({{ self::render_expression(expr, module_path_level)|safe }})
3+
@{{ attr.name() }}({{ self::render_expression(expr, module_path_level)|safe }})
44
{%- when Attribute::Binding with (expr) -%}
5-
@binding({{ self::render_expression(expr, module_path_level)|safe }})
5+
@{{ attr.name() }}({{ self::render_expression(expr, module_path_level)|safe }})
66
{%- when Attribute::BlendSrc with (expr) -%}
7-
@blend_src({{ self::render_expression(expr, module_path_level)|safe }})
7+
@{{ attr.name() }}({{ self::render_expression(expr, module_path_level)|safe }})
88
{%- when Attribute::Builtin with (builtin) -%}
9-
@builtin({{ self::builtin_str(builtin) }})
9+
@{{ attr.name() }}({{ self::builtin_str(builtin) }})
1010
{%- when Attribute::Const -%}
11-
@const
11+
@{{ attr.name() }}
1212
{%- when Attribute::Diagnostic with { severity, rule } -%}
13-
@diagnostic({{ self::severity_str(severity) }}, {{ rule }})
13+
@{{ attr.name() }}({{ self::severity_str(severity) }}, {{ rule }})
1414
{%- when Attribute::Group with (expr) -%}
15-
@group({{ self::render_expression(expr, module_path_level)|safe }})
15+
@{{ attr.name() }}({{ self::render_expression(expr, module_path_level)|safe }})
1616
{%- when Attribute::Id with (expr) -%}
17-
@id({{ self::render_expression(expr, module_path_level)|safe }})
17+
@{{ attr.name() }}({{ self::render_expression(expr, module_path_level)|safe }})
1818
{%- when Attribute::Interpolate with { ty, sampling } -%}
19-
@interpolate({{ self::interpolation_str(ty) }}
19+
@{{ attr.name() }}({{ self::interpolation_str(ty) }}
2020
{%- if let Some(sampling) = sampling %}, {{ self::sampling_str(sampling) }}{% endif %})
2121
{%- when Attribute::Invariant -%}
22-
@invariant
22+
@{{ attr.name() }}
2323
{%- when Attribute::Location with (expr) -%}
24-
@location({{ self::render_expression(expr, module_path_level)|safe }})
24+
@{{ attr.name() }}({{ self::render_expression(expr, module_path_level)|safe }})
2525
{%- when Attribute::MustUse -%}
26-
@must_use
26+
@{{ attr.name() }}
2727
{%- when Attribute::Size with (expr) -%}
28-
@size({{ self::render_expression(expr, module_path_level)|safe }})
28+
@{{ attr.name() }}({{ self::render_expression(expr, module_path_level)|safe }})
2929
{%- when Attribute::WorkgroupSize with { x, y, z } -%}
30-
@workgroup_size({{ self::render_expression(x, module_path_level)|safe }}
30+
@{{ attr.name() }}({{ self::render_expression(x, module_path_level)|safe }}
3131
{%- if let Some(y) = y %}, {{ self::render_expression(y, module_path_level)|safe }}{% endif %}
3232
{%- if let Some(z) = z %}, {{ self::render_expression(z, module_path_level)|safe }}{% endif %})
3333
{%- when Attribute::Vertex -%}
34-
@vertex
34+
@{{ attr.name() }}
3535
{%- when Attribute::Fragment -%}
36-
@fragment
36+
@{{ attr.name() }}
3737
{%- when Attribute::Compute -%}
38-
@compute
39-
{%- when Attribute::Custom with { name, arguments } -%}
40-
@{{ name }}{% if let Some(arguments) = arguments -%}
38+
@{{ attr.name() }}
39+
{%- when Attribute::Custom with { name: _, arguments } -%}
40+
@{{ attr.name() }}{% if let Some(arguments) = arguments -%}
4141
({% for arg in arguments %}
4242
{%- if !loop.first %}, {% endif %}{{ self::render_expression(arg, module_path_level)|safe }}
4343
{%- endfor %})

0 commit comments

Comments
 (0)