From 392330fbfc3bcd69e0dcda749f091030b4913aa2 Mon Sep 17 00:00:00 2001 From: Alex Povel Date: Fri, 16 Aug 2024 19:23:08 +0200 Subject: [PATCH] feat(go): Scope `struct` and `interface` type definitions --- README.md | 2 + src/scoping/langs/go.rs | 10 +++ tests/langs/mod.rs | 10 +++ .../r#mod__langs__base.go_interface.snap | 28 +++++++ .../r#mod__langs__base.go_struct.snap | 79 +++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 tests/langs/snapshots/r#mod__langs__base.go_interface.snap create mode 100644 tests/langs/snapshots/r#mod__langs__base.go_struct.snap diff --git a/README.md b/README.md index dbab2e7f..bfcb0c66 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/scoping/langs/go.rs b/src/scoping/langs/go.rs index e2ed9858..adb7e3dc 100644 --- a/src/scoping/langs/go.rs +++ b/src/scoping/langs/go.rs @@ -21,6 +21,10 @@ pub enum PreparedGoQuery { Imports, /// Type definitions. TypeDef, + /// `struct` type definitions. + Struct, + /// `interface` type definitions. + Interface, /// Struct tags. StructTags, } @@ -48,6 +52,12 @@ impl From 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)", }, ) diff --git a/tests/langs/mod.rs b/tests/langs/mod.rs index 8132a856..abdac82b 100644 --- a/tests/langs/mod.rs +++ b/tests/langs/mod.rs @@ -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"), diff --git a/tests/langs/snapshots/r#mod__langs__base.go_interface.snap b/tests/langs/snapshots/r#mod__langs__base.go_interface.snap new file mode 100644 index 00000000..83823808 --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.go_interface.snap @@ -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: "^ " diff --git a/tests/langs/snapshots/r#mod__langs__base.go_struct.snap b/tests/langs/snapshots/r#mod__langs__base.go_struct.snap new file mode 100644 index 00000000..56afeb84 --- /dev/null +++ b/tests/langs/snapshots/r#mod__langs__base.go_struct.snap @@ -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: "^ "