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

Long go files cause syntax highlighting to think everything is a string #1447

Closed
cgonyeo opened this issue Sep 13, 2017 · 4 comments
Closed

Comments

@cgonyeo
Copy link

cgonyeo commented Sep 13, 2017

Behavior

Sometimes I'll be editing a somewhat long (1000 lines +) go file, and everything is working great until I go to save the file. On a :w the entirety of what I can see in a file will change to be colored as if it's a string (yellow in my colorscheme).

If I scroll up in the file, eventually I'll hit some section that isn't colored like a string, and then if I scroll back to where I was everything is colored correctly. I can even use this trick to make the same part of a file colored both correctly and not correctly in two different vim windows.

Here's a screenshot of that happening:
2017-09-13_11 21 53_3200x1800_scrot

I have vim set to run gofmt on write, so my uneducated guess as to what's happening is that vim is saving the file, gofmt does its thing, and then vim loads in less of the file than it had saved before. The syntax highlighting starts parsing from the start of what vim has loaded, and if it starts in the middle of a string everything gets confused.

Running :e sometimes fixes the problem (but usually doesn't), and changing some text and running :w sometimes fixes the problem (but usually doesn't).

Steps to reproduce:

Here's a file that I believe shows this problem. There's a very long string in the beginning of the file, and thus by the end of the file the beginning of whatever buffer vim has loaded will be in the string. For all I know though, this could be a related problem and not exactly what I'm experiencing.

https://gist.github.com/dgonyeo/31f1c3076328dbb6719337d3ab5cf5ac

Opening that file with vim, and pressing G to go to the bottom, note that the main function is not colored correctly.

I reproduced this in a fedora docker container (so no preexisting configuration), installing git, go, and vim and installing vim-go with the following command:

git clone https://github.com/fatih/vim-go.git ~/.vim/pack/plugins/start/vim-go

There was no .vimrc or other plugins.

Configuration

Here's a wall of text with information about the environment I reproduced this in:

[root@11b6ca7adaf4 /]# vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Sep  1 2017 13:16:51)
Included patches: 1-1030
Modified by <[email protected]>
Compiled by <[email protected]>
Huge version without GUI.  Features included (+) or not (-):
+acl             +file_in_path    +mouse_sgr       +tag_old_static
+arabic          +find_in_path    -mouse_sysmouse  -tag_any_white
+autocmd         +float           +mouse_urxvt     -tcl
-balloon_eval    +folding         +mouse_xterm     +termguicolors
-browse          -footer          +multi_byte      -terminal
++builtin_terms  +fork()          +multi_lang      +terminfo
+byte_offset     +gettext         -mzscheme        +termresponse
+channel         -hangul_input    +netbeans_intg   +textobjects
+cindent         +iconv           +num64           +timers
-clientserver    +insert_expand   +packages        +title
-clipboard       +job             +path_extra      -toolbar
+cmdline_compl   +jumplist        +perl/dyn        +user_commands
+cmdline_hist    +keymap          +persistent_undo +vertsplit
+cmdline_info    +lambda          +postscript      +virtualedit
+comments        +langmap         +printer         +visual
+conceal         +libcall         +profile         +visualextra
+cryptv          +linebreak       +python/dyn      +viminfo
+cscope          +lispindent      +python3/dyn     +vreplace
+cursorbind      +listcmds        +quickfix        +wildignore
+cursorshape     +localmap        +reltime         +wildmenu
+dialog_con      +lua/dyn         +rightleft       +windows
+diff            +menu            +ruby/dyn        +writebackup
+digraphs        +mksession       +scrollbind      -X11
-dnd             +modify_fname    +signs           -xfontset
-ebcdic          +mouse           +smartindent     -xim
+emacs_tags      -mouseshape      +startuptime     -xpm
+eval            +mouse_dec       +statusline      -xsmp
+ex_extra        +mouse_gpm       -sun_workshop    -xterm_clipboard
+extra_search    -mouse_jsbterm   +syntax          -xterm_save
+farsi           +mouse_netterm   +tag_binary      
   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/etc"
 f-b for $VIMRUNTIME: "/usr/share/vim/vim80"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H     -O2 -g -pipe -Wall -Werror=format-security -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/python3.6m -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1       
