Vim 中的紧凑型 C 折叠

发布于 2024-07-20 10:23:19 字数 1062 浏览 9 评论 0原文

我正在尝试制作一个简单的 Vim 脚本,它可以为 c 文件创建非常紧凑的顶级折叠。 理想情况下,如果它在这段代码上运行:

static void funca(...)
{
   ... 
}

/* Example comment */
static void funcb(...)
{
   ... 
}

那么它会创建折叠,关闭时看起来像这样:

+-- x Lines: static void funca(...)----------------------
+-- x Lines: static void funcb(...)----------------------

所以基本上它会像 Foldmethod=syntax 和 Foldlevel=1 一样,除了每个折叠都会向上一行开始,并且会进一步向下延伸以包含以下所有空白行。

我知道如何进行这些折叠之一(假设foldmethod=manual):

/^{<cr>kVnn?^$<cr>zf

但我不确定如何将其放入函数中。 这是我的努力:

function Cfold()
  set foldmethod=manual  " Manual folds
  ggzE                   " Delete all folds
  while (/^{<cr>)        " Somehow loop through each match
     kVnn?^$<cr>zf       " This would work fine except for the last function
  endwhile
endfunction
map <Leader>f  :call Cfold()<cr>

但它无效,我不完全确定函数是如何工作的。 此外,它不适用于文件中的最后一个函数,因为它不会再次找到“^{”。 如果有人可以帮助我完成这项工作,并以某种方式为文件中的最后一个函数添加一个案例,我将非常感激。

提前致谢 :)

I'm trying to make a simple Vim script that would create very compact top-level folds for c files. Ideally, if it was run on this code:

static void funca(...)
{
   ... 
}

/* Example comment */
static void funcb(...)
{
   ... 
}

Then it would create folds which would look like this when closed:

+-- x Lines: static void funca(...)----------------------
+-- x Lines: static void funcb(...)----------------------

So basically it would be like foldmethod=syntax with foldlevel=1, except that each fold would start one line further up, and would extend further down to include all following blank lines.

I know how to make one of these folds (assuming foldmethod=manual):

/^{<cr>kVnn?^
lt;cr>zf

But I'm not sure how to put it into a function. This is my effort:

function Cfold()
  set foldmethod=manual  " Manual folds
  ggzE                   " Delete all folds
  while (/^{<cr>)        " Somehow loop through each match
     kVnn?^
lt;cr>zf       " This would work fine except for the last function
  endwhile
endfunction
map <Leader>f  :call Cfold()<cr>

But it isn't valid, I'm not entirely sure how functions work. Also, it won't work for the last function in the file, since it won't find '^{' again. If someone could help me get this working, and somehow add a case for the last function in a file, I would be extremely grateful.

Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最终幸福 2024-07-27 10:23:19

您可以使用 foldexprfoldtext 以编程方式创建折叠。 尝试一下这个,尽管您可能需要调整 CFoldLevel 以便它不会吞掉代码的非功能部分:

function! CFoldLevel(lnum)
  let line = getline(a:lnum)
  if line =~ '^/\*'
    return '>1' " A new fold of level 1 starts here.
  else
    return '1' " This line has a foldlevel of 1.
  endif
endfunction

function! CFoldText()
  " Look through all of the folded text for the function signature.
  let signature = ''
  let i = v:foldstart
  while signature == '' && i < v:foldend
    let line = getline(i)
    if line =~ '\w\+(.*)

请参阅 :help 'foldexpr' 了解更多详细信息。

let signature = line endif let i = i + 1 endwhile " Return what the fold should show when folded. return '+-- ' . (v:foldend - v:foldstart) . ' Lines: ' . signature . ' ' endfunction function! CFold() set foldenable set foldlevel=0 set foldmethod=expr set foldexpr=CFoldLevel(v:lnum) set foldtext=CFoldText() set foldnestmax=1 endfunction

请参阅 :help 'foldexpr' 了解更多详细信息。

You can create folds programmatically using the foldexpr and foldtext. Try this, though you may have to tweak CFoldLevel so it doesn't swallow non-function parts of the code:

function! CFoldLevel(lnum)
  let line = getline(a:lnum)
  if line =~ '^/\*'
    return '>1' " A new fold of level 1 starts here.
  else
    return '1' " This line has a foldlevel of 1.
  endif
endfunction

function! CFoldText()
  " Look through all of the folded text for the function signature.
  let signature = ''
  let i = v:foldstart
  while signature == '' && i < v:foldend
    let line = getline(i)
    if line =~ '\w\+(.*)

See :help 'foldexpr' for more details.

let signature = line endif let i = i + 1 endwhile " Return what the fold should show when folded. return '+-- ' . (v:foldend - v:foldstart) . ' Lines: ' . signature . ' ' endfunction function! CFold() set foldenable set foldlevel=0 set foldmethod=expr set foldexpr=CFoldLevel(v:lnum) set foldtext=CFoldText() set foldnestmax=1 endfunction

See :help 'foldexpr' for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文