pearofducks / ansible-vim

A vim plugin for syntax highlighting Ansible's common filetypes
MIT License
800 stars 98 forks source link

Heredocs break syntax highlighting #45

Closed beanaroo closed 7 years ago

beanaroo commented 7 years ago

Highlighting gets strange after the <<. I presume it's trying to do HTML related stuff? The file name is sqs_queues.tf.j2 (Final product is Terraform HCL)

{% for queue_type, queue in sqs.queues.items() %}
# ----- {{ queue_type|replace('_', ' ')|title }} Queue ----- #

resource "aws_sqs_queue" "{{ queue_type }}" {
  name                       = "{{ queue.name }}-queue"
  delay_seconds              = "{{ queue.delay }}"
  max_message_size           = "{{ queue.max_msg_size }}"
  message_retention_seconds  = "{{ queue.msg_retention_secs }}"
  receive_wait_time_seconds  = "{{ queue.rcv_wait_secs }}"
  visibility_timeout_seconds = "{{ queue.visibility_timeout_secs }}"

  redrive_policy = <<EOF
{
  "deadLetterTargetArn":"${aws_sqs_queue.{{ queue_type }}_deadletter.arn}",
  "maxReceiveCount":{{ queue.redrive_max_rcv_count|int }}
}
EOF
}

resource "aws_sqs_queue" "{{ queue_type }}_deadletter" {
  name = "{{ queue.name }}-queue-deadletter"
}

{% if queue.incoming_account is defined %}
resource "aws_sqs_queue_policy" "{{ queue_type }}_allow_sqs_write" {
    queue_url = "${aws_sqs_queue.{{ queue_type }}.id}"
    policy    = "${data.aws_iam_policy_document.{{ queue_type }}_allow_sqs_write.json}"
}

data "aws_iam_policy_document" "{{ queue_type }}_allow_sqs_write" {
    statement {
    sid = "AllowSQSWrite"

    actions = [
      "SQS:SendMessage"
    ]

    resources = [
      "${aws_sqs_queue.{{ queue_type }}.arn}",
    ]

    principals {
      type        = "AWS"
      identifiers = ["arn:aws:iam::{{ queue.incoming_account }}:root"]
    }
  }
}
{% endif %}
pearofducks commented 7 years ago

I don't think this is a bug, but rather I'd guess the underlying syntax file (in this case Terraform) isn't being used.

Unfortunately detecting and auto-using the syntaxes isn't something this plugin does at the moment, but the workaround is pretty painless.

  1. Add vim-terraform to your vim plugins, maybe you have already?
  2. Add the line let g:ansible_extra_syntaxes = "terraform.vim" to your vimrc.

If you want to check and confirm syntax highlighting, you can use this monster of a mapping in your vimrc, and then press F10 over anything that seems off. In this case; without the above fix, the highlights show no data, but with the fix, they correctly show a TerraformHeredoc.

map <F10> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
\ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<"
\ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>
beanaroo commented 7 years ago

I did not think of that. Thank you so much! Works like a charm! And thanks for the mapping. Sorry for the noise!

pearofducks commented 7 years ago

No problem at all, glad it's working :)