Repository: fatih/gomodifytags (go)

Issue:
:GoAddTags cannot add tags to variable defined with struct literal
(originally opened in [vim-go](https://github.com/fatih/vim-go/issues/1793))
### What did you do?
I tried to run :GoAddTags inside a variable defined with a struct literal:
```go
var thing struct{
  id int
  value string
}
```

### What did you expect to happen?
I expected struct tags to be added per usual.

### What happened instead?
I got the error message:
```
vim-go: offset is not inside a struct
```

### Configuration:

* vim-go version: v1.17

* `vimrc` you used to reproduce (use a *minimal* vimrc with other plugins disabled; do not link to a 2,000 line vimrc):
[My vimrc](https://github.com/Korede-TA/dotfiles/blob/master/.vimrc)

* Vim version (first three lines from `:version`):
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Nov 29 2017 18:37:46)
Included patches: 1-503, 505-608, 682-1283
Compiled by root@apple.com

* Go version (`go version`): go1.10.1

* Go environment (`go env`): darwin/amd64



Pull request:
Fix offset mode for struct literals
Fixes #34 

Patch:
diff --git a/main.go b/main.go
index 117ead9..6e4700c 100644
--- a/main.go
+++ b/main.go
@@ -433,6 +433,9 @@ func collectStructs(node ast.Node) map[token.Pos]*structType {
 			t = x.Type
 		case *ast.CompositeLit:
 			t = x.Type
+		case *ast.ValueSpec:
+			structName = x.Names[0].Name
+			t = x.Type
 		}
 
 		x, ok := t.(*ast.StructType)


Test patch:
diff --git a/main_test.go b/main_test.go
index 818c991..b611c43 100644
--- a/main_test.go
+++ b/main_test.go
@@ -258,6 +258,24 @@ func TestRewrite(t *testing.T) {
 				transform: "snakecase",
 			},
 		},
+		{
+			file: "offset_add_literal_in",
+			cfg: &config{
+				add:       []string{"json"},
+				output:    "source",
+				offset:    46,
+				transform: "snakecase",
+			},
+		},
+		{
+			file: "offset_add_literal_out",
+			cfg: &config{
+				add:       []string{"json"},
+				output:    "source",
+				offset:    32,
+				transform: "snakecase",
+			},
+		},
 		{
 			file: "errors",
 			cfg: &config{
diff --git a/test-fixtures/offset_add_literal_in.golden b/test-fixtures/offset_add_literal_in.golden
new file mode 100644
index 0000000..2d30eaf
--- /dev/null
+++ b/test-fixtures/offset_add_literal_in.golden
@@ -0,0 +1,7 @@
+package main
+
+func main() {
+	var in struct {
+		Foo string `json:"foo"`
+	}
+}
diff --git a/test-fixtures/offset_add_literal_in.input b/test-fixtures/offset_add_literal_in.input
new file mode 100644
index 0000000..af1d7bb
--- /dev/null
+++ b/test-fixtures/offset_add_literal_in.input
@@ -0,0 +1,7 @@
+package main
+
+func main() {
+	var in struct {
+		Foo string
+	}
+}
diff --git a/test-fixtures/offset_add_literal_out.golden b/test-fixtures/offset_add_literal_out.golden
new file mode 100644
index 0000000..640f381
--- /dev/null
+++ b/test-fixtures/offset_add_literal_out.golden
@@ -0,0 +1,5 @@
+package main
+
+var out struct {
+	Foo string `json:"foo"`
+}
diff --git a/test-fixtures/offset_add_literal_out.input b/test-fixtures/offset_add_literal_out.input
new file mode 100644
index 0000000..89db6fa
--- /dev/null
+++ b/test-fixtures/offset_add_literal_out.input
@@ -0,0 +1,5 @@
+package main
+
+var out struct {
+	Foo string
+}