lervag / vimtex

VimTeX: A modern Vim and neovim filetype plugin for LaTeX files.
MIT License
5.45k stars 388 forks source link

Highlight illegal characters #2960

Closed Unturned3 closed 4 months ago

Unturned3 commented 4 months ago

Description

The syntax highlighting that vimtex provides doesn't seem to highlight illegal characters, e.g. an underscore in the document body.

Steps to reproduce

minimal.vim

set runtimepath^=~/.vim
let &packpath = &runtimepath
call plug#begin()
    Plug 'lervag/vimtex'
call plug#end()

minimal.tex

\documentclass{article}
\begin{document}
Illegal characters should be highlighted: _
\end{document}

Expected behavior

If I run nvim -u none minimal.tex (i.e. default colorscheme, not loading vimtex), then the illegal underscore is highlighted red.

Screenshot 2024-05-28 at 22 05 15

Actual behavior

If I run nvim -u minimal.vim minimal.tex, the illegal underscore appears normal.

Screenshot 2024-05-28 at 22 05 03

Do you use a latexmkrc file?

No

VimtexInfo

System info:           
  OS: macOS 12.7.1 (21G920)
  Vim version: NVIM v0.9.5                 
  Has clientserver: true
  Servername: /var/folders/3d/_tm29bhn48l851qxdsg5w39m0000gn/T/nvim.richard/YOxDmr/nvim.72266.0

VimTeX project: test
  base: test.tex
  root: /Users/richard/Desktop/vimtex
  tex: /Users/richard/Desktop/vimtex/test.tex
  main parser: current file verified
  document class: article
  compiler: latexmk
    engine: -pdf
    options:
      -verbose
      -file-line-error
      -synctex=1
      -interaction=nonstopmode
    callback: 1
    continuous: 1
    executable: latexmk
  viewer: General
  qf method: LaTeX logfile
lervag commented 4 months ago

The syntax highlighting that vimtex provides doesn't seem to highlight illegal characters, e.g. an underscore in the document body.

I decided quite early on that I did not want to make the syntax highlighting more complex than necessary. I figured, for instance, that some of the error highlighting was more or less unnecessary. So, first, I want to say that I won't aim to match everything that may be wrong as an error.

However, I think this particular case is straightforward and it will probably bring some value. So, I'll add a match for "loose" ^ and _.

Are there other error cases you think I should consider?

Unturned3 commented 4 months ago

Gotcha! What are some examples of error highlighting that you considered to be unnecessary?

Highlighting rogue ^ and _ characters is the only problem I have in mind for now, because I always write these in my tex files by accident. I'll let you know if I run into other errors that might be useful if highlighted.

Thanks for this plugin by the way! I'm having a lot of fun writing my thesis with it.

lervag commented 4 months ago

Gotcha! What are some examples of error highlighting that you considered to be unnecessary?

My point is that we, in general, don't want the syntax engine to be responsible for highlighting errors and warnings. Instead, this should be delegated to a linter, which should be much more capable of stuff like this.

Still, for the most basic things that are also easy to implement, I can agree it makes sense to take a pragmatic action and let it be highlighted by the syntax rules as well.

Highlighting rogue ^ and _ characters is the only problem I have in mind for now, because I always write these in my tex files by accident. I'll let you know if I run into other errors that might be useful if highlighted.

Great! Then I'll close the issue; feel free to open a new one if you notice other things that seem "trivial" to match with syntax rules. I might be persuaded to make more "pragmatic" choices ;)

Thanks for this plugin by the way! I'm having a lot of fun writing my thesis with it.

Glad to hear it!

lervag commented 4 months ago

Notice that you may be interested in reading :help vimtex-af-linting.

jrotheneder commented 4 months ago

A drawback of highlighting underscores as syntax errors is that it generates false positives in some cases, e.g. \myref{appendix_A}.

lervag commented 4 months ago

false positives in some cases, e.g. \myref{appendix_A}.

Yes, that's also a valid and good point, and one of the reasons I decided to remove it/not include this in the first place.

However, I don't think there will be many false positives today, as most \ref style commands should be supported out of the box. If you insist on creating your own, like \myref, then you can easily support it as a reference style command with something like this:

let g:vimtex_syntax_custom_cmds = [
      \ {
      \   'name': 'myref',
      \   'hlgroup': 'texCmdRef',
      \   'argspell': 0,
      \   'nextgroup': 'texRefArg'
      \ },
      \]
jrotheneder commented 4 months ago

I was not aware of vimtex_syntax_custom_cmds, thanks (and, btw, many thanks for the amazing vimtex)! Can this be used with two-argument commands, such as \crefpart{big_theorem}{item_a}?

lervag commented 4 months ago

I was not aware of vimtex_syntax_custom_cmds, thanks

No problem!

(and, btw, many thanks for the amazing vimtex)!

<3

Can this be used with two-argument commands, such as \crefpart{big_theorem}{item_a}?

Yes, or, at least partly. If you load the biblatex package, then you can point to the texRefArgs group defined by the biblatex syntax package. Something like this should work:

" Ensure that biblatex is always loaded
let g:vimtex_syntax_packages = {
      \ 'biblatex': {'load': 2},
      \}

let g:vimtex_syntax_custom_cmds = [
      \ {
      \   'name': 'crefpart',
      \   'hlgroup': 'texCmdRef',
      \   'argspell': 0,
      \   'nextgroup': 'texRefArgs'
      \ },
      \]

However, it seems \crefpart may be part of cleveref. I've tried to bring builtin support for the cleveref package, so if I've missed some commands, please feel free to create a new issue and let me know. If so, please also link to the relevant docs for those commands.

jrotheneder commented 4 months ago

Works perfectly, thanks again! And nothing was forgotten, \crefpart is a custom command not included in cleveref.

lervag commented 4 months ago

Great, glad to hear it works! :)