【问题标题】:What would be the fold expression in Vim to fold unittests when using D?使用 D 时,Vim 中折叠单元测试的折叠表达式是什么?
【发布时间】:2013-07-24 20:33:32
【问题描述】:

我最近经常使用 D 并使用 Vim 作为编辑器。我处理的代码有很多内联单元测试,我想折叠这些,所以我只能看到代码。要自动折叠这些单元测试,Vim 中的折叠表达式需要是什么?

以下是它们在 D 代码中的外观示例:

T getUnixTime(T, A...)(A args)
{
    return to!T(SysTime(DateTime(args)).toUnixTime());
}

unittest
{
    assert(getUnixTime!string(2013, 7, 18, 14, 49, 43) == "1374155383");
    assert(getUnixTime!uint(2071, 12, 5, 12, 9, 5) == 3216542945);
}

我希望它看起来像:

T getUnixTime(T, A...)(A args)
{
    return to!T(SysTime(DateTime(args)).toUnixTime());
}

+--  5 lines: unittest----------------------------------------------------------

【问题讨论】:

  • 无。我完全不知道从哪里开始!因此,我在程序员问答网站上寻求帮助。你有什么想法吗?
  • 只是我听说在 Stackoverflow 上“给我发代码”风格的问题有点不受欢迎。帮不了你,但是LVtHW 中有关于折叠表达式的深入教程,the other questions 中有很多关于这个主题的进一步提示。

标签: vim editor d folding


【解决方案1】:

您是在寻找“折叠表达式”还是“折叠命令”?

假设你的光标在unittest,你可以这样做

zf/{/e

创建一个折叠。

使其成为更快的映射。

【讨论】:

  • 我会假设一个折叠表达式。以下是我主要用作 modline 的内容,但不足以满足 OP 的目的vim:fdc=3:fdm=expr:fde=getline(v\:lnum)=~'\unittest'?'>1'\:'1'。请告诉我们它是如何完成的:)。
【解决方案2】:

抱歉回复晚了。我一直在寻找相同的功能,最终想出了这个:

set foldexpr=DlangUnitTestFold(v:lnum)

" If the line matches `unittest {`, increase the indentation.
" Keep the indentation level steady until we encounter a line
" that only matches `}`: if so, decrease the indentation.

function! DlangUnitTestFold(lnum)
  if getline(a:lnum) =~ '^\s*unittest\s{\s*$'
    return "a1"
  elseif getline(a:lnum) =~ '^\s*}\s*$'
    return "s1"
  else
    return "="
  endif
endfunction

不确定这是否太老套,但它对我有用:)

编辑:理想情况下,您会将第一行放在 setlocal 中,例如:

au BufNewFile,BufRead *.d setlocal foldexpr=DlangUnitTestFold(v:lnum)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 2013-01-16
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2023-03-24
    相关资源
    最近更新 更多