Skip to content

Syntax Highlighting Breaks In Multi-language Buffers #10452

@Swivelgames

Description

@Swivelgames

This issued is based on the comment by @Swivelgames in #2790

Anyone still experiencing syntax highlighting issues with large files should comment in the previous ticket or make a new one. This issue is specifically related to multi-language buffers.

Syntax Highlighting Breaks In Multi-language Buffers

Reproducible in VIM 8.2 on multiple platforms
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled May 13 2022 20:56:28)
macOS version - x86_64
Included patches: 1-4950
Compiled by Homebrew
Huge version without GUI.  Features included (+) or not (-):
+acl               +file_in_path      +mouse_urxvt       -tag_any_white
+arabic            +find_in_path      +mouse_xterm       -tcl
+autocmd           +float             +multi_byte        +termguicolors
+autochdir         +folding           +multi_lang        +terminal
-autoservername    -footer            -mzscheme          +terminfo
-balloon_eval      +fork()            +netbeans_intg     +termresponse
+balloon_eval_term +gettext           +num64             +textobjects
-browse            -hangul_input      +packages          +textprop
++builtin_terms    +iconv             +path_extra        +timers
+byte_offset       +insert_expand     +perl              +title
+channel           +ipv6              +persistent_undo   -toolbar
+cindent           +job               +popupwin          +user_commands
-clientserver      +jumplist          +postscript        +vartabs
+clipboard         +keymap            +printer           +vertsplit
+cmdline_compl     +lambda            +profile           +vim9script
+cmdline_hist      +langmap           -python            +viminfo
+cmdline_info      +libcall           +python3           +virtualedit
+comments          +linebreak         +quickfix          +visual
+conceal           +lispindent        +reltime           +visualextra
+cryptv            +listcmds          +rightleft         +vreplace
+cscope            +localmap          +ruby              +wildignore
+cursorbind        +lua               +scrollbind        +wildmenu
+cursorshape       +menu              +signs             +windows
+dialog_con        +mksession         +smartindent       +writebackup
+diff              +modify_fname      -sodium            -X11
+digraphs          +mouse             -sound             -xfontset
-dnd               -mouseshape        +spell             -xim
-ebcdic            +mouse_dec         +startuptime       -xpm
+emacs_tags        -mouse_gpm         +statusline        -xsmp
+eval              -mouse_jsbterm     -sun_workshop      -xterm_clipboard
+ex_extra          +mouse_netterm     +syntax            -xterm_save
+extra_search      +mouse_sgr         +tag_binary        
-farsi             -mouse_sysmouse    -tag_old_static    
   system vimrc file: "$VIM/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: "/usr/local/share/vim"
Compilation: clang -c -I. -Iproto -DHAVE_CONFIG_H -DMACOS_X -DMACOS_X_DARWIN -g -O2 -D_REENTRANT -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 
Linking: clang -L. -fstack-protector-strong -L/usr/local/lib -L/usr/local/opt/libyaml/lib -L/usr/local/opt/openssl@1.1/lib -L/usr/local/opt/readline/lib -L/usr/local/lib -o vim -lm -lncurses -liconv -lintl -framework AppKit -L/usr/local/opt/lua/lib -llua5.4 -mmacosx-version-min=12.0 -fstack-protector-strong -L/usr/local/lib -L/usr/local/Cellar/perl/5.34.0/lib/perl5/5.34.0/darwin-thread-multi-2level/CORE -lperl -L/usr/local/opt/python@3.10/Frameworks/Python.framework/Versions/3.10/lib/python3.10/config-3.10-darwin -lpython3.10 -framework CoreFoundation -lruby.3.1 -L/usr/local/Cellar/ruby/3.1.2/lib 

When editing a file that contains more than one syntax-highlighted language, certain scenarios cause VIM to revert to highlighting only the primary language.

@Tao-Quixote had a great example of this, where a file that is using HTML/Jade, CSS/SASS/Stylus, and JavaScript/TypeScript/CoffeeScript appears to only highlight the first parent syntax language after the breakage occurs.

So far, the only thing that works is manually running :syntax sync fromstart whenever this happens.

Reproduction Steps

The easiest way to reproduce the issue, that I have found, is to:

  • Open VIM (# vim)
  • Edit the problematic file (:e my-file) (In my case, it was a ~1000 line Vue.js file)
  • Jump to the bottom of the problematic file (G)
  • Open a new Buffer in the same window (:e some-other-file)
  • Switch back to the problematic buffer (:bp)

Expected

  • Syntax highlighting will be applied appropriately for all languages present in the file

Actual

  • Syntax highlighting is only performed for primary language; Remaining languages are either highlighted with that syntax, or not at all.
Example File

Example derived from the comment by @Tao-Quixote in #2790

<template>
  <div class="cntr">
    <!-- ... -->
  </div>
</template>

<style>
  .cntr {
    color: red;
    /* ... */
  }
</style>

<script>
  export default {
    props: {
      ...
    },
    methods: {
      function foo () {}
    }
  }
</script>

Blah, blah, blah. Proper name. Place name. Backstory stuff...

EDIT: This section used to be collapsed. I removed the collapsible portion to make sure this section does not get overlooked, since it already addresses the first response to this ticket.

This issue is based on the original issue outlined in #2790. However, upon further inspection, I believe there are actually two separate problems being discussed in that story:

  • Syntax Highlighting fails for large files (this is most likely due to redrawtime)
  • Syntax Highlighting inconsistently applies syntax when multiple syntaxes are present in a file

The former may be fixed sometimes with one of these two configuration changes, because syntax highlighting seems to halt when it takes longer to execute than the threshold defined by redrawtime:

set redrawtime=10000 " If something breaks, you may have to wait 10 seconds for VIM to unfreeze
set re=0 " Force VIM to use the new RegEx engine, because it's faster/more-efficient/etc

The latter issue mentioned above is fixed during each occurrence by running :syntax sync fromstart.

These two solutions are mutally exclusive to their respective issues, highlighting the separation (no pun intended).

Upon inspection, both of these issues are valid and continue to garner attention despite the issue remaining closed. Neither of these should be broken out of the box, of course, as these are major usability issues. And, of course, anything should still be considered a bug if the solution is to introduce extraneous configuration in order to make a particular feature work consistently; Especially if that configuration is not immediately obvious, requires excessive values, and requires the user to dig around through closed issues in order to find the workaround. It may be worth it to explore whether or not the former issue is still valid.

However, again, discussions related to issues with syntax highlighting for large files should be limited to the previous ticket or a new one. This issue is specifically related to multi-language buffers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions