Skip to content

Commit 8b89af6

Browse files
Jarrad Whitakerbarklan
authored andcommitted
add better multiline golang injection
this changes the golang match strategy to match within a string instead of detecting the starting string delimiter. this means constructs like query := ` select from my_table ` are now automatically detected.
1 parent 2d10fef commit 8b89af6

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"scopeName": "inline-sql.injection",
5858
"injectTo": [
5959
"source.python",
60-
"source.go",
6160
"source.java",
6261
"source.ruby",
6362
"source.cs",
@@ -117,6 +116,16 @@
117116
"meta.embedded.sql": "sql",
118117
"meta.template.expression.ts": "typescript"
119118
}
119+
},
120+
{
121+
"path": "./syntaxes/golang-multiline.json",
122+
"scopeName": "golang-multiline.injection",
123+
"injectTo": [
124+
"source.go"
125+
],
126+
"embeddedLanguages": {
127+
"meta.embedded.sql": "sql"
128+
}
120129
}
121130
],
122131
"configuration": {

syntaxes/golang-multiline.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"scopeName": "golang-multiline.injection",
3+
"fileTypes": [
4+
"go"
5+
],
6+
"injectionSelector": [
7+
"L:string.quoted.raw.go -comment"
8+
],
9+
"patterns": [
10+
{
11+
"name": "meta.embedded.sql",
12+
"begin": "\\s*((?i)(select|with|insert|update|create table|create index)(?=\\s))|(--\\w+)",
13+
"end": "(?=`)",
14+
"beginCaptures": {
15+
"2": { "name": "keyword.other.DML.sql" },
16+
"3": { "name": "comment.line.double-dash.sql" }
17+
},
18+
"patterns": [
19+
{ "include": "source.sql" }
20+
]
21+
}
22+
]
23+
}

syntaxes/highlight-sql-string.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"scopeName": "inline-sql.injection",
33
"fileTypes": [
44
"py",
5-
"go",
65
"js",
76
"jsx",
87
"ts",

test/testdata/multiline.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ select * from book where id = 34;
1212
`
1313

1414
notQuery := `
15-
SELECT This is not a query and should not be considered
16-
as one as --sql comment is not present.
15+
SELECT This a query and should be considered
16+
as one even though --sql comment is not present.
1717
Also, I hate manual testing.;
1818
`
1919

@@ -32,7 +32,17 @@ select * from book where id = 34;
3232

3333
more5 := `SELECT * from book;`
3434

35-
print(query, another, more, notQuery, more2, more3, more4, more5)
35+
more6 := `
36+
SELECT * from book;
37+
`
38+
39+
more7 := `
40+
with hihi as (
41+
42+
)
43+
`
44+
45+
print(query, another, more, notQuery, more2, more3, more4, more5, more6, more7)
3646
}
3747

3848
func add(a, b int) int {

0 commit comments

Comments
 (0)