Skip to content

Commit

Permalink
feat(go): Scope func init() definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 16, 2024
1 parent 6be3f2a commit 106c4a6
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,7 @@ Language scopes:
- func: `func` definitions
- method: Method `func` definitions (`func (recv Recv) SomeFunc()`)
- free-func: Free `func` definitions (`func SomeFunc()`)
- init-func: `func init()` definitions
- type-params: Type parameters (generics)
- defer: `defer` blocks
- select: `select` blocks
Expand Down
7 changes: 7 additions & 0 deletions src/scoping/langs/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub enum PreparedGoQuery {
Method,
/// Free `func` definitions (`func SomeFunc()`).
FreeFunc,
/// `func init()` definitions.
InitFunc,
/// Type parameters (generics).
TypeParams,
/// `defer` blocks.
Expand Down Expand Up @@ -101,6 +103,11 @@ impl From<PreparedGoQuery> for TSQuery {
}
PreparedGoQuery::Method => "(method_declaration) @method",
PreparedGoQuery::FreeFunc => "(function_declaration) @free_func",
PreparedGoQuery::InitFunc => {
r#"(function_declaration
name: (identifier) @id (#eq? @id "init")
) @init_func"#
}
PreparedGoQuery::TypeParams => "(type_parameter_declaration) @type_params",
PreparedGoQuery::Defer => "(defer_statement) @defer",
PreparedGoQuery::Select => "(select_statement) @select",
Expand Down
4 changes: 0 additions & 4 deletions tests/langs/go/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,6 @@ func closure() func() int {
}
}

func init() {
// Initialization code here
}

type (
rectangles = []*Rectangle
Polar = polar
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::FreeFunc)),
)]
#[case(
"base.go_init-func",
include_str!("go/base.go"),
Go::new(CodeQuery::Prepared(PreparedGoQuery::InitFunc)),
)]
#[case(
"base.go_type-params",
include_str!("go/base.go"),
Expand Down
3 changes: 0 additions & 3 deletions tests/langs/snapshots/r#mod__langs__base.go_comments.snap
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,3 @@ expression: inscope_parts
- n: 301
l: "*/\n"
m: "^^ "
- n: 356
l: "\t// Initialization code here\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^^^ "
9 changes: 0 additions & 9 deletions tests/langs/snapshots/r#mod__langs__base.go_free-func.snap
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,3 @@ expression: inscope_parts
- n: 353
l: "}\n"
m: "^ "
- n: 355
l: "func init() {\n"
m: ^^^^^^^^^^^^^^^
- n: 356
l: "\t// Initialization code here\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 357
l: "}\n"
m: "^ "
9 changes: 0 additions & 9 deletions tests/langs/snapshots/r#mod__langs__base.go_func.snap
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,3 @@ expression: inscope_parts
- n: 353
l: "}\n"
m: "^ "
- n: 355
l: "func init() {\n"
m: ^^^^^^^^^^^^^^^
- n: 356
l: "\t// Initialization code here\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 357
l: "}\n"
m: "^ "
13 changes: 13 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.go_init-func.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 259
l: "func init() {\n"
m: ^^^^^^^^^^^^^^^
- n: 260
l: "\tfmt.Println(\"Initializing package\")\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 261
l: "}\n"
m: "^ "
4 changes: 2 additions & 2 deletions tests/langs/snapshots/r#mod__langs__base.go_type-alias.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 360
- n: 356
l: "\trectangles = []*Rectangle\n"
m: " ^^^^^^^^^^^^^^^^^^^^^^^^^ "
- n: 361
- n: 357
l: "\tPolar = polar\n"
m: " ^^^^^^^^^^^^^^^^^^ "
8 changes: 4 additions & 4 deletions tests/langs/snapshots/r#mod__langs__base.go_type-def.snap
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ expression: inscope_parts
- n: 325
l: "}\n"
m: "^ "
- n: 359
- n: 355
l: "type (\n"
m: ^^^^^^^^
- n: 360
- n: 356
l: "\trectangles = []*Rectangle\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 361
- n: 357
l: "\tPolar = polar\n"
m: ^^^^^^^^^^^^^^^^^^^^^^
- n: 362
- n: 358
l: ")\n"
m: "^ "

0 comments on commit 106c4a6

Please sign in to comment.