vim-go icon indicating copy to clipboard operation
vim-go copied to clipboard

Minor Improvement to :GoFmt Requested

Open nobozo opened this issue 3 years ago • 8 comments

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

I setup vim 8.2.4386 on Fedora 35 Server x86_64 to run :GoFmt when I write out a Go file. This works fine, except because I make a lot of mistakes, the :GoFmt output display often doesn't start at error #1. This means I have to scroll the :GoFmt up to the first error. I've attached a screen dump to this report.

I only have to save a Go file.

What did you expect to happen?

I'd like the error display to start with error #1

What happened instead?

The error display starts with an error after #1.

Configuration (MUST fill this out):

vim-go version:

I'm not sure how to see this. It's the version that I get today 2/17/2022.

vimrc you used to reproduce:

vimrc

Vim version (first three lines from :version):

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Feb 15 2022 00:00:00) Included patches: 1-4386 Modified by [email protected]

Go version (go version):

go version go1.17.7 linux/amd64

Go environment

go env Output:

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jonf/.cache/go-build"
GOENV="/home/jonf/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/jonf/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jonf/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.7"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build198855228=/tmp/go-build -gno-record-gcc-switches"

gopls version

gopls version Output:

golang.org/x/tools/gopls v0.7.5
    golang.org/x/tools/[email protected] h1:8Az52YwcFXTWPvrRomns1C0N+zlgTyyPKWvRazO9GG8=
vim

nobozo avatar Feb 18 '22 04:02 nobozo

What value are you using for g:go_fmt_command?

bhcleek avatar Feb 18 '22 05:02 bhcleek

On 2/17/2022 9:48 PM, Billie Cleek wrote:

What value are you using for |g:go_fmt_command|?

I'm not sure. I'm just using all the defaults. There's no such line in my .vimrc file. How could I see what's being used?

Thanks, Jon

nobozo avatar Feb 18 '22 05:02 nobozo

Open vim and run :echo g:go_fmt_command. That should show the current value.

telemachus avatar Feb 18 '22 12:02 telemachus

This variable isn't defined. I've attached a screen dump of what I see right after trying to write out a Go file. GoFmt is working fine, except for the irritation of not starting its window with the first error.

fmt .

nobozo avatar Feb 18 '22 19:02 nobozo

I'm having a hard time getting enough errors and of the right types to duplicate what you're seeing. Do you have a file I could use as a test case?

bhcleek avatar Feb 18 '22 19:02 bhcleek

On 2/18/2022 11:36 AM, Billie Cleek wrote:

I'm having a hard time getting enough errors and of the right types to duplicate what you're seeing. Do you have a file I could use as a test case?

That file I'm showing in the screen dump is part of a much larger package. I'll try create a minimal file that shows the problem.

Thanks for looking at this.

Jon

nobozo avatar Feb 18 '22 19:02 nobozo

I've been able to duplicate this with g:go_fmt_command set to gofmt, but not with the default, gopls -- gopls seems to abbreviate the number of errors in the cases I've tested with. That's ok, though, the overall effect is the same.

I don't see a good way to handle this from within vim-go, though. In my testing, the current cursor location in the location list window seems to depend on where the cursor is in the code window; I've seen the location list window be as you want it to be, as it is in your examples, and somewhere in the middle.

When vim-go opens the location list window, it sets the height according to g:go_list_height, and that seems to play some role in the behavior you're seeing, though I don't see a way to tell vim whether to reduce from the bottom of the window or from the top 🤔

bhcleek avatar Feb 19 '22 00:02 bhcleek

Thanks for following up on this. I'm surprised I'm the first person to mention this.

I'll leave it to you to figure out the solution. I've never played with vim scripting so I have nothing to contribute.

Good luck!

Jon

nobozo avatar Feb 19 '22 00:02 nobozo

I took another look at this today, and I realized that it would be relatively easy to solve by including a call in the right place in the formatting handlers, but I'm a little reluctant to do so. I'm not sure if it's the right thing to do when formatting. I'm in the habit of saving often and sometimes my code above the cursor line is not yet in a state where I'd want to fix it. Jumping the cursor around in that case would be disruptive, and I suspect others would feel the same.

However, there's a simple solution for you when do want to jump to the first. Assuming you're using the default list settings for the formatting errors, a :ll 1 will take you to the first error location.

bhcleek avatar Apr 23 '23 20:04 bhcleek