pearofducks / ansible-vim

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

weird playbook indenting issue #51

Closed jdlovins closed 6 years ago

jdlovins commented 6 years ago

Hey,

I'm more or less new to ansible and im working on writing a playbook using the generic format as follows

.
├── roles
│   └── sshd
│       ├── defaults
│       │   └── main.yml
│       ├── files
│       │   └── laptop.pub
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       │   └── main.yml
│       ├── README.md
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       ├── tests
│       │   ├── inventory
│       │   └── test.yml
│       └── vars
│           └── main.yml
└── sshd.yml

While editing the sshd.yml file which is the main playbook and starting it off with

---
- name: configure sshd

then i want to add the hosts: setting, it unindents it once i type ':' which is super annoying. According to what i've found this is the right format so im not sure whats going on.

What I'm looking for

---
- name: sshd configs
  hosts: all

what im getting

---
- name: sshd configs
hosts: all

Thanks!

pearofducks commented 6 years ago

I can't reproduce this, and it doesn't sound like something this plugin causes.

Can you provide a reproduction through a vimrc?

What do you get when you do echo &ft as a command with this file open?

jdlovins commented 6 years ago

Hmm that's weird since this is a mostly bare bones centos 7 install. This is the only plugin i have installed besides Plug.

set nocp
call plug#begin('~/.vim/plugged')
    Plug 'pearofducks/ansible-vim'
    Plug 'stephpy/vim-yaml'
call plug#end()
filetype plugin indent on
syntax on
set encoding=utf-8

Is the vimrc file, i actually copied it from one of the other issues that i was poking around in.

Ah so echo &ft on that file shows yaml whereas doing it in tasks/main.yml shows ansible so its not registering the filetype properly. Which is weird since its in a folder that has roles as shown by the tree structure.

edit: I guess this is more of a me problem wanting the structure to be different. I think it would still be useful to have the plugin detect .yml files as ansible if they are in the same folder as roles or group_vars or something like that.

jdlovins commented 6 years ago

I'm sure theres a better way to do it but this is what i did with modifying your ftdetect part.

function! s:isAnsible()
  let filepath = expand("%:p")
  let filename = expand("%:t")
  let pwd = expand("%:h")

  if filepath =~ '\v/(tasks|roles|handlers)/.*\.ya?ml$' | return 1 | en
  if filepath =~ '\v/(group|host)_vars/' | return 1 | en
  if filename =~ '\v(playbook|site|main|local)\.ya?ml$' | return 1 | en

  let localfiles = split(globpath(pwd, '*'), '\n')

  for file in localfiles
        if file =~ '\v/(roles|group_vars|host_vars)$' | return 1 | en
  endfor

  let shebang = getline(1)
  if shebang =~# '^#!.*/bin/env\s\+ansible-playbook\>' | return 1 | en
  if shebang =~# '^#!.*/bin/ansible-playbook\>' | return 1 | en

  return 0
endfunction
pearofducks commented 6 years ago

In the top of the README there's a command you can put in your vimrc to let you set any regex to any filetype. That should support any use-cases you have.

e.g. au BufRead,BufNewFile */playbooks/*.yml set filetype=ansible