Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gopls does not suggest function written after the cursor position in the same file #32158

Closed
sluongng opened this issue May 21, 2019 · 1 comment

Comments

@sluongng
Copy link

sluongng commented May 21, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/son.luongngoc/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/son.luongngoc/work/golang"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.5/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ys/th7kt1f55_d4_n9lg3575851v7_0yb/T/go-build414786608=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Given the code below

package main

import (
    "bytes"
    "crypto/sha256"
    "strconv"
    "time"
)

type Block struct {
    Timestamp     int64
    Data          []byte
    PrevBlockHash []byte
    Hash          []byte
}

func NewGenesisBlock() *Block {
    return NewBlock("Genesis Block", []byte{})
}

func NewBlock(data string, prevBlockHash []byte) *Block {
    block := &Block{
        time.Now().Unix(),
        []byte(data),
        prevBlockHash,
        []byte{},
    }
    block.SetHash()

    return block
}

func (b *Block) SetHash() {
    timestamp := []byte(strconv.FormatInt(b.Timestamp, 10))
    headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{})
    hash := sha256.Sum256(headers)

    b.Hash = hash[:]
}

At line 28 block.SetHash(), if you write block. to trigger gopls suggestion (using Coc.Nvim), the SetHash() func is not suggested.

However if you move the code SetHash() on top of NewBlock() like so:

package main

import (
	"bytes"
	"crypto/sha256"
	"strconv"
	"time"
)

type Block struct {
	Timestamp     int64
	Data          []byte
	PrevBlockHash []byte
	Hash          []byte
}

func NewGenesisBlock() *Block {
	return NewBlock("Genesis Block", []byte{})
}

func (b *Block) SetHash() {
	timestamp := []byte(strconv.FormatInt(b.Timestamp, 10))
	headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{})
	hash := sha256.Sum256(headers)

	b.Hash = hash[:]
}

func NewBlock(data string, prevBlockHash []byte) *Block {
	block := &Block{
		time.Now().Unix(),
		[]byte(data),
		prevBlockHash,
		[]byte{},
	}
	block.SetHash()

	return block
}

gopls now return suggestion for block.SetHash() correctly

Below is exchange log between Coc.nvim and gopls showing that the completion response from gopls is missing SetHash() function

Working Log with SetHash position moved up in source code

[Trace - 2:05:29 PM] Sending request 'textDocument/completion - (16)'.
Params: {
    "textDocument": {
        "uri": "file:///Users/son.luongngoc/work/golang/src/github.com/sluongng/golang-blockchain/block.go"
    },
    "position": {
        "line": 36,
        "character": 7
    },
    "context": {
        "triggerKind": 2,
        "triggerCharacter": "."
    }
}


[Trace - 2:05:29 PM] Received response 'textDocument/completion - (16)' in 2ms.
Result: {
    "isIncomplete": false,
    "items": [
        {
            "label": "SetHash()",
            "kind": 2,
            "preselect": true,
            "sortText": "00000",
            "filterText": "SetHash",
            "insertTextFormat": 2,
            "textEdit": {
                "range": {
                    "start": {
                        "line": 36,
                        "character": 7
                    },
                    "end": {
                        "line": 36,
                        "character": 7
                    }
                },
                "newText": "SetHash()"
            },
            "command": {
                "title": "",
                "command": "editor.action.triggerParameterHints"
            }
        },
        {
            "label": "Timestamp",
            "kind": 5,
            "detail": "int64",
            "sortText": "00001",
            "filterText": "Timestamp",
            "insertTextFormat": 2,
            "textEdit": {
                "range": {
                    "start": {
                        "line": 36,
                        "character": 7
                    },
                    "end": {
                        "line": 36,
                        "character": 7
                    }
                },
                "newText": "Timestamp"
            }
        },
        {
            "label": "Data",
            "kind": 5,
            "detail": "[]byte",
            "sortText": "00002",
            "filterText": "Data",
            "insertTextFormat": 2,
            "textEdit": {
                "range": {
                    "start": {
                        "line": 36,
                        "character": 7
                    },
                    "end": {
                        "line": 36,
                        "character": 7
                    }
                },
                "newText": "Data"
            }
        },
        {
            "label": "PrevBlockHash",
            "kind": 5,
            "detail": "[]byte",
            "sortText": "00003",
            "filterText": "PrevBlockHash",
            "insertTextFormat": 2,
            "textEdit": {
                "range": {
                    "start": {
                        "line": 36,
                        "character": 7
                    },
                    "end": {
                        "line": 36,
                        "character": 7
                    }
                },
                "newText": "PrevBlockHash"
            }
        },
        {
            "label": "Hash",
            "kind": 5,
            "detail": "[]byte",
            "sortText": "00004",
            "filterText": "Hash",
            "insertTextFormat": 2,
            "textEdit": {
                "range": {
                    "start": {
                        "line": 36,
                        "character": 7
                    },
                    "end": {
                        "line": 36,
                        "character": 7
                    }
                },
                "newText": "Hash"
            }
        }
    ]
}


NOT WORKING LOG when moved the SetHash definition down


[Trace - 2:07:00 PM] Sending request 'textDocument/completion - (18)'.
Params: {
    "textDocument": {
        "uri": "file:///Users/son.luongngoc/work/golang/src/github.com/sluongng/golang-blockchain/block.go"
    },
    "position": {
        "line": 28,
        "character": 7
    },
    "context": {
        "triggerKind": 2,
        "triggerCharacter": "."
    }
}

[Trace - 2:07:00 PM] Received response 'textDocument/completion - (18)' in 2ms.
Result: {
    "isIncomplete": false,
    "items": [
        {
            "label": "Timestamp",
            "kind": 5,
            "detail": "int64",
            "preselect": true,
            "sortText": "00000",
            "filterText": "Timestamp",
            "insertTextFormat": 2,
            "textEdit": {
                "range": {
                    "start": {
                        "line": 28,
                        "character": 7
                    },
                    "end": {
                        "line": 28,
                        "character": 7
                    }
                },
                "newText": "Timestamp"
            }
        },
        {
            "label": "Data",
            "kind": 5,
            "detail": "[]byte",
            "sortText": "00001",
            "filterText": "Data",
            "insertTextFormat": 2,
            "textEdit": {
                "range": {
                    "start": {
                        "line": 28,
                        "character": 7
                    },
                    "end": {
                        "line": 28,
                        "character": 7
                    }
                },
                "newText": "Data"
            }
        },
        {
            "label": "PrevBlockHash",
            "kind": 5,
            "detail": "[]byte",
            "sortText": "00002",
            "filterText": "PrevBlockHash",
            "insertTextFormat": 2,
            "textEdit": {
                "range": {
                    "start": {
                        "line": 28,
                        "character": 7
                    },
                    "end": {
                        "line": 28,
                        "character": 7
                    }
                },
                "newText": "PrevBlockHash"
            }
        },
        {
            "label": "Hash",
            "kind": 5,
            "detail": "[]byte",
            "sortText": "00003",
            "filterText": "Hash",
            "insertTextFormat": 2,
            "textEdit": {
                "range": {
                    "start": {
                        "line": 28,
                        "character": 7
                    },
                    "end": {
                        "line": 28,
                        "character": 7
                    }
                },
                "newText": "Hash"
            }
        }
    ]
}

What did you expect to see?

gopls should return the correct suggestion function even if its written after the current cursor on the same file

What did you see instead?

suggestion function is missing on gopls RPC call

@stamblerre
Copy link
Contributor

Duplicate of #31687

@stamblerre stamblerre marked this as a duplicate of #31687 May 21, 2019
@golang golang locked and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants