【问题标题】:How to configure alignment and indentation of terraform code inside VS Code?如何在 VS Code 中配置 terraform 代码的对齐和缩进?
【发布时间】:2020-09-25 02:33:13
【问题描述】:

我使用 VS Code 来开发 terraform 代码。我目前的 terraform 插件是:

Name: Terraform
Id: hashicorp.terraform
Description: Syntax highlighting, linting, formatting, and validation for Hashicorp's Terraform
Version: 1.4.0
Publisher: HashiCorp
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform

考虑以下代码:

output "sql_server" {
  description = "A dictionary of objects containing various Azure Sql Server properties per respective location."
  value = {
    for k, instance in azurerm_sql_server.instance : k =>
    {
      resource_group_name = instance.resource_group_name
      fully_qualified_domain_name = instance.fully_qualified_domain_name 
      name = instance.name
      location = instance.location
      is_primary = instance.location == var.primary_location
      admin_login = instance.administrator_login
    }
  }
}

我希望在保存文件时像这样重新格式化:

output "sql_server" {
    description = "A dictionary of objects containing various Azure Sql Server properties per respective location."
    value = {
        for k, instance in azurerm_sql_server.instance : k =>
        {
            resource_group_name         = instance.resource_group_name
            fully_qualified_domain_name = instance.fully_qualified_domain_name 
            name                        = instance.name
            location                    = instance.location
            is_primary                  = instance.location == var.primary_location
            admin_login                 = instance.administrator_login
        }
    }
}

这里发生了两件事:

  • 应用了 4 个空格的缩进
  • 同一块中的所有分配都已对齐

网上有很多关于如何做到这一点的信息,但我一定是因为无法使其工作而特别愚蠢,所以我想得到一个包含以下详细信息的非常具体的答案:

  • 使用什么 terraform 插件
  • 在我的 settings.json 中具体写什么

我当前的用户 settings.json 文件是:

{
    "workbench.startupEditor": "welcomePage",
    "editor.minimap.enabled": false,
    "terminal.integrated.scrollback": 1000,
    "git.enableSmartCommit": true,
    "editor.detectIndentation": false,
    "window.zoomLevel": -1,
    "diffEditor.renderSideBySide": false,
    "extensions.ignoreRecommendations": false,
    "workbench.colorTheme": "PowerShell ISE",
    "powershell.codeFormatting.whitespaceBeforeOpenBrace": false,
    "git.autofetch": true,
    "terminal.integrated.rendererType": "dom",
    "terraform.path": "C:\\Users\\mkharitonov\\.terraform\\terraform.exe"
}

我的本​​地工作区 settings.json 文件是:

{
    "git.ignoreLimitWarning": true,
    "powershell.codeFormatting.openBraceOnSameLine": false,
    "powershell.codeFormatting.whitespaceBeforeOpenBrace": false,
    "editor.formatOnPaste": true,
    "editor.formatOnSave": true
}

【问题讨论】:

  • 该插件上的自动格式化程序 (ctrl+shift+i) 将运行 terraform fmt 命令。这是导致规范风格的固执己见的零选项自动格式化程序。即使它使用 2 个空格进行缩进,我也不会偏离这一点。自以为是的自动格式化程序有一个好处,这意味着好的 Terraform 代码看起来总是一样的。
  • 作业对齐怎么样?
  • 是的,它会这样做。我建议在代码上运行格式化程序,这样您就可以看到它所做的更改。
  • 嗯,我可以放弃 4 个字符。如果是这样的话,那我就准备好了,对吧?
  • 插件格式化程序不起作用,我想这就是我发布这个问题的原因。我记得我使用的是插件,但没有格式化。我将在插件 github repo 中发布一个错误。

标签: visual-studio-code terraform


【解决方案1】:

ma​​cOS + VSCode 解决方案

如果您在自动格式化时遇到问题,可以尝试这样做:

  • 安装 HashiCorp Terraform 插件

  • 安装 Terraform 版本管理器:

    brew install tfenv
    
  • tfenv使用您的项目使用的特定版本,例如:

    tfenv use 0.12.20
    
  • 在您的settings.json 中添加以下内容:

    "[terraform]": {
        "editor.formatOnSave": true
    }
    

【讨论】:

  • 不工作仍然说安装格式化程序
【解决方案2】:

建议对 terraform 代码使用 2 个空格,而不是制表符或 4 个空格。 你可能会在Style Conventions看到它

配置 VS 代码:

  1. 应在您的 Mac 上安装 terraform
  2. 编辑settings.json 并添加以下内容:
"[terraform]": {
    "editor.formatOnSave": true
}

在此之后,您的所有 *.tf 文件将在保存时自动格式化。

【讨论】:

    【解决方案3】:

    其他答案都很好并且有效。然而,有 4 个字符的缩进并让自动保存过程将其恢复为 2 个字符真的很烦人。

    无论出于何种原因,VSCODE 和 Terraform 插件默认为 4 个空格。这可以通过将这些块添加到您的 settings.json 来更改:

        "[terraform]": {
            "editor.tabSize": 2
        },
        "[terraform-vars]": {
            "editor.tabSize": 2
        },
    

    这些将在您编辑时将您的缩进保持在建议的 2 个空格,并避免在自动保存过程保存文件时从 4 个空格猛烈捕捉到 2 个空格。

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 2022-12-05
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 2016-06-02
      • 2017-10-24
      相关资源
      最近更新 更多