auto-fold Oracle inline views in Vim using .vimrc
I've seen magical Vim commands before that you could add to your .vimrc to have folds created upon opening a particular type开发者_如何学JAVA of file. I remember having such code that would create the folds, upon opening the file, at every Ruby method and class. Then, with one command, I could collapse all those method folds. Does anyone know how to do this with inline views in PL/SQL? Say I have the following SQL:
SELECT blah,
teh_max
FROM (
SELECT blah,
MAX(bar) AS teh_max
FROM (
SELECT blah,
bar
FROM foo
)
GROUP BY blah
)
ORDER BY blah
I would like folds to be created when I open this in Vim so that I can go to a FROM (
line, hit zc
in command mode, and have the inline view starting at that line be collapsed. It would be nice to collapse all the folds with one command, too.
Folding based on syntax is activated by setting foldmethod
to syntax
:
" for all windows
set foldmethod=syntax
" for the current window
setlocal foldmethod=syntax
The folding must then be specified within the syntax definition, which is done by providing the fold
argument to regions which should increase the fold level. To quote the documentation:
The "fold" argument makes the fold level increase by one for this item.
Example:
:syn region myFold start="{" end="}" transparent fold
:syn sync fromstart
:set foldmethod=syntax
This will make each {} block form one fold.
So you'll have to go into the syntax files for whatever filetypes you care about, and add the fold
argument to the appropriate regions, or potentially add in your own regions. In your case, it looks like it's fairly similar to the C/C++ syntax's fold-by-braces, except with parentheses.
The default syntax files are generally kept in /usr/share/vim/vimXX/syntax
on Linux (and presumably <vim-directory>\vimXX\syntax
on windows?) where XX is the version number without the period (e.g. 72). These may be overridden system-wide by files in /usr/share/vim/vimfiles/syntax
or per-user by files in ~/.vim/syntax
.
I would suggest just to avoid multilevel SQL queries. You can always create a view, a temporary table. I know that contradicts with the theory of Tom Kyte, but it's the 10 years PL/SQL practice. Divide the complex program into several simpler parts. Divide the complex SQL queries into simple queries.
In your example, the query is quite easy to grasp, folding would only disturb.
On the opposite for nested PL/SQL procedures folding works quite usefully. There is a script for VIM on vim.org.
精彩评论