Skip to content

Commit 5e687b5

Browse files
authored
feat(yaml): treesitter name array elements symbol based upon "name" key (#485)
Previously, these elements were not listed as symbols. redhat's yaml-language-server lists these as numbered elements such as 0, 1, 2, but treesitter strategy didn't match them at all. There's a de-facto standard for many yaml formats, to use the "name" key to solve this problem. It is a best practice for commonly used yaml files such as github workflows and ansible. Signed-off-by: Robert Muir <[email protected]>
1 parent 99a5fb8 commit 5e687b5

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

queries/yaml/aerial.scm

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,19 @@
99
value: (block_node
1010
(block_sequence) @symbol)
1111
(#set! "kind" "Enum")) @start
12+
13+
; "array element" with a "name:" key
14+
; ansible play/task names, etc
15+
; github workflow/actions jobs/steps, etc
16+
(block_sequence_item
17+
(block_node
18+
(block_mapping
19+
(block_mapping_pair
20+
key: (flow_node
21+
(plain_scalar
22+
(string_scalar) @_key))
23+
value: (flow_node
24+
(plain_scalar
25+
(string_scalar) @name)))))
26+
(#eq? @_key "name")
27+
(#set! "kind" "Module")) @symbol

tests/symbols/yaml_test2.json

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[
2+
{
3+
"children": [
4+
{
5+
"children": [
6+
{
7+
"children": [
8+
{
9+
"col": 6,
10+
"end_col": 40,
11+
"end_lnum": 15,
12+
"kind": "Module",
13+
"level": 3,
14+
"lnum": 10,
15+
"name": "Prepare",
16+
"selection_range": {
17+
"col": 14,
18+
"end_col": 21,
19+
"end_lnum": 10,
20+
"lnum": 10
21+
}
22+
},
23+
{
24+
"col": 6,
25+
"end_col": 0,
26+
"end_lnum": 19,
27+
"kind": "Module",
28+
"level": 3,
29+
"lnum": 17,
30+
"name": "Run Luacheck",
31+
"selection_range": {
32+
"col": 14,
33+
"end_col": 26,
34+
"end_lnum": 17,
35+
"lnum": 17
36+
}
37+
}
38+
],
39+
"col": 4,
40+
"end_col": 0,
41+
"end_lnum": 19,
42+
"kind": "Enum",
43+
"level": 2,
44+
"lnum": 7,
45+
"name": "steps",
46+
"selection_range": {
47+
"col": 4,
48+
"end_col": 9,
49+
"end_lnum": 7,
50+
"lnum": 7
51+
}
52+
}
53+
],
54+
"col": 2,
55+
"end_col": 0,
56+
"end_lnum": 19,
57+
"kind": "Class",
58+
"level": 1,
59+
"lnum": 4,
60+
"name": "luacheck",
61+
"selection_range": {
62+
"col": 2,
63+
"end_col": 10,
64+
"end_lnum": 4,
65+
"lnum": 4
66+
}
67+
}
68+
],
69+
"col": 0,
70+
"end_col": 0,
71+
"end_lnum": 19,
72+
"kind": "Class",
73+
"level": 0,
74+
"lnum": 3,
75+
"name": "jobs",
76+
"selection_range": {
77+
"col": 0,
78+
"end_col": 4,
79+
"end_lnum": 3,
80+
"lnum": 3
81+
}
82+
}
83+
]

tests/treesitter/yaml_test2.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Run tests
2+
3+
jobs:
4+
luacheck:
5+
name: Luacheck
6+
runs-on: ubuntu-24.04
7+
steps:
8+
- uses: actions/checkout@v4
9+
10+
- name: Prepare
11+
run: |
12+
sudo apt-get update
13+
sudo add-apt-repository universe
14+
sudo apt install luarocks -y
15+
sudo luarocks install luacheck
16+
17+
- name: Run Luacheck
18+
run: luacheck lua tests

0 commit comments

Comments
 (0)