“Google” Python 风格脚本无法运行

1 投票
3 回答
2967 浏览
提问于 2025-04-16 15:55

我正在尝试使用谷歌的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

3 个回答

0

在我的情况下,我需要把脚本复制到 ~/.vim/after/indent/python.vim 这个位置,因为内置的 Python 缩进脚本覆盖了我放在 ~/.vim/indent/python.vim 的那个脚本。

我可以通过输入 :scriptnames 命令来查看加载的顺序。

2

你可能在你的 .vimrc 文件里漏掉了 filetype indent on 这一行。

0

通过修改脚本并添加了一行缺失的代码解决了问题。

撰写回答