visual-studio-code - 如何在 VS Code 中配置 terraform 代码的对齐和缩进?

标签 visual-studio-code terraform

我使用 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
    }
    

    最佳答案

    macOS + VSCode 解决方案
    如果您在自动格式化方面遇到问题,您可以尝试这样做:

  • 安装 HashiCorp Terraform 插件
  • 安装 Terraform 版本管理器:
    brew install tfenv
    
  • 询问 tfenv使用您的项目使用的特定版本,例如:
    tfenv use 0.12.20
    
  • 在您的 settings.json 中添加以下内容:
    "[terraform]": {
        "editor.formatOnSave": true
    }
    
  • 关于visual-studio-code - 如何在 VS Code 中配置 terraform 代码的对齐和缩进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62222899/

    相关文章:

    c++ - VS Code C++ 自动语法错误高亮?

    terraform - 使用另一个项目的 Terraform 状态输出

    amazon-web-services - AWS ElasticBeanstalk Terraform 禁用IMDSv1 未知配置设置

    python - 为什么说模块pygame没有init成员?

    flutter - 谁能告诉我为什么我在 Flutter 中会出现这个错误?

    azure - Terraform 强制替换 AKS 节点池而不进行任何更改

    terraform - 如何处理 terraform 中的可选动态 block

    terraform - Terraform可以监视目录中的更改吗?

    django - VSCode + 远程开发 + django : avoiding forwarding ports

    angular - VSCode tasks.json for ng build --watch