fatih / vim-go

Go development plugin for Vim
https://www.patreon.com/bhcleek
Other
15.9k stars 1.45k forks source link

GoFillStruct does not work with []struct{{ _ }} #3592

Closed onns closed 7 months ago

onns commented 8 months ago

What did you do? (required: The issue will be closed when not provided)

type foo struct{}

func a(){
a := []foo{
{
_,
}
}
}

_means the cursor, under command model and I type GoFillStruct, it doesn't do what I expected.

图片

What did you expect to happen?

What happened instead?

Configuration (MUST fill this out):

vim-go version:

vimrc you used to reproduce:

vimrc ```vim ```

Vim version (first three lines from :version):

Go version (go version):

Go environment

go env Output:


gopls version

gopls version Output:


bhcleek commented 8 months ago

The syntax in your example is invalid. The comma after the cursor is unnecessary, because you're dealing with the empty struct and there are no fields to separate. Also, there is no comma after the final element's } and just before the newline after which follows the slice's closing }.

I assume that the example you provided is not quite what you're seeing in practice. If you can provide an example that's syntactically correct, we may be able to discover the root of the problem you're having.

onns commented 8 months ago
package main

import "fmt"

type Foo struct {
    S string `json:"s"`
    N int64  `json:"n"`
}

func main() {
    fs := []*Foo{
        {
            // cursor here does not work
        },
    }
    fs := []Foo{
        {
                        // cursor here work well.
            S: "",
            N: 0,
        },
    }
}

sorry for the bad example, I thought it wouldn't work while handling struct slice so I write a simple example, but after I tested, only []*struct has the problem.

bhcleek commented 8 months ago

If you'll change change the fillstruct mode with let g:go_fillstruct_mode='gopls', I think the case slice of pointers case will work, too.

If you want to continue to use fillstruct for g:go_fillstruct_mode, then I'd suggest opening an issue at https://github.com/davidrjenni/reftools.