Skip to content

Commit

Permalink
feat(go): Scope defer blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 16, 2024
1 parent 0be56d0 commit 76a91b8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,7 @@ Language scopes:
- method: Method `func` definitions (`func (recv Recv) SomeFunc()`)
- free-func: Free `func` definitions (`func SomeFunc()`)
- type-params: Type parameters (generics)
- defer: `defer` blocks
- struct-tags: Struct tags

--go-query <TREE-SITTER-QUERY>
Expand Down
3 changes: 3 additions & 0 deletions src/scoping/langs/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub enum PreparedGoQuery {
FreeFunc,
/// Type parameters (generics).
TypeParams,
/// `defer` blocks.
Defer,
/// Struct tags.
StructTags,
}
Expand Down Expand Up @@ -90,6 +92,7 @@ impl From<PreparedGoQuery> for TSQuery {
PreparedGoQuery::Method => "(method_declaration) @method",
PreparedGoQuery::FreeFunc => "(function_declaration) @free_func",
PreparedGoQuery::TypeParams => "(type_parameter_declaration) @type_params",
PreparedGoQuery::Defer => "(defer_statement) @defer",
PreparedGoQuery::StructTags => "(field_declaration tag: (raw_string_literal) @tag)",
},
)
Expand Down
5 changes: 5 additions & 0 deletions tests/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,11 @@ impl InScopeLinePart {
include_str!("go/base.go"),
Go::new(CodeQuery::Prepared(PreparedGoQuery::TypeParams)),
)]
#[case(
"base.go_defer",
include_str!("go/base.go"),
Go::new(CodeQuery::Prepared(PreparedGoQuery::Defer)),
)]
#[case(
"base.go_struct-tags",
include_str!("go/base.go"),
Expand Down
31 changes: 31 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.go_defer.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 106
l: "\tdefer func() {\n"
m: " ^^^^^^^^^^^^^^^^"
- n: 107
l: "\t\t// Defer statement\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^
- n: 108
l: "\t\tfmt.Println(\"Cleanup after tests\")\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 109
l: "\t}()\n"
m: "^^^^^ "
- n: 162
l: "\tdefer func() {\n"
m: " ^^^^^^^^^^^^^^^^"
- n: 163
l: "\t\tif r := recover(); r != nil {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 164
l: "\t\t\tt.Log(\"Recovered from panic:\", r)\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 165
l: "\t\t}\n"
m: ^^^^^^^
- n: 166
l: "\t}()\n"
m: "^^^^^ "

0 comments on commit 76a91b8

Please sign in to comment.