Skip to content

Commit 1f47b1e

Browse files
authored
feat(orion): add Go grammar (#8249)
A customer is using https://templ.guide and wants to write a Starlark gazelle extension. See slack context: https://bazelbuild.slack.com/archives/C01HMGN77Q8/p1726265956403669?thread_ts=1726265956.403669&cid=C01HMGN77Q8 --- ### Changes are visible to end-users: no ### Test plan - New test cases added
1 parent 57d4ac4 commit 1f47b1e

File tree

11 files changed

+156
-0
lines changed

11 files changed

+156
-0
lines changed

cli/core/gazelle/common/treesitter/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ go_library(
1313
visibility = ["//visibility:public"],
1414
deps = [
1515
"//cli/core/gazelle/common",
16+
"//cli/core/gazelle/common/treesitter/grammars/golang",
1617
"//cli/core/gazelle/common/treesitter/grammars/java",
1718
"//cli/core/gazelle/common/treesitter/grammars/json",
1819
"//cli/core/gazelle/common/treesitter/grammars/kotlin",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "golang",
5+
srcs = [
6+
"binding.go",
7+
"@tree-sitter-go//:srcs", #keep
8+
],
9+
cgo = True,
10+
copts = ["-Iexternal/tree-sitter-go"], #keep
11+
importpath = "github.com/aspect-build/silo/cli/core/gazelle/common/treesitter/grammars/golang",
12+
visibility = ["//cli/core/gazelle/common/treesitter:__subpackages__"],
13+
deps = ["@com_github_smacker_go_tree_sitter//:go-tree-sitter"],
14+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package golang
2+
3+
//#include "src/tree_sitter/parser.h"
4+
//TSLanguage *tree_sitter_go();
5+
import "C"
6+
import (
7+
"unsafe"
8+
9+
sitter "github.com/smacker/go-tree-sitter"
10+
)
11+
12+
func GetLanguage() *sitter.Language {
13+
ptr := unsafe.Pointer(C.tree_sitter_go())
14+
return sitter.NewLanguage(ptr)
15+
}

cli/core/gazelle/common/treesitter/grammars/grammars.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ filegroup(
1515

1616
# buildifier: disable=function-docstring
1717
def fetch_grammars():
18+
http_archive(
19+
name = "tree-sitter-go",
20+
integrity = "sha256-M7w7RN4de4FfUv6fQuSXhsWDPZGB/Vn9aRDBnIJVkik=",
21+
urls = ["https://github.com/tree-sitter/tree-sitter-go/releases/download/v0.23.4/tree-sitter-go.tar.xz"],
22+
build_file_content = BUILD,
23+
)
24+
1825
http_archive(
1926
name = "tree-sitter-java",
2027
sha256 = "ed766e1045be236e50a7f99295996f6705d7506628b79af80d1fd5efb63c86a7",

cli/core/gazelle/common/treesitter/parser.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"log"
2323
"path"
2424

25+
golang "github.com/aspect-build/silo/cli/core/gazelle/common/treesitter/grammars/golang"
2526
"github.com/aspect-build/silo/cli/core/gazelle/common/treesitter/grammars/java"
2627
"github.com/aspect-build/silo/cli/core/gazelle/common/treesitter/grammars/json"
2728
"github.com/aspect-build/silo/cli/core/gazelle/common/treesitter/grammars/kotlin"
@@ -40,6 +41,7 @@ const (
4041
TypescriptX = "tsx"
4142
JSON = "json"
4243
Java = "java"
44+
Go = "go"
4345
)
4446

4547
type ASTQueryResult interface {
@@ -71,6 +73,8 @@ func (tree *treeAst) String() string {
7173

7274
func toSitterLanguage(lang LanguageGrammar) *sitter.Language {
7375
switch lang {
76+
case Go:
77+
return golang.GetLanguage()
7478
case Java:
7579
return java.GetLanguage()
7680
case JSON:
@@ -95,6 +99,8 @@ func PathToLanguage(p string) LanguageGrammar {
9599

96100
// Based on https://github.com/github-linguist/linguist/blob/master/lib/linguist/languages.yml
97101
var EXT_LANGUAGES = map[string]LanguageGrammar{
102+
"go": Go,
103+
98104
"kt": Kotlin,
99105
"ktm": Kotlin,
100106
"kts": Kotlin,

cli/pro/gazelle/tests/starzelle/query-templ/BUILD.in

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@deps-test//my:rules.bzl", "templ_library")
2+
3+
templ_library(
4+
name = "file_lib",
5+
srcs = ["file.templ"],
6+
tags = [
7+
"imports-github.com/org/central/demo/htmx/models",
8+
"imports-github.com/org/central/demo/htmx/views/components",
9+
],
10+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Templ
2+
3+
This folder shows that we can parse https://templ.guide/syntax-and-usage/basic-syntax
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "query-templ")
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package pages
2+
3+
import "github.com/org/central/demo/htmx/models"
4+
import "github.com/org/central/demo/htmx/views/components"
5+
6+
templ Debug(debugInfo models.DebugInfo) {
7+
<h1 class="m-6 text-3xl font-bold">Debug details</h1>
8+
9+
<h2 class="text-xl font-bold">Who are you?</h2>
10+
11+
<p class="p-3">Your user identity (User-Id) is:</p>
12+
13+
<pre>{ debugInfo.UserIdentity }</pre>
14+
15+
<p class="p-3">Your decoded JWT, if any, is:</p>
16+
17+
<pre>{ debugInfo.JWT }</pre>
18+
19+
<h2 class="text-xl font-bold">Request headers for a non-HTMX GET</h2>
20+
21+
<div class="w-full flex flex-col justify-center p-3">
22+
@components.KeyValues(debugInfo.RequestHeader.KeyValues)
23+
</div>
24+
25+
<h2 class="text-xl font-bold">Request headers for an HTMX GET</h2>
26+
27+
<div class="w-full flex flex-col justify-center p-3" id="inner-content">
28+
<div id="inner-content" hx-get="/debug/info" hx-target="#inner-content" hx-swap="innerHTML" hx-trigger="load"></div>
29+
</div>
30+
31+
<!-- Dropdown for Organization Selection -->
32+
<h2 class="text-xl font-bold">Switch Organization</h2>
33+
<div class="w-full flex flex-col justify-center p-3">
34+
<select id="org-selector" name="org_id" class="p-2 border border-gray-300 rounded">
35+
<option value="">Select an organization</option>
36+
<option value="b59bbfd8-7e08-42d3-b708-1b1dec308bdd">Media Group = 1 = b59bbfd8-7e08-42d3-b708-1b1dec308bdd</option>
37+
<option value="275ac542-23c7-4777-8e32-bd608861d331">Demo Group = 4 = 275ac542-23c7-4777-8e32-bd608861d331</option>
38+
<option value="14e001fc-14fb-49ef-b5eb-5f272128fb0c">Other Group = 6 = 14e001fc-14fb-49ef-b5eb-5f272128fb0c</option>
39+
<option value="d23507a0-4f4f-49fb-a787-1fd78d3e6d4b">Test Holding Company = 333 = d23507a0-4f4f-49fb-a787-1fd78d3e6d4b</option>
40+
<option value="a0772c22-4a81-4091-89df-14beac81a6f7">OpenAPI = 468 = a0772c22-4a81-4091-89df-14beac81a6f7</option>
41+
</select>
42+
</div>
43+
44+
<script>
45+
document.getElementById('org-selector').addEventListener('change', function(event) {
46+
const selectedOrgId = event.target.value;
47+
if (selectedOrgId) {
48+
const url = `/v1/organizations/${selectedOrgId}/switch`;
49+
50+
// Submit request when an organization is selected from the dropdown
51+
htmx.ajax('POST', url, {
52+
target: 'document',
53+
swap: 'outerHTML',
54+
headers: {
55+
'X-Requested-With': 'XMLHttpRequest'
56+
},
57+
withCredentials: true
58+
});
59+
}
60+
});
61+
</script>
62+
63+
64+
}

0 commit comments

Comments
 (0)