我想知道是否有可能自动折叠 vim 中的特定代码部分(特别是针对 python 文件)。
我目前正在使用缩进折叠方法。如果标题与模式匹配,我只想折叠缩进的块。
例如,如果我在具有此功能的文件中,我希望所有以“Example:”或“References:”开头的块都自动折叠,但我不希望折叠其他任何内容。
有没有简单的方法可以做到这一点?
def spawn_background_process(func, *args, **kwargs):
"""
Run a function in the background
(like rebuilding some costly data structure)
References:
http://stackoverflow.com/questions/2046603/is-it-possible-to-run-function-in-a-subprocess-without-threading-or-writing-a-se
http://stackoverflow.com/questions/1196074/starting-a-background-process-in-python
http://stackoverflow.com/questions/15063963/python-is-thread-still-running
Args:
func (function):
CommandLine:
python -m utool.util_parallel --test-spawn_background_process
Example:
>>> # DISABLE_DOCTEST
>>> from utool.util_parallel import * # NOQA
>>> import utool as ut
>>> import time
>>> from os.path import join
>>> # build test data
>>> fname = 'test_bgfunc_output.txt'
>>> dpath = ut.get_app_resource_dir('utool')
>>> ut.ensuredir(dpath)
>>> fpath = join(dpath, fname)
>>> # ensure file is not around
>>> sleep_time = 1
>>> ut.delete(fpath)
>>> assert not ut.checkpath(fpath, verbose=True)
>>> def backgrond_func(fpath, sleep_time):
... import utool as ut
... import time
... print('[BG] Background Process has started')
... time.sleep(sleep_time)
... print('[BG] Background Process is writing')
... ut.write_to(fpath, 'background process')
... print('[BG] Background Process has finished')
... #raise AssertionError('test exception')
>>> # execute function
>>> func = backgrond_func
>>> args = (fpath, sleep_time)
>>> kwargs = {}
>>> print('[FG] Spawning process')
>>> threadid = ut.spawn_background_process(func, *args, **kwargs)
>>> assert threadid.is_alive() is True, 'thread should be active'
>>> print('[FG] Spawned process. threadid=%r' % (threadid,))
>>> # background process should not have finished yet
>>> assert not ut.checkpath(fpath, verbose=True)
>>> print('[FG] Waiting to check')
>>> time.sleep(sleep_time + .1)
>>> print('[FG] Finished waiting')
>>> # Now the file should be there
>>> assert ut.checkpath(fpath, verbose=True)
>>> assert threadid.is_alive() is False, 'process should have died'
"""
import utool as ut
func_name = ut.get_funcname(func)
name = 'mp.Progress-' + func_name
proc_obj = multiprocessing.Process(target=func, name=name, args=args, kwargs=kwargs)
#proc_obj.isAlive = proc_obj.is_alive
proc_obj.start()
return proc_obj
答案1
有一种方法可以做到这一点,你只需要自己写一个自定义折叠函数。
输入以下代码.vim/after/ftplugin/python/folding.vim
(如果不存在则创建目录和文件):
function! ExampleFolds(lnum)
let s:thisline = getline(a:lnum)
if match(s:thisline, '^\s*Example:$') >= 0
return '>1'
elseif match(s:thisline, '^\s*$') >= 0
return '0'
else
return '='
endfunction
setlocal foldmethod=expr
setlocal foldexpr=ExampleFolds(v:lnum)
您仍需要稍微调整该函数以满足您的需求。它现在所做的是,每当遇到块时,以 foldlevel 1 开始折叠Example:
。折叠包括所有后续行,直到出现新的示例块(开始新的折叠)或空行将其关闭。
除了链接之外,请检查:h foldexpr
和:h foldlevel
。