Auto-folding #defines in vim

I work with quite a lot of multi-platform C / C ++ code, separated by the usual #defines (#if WIN, #if UNIX, etc.). It would be nice if I could automatically flush partitions that I’m not interested in right now when I open the file. I looked through the vim script archives, but I did not find anything useful. Any suggestions? Places to run?

+4
source share
5 answers

Just add the folding area to your syntax http://vim.wikia.com/wiki/Syntax_folding_of_Vim_scripts#Syntax_definitions

:syn region myFold start="\#IF" end="\#ENDIF" transparent fold :syn sync fromstart :set foldmethod=syntax 
+7
source

To add an answer to @hometoasts, you can add this command as a comment in the first or ten ten lines of the file, and vim will automatically use it for this file.

  / * vim: region scope synNName start = "regex" end = "regex": * /
+2
source

A quick addition to the Denton add-on: use the new syntax rule with any C or C ++ code, add it to the file in $VIMRUNTIME/syntax/c.vim and cpp.vim . ( $VIMRUNTIME where your local Vim code lives: ~/.vim on Unix.) Also, the values ​​for start and end in the syntax definition are regular expressions, so you can use ^#if and ^#endif to match only these lines at the beginning of the line.

0
source

I always used the forldmethod = marker and defined my own tag labels placed in the comments.

it is a definition of characters that define open and closed folds. in this case open is "<(" and close is ")>" replace them with what you want.

 set foldmethod=marker set foldmarker=<(,)> 

This is my custom function to decide what to display folded text:

 set foldtext=GetCustomFoldText() function GetCustomFoldText() let preline = substitute(getline(v:foldstart),'<(','<(+)','') let line = substitute(preline,"\t",' ','g') let nextLnNum = v:foldstart + 1 let nextline = getline(nextLnNum) let foldTtl = v:foldend - v:foldstart return line . ' | ' . nextline . ' (' . foldTtl . ' lines)>' endfunction 

Hope this helps.

0
source

I have a huge code base and a lot of #defines. Each file has many # ifdef's and in most cases they are nested. I tried many vim scripts, but they are always used for some errors with the code that I have. Therefore, in the end, all my definitions are the header file and included it in the file I wanted to work with, and made gcc on it like this

gcc -E -C -P source.cpp> output.cpp

The -E command gets gcc to run only the preprocessor in the file, so all unwanted code in undefined #ifdef is deleted. The -C option saves comments in a file. The -P option prevents the creation of linear markers at the exit of the preprocessor.

0
source

Source: https://habr.com/ru/post/1277072/


All Articles