fatih / vim-go

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

Why is vim-go removing semicolons? #3658

Closed bjt-user closed 6 months ago

bjt-user commented 6 months ago

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

I wrote a go program with a semicolon at the end of a statement:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!");
}

Then I saved the file.

What did you expect to happen?

Since semicolons are optional I would expect them to remain in the code when the author/programmer wants to use them.

What happened instead?

The semicolon disappeared.

Configuration (MUST fill this out):

vim-go version:

I guess 1.28. master branch was cloned on 2024-04-13.

vimrc you used to reproduce:

" colors

if $COLOR=='OFF'
  set t_Co=0
endif

set background=dark

" use different colors for vimdiff
if &diff
    " Vimdiff custom color scheme
    hi DiffAdd ctermfg=black ctermbg=cyan
    hi DiffDelete ctermfg=black ctermbg=red
    hi DiffChange ctermfg=black ctermbg=lightgrey
    hi DiffText ctermfg=black ctermbg=red cterm=bold
else
    " when specifiying a color scheme all custom colors won't take effect
    color default
endif

filetype on

filetype plugin on
syntax on

" replace tabs with spaces?
"set expandtab

set tabstop=4

" use 2 spaces when indenting with << or >> "
set shiftwidth=4

set autoindent

set smartindent

" dont scroll down before cursor is at end of the page
set scrolloff=0

set number

" open new splits right (vsplit) or below (hsplit)
set splitbelow
set splitright

"KEYBINDINGS (keybindings for plugins are further down)

"set the leader key:
let mapleader=","

"trying to use my own plugin: (is added in ftplugin/sh_comment.vim now)
"map <unique> <Leader>c :call Commenter() <CR>

nmap <F1> :w <bar> so % <CR>
nmap <F2> :w <bar> !clear;chmod +x % <CR>

" <bar> is needed to change between internal vim commands and terminal commands
" shortcut to save file, clear terminal and execute file:
nmap <F5> :w <bar> !clear;%:p <CR>

nmap <F6> :w <bar> !clear;bash -x % <CR>

nmap <F7> :w <bar> !clear;python3 % <CR>

nmap <F8> :w <bar> !clear;gcc -Wall *.c && ./a.out <CR>

"improving switching between windows
"(ctrl+h to go left)
"(ctrl+l to go right)
"(ctrl+k to go up)
"(ctrl+j to go down)
nmap <C-j> <C-W>j
nmap <C-k> <C-W>k
nmap <C-h> <C-W>h
nmap <C-l> <C-W>l

" STATUS LINE ------------------------------------------------------------ {{{

set statusline=

set statusline+=%F
set statusline+=\ %M
set statusline+=\ %Y
set statusline+=\ %R
set statusline+=\ %{&fileencoding}

" Use a divider to separate the left side from the right side.
set statusline+=%=

" Status line right side.
set statusline+=\ row:\ %l
set statusline+=\ col:\ %c
set statusline+=\ percent:\ %p%%

" Show the status on the second to last line.
set laststatus=2
"
" " }}}
"

" ----------------- snippets from chatgpt -----------------------------------
" Define the path where the snippets will be stored
let g:snippets_path = $HOME . "/.vim/snippets"

" Define the command to add a new snippet
command! -nargs=+ AddSnippet call AddSnippet(<f-args>)
function! AddSnippet(name)
  let snippet = input("Enter the code for " . a:name . ": ")
  let file = g:snippets_path . "/" . a:name . ".snip"
  call writefile([snippet], file)
  echo "Snippet " . a:name . " added!"
endfunction

" Define the command to load a snippet
command! -nargs=1 LoadSnippet call LoadSnippet(<f-args>)
function! LoadSnippet(name)
  let file = g:snippets_path . "/" . a:name . ".snip"
  if filereadable(file)
    let snippet = join(readfile(file))
    call setreg('"', snippet)
    echo "Snippet " . a:name . " loaded!"
  else
    echo "Snippet " . a:name . " not found."
  endif
endfunction

Vim version (first three lines from :version):

VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Apr 01 2024 14:35:29)
Included patches: 1-252
Compiled by Arch Linux

Go version (go version):

go version go1.22.2 linux/amd64

Go environment

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/bf/.cache/go-build'
GOENV='/home/bf/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/bf/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/bf/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.2'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1076461058=/tmp/go-build -gno-record-gcc-switches'

gopls version

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

Vim-go formats Go code with the formatter of your choice (see :help g:go_fmt_command) on save (see :help g:go_fmt_autosave). All the formatters provide the same style; that of gofmt. While semicolons are optional in Go, the community consistently uses gofmt's style.

https://go.dev/doc/effective_go#semicolons tells us

Idiomatic Go programs have semicolons only in places such as for loop clauses, to separate the initializer, condition, and continuation elements. They are also necessary to separate multiple statements on a line, should you write code that way.

You may be interested in reading https://go.dev/blog/gofmt, too, to learn about gofmt; it's been standard in the community for years now.

One of the Go Proverbs is Gofmt's style is no one's favorite, yet gofmt is everyone's favorite.

bjt-user commented 6 months ago

Thank you for the explanation @bhcleek .