Skip to content

Commit

Permalink
feat(go): Scope struct and interface type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 16, 2024
1 parent bf2e90c commit 392330f
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,8 @@ Language scopes:
- strings: Strings (interpreted and raw; excluding struct tags)
- imports: Imports
- type-def: Type definitions
- struct: `struct` type definitions
- interface: `interface` type definitions
- struct-tags: Struct tags

--go-query <TREE-SITTER-QUERY>
Expand Down
10 changes: 10 additions & 0 deletions src/scoping/langs/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub enum PreparedGoQuery {
Imports,
/// Type definitions.
TypeDef,
/// `struct` type definitions.
Struct,
/// `interface` type definitions.
Interface,
/// Struct tags.
StructTags,
}
Expand Down Expand Up @@ -48,6 +52,12 @@ impl From<PreparedGoQuery> for TSQuery {
r"(import_spec path: (interpreted_string_literal) @path)"
}
PreparedGoQuery::TypeDef => r"(type_declaration) @type_decl",
PreparedGoQuery::Struct => {
r"(type_declaration (type_spec type: (struct_type))) @struct"
}
PreparedGoQuery::Interface => {
r"(type_declaration (type_spec type: (interface_type))) @interface"
}
PreparedGoQuery::StructTags => "(field_declaration tag: (raw_string_literal) @tag)",
},
)
Expand Down
10 changes: 10 additions & 0 deletions tests/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ impl InScopeLinePart {
include_str!("go/base.go"),
Go::new(CodeQuery::Prepared(PreparedGoQuery::TypeDef)),
)]
#[case(
"base.go_struct",
include_str!("go/base.go"),
Go::new(CodeQuery::Prepared(PreparedGoQuery::Struct)),
)]
#[case(
"base.go_interface",
include_str!("go/base.go"),
Go::new(CodeQuery::Prepared(PreparedGoQuery::Interface)),
)]
#[case(
"base.go_struct-tags",
include_str!("go/base.go"),
Expand Down
28 changes: 28 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.go_interface.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 41
l: "type Testable interface {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 42
l: "\tTest() bool\n"
m: ^^^^^^^^^^^^^^^
- n: 43
l: "}\n"
m: "^ "
- n: 313
l: "type Block interface {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^
- n: 314
l: "\tBlockSize() int\n"
m: ^^^^^^^^^^^^^^^^^^^
- n: 315
l: "\tEncrypt(src, dst []byte)\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 316
l: "\tDecrypt(src, dst []byte)\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 317
l: "}\n"
m: "^ "
79 changes: 79 additions & 0 deletions tests/langs/snapshots/r#mod__langs__base.go_struct.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
source: tests/langs/mod.rs
expression: inscope_parts
---
- n: 32
l: "type TestError struct {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 33
l: "\tmessage string\n"
m: ^^^^^^^^^^^^^^^^^^
- n: 34
l: "}\n"
m: "^ "
- n: 46
l: "type TestCase struct {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^
- n: 47
l: "\tName string `json:\"name,omitempty\" db:\"name\"`\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 48
l: "\tInput interface{} `json:\"input\" db:\"input\"`\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 49
l: "\tExpected interface{} `json:\"expected\" db:\"expected\"`\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 50
l: "\tunequal bool // Unexported field\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 51
l: "}\n"
m: "^ "
- n: 54
l: "type ExtendedTestCase struct {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 55
l: "\tTestCase\n"
m: ^^^^^^^^^^^^
- n: 56
l: "\ttimeout time.Duration\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 57
l: "}\n"
m: "^ "
- n: 78
l: "type GenericPair[T any] struct {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 79
l: "\tFirst T\n"
m: ^^^^^^^^^^^^
- n: 80
l: "\tSecond T\n"
m: ^^^^^^^^^^^^
- n: 81
l: "}\n"
m: "^ "
- n: 303
l: "type (\n"
m: ^^^^^^^^
- n: 304
l: "\tPoint struct{ x, y float64 }\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 305
l: "\tpolar Point\n"
m: ^^^^^^^^^^^^^^^
- n: 306
l: ")\n"
m: "^ "
- n: 308
l: "type TreeNode struct {\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^
- n: 309
l: "\tleft, right *TreeNode\n"
m: ^^^^^^^^^^^^^^^^^^^^^^^^^
- n: 310
l: "\tvalue any\n"
m: ^^^^^^^^^^^^^^^^^^^
- n: 311
l: "}\n"
m: "^ "

0 comments on commit 392330f

Please sign in to comment.