Skip to content

Commit 79aee42

Browse files
Added support for capture aliases in TreeSitter queries (#3352)
1 parent 6daec45 commit 79aee42

40 files changed

Lines changed: 1154 additions & 325 deletions

‎packages/app-web-docs/src/docs/contributing/tree-sitter-query-syntax.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ In addition to the above aspects, you can also use the following inline operator
1919
- `@foo.start` and `@foo.end` to construct the scope using a range between two nodes (inclusive).
2020
- `@foo.startOf` and `@foo.endOf` to refer to the start and end positions of a node. For example, you could use `@foo.start.endOf` to indicate that the scope should start at the end of the node.
2121

22+
## Iteration capture aliases
23+
24+
Use these aliases when several scopes share an iteration range:
25+
26+
- `@statementNameValue.iteration` expands to `@statement.iteration`,
27+
`@name.iteration`, and `@value.iteration`.
28+
- `@statementNameValueType.iteration` expands to the same captures plus
29+
`@type.iteration`, for languages and contexts that need type iteration.
30+
31+
Both aliases support `.domain`, `.start.endOf`, `.end.startOf`, and `.end.endOf`. For example, a Java block can share iteration boundaries without repeating the pattern for each scope:
32+
33+
```scm
34+
(_
35+
"{" @statementNameValueType.iteration.start.endOf
36+
"}" @statementNameValueType.iteration.end.startOf
37+
)
38+
```
39+
2240
## Query predicate operators
2341

2442
We also support a number of query predicate operators for modifying the scope. See [`queryPredicateOperators.ts`](../../../../../packages/lib-engine/src/languages/TreeSitterQuery/queryPredicateOperators.ts) for a list of available operators.

‎packages/app-web-docs/src/docs/user/languages/fixtures/shellscript.json‎

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,46 @@
915915
}
916916
]
917917
},
918+
{
919+
"name": "scopes/shellscript/name/name.iteration.block2",
920+
"languageId": "shellscript",
921+
"facet": "name.iteration.block",
922+
"code": "if true; then\n :\nelif false; then\n :\nelse\n :\nfi",
923+
"scopes": [
924+
{
925+
"targets": [
926+
{
927+
"content": "0:0-6:2"
928+
}
929+
],
930+
"domain": "0:0-6:2"
931+
},
932+
{
933+
"targets": [
934+
{
935+
"content": "0:13-2:0"
936+
}
937+
],
938+
"domain": "0:13-2:0"
939+
},
940+
{
941+
"targets": [
942+
{
943+
"content": "2:16-4:0"
944+
}
945+
],
946+
"domain": "2:16-4:0"
947+
},
948+
{
949+
"targets": [
950+
{
951+
"content": "4:4-6:0"
952+
}
953+
],
954+
"domain": "4:4-6:0"
955+
}
956+
]
957+
},
918958
{
919959
"name": "scopes/shellscript/name/name.iteration.block3",
920960
"languageId": "shellscript",
@@ -939,6 +979,30 @@
939979
}
940980
]
941981
},
982+
{
983+
"name": "scopes/shellscript/name/name.iteration.block4",
984+
"languageId": "shellscript",
985+
"facet": "name.iteration.block",
986+
"code": "case $foo in\n 0) bar=0 ;;\nesac",
987+
"scopes": [
988+
{
989+
"targets": [
990+
{
991+
"content": "0:0-2:4"
992+
}
993+
],
994+
"domain": "0:0-2:4"
995+
},
996+
{
997+
"targets": [
998+
{
999+
"content": "1:6-1:13"
1000+
}
1001+
],
1002+
"domain": "1:6-1:13"
1003+
}
1004+
]
1005+
},
9421006
{
9431007
"name": "scopes/shellscript/name/name.iteration.document",
9441008
"languageId": "shellscript",
@@ -1441,6 +1505,46 @@
14411505
}
14421506
]
14431507
},
1508+
{
1509+
"name": "scopes/shellscript/statement/statement.iteration.block2",
1510+
"languageId": "shellscript",
1511+
"facet": "statement.iteration.block",
1512+
"code": "if true; then\n :\nelif false; then\n :\nelse\n :\nfi",
1513+
"scopes": [
1514+
{
1515+
"targets": [
1516+
{
1517+
"content": "0:0-6:2"
1518+
}
1519+
],
1520+
"domain": "0:0-6:2"
1521+
},
1522+
{
1523+
"targets": [
1524+
{
1525+
"content": "0:13-2:0"
1526+
}
1527+
],
1528+
"domain": "0:13-2:0"
1529+
},
1530+
{
1531+
"targets": [
1532+
{
1533+
"content": "2:16-4:0"
1534+
}
1535+
],
1536+
"domain": "2:16-4:0"
1537+
},
1538+
{
1539+
"targets": [
1540+
{
1541+
"content": "4:4-6:0"
1542+
}
1543+
],
1544+
"domain": "4:4-6:0"
1545+
}
1546+
]
1547+
},
14441548
{
14451549
"name": "scopes/shellscript/statement/statement.iteration.block3",
14461550
"languageId": "shellscript",
@@ -1465,6 +1569,30 @@
14651569
}
14661570
]
14671571
},
1572+
{
1573+
"name": "scopes/shellscript/statement/statement.iteration.block4",
1574+
"languageId": "shellscript",
1575+
"facet": "statement.iteration.block",
1576+
"code": "case $foo in\n 0) bar=0 ;;\nesac",
1577+
"scopes": [
1578+
{
1579+
"targets": [
1580+
{
1581+
"content": "0:0-2:4"
1582+
}
1583+
],
1584+
"domain": "0:0-2:4"
1585+
},
1586+
{
1587+
"targets": [
1588+
{
1589+
"content": "1:6-1:13"
1590+
}
1591+
],
1592+
"domain": "1:6-1:13"
1593+
}
1594+
]
1595+
},
14681596
{
14691597
"name": "scopes/shellscript/statement/statement.iteration.document",
14701598
"languageId": "shellscript",
@@ -1864,6 +1992,46 @@
18641992
}
18651993
]
18661994
},
1995+
{
1996+
"name": "scopes/shellscript/value/value.iteration.block2",
1997+
"languageId": "shellscript",
1998+
"facet": "value.iteration.block",
1999+
"code": "if true; then\n :\nelif false; then\n :\nelse\n :\nfi",
2000+
"scopes": [
2001+
{
2002+
"targets": [
2003+
{
2004+
"content": "0:0-6:2"
2005+
}
2006+
],
2007+
"domain": "0:0-6:2"
2008+
},
2009+
{
2010+
"targets": [
2011+
{
2012+
"content": "0:13-2:0"
2013+
}
2014+
],
2015+
"domain": "0:13-2:0"
2016+
},
2017+
{
2018+
"targets": [
2019+
{
2020+
"content": "2:16-4:0"
2021+
}
2022+
],
2023+
"domain": "2:16-4:0"
2024+
},
2025+
{
2026+
"targets": [
2027+
{
2028+
"content": "4:4-6:0"
2029+
}
2030+
],
2031+
"domain": "4:4-6:0"
2032+
}
2033+
]
2034+
},
18672035
{
18682036
"name": "scopes/shellscript/value/value.iteration.block3",
18692037
"languageId": "shellscript",
@@ -1888,6 +2056,30 @@
18882056
}
18892057
]
18902058
},
2059+
{
2060+
"name": "scopes/shellscript/value/value.iteration.block4",
2061+
"languageId": "shellscript",
2062+
"facet": "value.iteration.block",
2063+
"code": "case $foo in\n 0) bar=0 ;;\nesac",
2064+
"scopes": [
2065+
{
2066+
"targets": [
2067+
{
2068+
"content": "0:0-2:4"
2069+
}
2070+
],
2071+
"domain": "0:0-2:4"
2072+
},
2073+
{
2074+
"targets": [
2075+
{
2076+
"content": "1:6-1:13"
2077+
}
2078+
],
2079+
"domain": "1:6-1:13"
2080+
}
2081+
]
2082+
},
18912083
{
18922084
"name": "scopes/shellscript/value/value.iteration.document",
18932085
"languageId": "shellscript",

‎packages/app-web-docs/src/docs/user/languages/shellscript.mdx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,12 @@ Below are visualizations of all our scope tests for this language. These were cr
313313

314314
<ScopeVisualizer fixtureName="scopes/shellscript/name/name.iteration.block" languageId="shellscript" />
315315

316+
<ScopeVisualizer fixtureName="scopes/shellscript/name/name.iteration.block2" languageId="shellscript" />
317+
316318
<ScopeVisualizer fixtureName="scopes/shellscript/name/name.iteration.block3" languageId="shellscript" />
317319

320+
<ScopeVisualizer fixtureName="scopes/shellscript/name/name.iteration.block4" languageId="shellscript" />
321+
318322
#### 9. Name (iteration document)
319323

320324
<i>Iteration scope for names: the entire document including leading and trailing empty lines.</i>
@@ -469,8 +473,12 @@ Below are visualizations of all our scope tests for this language. These were cr
469473

470474
<ScopeVisualizer fixtureName="scopes/shellscript/statement/statement.iteration.block" languageId="shellscript" />
471475

476+
<ScopeVisualizer fixtureName="scopes/shellscript/statement/statement.iteration.block2" languageId="shellscript" />
477+
472478
<ScopeVisualizer fixtureName="scopes/shellscript/statement/statement.iteration.block3" languageId="shellscript" />
473479

480+
<ScopeVisualizer fixtureName="scopes/shellscript/statement/statement.iteration.block4" languageId="shellscript" />
481+
474482
#### 19. Statement (iteration document)
475483

476484
<i>Iteration scope for statements: the entire document including leading and trailing empty lines.</i>
@@ -537,8 +545,12 @@ Below are visualizations of all our scope tests for this language. These were cr
537545

538546
<ScopeVisualizer fixtureName="scopes/shellscript/value/value.iteration.block" languageId="shellscript" />
539547

548+
<ScopeVisualizer fixtureName="scopes/shellscript/value/value.iteration.block2" languageId="shellscript" />
549+
540550
<ScopeVisualizer fixtureName="scopes/shellscript/value/value.iteration.block3" languageId="shellscript" />
541551

552+
<ScopeVisualizer fixtureName="scopes/shellscript/value/value.iteration.block4" languageId="shellscript" />
553+
542554
#### 10. Value (iteration document)
543555

544556
<i>Iteration scope for values: the entire document including leading and trailing empty lines.</i>

‎packages/app-web-docs/src/docs/user/scopes/fixtures/name.json‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12083,6 +12083,46 @@
1208312083
}
1208412084
]
1208512085
},
12086+
{
12087+
"name": "scopes/shellscript/name/name.iteration.block2",
12088+
"languageId": "shellscript",
12089+
"facet": "name.iteration.block",
12090+
"code": "if true; then\n :\nelif false; then\n :\nelse\n :\nfi",
12091+
"scopes": [
12092+
{
12093+
"targets": [
12094+
{
12095+
"content": "0:0-6:2"
12096+
}
12097+
],
12098+
"domain": "0:0-6:2"
12099+
},
12100+
{
12101+
"targets": [
12102+
{
12103+
"content": "0:13-2:0"
12104+
}
12105+
],
12106+
"domain": "0:13-2:0"
12107+
},
12108+
{
12109+
"targets": [
12110+
{
12111+
"content": "2:16-4:0"
12112+
}
12113+
],
12114+
"domain": "2:16-4:0"
12115+
},
12116+
{
12117+
"targets": [
12118+
{
12119+
"content": "4:4-6:0"
12120+
}
12121+
],
12122+
"domain": "4:4-6:0"
12123+
}
12124+
]
12125+
},
1208612126
{
1208712127
"name": "scopes/shellscript/name/name.iteration.block3",
1208812128
"languageId": "shellscript",
@@ -12107,6 +12147,30 @@
1210712147
}
1210812148
]
1210912149
},
12150+
{
12151+
"name": "scopes/shellscript/name/name.iteration.block4",
12152+
"languageId": "shellscript",
12153+
"facet": "name.iteration.block",
12154+
"code": "case $foo in\n 0) bar=0 ;;\nesac",
12155+
"scopes": [
12156+
{
12157+
"targets": [
12158+
{
12159+
"content": "0:0-2:4"
12160+
}
12161+
],
12162+
"domain": "0:0-2:4"
12163+
},
12164+
{
12165+
"targets": [
12166+
{
12167+
"content": "1:6-1:13"
12168+
}
12169+
],
12170+
"domain": "1:6-1:13"
12171+
}
12172+
]
12173+
},
1211012174
{
1211112175
"name": "scopes/shellscript/name/name.iteration.document",
1211212176
"languageId": "shellscript",

0 commit comments

Comments
 (0)