Linking: gcc   -L. -Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,--enable-new-dtags -Wl,-z,relro   -Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -L/usr/local/lib -Wl,--as-needed -o vim        -lm -lnsl  -lselinux   -lncurses -lacl -lattr -lgpm -ldl   -Wl,--enable-new-dtags -Wl,-z,relro  -Wl,-z,relro  -fstack-protector-strong -L/usr/local/lib  -L/usr/lib64/perl5/CORE -lperl -lpthread -lresolv -lnsl -ldl -lm -lcrypt -lutil -lc 
[root@11b6ca7adaf4 /]# go version
go version go1.8.3 linux/amd64
[root@11b6ca7adaf4 /]# cd /root/.vim/pack/plugins/start/vim-go/
[root@11b6ca7adaf4 vim-go]# git show
commit 262fb6308e81c086bf01fa0ca42bbb9e570c6298 (HEAD -> master, origin/master, origin/HEAD)
Author: Fatih Arslan <[email protected]>
Date:   Tue Sep 12 11:25:19 2017 +0300

    doc: add example for go_list_type_commands

diff --git a/doc/vim-go.txt b/doc/vim-go.txt
index 6803d14..d1f8cd9 100644
--- a/doc/vim-go.txt
+++ b/doc/vim-go.txt
@@ -1469,6 +1469,12 @@ Supported keys are "GoBuild", "GoErrCheck", "GoFmt", "GoInstall", "GoLint",
 >
   let g:go_list_type_commands = {}
 <
+As an example, the following settings will change all list types to
+`locationlist` except for `:GoBuild` where `quickfix` is used:
+>
+  let g:go_list_type = "locationlist"
+  let g:go_list_type_commands = {"GoBuild": "quickfix"}
+<
 
                                                        *'g:go_list_autoclose'*
 

For what it's worth, I have the same problem in neovim.

@cgonyeo
Copy link
Author

cgonyeo commented Sep 13, 2017

Oh and FWIW vim-go was definitely installed in vim despite the lack of .vimrc (I guess I've never seen vim 8 packages before), because :GoInstallBinaries worked fine.

@arp242
Copy link
Contributor

arp242 commented Sep 13, 2017

The core problem is that "syntax highlighting" in Vim is a series of regular expressions. Because you don't want to re-run that on the entire buffer on every screen update Vim tries to be a bit smarter about what to update.

Unfortunately this sometimes fails, and stuff gets highlighted wrong; the large the file is, the greater the chance that this will happen.

This is kind of a limitation of Vim's syntax highlighting, and it's not a problem in just vim-go; I've seen it with many filetypes.

Usually doing a screen update <C-l> or :redraw will fix it; you can also run :syntax sync fromstart. I actually map that (and some other stuff) to <C-l> with:

nnoremap <silent> <C-l> :nohlsearch<CR>:setl nolist nospell<CR>:diffupdate<CR>:syntax sync fromstart<CR><C-l>

I also run it when entering a buffer:

augroup basic
    autocmd!
    autocmd BufEnter * syntax sync fromstart
augroup end

You can also try setting synmaxcol to a lower value; I set it to 500 (default is 3000).

Maybe we can run this automatically after running gofmt?

@cgonyeo
Copy link
Author

cgonyeo commented Sep 13, 2017

Oh cool, that sounds like it's a nice workaround.

+1 to automatically doing that after gofmt.

@arp242
Copy link
Contributor

arp242 commented Nov 24, 2017

PR here: #1582

arp242 added a commit that referenced this issue Nov 25, 2017
This should ensure that it breaks less often. The downside is that it's
a bit slower, but it still seems plenty fast enough on my system even
with large files.

Fixes #1447
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants