“谷歌” python 样式脚本不工作

发布于 2024-11-02 12:34:38 字数 1397 浏览 4 评论 0原文

我正在尝试使用 Google python 缩进脚本,但事实并非如此为我工作。我希望它缩进如下:

very_long_function_name(
    first_param,

我将其文本粘贴到 这个 vim 脚本的末尾: 并将其放入 ~/.vim/indent/python.vim 中。不知道为什么它不起作用。


编辑:已修复。

我修改了缩进文件如下:

function GetGooglePythonIndent(lnum)
  " Indent inside parens.
  " Align with the open paren unless it is at the end of the line.
  " E.g.
  "   open_paren_not_at_EOL(100,
  "                         (200,
  "                          300),
  "                         400)
  "   open_paren_at_EOL(
  "       100, 200, 300, 400)
  call cursor(a:lnum, 1)
  let [par_line, par_col] = searchpairpos('(\|{\|\[', '', ')\|}\|\]', 'bW',
        \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
        \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
        \ . " =~ '\\(Comment\\|String\\)$'")
  echo par_line par_col
  if par_line > 0
    call cursor(par_line, 1)
    if par_col != col("$") - 1
      return par_col
    else
      return indent(par_line) + &sw " FIXED HERE. FIXED BY ADDING THIS LINE
    endif
  endif

  " Delegate the rest to the original function.
  return GetPythonIndent(a:lnum)
endfunction

I'm trying to use the Google python indentation script, but it's not working for me. I want it to indent as follows:

very_long_function_name(
    first_param,

I pasted its text onto the end of this vim script: and put it into ~/.vim/indent/python.vim. Not sure why it's not working.


Edit: FIXED.

I modified the indent file as follows:

function GetGooglePythonIndent(lnum)
  " Indent inside parens.
  " Align with the open paren unless it is at the end of the line.
  " E.g.
  "   open_paren_not_at_EOL(100,
  "                         (200,
  "                          300),
  "                         400)
  "   open_paren_at_EOL(
  "       100, 200, 300, 400)
  call cursor(a:lnum, 1)
  let [par_line, par_col] = searchpairpos('(\|{\|\[', '', ')\|}\|\]', 'bW',
        \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
        \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
        \ . " =~ '\\(Comment\\|String\\)
")
  echo par_line par_col
  if par_line > 0
    call cursor(par_line, 1)
    if par_col != col("$") - 1
      return par_col
    else
      return indent(par_line) + &sw " FIXED HERE. FIXED BY ADDING THIS LINE
    endif
  endif

  " Delegate the rest to the original function.
  return GetPythonIndent(a:lnum)
endfunction

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

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

发布评论

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

评论(3

诗酒趁年少 2024-11-09 12:34:38

您的 .vimrc 中可能缺少 filetype indent on

You are probably missing filetype indent on from your .vimrc.

呢古 2024-11-09 12:34:38

就我而言,我必须将脚本复制到 ~/.vim/after/indent/python.vim,因为内置的 python 缩进脚本会覆盖我添加到 ~/ 的脚本。 vim/indent/python.vim

我能够通过 :scriptnames 命令查看加载的顺序。

In my case, I had to copy the script to ~/.vim/after/indent/python.vim as the built-in python indent script overridden the one I added to ~/.vim/indent/python.vim.

I was able to see the loaded order by :scriptnames command.

许一世地老天荒 2024-11-09 12:34:38

通过修改脚本并添加缺失的行来修复。

Fixed by modifying the script and adding a missing line.

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