Code Monkey home page Code Monkey logo

grayout.vim's Introduction

grayout.vim

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 supports compile_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 should theoretically work on other platforms, too.

Note

This plugin was rewritten from scratch in c378ecb. If you have been using it before, please read the docs again, as many things changed in the new version.

Related Work

There are some other plugins providing similar functionality, but in different ways.

  • ifdef highlighting adds static vim syntax rules for each manually 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 with vim-lsp and uses the cquery or ccls language server to retrieve skipped preprocessor regions.

  • vim-lsp-cxx-highlight integrates with various LSP plugins and uses the cquery or ccls language server to provide full semantic highlighting, including skipped preprocessor regions.

  • coc.nvim + clangd provide semantic highlighting similar to the option above. Semantic highlighting support in coc.nvim needs to be enabled first, see :h coc-semantic-highlights.

Therefore, if you are using LSP for completion or syntax checking, you should try coc.nvim with clangd or alternatively vim-lsp-cxx-highlight with ccls. Otherwise, this plugin is probably the best choice.

Installation

  1. Requirements
    • Vim 8.1+ or Neovim
    • Python 3
    • Clang
  2. Install the plugin
    • Using a plugin manager
    • Alternatively, copy the contents of this repository to your vim directory
  3. Read the docs
  4. ...
  5. Profit

Commands

  • 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.

Configuration

Example Configuration

" Bind :GrayoutUpdate to F5
nnoremap <F5> :GrayoutUpdate<CR>

" Run GrayoutUpdate when opening and saving a buffer
autocmd BufReadPost,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.
autocmd CursorHold,CursorHoldI * if &ft == 'c' || &ft == 'cpp' || &ft == 'objc' | exec 'GrayoutUpdate' | endif

Variables

" Set default compile flags.
" These are used, when no `compile_commands.json` or `.grayout.conf` file was found.
let g: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.
let g:grayout_libclang_path = ''

" Enable to print debug messages inside vim.
let g:grayout_debug = 0

" Enable to write debug messages to `grayout.log`.
let g:grayout_debug_logfile = 0

Compile Flags

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.

    A compilation database contains information on how to compile each translation unit. It can be auto-generated by build systems like cmake, or tools like Bear or compdb.

    For example, using cmake, add this line to your CMakeLists.txt:

    set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  • .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:

    let g:grayout_default_args = [ '-x', 'c++', '-std=c++11', '-DFOOBAR_MACRO' ]

Colors

If you don't like the default colors for grayouts, you can change them by altering the PreprocessorGrayout style. By default it links to the Comment style.

Example:

highlight PreprocessorGrayout cterm=italic ctermfg=DarkGray gui=italic guifg=#6c6c6c

Contributing

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 add let g:grayout_debug_logfile = 1 to your vimrc and attach the grayout.log logfile to your issue.

Pull requests are welcome.

Todo

  • Support textprops or nvim_buf_add_highlight
  • Fix edge case where consecutive active if/elif/else lines are grayed out when their contents are inactive

grayout.vim's People

Contributors

mphe avatar skade avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

grayout.vim's Issues

Error parsing translation unit.

Hi,

grayout.vim is currently not working for me and throws the following error:

Error parsing translation unit.

This is clang related, but I'm not sure what is causes it?

I'm running ubuntu 18.04.02 LTS and invoking grayout from neovim
I have clang 6.0 and 9 installed... any specific version needed?

I remember a long time ago you had grayout_debug_compiler_inout but that doesn't seem to work anymore?

Any suggestions on how to aid in debugging this?

Thanks.

Highlighting stops working after enabling and then disabling Goyo

https://github.com/junegunn/goyo.vim
vimrc goyo-specific setup:

function! s:goyo_enter()
  if executable('tmux') && strlen($TMUX)
    silent !tmux set status off
  endif
  set noshowmode
  set noshowcmd
  set scrolloff=999
  Limelight
  " ...
endfunction

function! s:goyo_leave()
  if executable('tmux') && strlen($TMUX)
    silent !tmux set status on
  endif
  set showmode
  set showcmd
  set scrolloff=1
  Limelight!
endfunction

Steps to reproduce:
Enter goyo mode with :Goyo
Leave goyo mode with :Goyo
Result:
grayout highligting stops working

AttributeError

Getting this error when running ':GrayoutUpdate' :
Error detected while processing function grayout#UpdateGrayout:
line 1:
Traceback (most recent call last):
File "", line 1, in
File "/Users/rwmitch1/.vim/bundle/grayout.vim/autoload/../python/grayout.py", line 52, in grayout
tu = INDEX.parse(
AttributeError: 'NoneType' object has no attribute 'parse'

I have installed the ~latest python: python3 --version
Python 3.8.2

OSX: 10.14.6

MacVim is compiled with:
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_MACVIM -Wall -Wno-unknown-pragmas -pipe -DMACOS_X -DMACOS_X_DARWIN -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc -L/usr/local/lib -o Vim -framework Cocoa -framework Carbon -lm -lncurses -lico
nv -framework AppKit -fstack-protector -L/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE -lperl

:version shows I have both +python/dyn and +python3/dyn

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled May 4 2020 07:08:21)
macOS version
Included patches: 1-607

