- Notifications
You must be signed in to change notification settings - Fork7
A Vim/Neovim plugin that grays out inactive C/C++/Obj-C preprocessor regions.
License
mphe/grayout.vim
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This plugin was developed before LSP became popular.Common C/C++ language servers likeclangd orccls provide full semantic highlighting including skipped preprocessor regions in addition to code completion, syntax checking, code actions, etc.They can be integrated into Vim/Neovim using LSP plugins likecoc.nvim,nvim-lspconfig orvim-lsp.
Since this plugin is pretty much obsolete nowadays, I don't see much value in further supporting it.
It will likely still work as expected in most scenarios, though.
grayout.vim is a vim plugin that grays out inactive C/C++/Obj-C preprocessor regions using libclang.
In addition to custom config files, it also supportscompile_commands.json
compilation databases, allowing quick and easy project setup.
Even though it is intended to be used with C/C++/Obj-C, it should work with all filetypes, as long as the-x <language>
compile flag is set.
It was only tested on Linux but shouldtheoretically work on other platforms, too.
There are some other plugins providing similar functionality, but in different ways.
ifdef highlighting adds static vim syntax rules for eachmanually defined macro. It does not make use of a compiler and requires the user to manually specify which macros are defined, thus being rather unflexible and often fails to properly detect skipped regions.
DyeVim integrates with (a custom fork of)YouCompleteMe to retrieve extended syntax information for semantic highlighting, including skipped preprocessor regions.However, it only works with YCM's libclang completer, not the newer and more advanced clangd completer.
vim-lsp-inactive-regions integrates withvim-lsp and uses thecquery orccls language server to retrieve skipped preprocessor regions.
vim-lsp-cxx-highlight integrates with various LSP plugins and uses thecquery orccls language server to provide full semantic highlighting, including skipped preprocessor regions.
Common C/C++ language servers likeclangd andccls support full semantic highlighting including skipped preprocessor regions.They can be integrated into Vim/Neovim using LSP plugins likecoc.nvim,nvim-lspconfig orvim-lsp.
See also themaintenance note.
- Requirements
- Vim 8.1+ or Neovim
- Python 3
- Clang
- Install the plugin
- Using a plugin manager
- Alternatively, copy the contents of this repository to your vim directory
- Read the docs
- ...
- Profit
- Run
:GrayoutUpdate
to parse the current file and apply highlighting. - Run
:GrayoutClear
to clear all grayout highlights from current buffer - Run
:GrayoutClearCache
to clear the compile command cache, forcing to reload all config files when needed. - Run
:GrayoutShowCommand
to print the current file's compile flags.
" Bind :GrayoutUpdate to F5nnoremap<F5>:GrayoutUpdate<CR>" Run GrayoutUpdate when opening and saving a bufferautocmdBufReadPost,BufWritePost*if &ft=='c'|| &ft=='cpp'|| &ft=='objc' |exec'GrayoutUpdate' |endif" Run GrayoutUpdate when cursor stands still. This can cause lag in more complex files.autocmdCursorHold,CursorHoldI*if &ft=='c'|| &ft=='cpp'|| &ft=='objc' |exec'GrayoutUpdate' |endif
" Set default compile flags." These are used, when no `compile_commands.json` or `.grayout.conf` file was found.letg:grayout_default_args= ['-x','c++','-std=c++11' ]" Set libclang searchpath. This should point to the directory containing `libclang.so`." Leave empty to auto-detect.letg:grayout_libclang_path=''" Enable to print debug messages inside vim.letg:grayout_debug=0" Enable to write debug messages to `grayout.log`.letg:grayout_debug_logfile=0
As with every compiler, clang needs to know your project's compile flags to compile your code.
There are three ways of defining compile flags, that are prioritized in the respective order.
Note: If you make any changes related to compile flags at runtime, you will need to run:GrayoutClearCache
in order to reload those configs.
Compilation Database (recommended)
Instruct your build system to generate a
compile_commands.json
and symlink or move it to your project root.Acompilation database contains information on how to compile each translation unit. It can be auto-generated by build systems like cmake, or tools likeBear orcompdb.
For example, using cmake, add this line to your
CMakeLists.txt
:set(CMAKE_EXPORT_COMPILE_COMMANDSON)
.grayout.conf
Create a
.grayout.conf
file in your project root containing the compile flags. Linebreaks are ignored.
It is recommended to specify-x <language>
in order to avoid ambiguity, especially with header files.Example:
-x c-DENABLE_FEATURE_X-DANOTHER_FLAG-DSOME_CONSTANT=42
g:grayout_default_args
The global
g:grayout_default_args
variable holds a list of compile flags and is used when no other configs were found.It is recommended to specify-x <language>
in order to avoid ambiguity, especially with header files.Example:
letg:grayout_default_args= ['-x','c++','-std=c++11','-DFOOBAR_MACRO' ]
If you don't like the default colors for grayouts, you can change them by altering thePreprocessorGrayout
style.By default it links to theComment
style.
Example:
highlight PreprocessorGrayout cterm=italic ctermfg=DarkGraygui=italic guifg=#6c6c6c
If you encounter any bugs or problems, please make sure to read the docs. If the problem persists, open a new issue on Github. Remember to addlet g:grayout_debug_logfile = 1
to your vimrc and attach thegrayout.log
logfile to your issue.
Pull requests are welcome.
- Support
textprops
ornvim_buf_add_highlight
- Fix edge case where consecutive active if/elif/else lines are grayed out when their contents are inactive