【问题标题】:Is there a way to prevent autoformatter in VSC to change certain line/piece of code?有没有办法防止 VSC 中的自动格式化程序更改某些行/代码段?
【发布时间】:2017-12-19 14:55:02
【问题描述】:

我编写了一些 python 脚本,而且在大多数情况下,我对自动格式化程序的工作方式完全满意。但有时我想保持垂直一致性或在逻辑上将代码分成几行。

# autoformatter removes all leading spaces here
array = numpy.array([[
        [       0,     1589, 25825225,     1589,        0],
        [    1589, 26265625, 26265625, 26265625,     1589],
        [25825225, 26265625, 26265625, 26265625, 25825225],
        [    1589, 26265625, 26265625, 26265625,     1589],
        [       0,     1589, 25825225,     1589,        0],
]])
# autoformatter splits line at '-' sign in the first brackets
links[point.degree - 1].append([
    neighbor.index for neighbor in point.neighbors
])

有没有办法告诉自动格式化程序(我使用 VSC 的默认 Python 包)忽略这些行(类似于 # pylint: disable=C0123 魔术注释)?

【问题讨论】:

    标签: python visual-studio-code autoformatting


    【解决方案1】:

    Python 扩展支持两种格式化程序:autopep8(默认)和 yapf。您可以使用以下配置切换到 yapf:

    "python.formatting.provider": "yapf"
    

    Yapf 支持通过 cmets 从格式化中排除区域:

    # yapf: disable
    links[point.degree - 1].append([
       neighbor.index for neighbor in point.neighbors
    ])
    # yapf: enable
    

    我还没有为 autopep8 找到类似的功能(尽管您可以使用 --ignore 全局禁用 specific fixes)。

    【讨论】:

      猜你喜欢
      • 2011-11-24
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多