fatih / vim-go

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

GoExtract sends incorrect location to LSP #3615

Closed novadev94 closed 6 months ago

novadev94 commented 6 months ago

What did you do?

I tried to use :GoExtract on lines of code. Take this code as an example

package main        // 1
                    // 2
import "fmt"        // 3
                    // 4
func main() {       // 5
    fmt.Println(1)  // 6
    fmt.Println(2)  // 7
    fmt.Println(3)  // 8
}                   // 9

I selected lines 6 -> 8 (either via linewise visual mode or regular one) then run :'<,'>GoExtract.

What did you expect to happen?

All the 3 fmt.Println lines get extracted to a new function, resulting in something like this.

package main

import "fmt"

func main() {
    newFunction()
}

func newFunction() {
    fmt.Println(1)
    fmt.Println(2)
    fmt.Println(3)
}

What happened instead?

Only the last 2 fmt.Println lines get extracted. Resulting in.

package main

import "fmt"

func main() {
    fmt.Println(1)
    newFunction()
}

func newFunction() {
    fmt.Println(2)
    fmt.Println(3)
}

Configuration (MUST fill this out):

vim-go version:

Commit 7fb38264e3ab5c57a43de2470f22eccc62f82c02 (latest commit on master).

vimrc you used to reproduce:

vimrc ```vim call plug#begin('~/.nvim/.plugs') Plug 'fatih/vim-go' call plug#end() ```

Vim version (first three lines from :version):

NVIM v0.9.4
Build type: Release
LuaJIT 2.1.1700008891

Go version (go version):

go version go1.21.5 darwin/arm64

Go environment

go env Output:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/novadev/Library/Caches/go-build'
GOENV='/Users/novadev/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/novadev/.go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/novadev/.go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.21.5/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.21.5/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.5'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/lr/52fjbm9x3gd6qq33hmy91mxw0000gn/T/go-build3061960217=/tmp/go-build -gno-record-gcc-switches -fno-common'

gopls version

gopls version Output:

golang.org/x/tools/gopls (devel)
    golang.org/x/tools/gopls@(devel)
bhcleek commented 6 months ago

I can duplicate what you're describing. Interestingly, Vim's help has this to say about the visual markers:

                            *'<* *`<*
'<  `<          To the first line or character of the last selected
            Visual area in the current buffer.  For block mode it
            may also be the last character in the first line (to
            be able to define the block).

                            *'>* *`>*
'>  `>          To the last line or character of the last selected
            Visual area in the current buffer.  For block mode it
            may also be the first character of the last line (to
            be able to define the block).  Note that 'selection'
            applies, the position may be just after the Visual
            area.

Which corresponds to what we're seeing here. I'll see if there's a correct way to figure out the full extent of the visual selection.