I've used the default grayout configuration arguments and just '-x c'. I've attached the debug log showing both.

What do I have setup incorrectly?

grayout.log

How to initialize this program?

I'm installing it using vim-plugged by doing like this, omitting other plugins:

call plug#begin('~/.vim/plugged')
Plug 'mphe/grayout.vim'
call plug#end()

After this, I called :PlugInstall with success.

I appended the followings into ~/.vimrc :

" Run GrayoutUpdate when cursor stands still. This can cause lag in more complex files.
autocmd CursorHold,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.
let g:grayout_default_args = [ '-x', 'c', '-std=c11' ]
" Set libclang searchpath. Leave empty for auto-detect.
let g:grayout_libclang_path = ''
" Enable to print debug messages inside vim.
let g:grayout_debug = 0
" Enable to write debug messages to `grayout.log`.
let g:grayout_debug_logfile = 0

Now, I open a .c file and move the cursor over a #define, and vim's command line shows grayout.vim: ERROR: Not initialized

My vim is 8.1 (2018 May 18, compiled Nov 08 2021 14:21:34),
Python 3.8.10,
clang version 10.0.0-4ubuntu1
Ubuntu 20.04.3 LTS

Could anyone tell me what I've lost or what I can try?

errors on non C code

After recently updating both vim and plugins, I've started getting a bunch of errors related to Grayout when opening non C files:

Error detected while processing function 52_UpdateGrayout[13]..52_RunPyScript:

What is the proper way to limit the plugin to just .c.h files?

python plugin not detected (neovim)

Hi :)
Is this plugin compatible with neovim?
I got an error with neovim:

Error detected while processing /home/gohm/.vim/bundle/grayout.vim/plugin/grayout.vim:
line   19:
This plugin requires python.
Press ENTER or type command to continue`

I have installed the latest pynvim:

pip2 show pynvim
Name: pynvim
Version: 0.4.0
Summary: Python client to neovim
Home-page: http://github.com/neovim/python-client
Author: Thiago de Arruda
Author-email: [email protected]
License: Apache
Location: /usr/local/lib/python2.7/dist-packages
Requires: greenlet, msgpack, trollius`
pip3 show pynvim
Name: pynvim
Version: 0.4.0
Summary: Python client to neovim
Home-page: http://github.com/neovim/python-client
Author: Thiago de Arruda
Author-email: [email protected]
License: Apache
Location: /usr/local/lib/python3.7/dist-packages
Requires: msgpack, greenlet
Required-by:

:checkhealth

## Python 2 provider (optional)
  - INFO: Using: g:python_host_prog = "/usr/bin/python2"
  - INFO: Executable: /usr/bin/python2
  - INFO: Python version: 2.7.17
  - INFO: pynvim version: 0.4.0
  - OK: Latest pynvim is installed.

## Python 3 provider (optional)
  - INFO: Using: g:python3_host_prog = "/usr/bin/python3"
  - INFO: Executable: /usr/bin/python3
  - INFO: Python version: 3.7.5
  - INFO: pynvim version: 0.4.0
  - OK: Latest pynvim is installed.

## Ruby provider (optional)
  - INFO: Ruby: ruby 2.5.5p157 (2019-03-15 revision 67260

Tags are not injected after an "#endif" in multi-line comments

Thank you for your great work. It helps me a lot.

Source code like this (which is very common in those "lagacy" projects I am mentaining) will trigger the issue.

/*#ifdef A
#ifdef B
// a tag is injected here
#endif
#endif*/
// no tags will be injected from here

#if D
// no tag injected here
#endif

It seems that "#endif*/" thing caused _parseblock() called directly from _parse() returned, and left the rest of source code unparsed.

I have a solution that works for me. I can not give a qualified pull request, since I am not familiar with python, and it is not well tested.
grayout.py.txt

And I guess this will trigger the same issue as well.

char somestring[] = "\
#endif";

ARM Project not seeing #define

GrayoutUpdate seems to gray-out everything, even if the #define is in the same c file.

`#define HELLO

#ifdef HELLO
int x = 0;
#endif`

If I run :GrayoutUpdate, int x = 0; is grayed out even though HELLO was defined right above it.

This is my .grayout.conf:

/usr/bin/arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F411xE -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -IInc -IInc -IBT

Does grayout.vim support GCC?

Hi
I have tried to use this script in my vim file, using it helps me read the Linux kernel easily.
But, It seems NOT support the compile_commands.json generated by GCC, is really?

Regards,
Rongwei Wang

How to make the gray area not beyond a certain column?

Hello, mphe
I am always use this script to read code. Thanks for your nice grayout.vim!
Recently, I want to change the style of grayout.vim shown, but failed. And the change that I am try is described as following:

In my vimrc, I always add "set cc=80" to code. And now, the grayout.vim will gray an area, but the area beyonds column 80.
So, I am think about whether the grayout.vim can limits this gray area to a certain comumn.

likes this image:
image

P.S. I had searched how to gray an area and limit the column of the gray area. But I failed. So asking you for help!

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.