saibing / bingo

Bingo is a Go language server that speaks Language Server Protocol.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Format structs in quick documentation on hover

inliquid opened this issue · comments

In VS Code quick documentation on hover usually looks like this:
изображение

However, I think it should be formatted before it's shown like in the source code:

type HTMLInputElement struct {
	*BasicHTMLElement
	Accept             string    `js:"accept"`
	Alt                string    `js:"alt"`
	Autocomplete       string    `js:"autocomplete"`
	Autofocus          bool      `js:"autofocus"`
	Checked            bool      `js:"checked"`
	DefaultChecked     bool      `js:"defaultChecked"`
	DefaultValue       string    `js:"defaultValue"`
	DirName            string    `js:"dirName"`
	Disabled           bool      `js:"disabled"`
	FormAction         string    `js:"formAction"`
	FormEncType        string    `js:"formEncType"`
	FormMethod         string    `js:"formMethod"`
	FormNoValidate     bool      `js:"formNoValidate"`
	FormTarget         string    `js:"formTarget"`
	Height             string    `js:"height"`
	Indeterminate      bool      `js:"indeterminate"`
	Max                string    `js:"max"`
	MaxLength          int       `js:"maxLength"`
	Min                string    `js:"min"`
	Multiple           bool      `js:"multiple"`
	Name               string    `js:"name"`
	Pattern            string    `js:"pattern"`
	Placeholder        string    `js:"placeholder"`
	ReadOnly           bool      `js:"readOnly"`
	Required           bool      `js:"required"`
	SelectionDirection string    `js:"selectionDirection"`
	SelectionEnd       int       `js:"selectionEnd"`
	SelectionStart     int       `js:"selectionStart"`
	Size               int       `js:"size"`
	Src                string    `js:"src"`
	Step               string    `js:"step"`
	TabIndex           int       `js:"tabIndex"`
	Type               string    `js:"type"`
	ValidationMessage  string    `js:"validationMessage"`
	Value              string    `js:"value"`
	ValueAsDate        time.Time `js:"valueAsDate"`
	ValueAsNumber      float64   `js:"valueAsNumber"`
	Width              string    `js:"width"`
	WillValidate       bool      `js:"willValidate"`
}

Could probably just call "go/format".Source as defined in https://github.com/golang/go/blob/master/src/go/format/format.go#L91.

I just ran a few checks:

$ echo "struct{Filename string; Want []*posRef}" | gofmt -e
<standard input>:1:22: expected expression
$ echo "var c struct{Filename string; Want []*posRef}" | gofmt -e
var c struct {
	Filename string
	Want     []*posRef
}
$ echo "[]struct{Filename string; Want []*posRef}" | gofmt -e
<standard input>:1:22: expected expression
$ echo "struct {
    Specification ModelAPISpecification `json:"apiSpecification"`
    TestCases []TestCase `json:"testCases"`
}" | gofmt -e
zsh: command not found: json:apiSpecification
zsh: command not found: json:testCases
<standard input>:1:22: expected expression
$ echo 'struct {
    Specification ModelAPISpecification `json:"apiSpecification"`
    TestCases []TestCase `json:"testCases"`
}' | gofmt -e
<standard input>:1:22: expected expression
$ echo 'var struct {
    Specification ModelAPISpecification `json:"apiSpecification"`
    TestCases []TestCase `json:"testCases"`
}' | gofmt -e
<standard input>:1:15: expected 'IDENT', found 'struct'
<standard input>:1:15: missing variable type or initialization
<standard input>:1:22: expected ';', found '{'
$ echo 'var c struct {
    Specification ModelAPISpecification `json:"apiSpecification"`
    TestCases []TestCase `json:"testCases"`
}' | gofmt -e
var c struct {
	Specification ModelAPISpecification `json:"apiSpecification"`
	TestCases     []TestCase            `json:"testCases"`
}

Seems to be doing the right thing...

@mbana

good idea, I will try this in gopls

@inliquid @mbana

Please use the enhanced version of gopls, I have fixed the issue in gopls.

Thanks, I see you fixed it in this commit saibing/tools@77df414.

I've tried it and it works for struct that are not anonymous.

I think this check might be appropriate here: https://github.com/kubernetes/gengo/blob/master/types/types.go#L357s

// IsAnonymousStruct returns true if the type is an anonymous struct or an alias
// to an anonymous struct.
func (t *Type) IsAnonymousStruct() bool {
	return (t.Kind == Struct && t.Name.Name == "struct{}") || (t.Kind == Alias && t.Underlying.IsAnonymousStruct())

I should have done it initially when I did the pull request, sorry, I just discovered this.

Anyway, should you enable issue reporting in https://github.com/saibing/tools